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

Last change on this file since 306 was 279, checked in by dai_9181, 17 years ago

sourceをObjectModuleに入れた

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 GetCpFromLine(int LineNum){
177 extern char *basbuf;
178
179 int i,nCount=0;
180 for(i=0;;i++){
181 if(basbuf[i]=='\n'){
182 nCount++;
183 if(LineNum==nCount){
184 for(i++;;i++){
185 if(IsCommandDelimitation(basbuf[i])) break;
186 }
187 return i;
188 }
189 }
190 }
191}
192
193char GetEndXXXCommand(char es){
194 switch(es){
195 case ESC_SUB:
196 return ESC_ENDSUB;
197 case ESC_FUNCTION:
198 return ESC_ENDFUNCTION;
199 case ESC_MACRO:
200 return ESC_ENDMACRO;
201 case ESC_TYPE:
202 return ESC_ENDTYPE;
203 case ESC_CLASS:
204 return ESC_ENDCLASS;
205 case ESC_INTERFACE:
206 return ESC_ENDINTERFACE;
207 case ESC_ENUM:
208 return ESC_ENDENUM;
209 }
210 return 0;
211}
212void GetDefaultNameFromES(char es,char *name){
213 name[0]=0;
214 switch(es){
215 case ESC_PSMEM:
216 lstrcpy(name,"->");
217 break;
218 case ESC_MOD:
219 lstrcpy(name,"Mod");
220 break;
221 case ESC_AND:
222 lstrcpy(name,"And");
223 break;
224 case ESC_OR:
225 lstrcpy(name,"Or");
226 break;
227 case ESC_XOR:
228 lstrcpy(name,"Xor");
229 break;
230 case ESC_NOT:
231 lstrcpy(name,"Not");
232 break;
233 case ESC_AS:
234 lstrcpy(name,"As");
235 break;
236 case ESC_THEN:
237 lstrcpy(name,"Then");
238 break;
239 case ESC_ELSE:
240 lstrcpy(name,"Else");
241 break;
242 case ESC_IF:
243 lstrcpy(name,"If");
244 break;
245 case ESC_ELSEIF:
246 lstrcpy(name,"ElseIf");
247 break;
248 case ESC_ENDIF:
249 lstrcpy(name,"End If");
250 break;
251 case ESC_DEF:
252 lstrcpy(name,"Def");
253 break;
254 case ESC_DECLARE:
255 lstrcpy(name,"Declare");
256 break;
257 case ESC_ABSTRACT:
258 lstrcpy(name,"Abstract");
259 break;
260 case ESC_VIRTUAL:
261 lstrcpy(name,"Virtual");
262 break;
263 case ESC_OVERRIDE:
264 lstrcpy(name,"Override");
265 break;
266 case ESC_SUB:
267 lstrcpy(name,"Sub");
268 break;
269 case ESC_ENDSUB:
270 lstrcpy(name,"End Sub");
271 break;
272 case ESC_EXITSUB:
273 lstrcpy(name,"Exit Sub");
274 break;
275 case ESC_FUNCTION:
276 lstrcpy(name,"Function");
277 break;
278 case ESC_ENDFUNCTION:
279 lstrcpy(name,"End Function");
280 break;
281 case ESC_EXITFUNCTION:
282 lstrcpy(name,"Exit Function");
283 break;
284 case ESC_BYVAL:
285 lstrcpy(name,"ByVal ");
286 break;
287 case ESC_BYREF:
288 lstrcpy(name,"ByRef ");
289 break;
290 case ESC_TYPE:
291 lstrcpy(name,"Type");
292 break;
293 case ESC_ENDTYPE:
294 lstrcpy(name,"End Type");
295 break;
296 case ESC_EXITFOR:
297 lstrcpy(name,"Exit For");
298 break;
299 case ESC_EXITWHILE:
300 lstrcpy(name,"Exit While");
301 break;
302 case ESC_EXITDO:
303 lstrcpy(name,"Exit Do");
304 break;
305 case ESC_SELECTCASE:
306 lstrcpy(name,"Select Case");
307 break;
308 case ESC_CASE:
309 lstrcpy(name,"Case");
310 break;
311 case ESC_CASEELSE:
312 lstrcpy(name,"Case Else");
313 break;
314 case ESC_ENDSELECT:
315 lstrcpy(name,"End Select");
316 break;
317 case ESC_CONST:
318 lstrcpy(name,"Const");
319 break;
320 case ESC_TYPEDEF:
321 lstrcpy(name,"TypeDef");
322 break;
323 case ESC_WITH:
324 lstrcpy(name,"With");
325 break;
326 case ESC_ENDWITH:
327 lstrcpy(name,"End With");
328 break;
329 case ESC_CDECL:
330 lstrcpy(name,"cdecl");
331 break;
332 case ESC_MACRO:
333 lstrcpy(name,"Macro");
334 break;
335 case ESC_ENDMACRO:
336 lstrcpy(name,"End Macro");
337 break;
338 case ESC_EXITMACRO:
339 lstrcpy(name,"Exit Macro");
340 break;
341 case ESC_EXPORT:
342 lstrcpy(name,"Export");
343 break;
344 case ESC_CONTINUE:
345 lstrcpy(name,"Continue");
346 break;
347 case ESC_CLASS:
348 lstrcpy(name,"Class");
349 break;
350 case ESC_ENDCLASS:
351 lstrcpy(name,"End Class");
352 break;
353 case ESC_INTERFACE:
354 lstrcpy(name,"Interface");
355 break;
356 case ESC_ENDINTERFACE:
357 lstrcpy(name,"End Interface");
358 break;
359 case ESC_ENUM:
360 lstrcpy(name,"Enum");
361 break;
362 case ESC_ENDENUM:
363 lstrcpy(name,"End Enum");
364 break;
365 }
366}
367const std::string &FormatEscapeSequenceStringToDefaultString( const std::string &source )
368{
369 int maxLength = (int)source.size();
370
371 char *temporary = (char *)malloc( source.size() + 8192 );
372 lstrcpy( temporary, source.c_str() );
373
374 for( int i=0; i<maxLength-1; i++ )
375 {
376 if( temporary[i] == 1 )
377 {
378 char temp2[255];
379 GetDefaultNameFromES( temporary[i+1], temp2 );
380 if( i>0 )
381 {
382 char temp3[255];
383 wsprintf( temp3, " %s", temp2 );
384 lstrcpy( temp2, temp3 );
385 }
386 if( i<maxLength-2 )
387 {
388 lstrcat( temp2, " " );
389 }
390
391 int length = lstrlen( temp2 );
392 SlideString( temporary + i+2, length-2 );
393 memcpy( temporary + i, temp2, length );
394 maxLength = lstrlen( temporary );
395 }
396 }
397
398 static std::string resultStr = "";
399 resultStr = temporary;
400
401 free( temporary );
402
403 return resultStr;
404}
405
406bool IsFileExist(const char *path){
407 WIN32_FIND_DATA wfd;
408 HANDLE hFind;
409
410 hFind=FindFirstFile(path,&wfd);
411 if(hFind==INVALID_HANDLE_VALUE){
412 return false;
413 }
414 FindClose(hFind);
415
416 return true;
417}
418BOOL GetFolder(HWND hWnd,char *folder,char *OpenFolderTitle){
419 BROWSEINFO BrowseInfo;
420 LPITEMIDLIST pidlBrowse;
421 LPMALLOC g_pMalloc;
422 char temporary[MAX_PATH];
423
424 BrowseInfo.hwndOwner=hWnd;
425 BrowseInfo.pszDisplayName=temporary;
426 BrowseInfo.pidlRoot=NULL;
427 BrowseInfo.lpszTitle=OpenFolderTitle;
428 BrowseInfo.ulFlags=BIF_RETURNONLYFSDIRS;
429 BrowseInfo.lpfn=NULL;
430 BrowseInfo.lParam=(LPARAM)0;
431 BrowseInfo.iImage=0;
432 pidlBrowse=SHBrowseForFolder(&BrowseInfo);
433 if(pidlBrowse){
434 if(SHGetMalloc(&g_pMalloc)!=0) return 0;
435 SHGetPathFromIDList(pidlBrowse,folder);
436 g_pMalloc->Free(pidlBrowse);
437 g_pMalloc->Release();
438 return 1;
439 }
440 return 0;
441}
442
443void ShortPathToLongPath(const char *ShortPath,char *LongPath){
444 HANDLE hFind;
445 WIN32_FIND_DATA wfd;
446 int i;
447 char dummy[MAX_PATH];
448 for(i=0;i<MAX_PATH;i++){
449 LongPath[i]=ShortPath[i];
450 if((ShortPath[i-1]==':'&&ShortPath[i]=='\\')||(ShortPath[i-1]=='\\'&&ShortPath[i]=='\\')){
451 LongPath[i+1]=0;
452 break;
453 }
454 }
455 if(ShortPath[i-1]=='\\'&&ShortPath[i]=='\\'){
456 for(i++;i<MAX_PATH;i++){
457 if(IsDBCSLeadByte(ShortPath[i])){
458 i++;
459 continue;
460 }
461 LongPath[i]=ShortPath[i];
462 if(ShortPath[i]=='\\'){
463 LongPath[i+1]=0;
464 break;
465 }
466 }
467 for(i++;i<MAX_PATH;i++){
468 if(IsDBCSLeadByte(ShortPath[i])){
469 i++;
470 continue;
471 }
472 LongPath[i]=ShortPath[i];
473 if(ShortPath[i]=='\\'){
474 LongPath[i+1]=0;
475 break;
476 }
477 }
478 }
479 for(i++;i<MAX_PATH;i++){
480 if(IsDBCSLeadByte(ShortPath[i])){
481 i++;
482 continue;
483 }
484 if(ShortPath[i]=='\\'||ShortPath[i]=='\0'){
485 strncpy(dummy,ShortPath,i);
486 dummy[i]=0;
487 if((hFind=FindFirstFile(dummy,&wfd))!=INVALID_HANDLE_VALUE) FindClose(hFind);
488 lstrcat(LongPath,wfd.cFileName);
489
490 if(ShortPath[i]=='\0') break;
491 lstrcat(LongPath,"\\");
492
493 if(ShortPath[i]=='\\'&&ShortPath[i+1]=='\0'){
494 break;
495 }
496 }
497 }
498}
Note: See TracBrowser for help on using the repository browser.