source: dev/trunk/abdev/BasicCompiler_Common/include/Source.h@ 280

Last change on this file since 280 was 280, checked in by dai_9181, 17 years ago
File size: 5.4 KB
Line 
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
14class IncludedFilesRelation
15{
16 std::vector<std::string> filePaths;
17 std::vector<int> lineFileNumbers;
18
19 // XMLシリアライズ用
20private:
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
30public:
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
71class Text{
72protected:
73 char *buffer;
74 int length;
75
76public:
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
120class BasicSource : public Text
121{
122 static const string generateDirectiveName;
123
124 IncludedFilesRelation includedFilesRelation;
125
126 // XMLシリアライズ用
127private:
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
173private:
174 void Realloc( int newLength ){
175 buffer = (char *)realloc( buffer, newLength + 255 );
176
177 length = newLength;
178
179 extern char *basbuf;
180 basbuf = buffer + 2;
181 }
182
183 void IncludeFiles();
184
185 void ChangeReturnLineChar();
186
187 void RemoveComments();
188
189 bool ReadFile_InIncludeDirective( const string &filePath );
190 void DirectiveIncludeOrRequire();
191
192 void RemoveReturnLineUnderbar();
193
194public:
195 BasicSource(){}
196 BasicSource( const BasicSource &basicSource )
197 : Text( basicSource )
198 , includedFilesRelation( basicSource.includedFilesRelation )
199 {
200 }
201 ~BasicSource(){}
202
203 char *GetBuffer(){
204 return buffer+2;
205 }
206 const char *GetBuffer() const
207 {
208 return buffer+2;
209 }
210 int GetLength() const
211 {
212 return length-2;
213 }
214
215 const IncludedFilesRelation &GetIncludedFilesRelation() const
216 {
217 return includedFilesRelation;
218 }
219
220 void SetBuffer( const char *buffer );
221
222 bool ReadFile( const string &filePath );
223
224 bool Generate( const string &genName, const char *buffer );
225
226 void Addition( const char *buffer );
227
228 bool GetLineInfo( int sourceCodePos, int &line, std::string &fileName ) const;
229
230 void operator = ( const BasicSource &source ){
231 Realloc( source.length );
232 lstrcpy( buffer, source.buffer );
233 }
234
235 char operator[]( int index ) const
236 {
237 if( index>GetLength() )
238 {
239 Jenga::Throw( "BasicSource bad access" );
240 }
241 return buffer[2+index];
242 }
243};
244typedef std::vector<BasicSource> BasicSources;
Note: See TracBrowser for help on using the repository browser.