source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/include/Source.h@ 519

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

jenga.hを追加。

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