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

Last change on this file since 735 was 735, checked in by イグトランス (egtra), 16 years ago

改行コード変換などを高速化

File size: 6.8 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 = strlen( buffer );
214 }
215
216 const IncludedFilesRelation &GetIncludedFilesRelation() const
217 {
218 return includedFilesRelation;
219 }
220
221 void SetBuffer( const char *buffer );
222
223 bool ReadFile( const std::string &filePath, bool isDebug, bool isDll, bool isUnicode, int majorVer, const std::string &mainSourceFilePath, const std::string &includeDirPath );
224
225 void Addition( const char *buffer );
226
227 bool GetLineInfo( int sourceCodePos, int &line, std::string &fileName ) const;
228
229 void operator = ( const BasicSource &source ){
230 Realloc( source.length );
231 strcpy( buffer, source.buffer );
232 }
233
234 char operator[]( int index ) const
235 {
236 if( index>GetLength() )
237 {
238 Jenga::Throw( "BasicSource bad access" );
239 }
240 return buffer[2+index];
241 }
242
243 std::string cannotIncludePath;
244 int cannotIncludeSourcePos;
245};
246class BasicSources
247 : public std::vector<BasicSource>
248 , public Jenga::Common::BoostSerializationSupport<BasicSources>
249{
250 // XMLシリアライズ用
251private:
252 virtual const char *RootTagName() const
253 {
254 return "basicSources";
255 }
256 friend class boost::serialization::access;
257 template<class Archive> void serialize(Archive& ar, const unsigned int version)
258 {
259 ar & boost::serialization::make_nvp("vector_BasicSource", boost::serialization::base_object<std::vector<BasicSource>>(*this));
260 }
261};
262
263class SourceCodePosition
264{
265 int relationalObjectModuleIndex;
266 int pos;
267
268 // XMLシリアライズ用
269private:
270 friend class boost::serialization::access;
271 template<class Archive> void serialize(Archive& ar, const unsigned int version)
272 {
273 ar & BOOST_SERIALIZATION_NVP( relationalObjectModuleIndex );
274 ar & BOOST_SERIALIZATION_NVP( pos );
275 }
276
277public:
278 SourceCodePosition( int relationalObjectModuleIndex, int pos )
279 : relationalObjectModuleIndex( relationalObjectModuleIndex )
280 , pos( pos )
281 {
282 }
283 SourceCodePosition()
284 : relationalObjectModuleIndex( -1 )
285 , pos( -1 )
286 {
287 }
288
289 int GetRelationalObjectModuleIndex() const;
290 void SetRelationalObjectModuleIndex( int relationalObjectModuleIndex )
291 {
292 this->relationalObjectModuleIndex = relationalObjectModuleIndex;
293 }
294 int GetPos() const
295 {
296 return pos;
297 }
298 void SetPos( int pos )
299 {
300 this->pos = pos;
301 }
302
303 bool IsNothing() const;
304};
Note: See TracBrowser for help on using the repository browser.