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