source: dev/trunk/abdev/BasicCompiler_Common/StrOperation.cpp@ 228

Last change on this file since 228 was 206, checked in by dai_9181, 17 years ago

コード全体のリファクタリングを実施

File size: 11.0 KB
Line 
1#include "stdafx.h"
2
3#include <jenga/include/smoothie/LexicalAnalysis.h>
4#include <jenga/include/smoothie/Source.h>
5
6#include "../BasicCompiler_Common/common.h"
7
8void KillSpaces(char *str1,char *str2){
9 int i,i2,i3,IsStr;
10 for(i=0,i2=0,IsStr=0;;i++,i2++){
11 if((str1[i]==' '||str1[i]=='\t')&&IsStr==0){
12 i3=i+1;
13 while(str1[i3]==' '||str1[i3]=='\t') i3++;
14 if(i!=0){
15 if(i==1){
16 if(IsVariableChar(str1[i-1])&&
17 IsVariableChar(str1[i3])){
18 //識別子間にある省略すべきでない空白
19 str2[i2++]=' ';
20 }
21 }
22 else{
23 if(str1[i-2]!=1&&IsVariableChar(str1[i-1])&&
24 IsVariableChar(str1[i3])){
25 //識別子間にある省略すべきでない空白
26 str2[i2++]=' ';
27 }
28 }
29 }
30 i=i3;
31 }
32 if(str1[i]=='\"') IsStr^=1;
33 str2[i2]=str1[i];
34 if(str1[i]=='\0') break;
35 }
36}
37void KillStringSpaces(char *str){
38 char *s2;
39
40 extern HANDLE hHeap;
41 s2=(char *)HeapAlloc(hHeap,0,lstrlen(str)+1);
42 lstrcpy(s2,str);
43 KillSpaces(s2,str);
44 HeapDefaultFree(s2);
45 return;
46}
47BOOL RemoveStringQuotes(char *str){
48 int i;
49 if(str[0]!='\"') return 0;
50 for(i=0;;i++){
51 str[i]=str[i+1];
52 if(str[i]=='\"') break;
53 }
54 str[i]=0;
55 if(str[i+2]!='\0') return 0;
56 return 1;
57}
58void RemoveStringPare(char *str){
59 int i;
60 i=lstrlen(str);
61 if(str[0]!='('||str[i-1]!=')') return;
62 SlideString(str+1,-1);
63 str[i-2]=0;
64}
65void RemoveStringBracket(char *str){
66 int i;
67 i=lstrlen(str);
68 if(str[0]!='['||str[i-1]!=']') return;
69 SlideString(str+1,-1);
70 str[i-2]=0;
71}
72void SetStringQuotes(char *str){
73 int i;
74 if(str[0]=='\"') return;
75 i=lstrlen(str);
76 SlideString(str,1);
77 str[0]='\"';
78 str[i+1]='\"';
79 str[i+2]=0;
80}
81int FormatString_EscapeSequence(char *buffer){
82 int i2,i3,i4;
83
84 for(i2=0,i3=0;;i2++,i3++){
85 if(IsDBCSLeadByte(buffer[i3])){
86 buffer[i2++]=buffer[i3++];
87 buffer[i2]=buffer[i3];
88 if(buffer[i3]=='\0') break;
89 continue;
90 }
91 if(buffer[i3]=='\\'){
92 switch(buffer[i3+1]){
93 case '\\':
94 buffer[i2]='\\';
95 i3++;
96 break;
97 case '0':
98 buffer[i2]='\0';
99 i3++;
100 break;
101 case 'n':
102 buffer[i2]='\n';
103 i3++;
104 break;
105 case 'q':
106 buffer[i2]='\"';
107 i3++;
108 break;
109 case 'r':
110 buffer[i2]='\r';
111 i3++;
112 break;
113 case 't':
114 buffer[i2]='\t';
115 i3++;
116 break;
117 case 'x':
118 //16進表記
119 char temporary[8];
120 temporary[0]=buffer[i3+2];
121 temporary[1]=buffer[i3+3];
122 temporary[2]=0;
123 CharUpper(temporary);
124
125 i4=temporary[0]-0x30;
126 if(i4<0||9<i4){
127 i4=temporary[0]-0x41+10;
128 if(i4<0xA||0xF<i4){
129 buffer[i2]=buffer[i3];
130 break;
131 }
132 }
133 temporary[0]=i4;
134
135 i4=temporary[1]-0x30;
136 if(i4<0||9<i4){
137 i4=temporary[1]-0x41+10;
138 if(i4<0xA||0xF<i4){
139 buffer[i2]=buffer[i3];
140 break;
141 }
142 }
143 temporary[1]=i4;
144
145 buffer[i2]=temporary[0]*0x10+temporary[1];
146 i3+=3;
147 break;
148 default:
149 buffer[i2]=buffer[i3];
150 break;
151 }
152 }
153 else{
154 buffer[i2]=buffer[i3];
155 if(buffer[i3]=='\0') break;
156 }
157 }
158 return i2;
159}
160void SlideString(char *buffer, int slide){
161 char *temp;
162 temp=(char *)malloc(lstrlen(buffer)+1);
163 lstrcpy(temp,buffer);
164 lstrcpy(buffer+slide,temp);
165 free(temp);
166}
167void SlideBuffer(char *buffer,int length,int slide){
168 void *temp;
169 temp=malloc(length+1);
170 memcpy(temp,buffer,length);
171 memcpy(buffer+slide,temp,length);
172 free(temp);
173}
174
175int GetCpFromLine(int LineNum){
176 extern char *basbuf;
177
178 int i,nCount=0;
179 for(i=0;;i++){
180 if(basbuf[i]=='\n'){
181 nCount++;
182 if(LineNum==nCount){
183 for(i++;;i++){
184 if(IsCommandDelimitation(basbuf[i])) break;
185 }
186 return i;
187 }
188 }
189 }
190}
191BOOL GetLineNum(int pos,int *pLine,char *FileName){
192 extern INCLUDEFILEINFO IncludeFileInfo;
193 extern char *basbuf;
194 int i,i2,i3,i4,i5;
195
196 i=pos;
197 if(basbuf[i]=='\n') i--;
198 for(i3=0,i2=0;i3<i;i3++){
199 if(basbuf[i3]=='\n') i2++;
200 if(basbuf[i3]=='\0') return 0;
201 }
202 i4=0;
203 while(IncludeFileInfo.LineOfFile[i2]!=IncludeFileInfo.LineOfFile[i4]) i4++;
204 for(i3=0,i5=0;i5<i4;i3++){
205 if(basbuf[i3]=='\n') i5++;
206 if(basbuf[i3]=='\0') return 0;
207 }
208 for(i5=0;i4<i2;i3++){
209 if(basbuf[i3]=='\n'){
210 i4++;
211 i5++;
212 if(IncludeFileInfo.LineOfFile[i2]<IncludeFileInfo.LineOfFile[i4]){
213 for(;IncludeFileInfo.LineOfFile[i2]!=IncludeFileInfo.LineOfFile[i4];i3++){
214 if(basbuf[i3]=='\n') i4++;
215 }
216 }
217 }
218 if(basbuf[i3]=='\0') return 0;
219 }
220
221 if(IncludeFileInfo.LineOfFile[i2]==-1){
222 //ファイル・行番号を特定できなかった場合
223 *pLine=-1;
224 FileName[0]=0;
225 }
226 else{
227 //行番号をセット
228 *pLine=i5;
229
230 //ファイル名をセット
231 lstrcpy(FileName,IncludeFileInfo.ppFileNames[IncludeFileInfo.LineOfFile[i2]]);
232 }
233
234 return 1;
235}
236
237char GetEndXXXCommand(char es){
238 switch(es){
239 case ESC_SUB:
240 return ESC_ENDSUB;
241 case ESC_FUNCTION:
242 return ESC_ENDFUNCTION;
243 case ESC_MACRO:
244 return ESC_ENDMACRO;
245 case ESC_TYPE:
246 return ESC_ENDTYPE;
247 case ESC_CLASS:
248 return ESC_ENDCLASS;
249 case ESC_INTERFACE:
250 return ESC_ENDINTERFACE;
251 case ESC_ENUM:
252 return ESC_ENDENUM;
253 }
254 return 0;
255}
256void GetDefaultNameFromES(char es,char *name){
257 name[0]=0;
258 switch(es){
259 case ESC_PSMEM:
260 lstrcpy(name,"->");
261 break;
262 case ESC_MOD:
263 lstrcpy(name,"Mod");
264 break;
265 case ESC_AND:
266 lstrcpy(name,"And");
267 break;
268 case ESC_OR:
269 lstrcpy(name,"Or");
270 break;
271 case ESC_XOR:
272 lstrcpy(name,"Xor");
273 break;
274 case ESC_NOT:
275 lstrcpy(name,"Not");
276 break;
277 case ESC_AS:
278 lstrcpy(name,"As");
279 break;
280 case ESC_THEN:
281 lstrcpy(name,"Then");
282 break;
283 case ESC_ELSE:
284 lstrcpy(name,"Else");
285 break;
286 case ESC_IF:
287 lstrcpy(name,"If");
288 break;
289 case ESC_ELSEIF:
290 lstrcpy(name,"ElseIf");
291 break;
292 case ESC_ENDIF:
293 lstrcpy(name,"End If");
294 break;
295 case ESC_DEF:
296 lstrcpy(name,"Def");
297 break;
298 case ESC_DECLARE:
299 lstrcpy(name,"Declare");
300 break;
301 case ESC_ABSTRACT:
302 lstrcpy(name,"Abstract");
303 break;
304 case ESC_VIRTUAL:
305 lstrcpy(name,"Virtual");
306 break;
307 case ESC_OVERRIDE:
308 lstrcpy(name,"Override");
309 break;
310 case ESC_SUB:
311 lstrcpy(name,"Sub");
312 break;
313 case ESC_ENDSUB:
314 lstrcpy(name,"End Sub");
315 break;
316 case ESC_EXITSUB:
317 lstrcpy(name,"Exit Sub");
318 break;
319 case ESC_FUNCTION:
320 lstrcpy(name,"Function");
321 break;
322 case ESC_ENDFUNCTION:
323 lstrcpy(name,"End Function");
324 break;
325 case ESC_EXITFUNCTION:
326 lstrcpy(name,"Exit Function");
327 break;
328 case ESC_BYVAL:
329 lstrcpy(name,"ByVal ");
330 break;
331 case ESC_BYREF:
332 lstrcpy(name,"ByRef ");
333 break;
334 case ESC_TYPE:
335 lstrcpy(name,"Type");
336 break;
337 case ESC_ENDTYPE:
338 lstrcpy(name,"End Type");
339 break;
340 case ESC_EXITFOR:
341 lstrcpy(name,"Exit For");
342 break;
343 case ESC_EXITWHILE:
344 lstrcpy(name,"Exit While");
345 break;
346 case ESC_EXITDO:
347 lstrcpy(name,"Exit Do");
348 break;
349 case ESC_SELECTCASE:
350 lstrcpy(name,"Select Case");
351 break;
352 case ESC_CASE:
353 lstrcpy(name,"Case");
354 break;
355 case ESC_CASEELSE:
356 lstrcpy(name,"Case Else");
357 break;
358 case ESC_ENDSELECT:
359 lstrcpy(name,"End Select");
360 break;
361 case ESC_CONST:
362 lstrcpy(name,"Const");
363 break;
364 case ESC_TYPEDEF:
365 lstrcpy(name,"TypeDef");
366 break;
367 case ESC_WITH:
368 lstrcpy(name,"With");
369 break;
370 case ESC_ENDWITH:
371 lstrcpy(name,"End With");
372 break;
373 case ESC_CDECL:
374 lstrcpy(name,"cdecl");
375 break;
376 case ESC_MACRO:
377 lstrcpy(name,"Macro");
378 break;
379 case ESC_ENDMACRO:
380 lstrcpy(name,"End Macro");
381 break;
382 case ESC_EXITMACRO:
383 lstrcpy(name,"Exit Macro");
384 break;
385 case ESC_EXPORT:
386 lstrcpy(name,"Export");
387 break;
388 case ESC_CONTINUE:
389 lstrcpy(name,"Continue");
390 break;
391 case ESC_CLASS:
392 lstrcpy(name,"Class");
393 break;
394 case ESC_ENDCLASS:
395 lstrcpy(name,"End Class");
396 break;
397 case ESC_INTERFACE:
398 lstrcpy(name,"Interface");
399 break;
400 case ESC_ENDINTERFACE:
401 lstrcpy(name,"End Interface");
402 break;
403 case ESC_ENUM:
404 lstrcpy(name,"Enum");
405 break;
406 case ESC_ENDENUM:
407 lstrcpy(name,"End Enum");
408 break;
409 }
410}
411const std::string &FormatEscapeSequenceStringToDefaultString( const std::string &source )
412{
413 int maxLength = (int)source.size();
414
415 char *temporary = (char *)malloc( source.size() + 8192 );
416 lstrcpy( temporary, source.c_str() );
417
418 for( int i=0; i<maxLength-1; i++ )
419 {
420 if( temporary[i] == 1 )
421 {
422 char temp2[255];
423 GetDefaultNameFromES( temporary[i+1], temp2 );
424 if( i>0 )
425 {
426 char temp3[255];
427 wsprintf( temp3, " %s", temp2 );
428 lstrcpy( temp2, temp3 );
429 }
430 if( i<maxLength-2 )
431 {
432 lstrcat( temp2, " " );
433 }
434
435 int length = lstrlen( temp2 );
436 SlideString( temporary + i+2, length-2 );
437 memcpy( temporary + i, temp2, length );
438 maxLength = lstrlen( temporary );
439 }
440 }
441
442 static std::string resultStr = "";
443 resultStr = temporary;
444
445 free( temporary );
446
447 return resultStr;
448}
449
450BOOL IsFile(char *path){
451 WIN32_FIND_DATA wfd;
452 HANDLE hFind;
453
454 hFind=FindFirstFile(path,&wfd);
455 if(hFind==INVALID_HANDLE_VALUE){
456 return 0;
457 }
458 FindClose(hFind);
459
460 return 1;
461}
462BOOL GetFolder(HWND hWnd,char *folder,char *OpenFolderTitle){
463 BROWSEINFO BrowseInfo;
464 LPITEMIDLIST pidlBrowse;
465 LPMALLOC g_pMalloc;
466 char temporary[MAX_PATH];
467
468 BrowseInfo.hwndOwner=hWnd;
469 BrowseInfo.pszDisplayName=temporary;
470 BrowseInfo.pidlRoot=NULL;
471 BrowseInfo.lpszTitle=OpenFolderTitle;
472 BrowseInfo.ulFlags=BIF_RETURNONLYFSDIRS;
473 BrowseInfo.lpfn=NULL;
474 BrowseInfo.lParam=(LPARAM)0;
475 BrowseInfo.iImage=0;
476 pidlBrowse=SHBrowseForFolder(&BrowseInfo);
477 if(pidlBrowse){
478 if(SHGetMalloc(&g_pMalloc)!=0) return 0;
479 SHGetPathFromIDList(pidlBrowse,folder);
480 g_pMalloc->Free(pidlBrowse);
481 g_pMalloc->Release();
482 return 1;
483 }
484 return 0;
485}
486
487void ShortPathToLongPath(const char *ShortPath,char *LongPath){
488 HANDLE hFind;
489 WIN32_FIND_DATA wfd;
490 int i;
491 char dummy[MAX_PATH];
492 for(i=0;i<MAX_PATH;i++){
493 LongPath[i]=ShortPath[i];
494 if((ShortPath[i-1]==':'&&ShortPath[i]=='\\')||(ShortPath[i-1]=='\\'&&ShortPath[i]=='\\')){
495 LongPath[i+1]=0;
496 break;
497 }
498 }
499 if(ShortPath[i-1]=='\\'&&ShortPath[i]=='\\'){
500 for(i++;i<MAX_PATH;i++){
501 if(IsDBCSLeadByte(ShortPath[i])){
502 i++;
503 continue;
504 }
505 LongPath[i]=ShortPath[i];
506 if(ShortPath[i]=='\\'){
507 LongPath[i+1]=0;
508 break;
509 }
510 }
511 for(i++;i<MAX_PATH;i++){
512 if(IsDBCSLeadByte(ShortPath[i])){
513 i++;
514 continue;
515 }
516 LongPath[i]=ShortPath[i];
517 if(ShortPath[i]=='\\'){
518 LongPath[i+1]=0;
519 break;
520 }
521 }
522 }
523 for(i++;i<MAX_PATH;i++){
524 if(IsDBCSLeadByte(ShortPath[i])){
525 i++;
526 continue;
527 }
528 if(ShortPath[i]=='\\'||ShortPath[i]=='\0'){
529 strncpy(dummy,ShortPath,i);
530 dummy[i]=0;
531 if((hFind=FindFirstFile(dummy,&wfd))!=INVALID_HANDLE_VALUE) FindClose(hFind);
532 lstrcat(LongPath,wfd.cFileName);
533
534 if(ShortPath[i]=='\0') break;
535 lstrcat(LongPath,"\\");
536
537 if(ShortPath[i]=='\\'&&ShortPath[i+1]=='\0'){
538 break;
539 }
540 }
541 }
542}
Note: See TracBrowser for help on using the repository browser.