[87] | 1 | #include <Source.h>
|
---|
| 2 |
|
---|
[88] | 3 | #include "../common.h"
|
---|
| 4 | #include <windows.h>
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | class CDefine{
|
---|
| 9 | vector<string> names;
|
---|
| 10 | public:
|
---|
| 11 | CDefine();
|
---|
| 12 | ~CDefine();
|
---|
| 13 | void Free();
|
---|
| 14 | void Init();
|
---|
| 15 |
|
---|
| 16 | BOOL add(char *name);
|
---|
| 17 | BOOL undef(char *name);
|
---|
| 18 | BOOL check(char *name);
|
---|
| 19 | void preprocessor_ifdef(char *buffer,bool isNot);
|
---|
| 20 | void DirectiveIfdef(char *buffer);
|
---|
| 21 | };
|
---|
| 22 | CDefine objDefine;
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | //////////////////////////////////////
|
---|
| 26 | // #requireの管理
|
---|
| 27 | //////////////////////////////////////
|
---|
| 28 | class CRequireFiles{
|
---|
| 29 | char **ppFilePath;
|
---|
| 30 | int count;
|
---|
| 31 | public:
|
---|
| 32 | CRequireFiles(){
|
---|
| 33 | ppFilePath = (char **)malloc( 1 );
|
---|
| 34 | count = 0;
|
---|
| 35 | }
|
---|
| 36 | ~CRequireFiles(){
|
---|
| 37 | for( int i = 0; i < count; i++ ){
|
---|
| 38 | free( ppFilePath[i] );
|
---|
| 39 | }
|
---|
| 40 | free( ppFilePath );
|
---|
| 41 | }
|
---|
| 42 | void clear(){
|
---|
| 43 | for( int i = 0; i < count; i++ ){
|
---|
| 44 | free( ppFilePath[i] );
|
---|
| 45 | }
|
---|
| 46 | free( ppFilePath );
|
---|
| 47 |
|
---|
| 48 | ppFilePath = (char **)malloc( 1 );
|
---|
| 49 | count = 0;
|
---|
| 50 | }
|
---|
| 51 | bool IsIncluded( const char *includeFilePath ){
|
---|
| 52 | // '/' → '\\'
|
---|
| 53 | char tempPath[MAX_PATH];
|
---|
| 54 | lstrcpy( tempPath, includeFilePath );
|
---|
| 55 | for( int i=0; tempPath[i]; i++ ){
|
---|
| 56 | if( tempPath[i] == '/' ){
|
---|
| 57 | tempPath[i] = '\\';
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | for( int i=0; i<count; i++ ){
|
---|
| 62 | if( lstrcmpi( ppFilePath[i], tempPath ) == 0 ){
|
---|
| 63 | return true;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | return false;
|
---|
| 67 | }
|
---|
| 68 | void Add( const char *includeFilePath ){
|
---|
| 69 | // '/' → '\\'
|
---|
| 70 | char tempPath[MAX_PATH];
|
---|
| 71 | lstrcpy( tempPath, includeFilePath );
|
---|
| 72 | for( int i=0; tempPath[i]; i++ ){
|
---|
| 73 | if( tempPath[i] == '/' ){
|
---|
| 74 | tempPath[i] = '\\';
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | //既に読み込まれているとき
|
---|
| 79 | if( IsIncluded( tempPath ) ) return;
|
---|
| 80 |
|
---|
| 81 | //追加
|
---|
| 82 | ppFilePath = (char **)realloc(ppFilePath, ( count + 1 ) * sizeof(char *) );
|
---|
| 83 | ppFilePath[count] = (char *)malloc( lstrlen(tempPath) + 1 );
|
---|
| 84 | lstrcpy( ppFilePath[count], tempPath );
|
---|
| 85 | count++;
|
---|
| 86 | }
|
---|
| 87 | };
|
---|
| 88 | CRequireFiles requireFiles;
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | //////////////////////////////////////
|
---|
| 92 | // #define間するクラス
|
---|
| 93 | //////////////////////////////////////
|
---|
| 94 |
|
---|
| 95 | CDefine::CDefine(){
|
---|
| 96 | Init();
|
---|
| 97 | }
|
---|
| 98 | CDefine::~CDefine(){
|
---|
| 99 | }
|
---|
| 100 | void CDefine::Init(){
|
---|
| 101 | names.clear();
|
---|
| 102 |
|
---|
| 103 | extern BOOL bDebugCompile;
|
---|
| 104 | if(bDebugCompile) add("_DEBUG");
|
---|
| 105 |
|
---|
| 106 | #ifdef _AMD64_
|
---|
| 107 | add("_WIN64");
|
---|
| 108 | #endif
|
---|
| 109 |
|
---|
| 110 | extern BOOL bDll;
|
---|
| 111 | if( bDll ){
|
---|
| 112 | add("_DLL");
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | extern bool isUnicode;
|
---|
| 116 | if( isUnicode ){
|
---|
| 117 | add( "UNICODE" );
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | char temporary[255];
|
---|
| 121 | sprintf(temporary,"_AB_VER%d",MAJOR_VER);
|
---|
| 122 | add(temporary);
|
---|
| 123 | }
|
---|
| 124 | BOOL CDefine::add(char *name){
|
---|
| 125 | extern HANDLE hHeap;
|
---|
| 126 |
|
---|
| 127 | //重複チェック
|
---|
| 128 | if(check(name)) return 0;
|
---|
| 129 |
|
---|
| 130 | //追加
|
---|
| 131 | names.push_back( name );
|
---|
| 132 |
|
---|
| 133 | return 1;
|
---|
| 134 | }
|
---|
| 135 | BOOL CDefine::undef(char *name){
|
---|
| 136 | vector<string>::iterator i = names.begin();
|
---|
| 137 | foreach( const string &temp, names ){
|
---|
| 138 | if( temp == name ){
|
---|
| 139 | names.erase( i );
|
---|
| 140 | return 1;
|
---|
| 141 | }
|
---|
| 142 | i++;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | return 0;
|
---|
| 146 | }
|
---|
| 147 | BOOL CDefine::check(char *name){
|
---|
| 148 |
|
---|
| 149 | //重複チェック
|
---|
| 150 | foreach( const string &temp, names ){
|
---|
| 151 | if( temp == name ){
|
---|
| 152 | return 1;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | return 0;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | int Search_endif(char *buffer,int i, int *pLine = 0){
|
---|
| 159 | for(;;i++){
|
---|
| 160 | if(buffer[i]=='\0') break;
|
---|
| 161 |
|
---|
| 162 | if( buffer[i] == '\n' ){
|
---|
| 163 | if( pLine ){
|
---|
| 164 | (*pLine)++;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | if(buffer[i-1]=='\n'){
|
---|
| 169 | if(memicmp(buffer+i,"#ifdef",6)==0||memicmp(buffer+i,"#ifndef",7)==0){
|
---|
| 170 | i=Search_endif(buffer,i+6, pLine);
|
---|
| 171 | if(buffer[i]=='\0') break;
|
---|
| 172 | continue;
|
---|
| 173 | }
|
---|
| 174 | else if(memicmp(buffer+i,"#endif",6)==0){
|
---|
| 175 | break;
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | return i;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | void CDefine::preprocessor_ifdef(char *buffer,bool isNot){
|
---|
| 183 | int i,i2,i3;
|
---|
| 184 | char temporary[VN_SIZE];
|
---|
| 185 |
|
---|
| 186 | if(isNot) i=lstrlen("#ifndef");
|
---|
| 187 | else i=lstrlen("#ifdef");
|
---|
| 188 | while(buffer[i]==' '||buffer[i]=='\t') i++;
|
---|
| 189 |
|
---|
| 190 | for(i2=0;;i++,i2++){
|
---|
| 191 | if(buffer[i]=='\n'||buffer[i]=='\0'){
|
---|
| 192 | temporary[i2]=0;
|
---|
| 193 | break;
|
---|
| 194 | }
|
---|
| 195 | temporary[i2]=buffer[i];
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | int sw=0;
|
---|
| 199 | if(check(temporary)) sw=1;
|
---|
| 200 |
|
---|
| 201 | if(isNot){
|
---|
| 202 | //#ifndefのとき(反対にする)
|
---|
| 203 | if(sw) sw=0;
|
---|
| 204 | else sw=1;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | //#ifdefの行を消去
|
---|
| 208 | SlideString(buffer+i,-i);
|
---|
| 209 | i=0;
|
---|
| 210 |
|
---|
| 211 | BOOL bElse=0;
|
---|
| 212 | if(sw){
|
---|
| 213 | //TRUEのとき
|
---|
| 214 |
|
---|
| 215 | //#else、#endifを探索
|
---|
| 216 | for(;;i++){
|
---|
| 217 | if(buffer[i]=='\0') break;
|
---|
| 218 |
|
---|
| 219 | if(i==0||buffer[i-1]=='\n'){
|
---|
| 220 | if(memicmp(buffer+i,"#ifdef",6)==0||memicmp(buffer+i,"#ifndef",7)==0){
|
---|
| 221 | i=Search_endif(buffer,i+6);
|
---|
| 222 | if(buffer[i]=='\0') break;
|
---|
| 223 | continue;
|
---|
| 224 | }
|
---|
| 225 | else if(memicmp(buffer+i,"#else",5)==0){
|
---|
| 226 | i2=5;
|
---|
| 227 | bElse=1;
|
---|
| 228 | break;
|
---|
| 229 | }
|
---|
| 230 | else if(memicmp(buffer+i,"#endif",6)==0){
|
---|
| 231 | i2=6;
|
---|
| 232 | bElse=0;
|
---|
| 233 | break;
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | //行を消去
|
---|
| 239 | SlideString(buffer+i+i2,-i2);
|
---|
| 240 |
|
---|
| 241 | if(bElse){
|
---|
| 242 | //#elseがある場合はその区間を消去
|
---|
| 243 |
|
---|
| 244 | for(i2=i,i3=0;;i2++){
|
---|
| 245 | if(buffer[i2]=='\0') break;
|
---|
| 246 |
|
---|
| 247 | if(buffer[i2]=='\n') i3++;
|
---|
| 248 |
|
---|
| 249 | if(i2==0||buffer[i2-1]=='\n'){
|
---|
| 250 | if(memicmp(buffer+i2,"#ifdef",6)==0||memicmp(buffer+i2,"#ifndef",7)==0){
|
---|
| 251 | i2=Search_endif(buffer,i2+6, &i3 );
|
---|
| 252 | if(buffer[i2]=='\0') break;
|
---|
| 253 | continue;
|
---|
| 254 | }
|
---|
| 255 | if(memicmp(buffer+i2,"#endif",6)==0){
|
---|
| 256 | i2+=6;
|
---|
| 257 | break;
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | //ソースコード区間を消去し、改行コードを挿入
|
---|
| 263 | SlideString(buffer+i2,i-i2+i3);
|
---|
| 264 | memset(buffer+i,'\n',i3);
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 | else{
|
---|
| 268 | //FALSEのとき
|
---|
| 269 |
|
---|
| 270 | //#else、#endifを探索
|
---|
| 271 | for(i2=i,i3=0;;i2++){
|
---|
| 272 | if(buffer[i2]=='\0') break;
|
---|
| 273 |
|
---|
| 274 | if(buffer[i2]=='\n') i3++;
|
---|
| 275 |
|
---|
| 276 | if(i2==0||buffer[i2-1]=='\n'){
|
---|
| 277 | if(memicmp(buffer+i2,"#ifdef",6)==0||memicmp(buffer+i2,"#ifndef",7)==0){
|
---|
| 278 | i2=Search_endif(buffer,i2+6, &i3 );
|
---|
| 279 | if(buffer[i2]=='\0') break;
|
---|
| 280 | continue;
|
---|
| 281 | }
|
---|
| 282 | else if(memicmp(buffer+i2,"#else",5)==0){
|
---|
| 283 | i2+=5;
|
---|
| 284 | bElse=1;
|
---|
| 285 | break;
|
---|
| 286 | }
|
---|
| 287 | else if(memicmp(buffer+i2,"#endif",6)==0){
|
---|
| 288 | i2+=6;
|
---|
| 289 | bElse=0;
|
---|
| 290 | break;
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | //ソースコード区間を消去し、改行コードを挿入
|
---|
| 296 | SlideString(buffer+i2,i-i2+i3);
|
---|
| 297 | memset(buffer+i,'\n',i3);
|
---|
| 298 |
|
---|
| 299 | if(bElse){
|
---|
| 300 | //#endifを探索
|
---|
| 301 | for(;;i++){
|
---|
| 302 | if(buffer[i]=='\0') break;
|
---|
| 303 |
|
---|
| 304 | if(i==0||buffer[i-1]=='\n'){
|
---|
| 305 | if(memicmp(buffer+i,"#ifdef",6)==0||memicmp(buffer+i,"#ifndef",7)==0){
|
---|
| 306 | i=Search_endif(buffer,i+6);
|
---|
| 307 | if(buffer[i]=='\0') break;
|
---|
| 308 | continue;
|
---|
| 309 | }
|
---|
| 310 | else if(memicmp(buffer+i,"#endif",6)==0){
|
---|
| 311 | i2=6;
|
---|
| 312 | bElse=0;
|
---|
| 313 | break;
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | //行を消去
|
---|
| 319 | SlideString(buffer+i+i2,-i2);
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 |
|
---|
| 325 | void CDefine::DirectiveIfdef(char *buffer){
|
---|
| 326 | int i,i2,i3,sw;
|
---|
| 327 | char temporary[VN_SIZE];
|
---|
| 328 |
|
---|
| 329 | for(i=0;;i++){
|
---|
| 330 | if(buffer[i]=='\0') break;
|
---|
| 331 |
|
---|
| 332 | if(i==0||(i>=1&&buffer[i-1]=='\n')){
|
---|
| 333 | sw=0;
|
---|
| 334 | if(memicmp(buffer+i,"#define",7)==0){
|
---|
| 335 | i2=i+7;
|
---|
| 336 | while(buffer[i2]==' '||buffer[i2]=='\t') i2++;
|
---|
| 337 |
|
---|
| 338 | for(i3=0;;i2++,i3++){
|
---|
| 339 | if(buffer[i2]=='\n'||buffer[i2]=='\0'){
|
---|
| 340 | temporary[i3]=0;
|
---|
| 341 | break;
|
---|
| 342 | }
|
---|
| 343 | temporary[i3]=buffer[i2];
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | add(temporary);
|
---|
| 347 |
|
---|
| 348 | i2-=i;
|
---|
| 349 |
|
---|
| 350 | //ディレクティブを消去
|
---|
| 351 | SlideString(buffer+i+i2,-i2);
|
---|
| 352 | }
|
---|
| 353 | if(memicmp(buffer+i,"#undef",6)==0){
|
---|
| 354 | i2=i+7;
|
---|
| 355 | while(buffer[i2]==' '||buffer[i2]=='\t') i2++;
|
---|
| 356 |
|
---|
| 357 | for(i3=0;;i2++,i3++){
|
---|
| 358 | if(buffer[i2]=='\n'||buffer[i2]=='\0'){
|
---|
| 359 | temporary[i3]=0;
|
---|
| 360 | break;
|
---|
| 361 | }
|
---|
| 362 | temporary[i3]=buffer[i2];
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | undef(temporary);
|
---|
| 366 |
|
---|
| 367 | i2-=i;
|
---|
| 368 |
|
---|
| 369 | //ディレクティブを消去
|
---|
| 370 | SlideString(buffer+i+i2,-i2);
|
---|
| 371 | }
|
---|
| 372 | else if(memicmp(buffer+i,"#ifdef",6)==0){
|
---|
| 373 | preprocessor_ifdef(buffer+i,false);
|
---|
| 374 | continue;
|
---|
| 375 | }
|
---|
| 376 | else if(memicmp(buffer+i,"#ifndef",7)==0){
|
---|
| 377 | preprocessor_ifdef(buffer+i,true);
|
---|
| 378 | continue;
|
---|
| 379 | }
|
---|
| 380 | else continue;
|
---|
| 381 | }
|
---|
| 382 | }
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 |
|
---|
| 386 |
|
---|
| 387 |
|
---|
[87] | 388 | bool Text::ReadFile( const string &filePath ){
|
---|
[88] | 389 | //ファイルオープン
|
---|
| 390 | HANDLE hFile=CreateFile(filePath.c_str(),GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
|
---|
| 391 | if(hFile==INVALID_HANDLE_VALUE){
|
---|
| 392 | return false;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | length = GetFileSize( hFile, NULL );
|
---|
| 396 |
|
---|
| 397 | buffer = (char *)realloc( buffer, length + 1 );
|
---|
| 398 |
|
---|
| 399 | //読み込み
|
---|
| 400 | DWORD dwAccBytes;
|
---|
| 401 | ::ReadFile(hFile,buffer,length,&dwAccBytes,0);
|
---|
| 402 | buffer[dwAccBytes]=0;
|
---|
| 403 |
|
---|
| 404 | //ファイルクローズ
|
---|
| 405 | CloseHandle(hFile);
|
---|
| 406 |
|
---|
[87] | 407 | return true;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[88] | 410 | void BasicSource::ChangeReturnLineChar(){
|
---|
| 411 | #ifdef _DEBUG
|
---|
| 412 | //改行コードの整合性チェック
|
---|
| 413 | for( int i=0; ; i++ ){
|
---|
| 414 | if( buffer[i] == '\0' ){
|
---|
| 415 | break;
|
---|
| 416 | }
|
---|
| 417 | if( buffer[i]!='\r' && buffer[i+1]=='\n'
|
---|
| 418 | || buffer[i]=='\r' && buffer[i+1]!='\n' ){
|
---|
| 419 | char temporary[255];
|
---|
| 420 | strncpy( temporary, buffer + i-100, 130 );
|
---|
| 421 | temporary[130] = 0;
|
---|
| 422 | for(int i2=0; ;i2++){
|
---|
| 423 | if(temporary[i2]=='\r') temporary[i2]='A';
|
---|
| 424 | if(temporary[i2]=='\n') temporary[i2]='B';
|
---|
| 425 | if(temporary[i2]=='\0') break;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | extern HWND hOwnerEditor;
|
---|
| 429 | MessageBox( hOwnerEditor, temporary, "改行コードの整合性チェック", MB_OK | MB_ICONEXCLAMATION );
|
---|
| 430 | }
|
---|
| 431 | }
|
---|
| 432 | #endif
|
---|
| 433 |
|
---|
| 434 | //改行コードのCRLFをLFに変換
|
---|
| 435 | int i,i2;
|
---|
| 436 | for(i=0,i2=0;;i++,i2++){
|
---|
| 437 | if(buffer[i]=='\r'&&buffer[i+1]=='\n') i++;
|
---|
| 438 | buffer[i2]=buffer[i];
|
---|
| 439 | if(buffer[i]=='\0') break;
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | length = i;
|
---|
[87] | 443 | }
|
---|
| 444 |
|
---|
[88] | 445 | void BasicSource::RemoveComments(){
|
---|
| 446 | int i,i2,i3,IsStr;
|
---|
| 447 | char *temporary;
|
---|
| 448 | temporary=(char *)GlobalAlloc(GMEM_FIXED,lstrlen(buffer)+1);
|
---|
| 449 | for(i=0,i2=0,i3=0,IsStr=0;;i++,i2++){
|
---|
| 450 | if(buffer[i]=='\"') IsStr^=1;
|
---|
| 451 | if(buffer[i]=='\n'||buffer[i]=='\0'){
|
---|
| 452 | i2--;
|
---|
| 453 | while(temporary[i2]==' '||temporary[i2]=='\t') i2--;
|
---|
| 454 | i2++;
|
---|
[87] | 455 |
|
---|
[88] | 456 | if(i3){
|
---|
| 457 | //複数行に渡る注釈文の中に改行が存在するとき
|
---|
| 458 | memset(temporary+i2,'\n',i3);
|
---|
| 459 | i2+=i3;
|
---|
| 460 | i3=0;
|
---|
| 461 | }
|
---|
| 462 | }
|
---|
| 463 | if(buffer[i]=='\''&&IsStr==0){
|
---|
| 464 | //注釈文
|
---|
| 465 | i2--;
|
---|
| 466 | while(temporary[i2]==' '||temporary[i2]=='\t') i2--;
|
---|
| 467 | i2++;
|
---|
| 468 | while(buffer[i]!='\n'&&buffer[i]!='\0') i++;
|
---|
| 469 | }
|
---|
| 470 | if(buffer[i]=='/'&&buffer[i+1]=='*'&&IsStr==0){
|
---|
| 471 | //注釈文(複数行)
|
---|
| 472 | i+=2;
|
---|
| 473 | i3=0;
|
---|
| 474 | while(!(buffer[i]=='*'&&buffer[i+1]=='/')){
|
---|
| 475 | if(buffer[i]=='\n') i3++;
|
---|
| 476 | if(buffer[i]=='\0') break;
|
---|
| 477 | i++;
|
---|
| 478 | }
|
---|
| 479 | if(buffer[i]){
|
---|
| 480 | i+=2;
|
---|
| 481 | }
|
---|
| 482 | i--;
|
---|
| 483 | i2--;
|
---|
| 484 | continue;
|
---|
| 485 | }
|
---|
| 486 | temporary[i2]=buffer[i];
|
---|
| 487 | if(buffer[i]=='\0') break;
|
---|
| 488 | }
|
---|
| 489 | lstrcpy(buffer,temporary);
|
---|
| 490 | GlobalFree(temporary);
|
---|
[87] | 491 | }
|
---|
| 492 |
|
---|
[88] | 493 | bool BasicSource::ReadFile_InIncludeDirective( const string &filePath ){
|
---|
| 494 | if( !Text::ReadFile( filePath ) ){
|
---|
| 495 | return false;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | // 改行コードをCRLFからLFに変換
|
---|
| 499 | ChangeReturnLineChar();
|
---|
| 500 |
|
---|
| 501 | // コメントを削除
|
---|
| 502 | RemoveComments();
|
---|
| 503 |
|
---|
| 504 | // #ifdefディレクティブを処理
|
---|
| 505 | objDefine.DirectiveIfdef( buffer );
|
---|
| 506 |
|
---|
| 507 | // アンダーバーによる改行を正規表現に戻す
|
---|
| 508 | RemoveReturnLineUnderbar();
|
---|
| 509 |
|
---|
| 510 | // ダミー改行をセット
|
---|
| 511 | Realloc( length + 2 );
|
---|
| 512 | SlideString( buffer, 2 );
|
---|
| 513 | buffer[0] = '\n';
|
---|
| 514 | buffer[1] = '\n';
|
---|
| 515 |
|
---|
| 516 | return true;
|
---|
[87] | 517 | }
|
---|
[88] | 518 |
|
---|
[87] | 519 | void BasicSource::DirectiveIncludeOrRequire(){
|
---|
[88] | 520 | extern HANDLE hHeap;
|
---|
| 521 | extern char szIncludeDir[MAX_PATH];
|
---|
| 522 | extern char BasicCurDir[MAX_PATH];
|
---|
| 523 | extern INCLUDEFILEINFO IncludeFileInfo;
|
---|
| 524 | int i,i2,i3,sw1,LineNum,FileLayer[255],layer,LastFileByte[255];
|
---|
| 525 | char temporary[MAX_PATH],temp2[MAX_PATH+255],*LayerDir[255];
|
---|
| 526 |
|
---|
| 527 | IncludeFileInfo.ppFileNames=(char **)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,sizeof(char *));
|
---|
| 528 | extern char SourceFileName[MAX_PATH];
|
---|
| 529 | IncludeFileInfo.ppFileNames[0]=(char *)HeapAlloc(hHeap,0,lstrlen(SourceFileName)+1);
|
---|
| 530 | lstrcpy(IncludeFileInfo.ppFileNames[0],SourceFileName);
|
---|
| 531 | IncludeFileInfo.FilesNum=1;
|
---|
| 532 |
|
---|
| 533 | layer=0;
|
---|
| 534 | FileLayer[layer]=0;
|
---|
| 535 | LastFileByte[layer]=GetLength();
|
---|
| 536 | LineNum=0;
|
---|
| 537 |
|
---|
| 538 | //参照ディレクトリ
|
---|
| 539 | LayerDir[0]=(char *)HeapAlloc(hHeap,0,lstrlen(BasicCurDir)+1);
|
---|
| 540 | lstrcpy(LayerDir[0],BasicCurDir);
|
---|
| 541 |
|
---|
| 542 | for(i=0;;i++){
|
---|
| 543 | if(buffer[i]=='\0'){
|
---|
| 544 | IncludeFileInfo.LineOfFile[LineNum]=-1;
|
---|
| 545 | break;
|
---|
| 546 | }
|
---|
| 547 | if(buffer[i]=='\n'){
|
---|
| 548 | IncludeFileInfo.LineOfFile[LineNum]=FileLayer[layer];
|
---|
| 549 | LineNum++;
|
---|
| 550 | }
|
---|
| 551 | if(i>LastFileByte[layer]){
|
---|
| 552 | HeapDefaultFree(LayerDir[layer]);
|
---|
| 553 | layer--;
|
---|
| 554 | }
|
---|
| 555 | if((buffer[i-1]=='\n'||i==0)&&buffer[i]=='#'){
|
---|
| 556 | bool isRequire = false;
|
---|
| 557 | if(memcmp( buffer + i + 1, "include", 7 ) == 0
|
---|
| 558 | || memcmp( buffer + i + 1, "require", 7 ) == 0){
|
---|
| 559 |
|
---|
| 560 | //#requireの場合
|
---|
| 561 | if( buffer[i + 1] == 'r' ) isRequire = true;
|
---|
| 562 |
|
---|
| 563 | i2=i+8;
|
---|
| 564 | while(buffer[i2]==' '||buffer[i2]=='\t') i2++;
|
---|
| 565 |
|
---|
| 566 | if(buffer[i2]=='\"') sw1=0;
|
---|
| 567 | else if(buffer[i2]=='<') sw1=1;
|
---|
| 568 | i2++;
|
---|
| 569 |
|
---|
| 570 | for(i3=0;;i2++,i3++){
|
---|
| 571 | if((buffer[i2]=='\"'&&sw1==0)||(buffer[i2]=='>'&&sw1==1)||buffer[i2]=='\n'||buffer[i2]=='\0'){
|
---|
| 572 | temporary[i3]=0;
|
---|
| 573 | break;
|
---|
| 574 | }
|
---|
| 575 | temporary[i3]=buffer[i2];
|
---|
| 576 | }
|
---|
| 577 | while(buffer[i2]!='\n'&&buffer[i2]!='\0') i2++;
|
---|
| 578 |
|
---|
| 579 | if(sw1){
|
---|
| 580 | sprintf(temp2,"%s%s",szIncludeDir,temporary);
|
---|
| 581 | lstrcpy(temporary,temp2);
|
---|
| 582 | }
|
---|
| 583 | else GetFullPath(temporary,LayerDir[layer]);
|
---|
| 584 | }
|
---|
| 585 | else if(memcmp(buffer+i+1,"prompt",6)==0){
|
---|
| 586 | i2=i+7;
|
---|
| 587 | sprintf(temporary,"%sbasic\\prompt.sbp",szIncludeDir);
|
---|
| 588 | }
|
---|
| 589 | else if(memcmp(buffer+i+1,"N88BASIC",8)==0){
|
---|
| 590 | i2=i+9;
|
---|
| 591 | sprintf(temporary,"%sbasic\\prompt.sbp",szIncludeDir);
|
---|
| 592 | }
|
---|
| 593 | else if(memcmp(buffer+i+1,"console",7)==0){
|
---|
| 594 | //サブシステム タイプをCUIに変更
|
---|
| 595 | extern unsigned short TypeOfSubSystem;
|
---|
| 596 | TypeOfSubSystem=IMAGE_SUBSYSTEM_WINDOWS_CUI;
|
---|
| 597 |
|
---|
| 598 | i2=i+8;
|
---|
| 599 | sprintf(temporary,"%sbasic\\dos_console.sbp",szIncludeDir);
|
---|
| 600 | }
|
---|
| 601 | else continue;
|
---|
| 602 |
|
---|
| 603 | if(i){
|
---|
| 604 | //ディレクティブが消えるため、一行減ってしまうのを防ぐ(basic.sbpを除く)
|
---|
| 605 | SlideString(buffer+i2,1);
|
---|
| 606 | buffer[i2]='\n';
|
---|
| 607 | for(i3=0;i3<=layer;i3++) LastFileByte[i3]++;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | IncludeFileInfo.ppFileNames=(char **)HeapReAlloc(hHeap,0,IncludeFileInfo.ppFileNames,(IncludeFileInfo.FilesNum+1)*sizeof(char *));
|
---|
| 611 | IncludeFileInfo.ppFileNames[IncludeFileInfo.FilesNum]=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1);
|
---|
| 612 | lstrcpy(IncludeFileInfo.ppFileNames[IncludeFileInfo.FilesNum],temporary);
|
---|
| 613 |
|
---|
| 614 | layer++;
|
---|
| 615 | FileLayer[layer]=IncludeFileInfo.FilesNum;
|
---|
| 616 | IncludeFileInfo.FilesNum++;
|
---|
| 617 |
|
---|
| 618 | //#requireの場合では、既に読み込まれているファイルは読み込まないようにする
|
---|
| 619 | bool isFake = false;
|
---|
| 620 | if( isRequire ){
|
---|
| 621 | if( requireFiles.IsIncluded( temporary ) ){
|
---|
| 622 | //既に読み込まれているとき
|
---|
| 623 | isFake = true;
|
---|
| 624 | }
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | BasicSource source;
|
---|
| 628 |
|
---|
| 629 | if( isFake ){
|
---|
| 630 | //既に読み込まれているときは空データ
|
---|
| 631 | source.SetBuffer( "" );
|
---|
| 632 | }
|
---|
| 633 | else{
|
---|
| 634 | //取り込まれたファイルを収集する
|
---|
| 635 | requireFiles.Add( temporary );
|
---|
| 636 |
|
---|
| 637 | //インクルードファイルを読み込む
|
---|
| 638 | if( !source.ReadFile_InIncludeDirective( temporary ) ){
|
---|
| 639 | sprintf(temp2,"インクルードファイル \"%s\" をオープンできません",temporary);
|
---|
| 640 | SetError(-1,temp2,i);
|
---|
| 641 | break;
|
---|
| 642 | }
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | i3=lstrlen(buffer)+source.GetLength();
|
---|
| 646 | Realloc( i3 );
|
---|
| 647 | SlideString(buffer+i2,source.GetLength()+(i-i2));
|
---|
| 648 | memcpy(buffer+i,source.GetBuffer(),source.GetLength());
|
---|
| 649 |
|
---|
| 650 | //新しい参照ディレクトリをセット
|
---|
| 651 | char temp4[MAX_PATH];
|
---|
| 652 | _splitpath(temporary,temp2,temp4,0,0);
|
---|
| 653 | lstrcat(temp2,temp4);
|
---|
| 654 | LayerDir[layer]=(char *)HeapAlloc(hHeap,0,lstrlen(temp2)+1);
|
---|
| 655 | lstrcpy(LayerDir[layer],temp2);
|
---|
| 656 |
|
---|
| 657 | //ファイル範囲をスライド
|
---|
| 658 | LastFileByte[layer]=i+source.GetLength()-1;
|
---|
| 659 | for(i3=0;i3<layer;i3++) LastFileByte[i3]+=source.GetLength()+(i-i2);
|
---|
| 660 |
|
---|
| 661 | i--;
|
---|
| 662 | }
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | HeapDefaultFree(LayerDir[0]);
|
---|
| 666 |
|
---|
| 667 | length = lstrlen(buffer);
|
---|
[87] | 668 | }
|
---|
| 669 |
|
---|
[88] | 670 | int KillReturnCode_InParameter(char *buffer,int *pRnum,char cBeginPare,char cEndPare){
|
---|
| 671 | int i,i2,i3,IsStr;
|
---|
| 672 |
|
---|
| 673 | //カッコ'('直下の改行
|
---|
| 674 | while(buffer[0]=='\n'){
|
---|
| 675 | SlideString(buffer+1,-1);
|
---|
| 676 | (*pRnum)++;
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | for(i=0,IsStr=0;;i++){
|
---|
| 680 | if(IsDBCSLeadByte(buffer[i])&&buffer[i+1]){
|
---|
| 681 | i++;
|
---|
| 682 | continue;
|
---|
| 683 | }
|
---|
| 684 |
|
---|
| 685 | if(buffer[i]=='\"') IsStr^=1;
|
---|
| 686 |
|
---|
| 687 | if(buffer[i]=='\0') break; //エラー
|
---|
| 688 | if(buffer[i]=='\n'){
|
---|
| 689 | i2=0;
|
---|
| 690 | i3=0;
|
---|
| 691 | while(buffer[i+i2]=='\n'){
|
---|
| 692 | i2++;
|
---|
| 693 | i3++;
|
---|
| 694 | while(buffer[i+i2]==' '||buffer[i+i2]=='\t') i2++;
|
---|
| 695 | }
|
---|
| 696 | while(buffer[i+i2]==' '||buffer[i+i2]=='\t') i2++;
|
---|
| 697 |
|
---|
| 698 | if(buffer[i+i2]==cEndPare){
|
---|
| 699 | SlideString(buffer+i+i2,-i2);
|
---|
| 700 | (*pRnum)+=i3;
|
---|
| 701 | break;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | //エラー
|
---|
| 705 | break;
|
---|
| 706 | }
|
---|
| 707 |
|
---|
| 708 | if(buffer[i]==cBeginPare&&IsStr==0){
|
---|
| 709 | i++;
|
---|
| 710 | i2=KillReturnCode_InParameter(buffer+i,pRnum,cBeginPare,cEndPare);
|
---|
| 711 | i+=i2;
|
---|
| 712 | if(buffer[i]!=cEndPare) break;
|
---|
| 713 | continue;
|
---|
| 714 | }
|
---|
| 715 | if(buffer[i]==cEndPare&&IsStr==0) break;
|
---|
| 716 |
|
---|
| 717 | if(buffer[i]==','&&buffer[i+1]=='\n'&&IsStr==0){
|
---|
| 718 | i++;
|
---|
| 719 | while(buffer[i]=='\n'){
|
---|
| 720 | SlideString(buffer+i+1,-1);
|
---|
| 721 | (*pRnum)++;
|
---|
| 722 | }
|
---|
| 723 | i--;
|
---|
| 724 | }
|
---|
| 725 | }
|
---|
| 726 | return i;
|
---|
| 727 | }
|
---|
[87] | 728 | void BasicSource::RemoveReturnLineUnderbar(){
|
---|
[88] | 729 | int i,i2;
|
---|
| 730 |
|
---|
| 731 | //アンダーバーによる改行
|
---|
| 732 | for(i=0;;i++){
|
---|
| 733 | i2=0;
|
---|
| 734 | while(buffer[i]=='_'&&buffer[i+1]=='\n'){
|
---|
| 735 | i2++;
|
---|
| 736 | SlideString(buffer+i+2,-2);
|
---|
| 737 | while(buffer[i]=='\n'){
|
---|
| 738 | SlideString(buffer+i+1,-1);
|
---|
| 739 | i2++;
|
---|
| 740 | }
|
---|
| 741 | for(;;i++){
|
---|
| 742 | if(buffer[i]=='_'&&buffer[i+1]=='\n') break;
|
---|
| 743 | if(buffer[i]=='\n'||buffer[i]=='\0'){
|
---|
| 744 | SlideString(buffer+i,i2);
|
---|
| 745 | memset(buffer+i,'\n',i2);
|
---|
| 746 | break;
|
---|
| 747 | }
|
---|
| 748 | }
|
---|
| 749 | }
|
---|
| 750 | if(buffer[i]=='\0') break;
|
---|
| 751 | }
|
---|
| 752 |
|
---|
| 753 | //カッコ内パラメータの改行
|
---|
| 754 | int IsStr,rnum;
|
---|
| 755 | for(i=0,IsStr=0,rnum=0;;i++){
|
---|
| 756 | if(IsDBCSLeadByte(buffer[i])&&buffer[i+1]){
|
---|
| 757 | i++;
|
---|
| 758 | continue;
|
---|
| 759 | }
|
---|
| 760 | if(buffer[i]=='\0') break;
|
---|
| 761 | if(buffer[i]=='\n'){
|
---|
| 762 | if(rnum){
|
---|
| 763 | SlideString(buffer+i+1,rnum);
|
---|
| 764 | memset(buffer+i+1,'\n',rnum);
|
---|
| 765 | rnum=0;
|
---|
| 766 | }
|
---|
| 767 | }
|
---|
| 768 | if(buffer[i]=='\"') IsStr^=1;
|
---|
| 769 | if(buffer[i]=='('&&IsStr==0){
|
---|
| 770 | i++;
|
---|
| 771 | i2=KillReturnCode_InParameter(buffer+i,&rnum,'(',')');
|
---|
| 772 | i+=i2;
|
---|
| 773 | if(buffer[i]!=')') break;
|
---|
| 774 | }
|
---|
| 775 | if(buffer[i]=='['&&IsStr==0){
|
---|
| 776 | i++;
|
---|
| 777 | i2=KillReturnCode_InParameter(buffer+i,&rnum,'[',']');
|
---|
| 778 | i+=i2;
|
---|
| 779 | if(buffer[i]!=']') break;
|
---|
| 780 | }
|
---|
| 781 | }
|
---|
| 782 |
|
---|
| 783 | length = lstrlen(buffer);
|
---|
[87] | 784 | }
|
---|
| 785 |
|
---|
[88] | 786 | void BasicSource::SetBuffer( const char *buffer ){
|
---|
| 787 | this->buffer = (char *)calloc( lstrlen(buffer) + 1, 1 );
|
---|
| 788 | lstrcpy( this->buffer, buffer );
|
---|
| 789 | length = lstrlen(buffer);
|
---|
| 790 |
|
---|
| 791 | // ダミー改行をセット
|
---|
| 792 | Realloc( length + 2 );
|
---|
| 793 | SlideString( this->buffer, 2 );
|
---|
| 794 | this->buffer[0] = '\n';
|
---|
| 795 | this->buffer[1] = '\n';
|
---|
[87] | 796 | }
|
---|
[88] | 797 |
|
---|
| 798 | bool BasicSource::ReadFile( const string &filePath ){
|
---|
| 799 | if( !Text::ReadFile( filePath ) ){
|
---|
| 800 | return false;
|
---|
| 801 | }
|
---|
| 802 |
|
---|
| 803 | // 改行コードをCRLFからLFに変換
|
---|
| 804 | ChangeReturnLineChar();
|
---|
| 805 |
|
---|
| 806 | // basic.sbpをインクルード
|
---|
| 807 | const char *headCode = "#include <basic.sbp>\n";
|
---|
| 808 | Realloc( length + lstrlen(headCode) );
|
---|
| 809 | SlideString( buffer, lstrlen(headCode) );
|
---|
| 810 | memcpy( buffer, headCode, lstrlen(headCode) );
|
---|
| 811 |
|
---|
| 812 | // #defineと#requireを初期化
|
---|
| 813 | objDefine.Init();
|
---|
| 814 | requireFiles.clear();
|
---|
| 815 |
|
---|
| 816 | // コメントを削除
|
---|
| 817 | RemoveComments();
|
---|
| 818 |
|
---|
| 819 | // #ifdefディレクティブを処理
|
---|
| 820 | objDefine.DirectiveIfdef( buffer );
|
---|
| 821 |
|
---|
| 822 | //最終行には文字を含ませないようにする
|
---|
| 823 | Realloc( length + 1 );
|
---|
| 824 | lstrcat( buffer, "\n" );
|
---|
| 825 |
|
---|
| 826 | // #include / #require ディレクティブを処理
|
---|
| 827 | DirectiveIncludeOrRequire();
|
---|
| 828 |
|
---|
| 829 | // アンダーバーによる改行を正規表現に戻す
|
---|
| 830 | RemoveReturnLineUnderbar();
|
---|
| 831 |
|
---|
| 832 | // ダミー改行をセット
|
---|
| 833 | Realloc( length + 2 );
|
---|
| 834 | SlideString( buffer, 2 );
|
---|
| 835 | buffer[0] = '\n';
|
---|
| 836 | buffer[1] = '\n';
|
---|
| 837 |
|
---|
| 838 | extern char *basbuf;
|
---|
| 839 | basbuf = GetBuffer();
|
---|
| 840 |
|
---|
| 841 | return true;
|
---|
[87] | 842 | }
|
---|
| 843 |
|
---|
| 844 | bool BasicSource::Generate( const string &genName, const char *buffer ){
|
---|
| 845 | return true;
|
---|
| 846 | }
|
---|
[88] | 847 |
|
---|
| 848 | void BasicSource::Addition( const char *buffer ){
|
---|
| 849 | Realloc( length + lstrlen(buffer) );
|
---|
| 850 | lstrcat( this->buffer, buffer );
|
---|
| 851 | }
|
---|