source: dev/trunk/ab5.0/abdev/ab_common/include/Lexical/Source.h@ 661

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

リンカの依存関係解決モジュールを製作中

File size: 6.9 KB
Line 
1#pragma once
2
3class IncludedFilesRelation
4{
5 std::vector<std::string> filePaths;
6 std::vector<int> lineFileNumbers;
7
8 // XMLシリアライズ用
9private:
10 friend class boost::serialization::access;
11 template<class Archive> void serialize(Archive& ar, const unsigned int version)
12 {
13 ar & BOOST_SERIALIZATION_NVP( filePaths );
14 ar & BOOST_SERIALIZATION_NVP( lineFileNumbers );
15 }
16
17public:
18 IncludedFilesRelation()
19 {
20 }
21 ~IncludedFilesRelation()
22 {
23 }
24
25 const int GetFileNumber( int lineNumber ) const
26 {
27 return lineFileNumbers[lineNumber];
28 }
29 const std::string &GetFilePath( int lineNumber ) const
30 {
31 return filePaths[GetFileNumber( lineNumber )];
32 }
33 const std::string &GetFilePathFromFileNumber( int fileNumber ) const
34 {
35 return filePaths[fileNumber];
36 }
37 int GetFileCounts() const
38 {
39 return (int)filePaths.size();
40 }
41
42 int AddFile( const std::string &filePath )
43 {
44 filePaths.push_back( filePath );
45 return (int)filePaths.size()-1;
46 }
47 void AddLine( int fileNumber )
48 {
49 lineFileNumbers.push_back( fileNumber );
50 }
51
52 int GetLineCounts() const
53 {
54 return (int)lineFileNumbers.size();
55 }
56};
57
58class Text{
59protected:
60 char *buffer;
61 int length;
62
63public:
64
65 Text(){
66 buffer = (char *)calloc( 1, 1 );
67 length = 0;
68 }
69 Text( const Text &text )
70 : length( text.length )
71 {
72 buffer = (char *)malloc( length + 1 );
73 memcpy( buffer, text.buffer, length );
74 buffer[length] = 0;
75 }
76 ~Text(){
77 free( buffer );
78 }
79 void Clear()
80 {
81 length = 0;
82 }
83 void Add( const std::string &str )
84 {
85 buffer = (char *)realloc( buffer, length + str.size() + 1 );
86 lstrcpy( buffer + length, str.c_str() );
87 length += (int)str.size();
88 }
89 void Add( const std::vector<char> &str )
90 {
91 buffer = (char *)realloc( buffer, length + str.size() + 1 );
92 lstrcpy( buffer + length, &str[0] );
93 length += (int)str.size();
94 }
95
96 bool ReadFile( const std::string &filePath );
97
98 static void Text::SlideString(char *buffer, int slide){
99 char *temp;
100 temp=(char *)malloc(lstrlen(buffer)+1);
101 lstrcpy(temp,buffer);
102 lstrcpy(buffer+slide,temp);
103 free(temp);
104 }
105};
106
107class BasicSource : public Text
108{
109 static const std::string generateDirectiveName;
110
111 IncludedFilesRelation includedFilesRelation;
112
113 // XMLシリアライズ用
114private:
115 friend class boost::serialization::access;
116 BOOST_SERIALIZATION_SPLIT_MEMBER();
117 template<class Archive> void load(Archive& ar, const unsigned int version)
118 {
119 std::string _buffer;
120 ar & BOOST_SERIALIZATION_NVP( _buffer );
121 ar & BOOST_SERIALIZATION_NVP( length );
122 ar & BOOST_SERIALIZATION_NVP( includedFilesRelation );
123
124 // 読み込み後の処理
125 Realloc( length );
126 for( int i=0; i<length; i++ )
127 {
128 ULONG_PTR l1 = ( ( _buffer[i*3] >= 'a' ) ? ( _buffer[i*3] - 'a' + 0x0a ) : ( _buffer[i*3] - '0' ) ) * 0x10;
129 ULONG_PTR l2 = ( _buffer[i*3+1] >= 'a' ) ? ( _buffer[i*3+1] - 'a' + 0x0a ) : ( _buffer[i*3+1] - '0' );
130 ULONG_PTR l = l1 + l2;
131 buffer[i] = static_cast<char>(l);
132 }
133 buffer[length] = 0;
134 }
135 template<class Archive> void save(Archive& ar, const unsigned int version) const
136 {
137 // 保存準備
138 char *tempCode = (char *)calloc( (length+1) * 3, 1 );
139 for( int i=0; i<length; i++ )
140 {
141 char temp[32];
142 sprintf( temp, "%02x,", (unsigned char)buffer[i] );
143 tempCode[i*3] = temp[0];
144 tempCode[i*3+1] = temp[1];
145 tempCode[i*3+2] = temp[2];
146 }
147
148 std::string _buffer = tempCode;
149 free( tempCode );
150
151 ar & BOOST_SERIALIZATION_NVP( _buffer );
152 ar & BOOST_SERIALIZATION_NVP( length );
153 ar & BOOST_SERIALIZATION_NVP( includedFilesRelation );
154 }
155
156private:
157 void Realloc( int newLength )
158 {
159 bool isEqualBasbuf = false;
160 extern char *basbuf;
161 if( basbuf == buffer + 2 )
162 {
163 isEqualBasbuf = true;
164 }
165
166 buffer = (char *)realloc( buffer, newLength + 255 );
167
168 length = newLength;
169
170 if( isEqualBasbuf )
171 {
172 basbuf = buffer + 2;
173 }
174 }
175
176 void IncludeFiles();
177
178 void ChangeReturnLineChar();
179
180 void RemoveComments();
181
182 bool ReadFile_InIncludeDirective( const std::string &filePath );
183 void DirectiveIncludeOrRequire( const std::string &mainSourceFilePath, const std::string &includeDirPath );
184
185 void RemoveReturnLineUnderbar();
186
187 void Initialize( const std::string &source );
188
189public:
190 BasicSource(){}
191 BasicSource( const BasicSource &basicSource )
192 : Text( basicSource )
193 , includedFilesRelation( basicSource.includedFilesRelation )
194 {
195 }
196 BasicSource( const std::string &source )
197 {
198 Initialize( source );
199 }
200 ~BasicSource(){}
201
202 char *GetBuffer(){
203 return buffer+2;
204 }
205 const char *GetBuffer() const
206 {
207 return buffer+2;
208 }
209 int GetLength() const
210 {
211 return length-2;
212 }
213 void _ResetLength()
214 {
215 length = lstrlen( buffer );
216 }
217
218 const IncludedFilesRelation &GetIncludedFilesRelation() const
219 {
220 return includedFilesRelation;
221 }
222
223 void SetBuffer( const char *buffer );
224
225 bool ReadFile( const std::string &filePath, bool isDebug, bool isDll, bool isUnicode, int majorVer, const std::string &mainSourceFilePath, const std::string &includeDirPath );
226
227 void Addition( const char *buffer );
228
229 bool GetLineInfo( int sourceCodePos, int &line, std::string &fileName ) const;
230
231 void operator = ( const BasicSource &source ){
232 Realloc( source.length );
233 lstrcpy( buffer, source.buffer );
234 }
235
236 char operator[]( int index ) const
237 {
238 if( index>GetLength() )
239 {
240 Jenga::Throw( "BasicSource bad access" );
241 }
242 return buffer[2+index];
243 }
244
245 std::string cannotIncludePath;
246 int cannotIncludeSourcePos;
247};
248class BasicSources
249 : public std::vector<BasicSource>
250 , public Jenga::Common::BoostSerializationSupport<BasicSources>
251{
252 // XMLシリアライズ用
253private:
254 virtual const char *RootTagName() const
255 {
256 return "basicSources";
257 }
258 friend class boost::serialization::access;
259 template<class Archive> void serialize(Archive& ar, const unsigned int version)
260 {
261 ar & boost::serialization::make_nvp("vector_BasicSource", boost::serialization::base_object<std::vector<BasicSource>>(*this));
262 }
263};
264
265class SourceCodePosition
266{
267 int relationalObjectModuleIndex;
268 int pos;
269
270 // XMLシリアライズ用
271private:
272 friend class boost::serialization::access;
273 template<class Archive> void serialize(Archive& ar, const unsigned int version)
274 {
275 ar & BOOST_SERIALIZATION_NVP( relationalObjectModuleIndex );
276 ar & BOOST_SERIALIZATION_NVP( pos );
277 }
278
279public:
280 SourceCodePosition( int relationalObjectModuleIndex, int pos )
281 : relationalObjectModuleIndex( relationalObjectModuleIndex )
282 , pos( pos )
283 {
284 }
285 SourceCodePosition()
286 : relationalObjectModuleIndex( -1 )
287 , pos( -1 )
288 {
289 }
290
291 int GetRelationalObjectModuleIndex() const;
292 void SetRelationalObjectModuleIndex( int relationalObjectModuleIndex )
293 {
294 this->relationalObjectModuleIndex = relationalObjectModuleIndex;
295 }
296 int GetPos() const
297 {
298 return pos;
299 }
300 void SetPos( int pos )
301 {
302 this->pos = pos;
303 }
304
305 bool IsNothing() const;
306};
Note: See TracBrowser for help on using the repository browser.