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

Last change on this file since 797 was 739, checked in by dai, 16 years ago

BasicSource::GetLineFromIndex関数を導入。

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