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

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

NamespaceSupporterクラスをab_commonプロジェクトに移動した。

File size: 5.2 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
44#include <abdev/ab_common/Environment.h>
45#include <abdev/ab_common/include/Namespace.h>
46#include <abdev/ab_common/include/NamespaceSupporter.h>
47
48using namespace ActiveBasic::Common::Lexical;
49
50#include <Hashmap.h>
51#include <Compiler.h>
52
53
54void ObjectModule::StaticLink( ObjectModule &objectModule )
55{
56 long dataSectionBaseOffset = dataTable.GetSize();
57 int sourceIndexBase = (int)sources.size();
58
59 // メタ情報を結合
60 meta.StaticLink( objectModule.meta, dataSectionBaseOffset, sourceIndexBase );
61
62 // グローバル ネイティブコードを結合
63 objectModule.globalNativeCode.ResetDataSectionBaseOffset( dataSectionBaseOffset );
64 objectModule.globalNativeCode.ResetSourceIndexes( sourceIndexBase );
65 globalNativeCode.PutEx( objectModule.globalNativeCode );
66
67 // データテーブルを結合
68 objectModule.dataTable.ResetDataSectionBaseOffset( dataSectionBaseOffset );
69 dataTable.Add( objectModule.dataTable );
70
71 // ソースコードを結合
72 BOOST_FOREACH( const BasicSource &source, objectModule.sources )
73 {
74 this->sources.push_back( source );
75 }
76
77 // TODO: basbufがいらなくなったら消す
78 extern char *basbuf;
79 basbuf = this->sources[0].GetBuffer();
80}
81
82bool ObjectModule::Read( const std::string &filePath )
83{
84 // XMLとして読み込む
85 bool isSuccessful = false;
86
87 try{
88#ifdef OBJECT_MODULE_IS_NOT_BINARY
89 std::ifstream ifs( filePath.c_str() );
90 boost::archive::xml_iarchive ia(ifs);
91#else
92 std::ifstream ifs( filePath.c_str(), ios::in | ios::binary );
93 boost::archive::binary_iarchive ia(ifs);
94#endif
95
96 // ファイルから読込
97 ia >> boost::serialization::make_nvp( RootTagName(), *this );
98
99 isSuccessful = true;
100 }
101 catch( boost::archive::archive_exception e )
102 {
103 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
104 }
105 catch(...){
106 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
107 }
108
109 if( !isSuccessful )
110 {
111 return false;
112 }
113
114 return true;
115}
116bool ObjectModule::Write( const std::string &filePath ) const
117{
118 bool isSuccessful = false;
119
120 try{
121#ifdef OBJECT_MODULE_IS_NOT_BINARY
122 std::ofstream ofs( filePath.c_str() );
123 boost::archive::xml_oarchive oa(ofs);
124#else
125 std::ofstream ofs( filePath.c_str(), ios::out | ios::binary );
126 boost::archive::binary_oarchive oa(ofs);
127#endif
128
129 // ファイルに書き出し
130 oa << boost::serialization::make_nvp( RootTagName(), *this );
131
132 isSuccessful = true;
133 }
134 catch( boost::archive::archive_exception e )
135 {
136 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
137 }
138 catch(...){
139 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
140 }
141
142 if( !isSuccessful )
143 {
144 return false;
145 }
146
147 return true;
148}
149bool ObjectModule::ReadString( const std::string &str )
150{
151 bool isSuccessful = false;
152
153 // 入力アーカイブの作成
154
155 try{
156#ifdef OBJECT_MODULE_IS_NOT_BINARY
157 std::istringstream iss( str );
158 boost::archive::xml_iarchive ia(iss);
159#else
160 std::istringstream iss( str, ios::in | ios::binary );
161 boost::archive::binary_iarchive ia(iss);
162#endif
163
164 // 文字列ストリームから読込
165 ia >> boost::serialization::make_nvp( RootTagName(), *this );
166
167 isSuccessful = true;
168 }
169 catch( boost::archive::archive_exception e )
170 {
171 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
172 }
173 catch(...){
174 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
175 }
176
177 if( !isSuccessful )
178 {
179 return false;
180 }
181
182 return true;
183}
184bool ObjectModule::WriteString( std::string &str ) const
185{
186 // 出力アーカイブの作成
187
188 bool isSuccessful = false;
189 try{
190#ifdef OBJECT_MODULE_IS_NOT_BINARY
191 std::ostringstream oss;
192 boost::archive::xml_oarchive oa(oss);
193#else
194 std::ostringstream oss( "", ios::out | ios::binary );
195 boost::archive::binary_oarchive oa(oss);
196#endif
197
198 // 文字列ストリームに書き出し
199 oa << boost::serialization::make_nvp( RootTagName(), *this );
200
201 str = oss.str();
202
203 isSuccessful = true;
204 }
205 catch( boost::archive::archive_exception e )
206 {
207 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
208 }
209 catch(...){
210 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
211 }
212
213 return isSuccessful;
214}
Note: See TracBrowser for help on using the repository browser.