[266] | 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 |
|
---|
[279] | 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 | {
|
---|
[308] | 52 | return (int)filePaths.size();
|
---|
[279] | 53 | }
|
---|
| 54 |
|
---|
| 55 | int AddFile( const std::string &filePath )
|
---|
| 56 | {
|
---|
| 57 | filePaths.push_back( filePath );
|
---|
[308] | 58 | return (int)filePaths.size()-1;
|
---|
[279] | 59 | }
|
---|
| 60 | void AddLine( int fileNumber )
|
---|
| 61 | {
|
---|
| 62 | lineFileNumbers.push_back( fileNumber );
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | int GetLineCounts() const
|
---|
| 66 | {
|
---|
[308] | 67 | return (int)lineFileNumbers.size();
|
---|
[279] | 68 | }
|
---|
| 69 | };
|
---|
| 70 |
|
---|
[266] | 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 | }
|
---|
[280] | 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 | }
|
---|
[266] | 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 | }
|
---|
[268] | 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 | }
|
---|
[266] | 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 |
|
---|
[279] | 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:
|
---|
[281] | 174 | void Realloc( int newLength )
|
---|
| 175 | {
|
---|
| 176 | bool isEqualBasbuf = false;
|
---|
| 177 | extern char *basbuf;
|
---|
[282] | 178 | if( basbuf == buffer + 2 )
|
---|
[281] | 179 | {
|
---|
| 180 | isEqualBasbuf = true;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[266] | 183 | buffer = (char *)realloc( buffer, newLength + 255 );
|
---|
| 184 |
|
---|
| 185 | length = newLength;
|
---|
| 186 |
|
---|
[281] | 187 | if( isEqualBasbuf )
|
---|
| 188 | {
|
---|
| 189 | basbuf = buffer + 2;
|
---|
| 190 | }
|
---|
[266] | 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(){}
|
---|
[280] | 206 | BasicSource( const BasicSource &basicSource )
|
---|
| 207 | : Text( basicSource )
|
---|
| 208 | , includedFilesRelation( basicSource.includedFilesRelation )
|
---|
| 209 | {
|
---|
| 210 | }
|
---|
[266] | 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 | }
|
---|
[304] | 224 | void _ResetLength()
|
---|
| 225 | {
|
---|
| 226 | length = lstrlen( buffer );
|
---|
| 227 | }
|
---|
[266] | 228 |
|
---|
[279] | 229 | const IncludedFilesRelation &GetIncludedFilesRelation() const
|
---|
| 230 | {
|
---|
| 231 | return includedFilesRelation;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[266] | 234 | void SetBuffer( const char *buffer );
|
---|
| 235 |
|
---|
| 236 | bool ReadFile( const string &filePath );
|
---|
| 237 |
|
---|
| 238 | bool Generate( const string &genName, const char *buffer );
|
---|
| 239 |
|
---|
| 240 | void Addition( const char *buffer );
|
---|
| 241 |
|
---|
[280] | 242 | bool GetLineInfo( int sourceCodePos, int &line, std::string &fileName ) const;
|
---|
[279] | 243 |
|
---|
[266] | 244 | void operator = ( const BasicSource &source ){
|
---|
| 245 | Realloc( source.length );
|
---|
| 246 | lstrcpy( buffer, source.buffer );
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | char operator[]( int index ) const
|
---|
| 250 | {
|
---|
| 251 | if( index>GetLength() )
|
---|
| 252 | {
|
---|
| 253 | Jenga::Throw( "BasicSource bad access" );
|
---|
| 254 | }
|
---|
| 255 | return buffer[2+index];
|
---|
| 256 | }
|
---|
| 257 | };
|
---|
[280] | 258 | typedef std::vector<BasicSource> BasicSources;
|
---|