source: dev/BasicCompiler_Common/StrOperation.cpp@ 105

Last change on this file since 105 was 77, checked in by dai_9181, 18 years ago

デフォルトパラメータに対応。

File size: 15.3 KB
RevLine 
[4]1#include "../BasicCompiler_Common/common.h"
2
3void KillSpaces(char *str1,char *str2){
4 int i,i2,i3,IsStr;
5 for(i=0,i2=0,IsStr=0;;i++,i2++){
6 if((str1[i]==' '||str1[i]=='\t')&&IsStr==0){
7 i3=i+1;
8 while(str1[i3]==' '||str1[i3]=='\t') i3++;
9 if(i!=0){
10 if(i==1){
11 if(IsVariableChar(str1[i-1])&&
12 IsVariableChar(str1[i3])){
13 //識別子間にある省略すべきでない空白
14 str2[i2++]=' ';
15 }
16 }
17 else{
18 if(str1[i-2]!=1&&IsVariableChar(str1[i-1])&&
19 IsVariableChar(str1[i3])){
20 //識別子間にある省略すべきでない空白
21 str2[i2++]=' ';
22 }
23 }
24 }
25 i=i3;
26 }
27 if(str1[i]=='\"') IsStr^=1;
28 str2[i2]=str1[i];
29 if(str1[i]=='\0') break;
30 }
31}
32void KillStringSpaces(char *str){
33 char *s2;
34
35 extern HANDLE hHeap;
36 s2=(char *)HeapAlloc(hHeap,0,lstrlen(str)+1);
37 lstrcpy(s2,str);
38 KillSpaces(s2,str);
39 HeapDefaultFree(s2);
40 return;
41}
42BOOL RemoveStringQuotes(char *str){
43 int i;
44 if(str[0]!='\"') return 0;
45 for(i=0;;i++){
46 str[i]=str[i+1];
47 if(str[i]=='\"') break;
48 }
49 str[i]=0;
50 if(str[i+2]!='\0') return 0;
51 return 1;
52}
53void RemoveStringPare(char *str){
54 int i;
55 i=lstrlen(str);
56 if(str[0]!='('||str[i-1]!=')') return;
57 SlideString(str+1,-1);
58 str[i-2]=0;
59}
60void RemoveStringBracket(char *str){
61 int i;
62 i=lstrlen(str);
63 if(str[0]!='['||str[i-1]!=']') return;
64 SlideString(str+1,-1);
65 str[i-2]=0;
66}
67void SetStringQuotes(char *str){
68 int i;
69 if(str[0]=='\"') return;
70 i=lstrlen(str);
71 SlideString(str,1);
72 str[0]='\"';
73 str[i+1]='\"';
74 str[i+2]=0;
75}
76int FormatString_EscapeSequence(char *buffer){
77 int i2,i3,i4;
78
79 for(i2=0,i3=0;;i2++,i3++){
80 if(IsDBCSLeadByte(buffer[i3])){
81 buffer[i2++]=buffer[i3++];
82 buffer[i2]=buffer[i3];
83 if(buffer[i3]=='\0') break;
84 continue;
85 }
86 if(buffer[i3]=='\\'){
87 switch(buffer[i3+1]){
88 case '\\':
89 buffer[i2]='\\';
90 i3++;
91 break;
92 case '0':
93 buffer[i2]='\0';
94 i3++;
95 break;
96 case 'n':
97 buffer[i2]='\n';
98 i3++;
99 break;
100 case 'q':
101 buffer[i2]='\"';
102 i3++;
103 break;
104 case 'r':
105 buffer[i2]='\r';
106 i3++;
107 break;
108 case 't':
109 buffer[i2]='\t';
110 i3++;
111 break;
112 case 'x':
113 //16進表記
114 char temporary[8];
115 temporary[0]=buffer[i3+2];
116 temporary[1]=buffer[i3+3];
117 temporary[2]=0;
118 CharUpper(temporary);
119
120 i4=temporary[0]-0x30;
121 if(i4<0||9<i4){
122 i4=temporary[0]-0x41+10;
123 if(i4<0xA||0xF<i4){
124 buffer[i2]=buffer[i3];
125 break;
126 }
127 }
128 temporary[0]=i4;
129
130 i4=temporary[1]-0x30;
131 if(i4<0||9<i4){
132 i4=temporary[1]-0x41+10;
133 if(i4<0xA||0xF<i4){
134 buffer[i2]=buffer[i3];
135 break;
136 }
137 }
138 temporary[1]=i4;
139
140 buffer[i2]=temporary[0]*0x10+temporary[1];
141 i3+=3;
142 break;
143 default:
144 buffer[i2]=buffer[i3];
145 break;
146 }
147 }
148 else{
149 buffer[i2]=buffer[i3];
150 if(buffer[i3]=='\0') break;
151 }
152 }
153 return i2;
154}
155void SlideString(char *buffer, int slide){
156 char *temp;
157 temp=(char *)malloc(lstrlen(buffer)+1);
158 lstrcpy(temp,buffer);
159 lstrcpy(buffer+slide,temp);
160 free(temp);
161}
162void SlideBuffer(char *buffer,int length,int slide){
163 void *temp;
164 temp=malloc(length+1);
165 memcpy(temp,buffer,length);
166 memcpy(buffer+slide,temp,length);
167 free(temp);
168}
169_int8 IsCommandDelimitation(char c){
170 if(c=='\n'||c==':'||c=='\0') return 1;
171 return 0;
172}
173BOOL IsBlank(char c){
174 if(c==' '||c=='\t') return 1;
175 return 0;
176}
[77]177int GetOneParameter(const char *Parameter,int pos,char *retAns){
[4]178 int i,i2,i3,IsStr;
179 for(i=pos,i2=0,IsStr=0;;i++,i2++){
180 if(Parameter[i]=='\"') IsStr^=1;
181 else if(Parameter[i]=='('&&IsStr==0){
182 i3=GetStringInPare(retAns+i2,Parameter+i);
183 i+=i3-1;
184 i2+=i3-1;
185 continue;
186 }
187 else if(Parameter[i]=='['&&IsStr==0){
188 i3=GetStringInBracket(retAns+i2,Parameter+i);
189 i+=i3-1;
190 i2+=i3-1;
191 continue;
192 }
193 else if(Parameter[i]==','&&IsStr==0){
194 retAns[i2]=0;
195 i++;
196 break;
197 }
[77]198
199 if(IsCommandDelimitation(Parameter[i])&&IsStr==0
200 || Parameter[i] == ')' && IsStr == 0 ){
201 retAns[i2]=0;
202 break;
203 }
204
[4]205 retAns[i2]=Parameter[i];
206 }
207 return i;
208}
209int JumpOneParameter(char *Parameter,int i){
210 int i2,IsStr;
211 for(i2=0,IsStr=0;;i++,i2++){
212 if(Parameter[i]=='\"') IsStr^=1;
213 else if(Parameter[i]=='('&&IsStr==0){
214 i=JumpStringInPare(Parameter,i+1);
215 continue;
216 }
217 else if(Parameter[i]=='['&&IsStr==0){
218 i=JumpStringInBracket(Parameter,i+1);
219 continue;
220 }
221 else if(Parameter[i]==','&&IsStr==0){
222 i++;
223 break;
224 }
225 if(IsCommandDelimitation(Parameter[i])) break;
226 }
227 return i;
228}
229int GetStringInQuotation(char *buffer,char *ReadBuffer){
230 int i;
231
232 if(ReadBuffer[0]=='\"'){
233 buffer[0]=ReadBuffer[0];
234 i=1;
235 }
236 else i=0;
237
238 for(;;i++){
239 buffer[i]=ReadBuffer[i];
240 if(IsDBCSLeadByte(ReadBuffer[i])){
241 i++;
242 buffer[i]=ReadBuffer[i];
243 continue;
244 }
245 if(ReadBuffer[i]=='\"'){
246 i++;
247 buffer[i]=0;
248 break;
249 }
250 if(ReadBuffer[i]=='\0') return 0;
251 }
252 return i;
253}
[15]254int GetStringInPare(char *buffer,const char *ReadBuffer){
[4]255 int i,IsStr,PareNum;
256 for(i=0,IsStr=0,PareNum=0;;i++){
257 buffer[i]=ReadBuffer[i];
258 if(IsDBCSLeadByte(ReadBuffer[i])){
259 i++;
260 buffer[i]=ReadBuffer[i];
261 continue;
262 }
263 if(ReadBuffer[i]=='\"') IsStr^=1;
264 else if(ReadBuffer[i]=='('&&IsStr==0) PareNum++;
265 else if(ReadBuffer[i]==')'&&IsStr==0){
266 PareNum--;
267 if(PareNum==0){
268 i++;
269 buffer[i]=0;
270 break;
271 }
272 }
273 else if(ReadBuffer[i]=='\0') return 0;
274 }
275 return i;
276}
277int GetStringInPare_RemovePare(char *buffer,char *ReadBuffer){
278 int i,IsStr,PareNum;
279 for(i=0,IsStr=0,PareNum=1;;i++){
280 buffer[i]=ReadBuffer[i];
281 if(IsDBCSLeadByte(ReadBuffer[i])){
282 i++;
283 buffer[i]=ReadBuffer[i];
284 continue;
285 }
286 if(ReadBuffer[i]=='\"') IsStr^=1;
287 else if(ReadBuffer[i]=='('&&IsStr==0) PareNum++;
288 else if(ReadBuffer[i]==')'&&IsStr==0){
289 PareNum--;
290 if(PareNum==0){
291 buffer[i]=0;
292 break;
293 }
294 }
295 else if(ReadBuffer[i]=='\0') return 0;
296 }
297 return i;
298}
[15]299int GetStringInBracket(char *buffer,const char *ReadBuffer){
[4]300 int i,IsStr,PareNum;
301 for(i=0,IsStr=0,PareNum=0;;i++){
302 buffer[i]=ReadBuffer[i];
303 if(IsDBCSLeadByte(ReadBuffer[i])){
304 i++;
305 buffer[i]=ReadBuffer[i];
306 continue;
307 }
308 if(ReadBuffer[i]=='\"') IsStr^=1;
309 else if(ReadBuffer[i]=='['&&IsStr==0) PareNum++;
310 else if(ReadBuffer[i]==']'&&IsStr==0){
311 PareNum--;
312 if(PareNum==0){
313 i++;
314 buffer[i]=0;
315 break;
316 }
317 }
318 else if(ReadBuffer[i]=='\0') return 0;
319 }
320 return i;
321}
[49]322int JumpStringInPare(const char *buffer,int pos){
[4]323 int PareNum;
324 for(PareNum=1;;pos++){
325 if(buffer[pos]=='\"'){
326 for(pos++;;pos++){
327 if(buffer[pos]=='\"') break;
328 }
329 continue;
330 }
331 else if(buffer[pos]=='(') PareNum++;
332 else if(buffer[pos]==')'){
333 PareNum--;
334 if(PareNum==0) return pos;
335 }
336 else if(buffer[pos]=='\0') break;
337 }
338 return 0;
339}
[49]340int JumpStringInBracket(const char *buffer,int pos){
[4]341 int PareNum;
342 for(PareNum=1;;pos++){
343 if(buffer[pos]=='\"'){
344 for(pos++;;pos++){
345 if(buffer[pos]=='\"') break;
346 }
347 continue;
348 }
349 else if(buffer[pos]=='[') PareNum++;
350 else if(buffer[pos]==']'){
351 PareNum--;
352 if(PareNum==0) return pos;
353 }
354 else if(buffer[pos]=='\0') break;
355 }
356 return 0;
357}
358
359int GetCpFromLine(int LineNum){
360 extern char *basbuf;
361
362 int i,nCount=0;
363 for(i=0;;i++){
364 if(basbuf[i]=='\n'){
365 nCount++;
366 if(LineNum==nCount){
367 for(i++;;i++){
368 if(IsCommandDelimitation(basbuf[i])) break;
369 }
370 return i;
371 }
372 }
373 }
374}
375BOOL GetLineNum(int pos,int *pLine,char *FileName){
376 extern INCLUDEFILEINFO IncludeFileInfo;
377 extern char *basbuf;
378 int i,i2,i3,i4,i5;
379
380 i=pos;
381 if(basbuf[i]=='\n') i--;
382 for(i3=0,i2=0;i3<i;i3++){
383 if(basbuf[i3]=='\n') i2++;
384 if(basbuf[i3]=='\0') return 0;
385 }
386 i4=0;
387 while(IncludeFileInfo.LineOfFile[i2]!=IncludeFileInfo.LineOfFile[i4]) i4++;
388 for(i3=0,i5=0;i5<i4;i3++){
389 if(basbuf[i3]=='\n') i5++;
390 if(basbuf[i3]=='\0') return 0;
391 }
392 for(i5=0;i4<i2;i3++){
393 if(basbuf[i3]=='\n'){
394 i4++;
395 i5++;
396 if(IncludeFileInfo.LineOfFile[i2]<IncludeFileInfo.LineOfFile[i4]){
397 for(;IncludeFileInfo.LineOfFile[i2]!=IncludeFileInfo.LineOfFile[i4];i3++){
398 if(basbuf[i3]=='\n') i4++;
399 }
400 }
401 }
402 if(basbuf[i3]=='\0') return 0;
403 }
404
405 if(IncludeFileInfo.LineOfFile[i2]==-1){
406 //ファイル・行番号を特定できなかった場合
407 *pLine=-1;
408 FileName[0]=0;
409 }
410 else{
411 //行番号をセット
412 *pLine=i5;
413
414 //ファイル名をセット
415 lstrcpy(FileName,IncludeFileInfo.ppFileNames[IncludeFileInfo.LineOfFile[i2]]);
416 }
417
418 return 1;
419}
420
421char GetEndXXXCommand(char es){
422 switch(es){
423 case ESC_SUB:
424 return ESC_ENDSUB;
425 case ESC_FUNCTION:
426 return ESC_ENDFUNCTION;
427 case ESC_MACRO:
428 return ESC_ENDMACRO;
429 case ESC_TYPE:
430 return ESC_ENDTYPE;
431 case ESC_CLASS:
432 return ESC_ENDCLASS;
433 case ESC_INTERFACE:
434 return ESC_ENDINTERFACE;
435 case ESC_ENUM:
436 return ESC_ENDENUM;
437 }
438 return 0;
439}
440void GetDefaultNameFromES(char es,char *name){
441 name[0]=0;
442 switch(es){
443 case ESC_PSMEM:
444 lstrcpy(name,"->");
445 break;
446 case ESC_MOD:
447 lstrcpy(name,"Mod");
448 break;
449 case ESC_AND:
450 lstrcpy(name,"And");
451 break;
452 case ESC_OR:
453 lstrcpy(name,"Or");
454 break;
455 case ESC_XOR:
456 lstrcpy(name,"Xor");
457 break;
458 case ESC_NOT:
459 lstrcpy(name,"Not");
460 break;
461 case ESC_AS:
462 lstrcpy(name,"As");
463 break;
464 case ESC_THEN:
465 lstrcpy(name,"Then");
466 break;
467 case ESC_ELSE:
468 lstrcpy(name,"Else");
469 break;
470 case ESC_IF:
471 lstrcpy(name,"If");
472 break;
473 case ESC_ELSEIF:
474 lstrcpy(name,"ElseIf");
475 break;
476 case ESC_ENDIF:
477 lstrcpy(name,"End If");
478 break;
479 case ESC_DEF:
480 lstrcpy(name,"Def");
481 break;
482 case ESC_DECLARE:
483 lstrcpy(name,"Declare");
484 break;
485 case ESC_ABSTRACT:
486 lstrcpy(name,"Abstract");
487 break;
488 case ESC_VIRTUAL:
489 lstrcpy(name,"Virtual");
490 break;
491 case ESC_OVERRIDE:
492 lstrcpy(name,"Override");
493 break;
494 case ESC_SUB:
495 lstrcpy(name,"Sub");
496 break;
497 case ESC_ENDSUB:
498 lstrcpy(name,"End Sub");
499 break;
500 case ESC_EXITSUB:
501 lstrcpy(name,"Exit Sub");
502 break;
503 case ESC_FUNCTION:
504 lstrcpy(name,"Function");
505 break;
506 case ESC_ENDFUNCTION:
507 lstrcpy(name,"End Function");
508 break;
509 case ESC_EXITFUNCTION:
510 lstrcpy(name,"Exit Function");
511 break;
512 case ESC_BYVAL:
513 lstrcpy(name,"ByVal ");
514 break;
515 case ESC_BYREF:
516 lstrcpy(name,"ByRef ");
517 break;
518 case ESC_TYPE:
519 lstrcpy(name,"Type");
520 break;
521 case ESC_ENDTYPE:
522 lstrcpy(name,"End Type");
523 break;
524 case ESC_EXITFOR:
525 lstrcpy(name,"Exit For");
526 break;
527 case ESC_EXITWHILE:
528 lstrcpy(name,"Exit While");
529 break;
530 case ESC_EXITDO:
531 lstrcpy(name,"Exit Do");
532 break;
533 case ESC_SELECTCASE:
534 lstrcpy(name,"Select Case");
535 break;
536 case ESC_CASE:
537 lstrcpy(name,"Case");
538 break;
539 case ESC_CASEELSE:
540 lstrcpy(name,"Case Else");
541 break;
542 case ESC_ENDSELECT:
543 lstrcpy(name,"End Select");
544 break;
545 case ESC_CONST:
546 lstrcpy(name,"Const");
547 break;
548 case ESC_TYPEDEF:
549 lstrcpy(name,"TypeDef");
550 break;
551 case ESC_WITH:
552 lstrcpy(name,"With");
553 break;
554 case ESC_ENDWITH:
555 lstrcpy(name,"End With");
556 break;
557 case ESC_CDECL:
558 lstrcpy(name,"cdecl");
559 break;
560 case ESC_MACRO:
561 lstrcpy(name,"Macro");
562 break;
563 case ESC_ENDMACRO:
564 lstrcpy(name,"End Macro");
565 break;
566 case ESC_EXITMACRO:
567 lstrcpy(name,"Exit Macro");
568 break;
569 case ESC_EXPORT:
570 lstrcpy(name,"Export");
571 break;
572 case ESC_CONTINUE:
573 lstrcpy(name,"Continue");
574 break;
575 case ESC_CLASS:
576 lstrcpy(name,"Class");
577 break;
578 case ESC_ENDCLASS:
579 lstrcpy(name,"End Class");
580 break;
581 case ESC_INTERFACE:
582 lstrcpy(name,"Interface");
583 break;
584 case ESC_ENDINTERFACE:
585 lstrcpy(name,"End Interface");
586 break;
587 case ESC_ENUM:
588 lstrcpy(name,"Enum");
589 break;
590 case ESC_ENDENUM:
591 lstrcpy(name,"End Enum");
592 break;
593 }
594}
595void GetCalcName(int idCalc,char *name){
596 switch(idCalc){
597 case CALC_XOR:
598 lstrcpy(name,"xor");
599 break;
600 case CALC_OR:
601 lstrcpy(name,"or");
602 break;
603 case CALC_AND:
604 lstrcpy(name,"and");
605 break;
606 case CALC_NOT:
607 lstrcpy(name,"Not");
608 break;
609
610 case CALC_PE:
611 lstrcpy(name,"<=");
612 break;
613 case CALC_QE:
614 lstrcpy(name,">=");
615 break;
616 case CALC_NOTEQUAL:
617 lstrcpy(name,"<>");
618 break;
619 case CALC_SUBSITUATION:
620 case CALC_EQUAL:
621 lstrcpy(name,"=");
622 break;
623 case CALC_P:
624 lstrcpy(name,"<");
625 break;
626 case CALC_Q:
627 lstrcpy(name,">");
628 break;
629
630 case CALC_SHL:
631 lstrcpy(name,"<<");
632 break;
633 case CALC_SHR:
634 lstrcpy(name,">>");
635 break;
636 case CALC_ADDITION:
637 lstrcpy(name,"+");
638 break;
639 case CALC_SUBTRACTION:
640 lstrcpy(name,"-");
641 break;
642 case CALC_MOD:
643 lstrcpy(name,"mod");
644 break;
645 case CALC_PRODUCT:
646 lstrcpy(name,"*");
647 break;
648 case CALC_QUOTIENT:
649 lstrcpy(name,"/");
650 break;
651 case CALC_INTQUOTIENT:
652 lstrcpy(name,"\\");
653 break;
654 case CALC_AS:
655 lstrcpy(name,"As");
656 break;
[41]657 case CALC_BYVAL:
658 lstrcpy(name,"ByVal");
659 break;
[4]660 case CALC_MINUSMARK:
661 lstrcpy(name,"-");
662 break;
663 case CALC_POWER:
664 lstrcpy(name,"^");
665 break;
666
667 case CALC_ARRAY_GET:
668 lstrcpy(name,"[]");
669 break;
670 case CALC_ARRAY_SET:
671 lstrcpy(name,"[]=");
672 break;
673 }
674}
675
676BOOL IsFile(char *path){
677 WIN32_FIND_DATA wfd;
678 HANDLE hFind;
679
680 hFind=FindFirstFile(path,&wfd);
681 if(hFind==INVALID_HANDLE_VALUE){
682 return 0;
683 }
684 FindClose(hFind);
685
686 return 1;
687}
688BOOL GetFolder(HWND hWnd,char *folder,char *OpenFolderTitle){
689 BROWSEINFO BrowseInfo;
690 LPITEMIDLIST pidlBrowse;
691 LPMALLOC g_pMalloc;
692 char temporary[MAX_PATH];
693
694 BrowseInfo.hwndOwner=hWnd;
695 BrowseInfo.pszDisplayName=temporary;
696 BrowseInfo.pidlRoot=NULL;
697 BrowseInfo.lpszTitle=OpenFolderTitle;
698 BrowseInfo.ulFlags=BIF_RETURNONLYFSDIRS;
699 BrowseInfo.lpfn=NULL;
700 BrowseInfo.lParam=(LPARAM)0;
701 BrowseInfo.iImage=0;
702 pidlBrowse=SHBrowseForFolder(&BrowseInfo);
703 if(pidlBrowse){
704 if(SHGetMalloc(&g_pMalloc)!=0) return 0;
705 SHGetPathFromIDList(pidlBrowse,folder);
706 g_pMalloc->Free(pidlBrowse);
707 g_pMalloc->Release();
708 return 1;
709 }
710 return 0;
711}
[31]712
713void ShortPathToLongPath(const char *ShortPath,char *LongPath){
714 HANDLE hFind;
715 WIN32_FIND_DATA wfd;
716 int i;
717 char dummy[MAX_PATH];
718 for(i=0;i<MAX_PATH;i++){
719 LongPath[i]=ShortPath[i];
720 if((ShortPath[i-1]==':'&&ShortPath[i]=='\\')||(ShortPath[i-1]=='\\'&&ShortPath[i]=='\\')){
721 LongPath[i+1]=0;
722 break;
723 }
724 }
725 if(ShortPath[i-1]=='\\'&&ShortPath[i]=='\\'){
726 for(i++;i<MAX_PATH;i++){
727 if(IsDBCSLeadByte(ShortPath[i])){
728 i++;
729 continue;
730 }
731 LongPath[i]=ShortPath[i];
732 if(ShortPath[i]=='\\'){
733 LongPath[i+1]=0;
734 break;
735 }
736 }
737 for(i++;i<MAX_PATH;i++){
738 if(IsDBCSLeadByte(ShortPath[i])){
739 i++;
740 continue;
741 }
742 LongPath[i]=ShortPath[i];
743 if(ShortPath[i]=='\\'){
744 LongPath[i+1]=0;
745 break;
746 }
747 }
748 }
749 for(i++;i<MAX_PATH;i++){
750 if(IsDBCSLeadByte(ShortPath[i])){
751 i++;
752 continue;
753 }
754 if(ShortPath[i]=='\\'||ShortPath[i]=='\0'){
755 strncpy(dummy,ShortPath,i);
756 dummy[i]=0;
757 if((hFind=FindFirstFile(dummy,&wfd))!=INVALID_HANDLE_VALUE) FindClose(hFind);
758 lstrcat(LongPath,wfd.cFileName);
759
760 if(ShortPath[i]=='\0') break;
761 lstrcat(LongPath,"\\");
762
763 if(ShortPath[i]=='\\'&&ShortPath[i+1]=='\0'){
764 break;
765 }
766 }
767 }
768}
Note: See TracBrowser for help on using the repository browser.