source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/src/ObjectModule.cpp@ 467

Last change on this file since 467 was 461, checked in by dai_9181, 16 years ago

smoothieプロジェクトが不要になったため、破棄。

File size: 5.0 KB
Line 
1#include <string>
2#include <vector>
3#include <fstream>
4
5#include <windows.h>
6#include <stdio.h>
7#include <string.h>
8#include <math.h>
9#include <commctrl.h>
10#include <time.h>
11#include <limits.h>
12#include <shlobj.h>
13
14//boost libraries
15#include <boost/foreach.hpp>
16
17#include <jenga/include/common/String.h>
18
19#include "../common.h"
20#include "../BasicFixed.h"
21
22
23// ObjectModuleの内容をXmlで生成する場合はこの行を有効にする
24//#define OBJECT_MODULE_IS_NOT_BINARY
25
26
27#ifdef OBJECT_MODULE_IS_NOT_BINARY
28#include <boost/archive/xml_oarchive.hpp>
29#include <boost/archive/xml_iarchive.hpp>
30#else
31#include <boost/archive/binary_oarchive.hpp>
32#include <boost/archive/binary_iarchive.hpp>
33#endif
34#include <boost/serialization/string.hpp>
35#include <boost/serialization/access.hpp>
36#include <boost/serialization/level.hpp>
37#include <boost/serialization/vector.hpp>
38#include <boost/serialization/map.hpp>
39#include <boost/serialization/version.hpp>
40#include <boost/serialization/is_abstract.hpp>
41
42#include <BoostSerializationSupport.h>
43#include <Hashmap.h>
44#include <Compiler.h>
45
46
47void ObjectModule::StaticLink( ObjectModule &objectModule )
48{
49 long dataSectionBaseOffset = dataTable.GetSize();
50 int sourceIndexBase = (int)sources.size();
51
52 // メタ情報を結合
53 meta.StaticLink( objectModule.meta, dataSectionBaseOffset, sourceIndexBase );
54
55 // グローバル ネイティブコードを結合
56 objectModule.globalNativeCode.ResetDataSectionBaseOffset( dataSectionBaseOffset );
57 objectModule.globalNativeCode.ResetSourceIndexes( sourceIndexBase );
58 globalNativeCode.PutEx( objectModule.globalNativeCode );
59
60 // データテーブルを結合
61 objectModule.dataTable.ResetDataSectionBaseOffset( dataSectionBaseOffset );
62 dataTable.Add( objectModule.dataTable );
63
64 // ソースコードを結合
65 BOOST_FOREACH( const BasicSource &source, objectModule.sources )
66 {
67 this->sources.push_back( source );
68 }
69
70 // TODO: basbufがいらなくなったら消す
71 extern char *basbuf;
72 basbuf = this->sources[0].GetBuffer();
73}
74
75bool ObjectModule::Read( const std::string &filePath )
76{
77 // XMLとして読み込む
78 bool isSuccessful = false;
79
80 try{
81#ifdef OBJECT_MODULE_IS_NOT_BINARY
82 std::ifstream ifs( filePath.c_str() );
83 boost::archive::xml_iarchive ia(ifs);
84#else
85 std::ifstream ifs( filePath.c_str(), ios::in | ios::binary );
86 boost::archive::binary_iarchive ia(ifs);
87#endif
88
89 // ファイルから読込
90 ia >> boost::serialization::make_nvp( RootTagName(), *this );
91
92 isSuccessful = true;
93 }
94 catch( boost::archive::archive_exception e )
95 {
96 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
97 }
98 catch(...){
99 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
100 }
101
102 if( !isSuccessful )
103 {
104 return false;
105 }
106
107 return true;
108}
109bool ObjectModule::Write( const std::string &filePath ) const
110{
111 bool isSuccessful = false;
112
113 try{
114#ifdef OBJECT_MODULE_IS_NOT_BINARY
115 std::ofstream ofs( filePath.c_str() );
116 boost::archive::xml_oarchive oa(ofs);
117#else
118 std::ofstream ofs( filePath.c_str(), ios::out | ios::binary );
119 boost::archive::binary_oarchive oa(ofs);
120#endif
121
122 // ファイルに書き出し
123 oa << boost::serialization::make_nvp( RootTagName(), *this );
124
125 isSuccessful = true;
126 }
127 catch( boost::archive::archive_exception e )
128 {
129 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
130 }
131 catch(...){
132 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
133 }
134
135 if( !isSuccessful )
136 {
137 return false;
138 }
139
140 return true;
141}
142bool ObjectModule::ReadString( const std::string &str )
143{
144 bool isSuccessful = false;
145
146 // 入力アーカイブの作成
147
148 try{
149#ifdef OBJECT_MODULE_IS_NOT_BINARY
150 std::istringstream iss( str );
151 boost::archive::xml_iarchive ia(iss);
152#else
153 std::istringstream iss( str, ios::in | ios::binary );
154 boost::archive::binary_iarchive ia(iss);
155#endif
156
157 // 文字列ストリームから読込
158 ia >> boost::serialization::make_nvp( RootTagName(), *this );
159
160 isSuccessful = true;
161 }
162 catch( boost::archive::archive_exception e )
163 {
164 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
165 }
166 catch(...){
167 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
168 }
169
170 if( !isSuccessful )
171 {
172 return false;
173 }
174
175 return true;
176}
177bool ObjectModule::WriteString( std::string &str ) const
178{
179 // 出力アーカイブの作成
180
181 bool isSuccessful = false;
182 try{
183#ifdef OBJECT_MODULE_IS_NOT_BINARY
184 std::ostringstream oss;
185 boost::archive::xml_oarchive oa(oss);
186#else
187 std::ostringstream oss( "", ios::out | ios::binary );
188 boost::archive::binary_oarchive oa(oss);
189#endif
190
191 // 文字列ストリームに書き出し
192 oa << boost::serialization::make_nvp( RootTagName(), *this );
193
194 str = oss.str();
195
196 isSuccessful = true;
197 }
198 catch( boost::archive::archive_exception e )
199 {
200 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
201 }
202 catch(...){
203 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
204 }
205
206 return isSuccessful;
207}
Note: See TracBrowser for help on using the repository browser.