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

Last change on this file since 277 was 266, checked in by dai_9181, 17 years ago

BasicSourceのシリアライズがうまくいっていない

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