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

Last change on this file since 353 was 313, checked in by dai_9181, 17 years ago

ステップ実行時の不正終了を回避

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