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

Last change on this file since 389 was 387, checked in by dai_9181, 17 years ago

メンバ情報を保持するようにした

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