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