source: GTP/trunk/Lib/Vis/Preprocessing/manual/code/c2tex.cpp @ 2066

Revision 2066, 8.5 KB checked in by mattausch, 17 years ago (diff)

worked on integration manual

Line 
1
2/**************************************************************************
3c2tex - Konverze C++ do TeXu
4Jiri Bittner, Cerven 1996
5**************************************************************************/
6
7#include <stdlib.h>
8#include <stdio.h>
9#include <string.h>
10#include <iostream.h>
11#include <fstream.h>
12
13
14#define KEYWORD1        1       // kody pro jednotlive znacky
15#define KEYWORD2        2
16#define SYMBOL          3
17#define COMMENT        4
18#define NORMAL          5
19
20#define WORD    10
21
22#define S_LEN   256
23
24int usePascal=0;
25
26char comment_1b='/',comment_2b='*',comment_1e='*',comment_2e='/';
27
28class BaseC
29{
30
31};
32
33
34typedef double Real;
35
36
37class OptionsC : public BaseC
38{
39public:
40
41  int    number;
42  char **strings;
43 
44//  OptionsC() { number=0; }
45  OptionsC(int n,char **a) { init(n,a); }
46
47  void init(int n,char **s) { number=n; strings=s; }
48  int isOption(char *string);
49  int isOption(char *string,int *number);
50  int getParam(char *s,int n=1);
51
52  int getOptionValue(char *string,int *result);
53  int getOptionValue(char *string,Real *r);
54  int getOptionValue(char *string,char *result);
55
56};
57
58
59class LexanC: public BaseC
60{
61public:
62FILE *file;
63
64int     nsep,nkey[2];
65char    *separators;
66char    **key[2];
67
68LexanC(){init("default.lex");}
69
70int     isSeparator(int c){ return strchr(separators,c)!=NULL; }
71int     isKey(char *s);
72
73int     init(const char *lexname);
74int     open(const char *filename);
75
76int     readWord(FILE *f,char *string);
77
78int     read(char *string);
79void    close(){fclose(file);}
80
81virtual ostream& operator>>(ostream &s) {
82        s<<nsep<<"\n"<<nkey[0]<<"\n"<<nkey[1]<<"\n";
83        return s;
84                                        }
85~LexanC(){fclose(file);}
86};
87
88class TexC: public BaseC
89{
90public:
91  FILE *file;
92  int numbers;
93  int lnumber;
94 
95  TexC(){numbers=0; init("output.tex");}
96  TexC(int n, const char *s){
97    lnumber=0;
98    numbers=n; init(s);}
99 
100  int init(const char *filename);
101  int write(const int code,const char *string);
102  ~TexC() { /* fprintf(file,"\n\\bye\n"); */ fclose(file);}
103
104};
105
106
107
108
109
110
111
112
113int OptionsC::isOption(char *s)
114{
115int found=0;
116 
117 for (int i=1;i<number;i++)
118   if (strcmp(s,strings[i])==0) { found = i; break;}
119
120 return found;
121}
122
123int OptionsC::isOption(char *s,int *n)
124{
125int found=0;
126
127for (int i=1;i<number;i++)
128  if (strcmp(s,strings[i])==0)
129    {
130      if (i+1<number)
131        {
132          *n=atoi(strings[i+1]);
133          found = i;
134          break;
135        }
136    }
137
138return found;
139}
140
141
142// search for occurence of non-option
143int OptionsC::getParam(char *s,int n)
144{
145int i,k=1,found=0;
146
147for (i=1;i<number;i++)
148  if (strings[i][0]!='-')
149    if (k==n) { found=1; break;}
150       else
151         k++;
152
153if (found) strcpy(s,strings[i]);
154
155return found;
156}
157
158
159
160int OptionsC::getOptionValue(char *s,int *result)
161{
162int found=0;
163char *pp;
164
165for (int i=1;i<number;i++)
166  {
167    if (strstr(strings[i],s))
168    {
169      pp=strings[i]+strlen(s);
170      *result=atoi(pp);
171      found = i;
172      break;
173    }
174  }
175return found;
176}
177
178int
179OptionsC::getOptionValue(char *s,Real *result)
180{
181int found=0;
182char *pp;
183
184for (int i=1;i<number;i++)
185  {
186    if (strstr(strings[i],s))
187    {
188      pp=strings[i]+strlen(s);
189      *result=atof(pp);
190      found = i;
191      break;
192    }
193  }
194return found;
195}
196
197int OptionsC::getOptionValue(char *s,char *result)
198{
199int found=0;
200char *pp;
201
202for (int i=1;i<number;i++)
203  {
204    if (strstr(strings[i],s))
205    {
206      pp=strings[i]+strlen(s);
207      strcpy(result,pp);
208      found = i;
209      break;
210    }
211  }
212return found;
213}
214
215
216int TexC::write(const int code,const char *string)
217{
218static int line=1;
219static int i=0;
220const char *sp;
221
222switch(code) {
223 
224 case KEYWORD1 : fprintf(file,"\\keya{}"); break;
225 case KEYWORD2 : fprintf(file,"\\keyb{}"); break;
226 case COMMENT  : fprintf(file,"\\comment{}"); break;
227 case SYMBOL   : fprintf(file,"\\symbol{}"); break;
228 case NORMAL   : fprintf(file,"\\normal{}"); break;
229}
230 
231 for(sp=string;*sp!=0;sp++,i++)
232   {
233     if (line) {
234       fprintf(file,"\\leftline{"); line=0;
235       if (strstr(string, "Algorithm") == string)
236         lnumber = 0;
237       if (numbers && lnumber)
238         fprintf(file,"%2d:\\ \\ \\ ",lnumber);
239       lnumber++;
240     }
241     
242     switch(*sp) {
243       
244#define TAB_SIZE        8
245     case '\t' :       
246       //               fprintf(file,"\\tab"); break;
247       int n,j;
248       n=((i/TAB_SIZE)+1)*TAB_SIZE-i;
249       for(j=0;j<n;j++) fprintf(file,"\\ ");
250       break;
251       
252       
253     case '\n':         fprintf(file," }\n");
254       line=1; i=0;
255       break;
256     case ' ' : fprintf(file,"\\ ");
257      break;
258      case '&' :        fprintf(file,"\\&"); break;
259      case '#' :        fprintf(file,"\\#"); break;
260        //      case '_' :      fprintf(file,"\\_{}"); break;
261      case '_' :        fprintf(file,"_"); break;
262      case '^' :        fprintf(file,"\\^{}"); break;
263      case '%' :        fprintf(file,"\\%%"); break;
264      case '{' :        fprintf(file,"$\\{$"); break;
265      case '}' :        fprintf(file,"$\\}$"); break;
266        //  case '\\' : fprintf(file,"$\\backslash$"); break;
267        //  case '$' :  fprintf(file,"\\$"); break;
268    //  case '<' :      fprintf(file,"\\<"); break;
269    //  case '>' :      fprintf(file,"\\>"); break;
270
271  case '~' :    fprintf(file,"\\~{}"); break;
272     
273   
274  default  :    fputc(*sp,file);
275  }
276}
277return 1;
278}
279
280
281int     LexanC::isKey(char *s)
282{
283int i,j;
284
285for (i=0;i<2;i++)
286 for(j=0;j<nkey[i];j++)
287  if(strcmp(s,key[i][j])==0) return i+1;
288
289return 0;
290}
291
292
293int     LexanC::readWord(FILE *f,char *s)
294{
295int     i=0;
296int     c;
297
298do
299{
300        c=fgetc(f);
301        if (c==EOF) return 0;
302        if (!isSeparator(c)) break;
303} while(1);
304
305
306do
307{
308        s[i++]=c;
309        c=fgetc(f);
310        if (c==EOF) break;
311        if (isSeparator(c)) break;
312} while(1);
313
314s[i]=0;
315return 1;
316}
317
318
319int     LexanC::init(const char *lexname)
320{
321FILE *f;
322
323if ((f=fopen(lexname,"rt"))==NULL) return 0;
324
325
326#define         NKEY    512
327
328separators = new char[256];
329key[0]   = new char*[NKEY];
330key[1]   = new char*[NKEY];
331
332nsep=0;
333separators[nsep++]=' ';
334separators[nsep++]='\n';
335separators[nsep++]='\t';
336
337fgets(separators+nsep,100,f);
338nsep=strlen(separators);
339
340int i=0,type=0;
341char s[64];
342
343while(readWord(f,s))
344        {
345        if (strcmp(s,"KEYWORDS2")==0)
346        {
347            printf("KEYWORDS2\n");
348            nkey[type]=i;
349            type++;
350            i=0; 
351        }
352        else
353        {
354                printf("%s\n",s);       
355                key[type][i]=new char[strlen(s)+1];
356                strcpy(key[type][i],s);
357                i++;
358        }
359        }
360
361nkey[type]=i;
362
363fclose(f);
364
365 
366return 1;
367}
368
369int     LexanC::open(const char *filename)
370{
371file=fopen(filename,"rt");
372return file!=NULL;
373}
374
375int     LexanC::read(char *s)
376{
377int     i=0;
378int     type=EOF;
379int     wasSlash=0;
380static  int     c=-2;
381
382if (c==-2)  c=fgetc(file);
383
384if (c==EOF) return c;
385
386do{
387if (isSeparator(c)) {
388        type=SYMBOL;
389        s[i++]=c;
390
391        if(wasSlash) {
392          if(c==comment_1b)  // line comment
393            {
394              if (i>2) { c=s[i-2]; fseek(file,-1,SEEK_CUR); s[i-2]=0;
395              return type; }
396              fgets(s+i,S_LEN,file);
397              c=fgetc(file);
398              return COMMENT;
399            }
400          else
401            if(c==comment_2b)  // line comment
402              {
403                if (i>2) { c=s[i-2]; fseek(file,-1,SEEK_CUR); s[i-2]=0;
404                return type; }
405                c=fgetc(file);
406                if(c!=EOF)
407                  {
408                    s[i++]=c;
409                    int count=1;
410                        do{
411                          c=fgetc(file);
412                          if(c==EOF) break;
413                          s[i++]=c;
414                          if (s[i-2]==comment_1e && s[i-1]==comment_2e) count--;
415                          if (s[i-2]==comment_1b &&s[i-1]==comment_2b) count++;
416                        } while(count!=0);
417                      }
418                    s[i]=0;
419                    c=fgetc(file);
420                    return COMMENT;
421                  }
422            }
423          else
424            wasSlash=(c==comment_1b);   
425} else
426  if (c=='`') {
427    if (i>=1) {
428      s[i]=0;
429      return type;
430    }
431    c=fgetc(file);
432    while(c!='`') {
433      if (c==EOF) break;
434      s[i++]=c;
435      c=fgetc(file);
436    }
437    s[i]=0;
438    c=fgetc(file);
439    return WORD;
440  } else {
441    if (type==EOF) type=WORD;
442    break;
443  }
444} while((c=fgetc(file))!=EOF);
445 
446if (c!=EOF)
447if (type==WORD)
448{
449do
450{
451s[i++]=c;
452c=fgetc(file);
453if (c==EOF) break;
454} while(!isSeparator(c));
455
456s[i]=0;
457int k=isKey(s);
458if (k==0) return NORMAL;
459return k;
460}
461
462s[i]=0;
463return type;
464
465}
466
467int TexC::init(const char *filename)
468{
469file=fopen(filename,"wt");
470fprintf(file,"\\input default.mac\n");
471return file!=NULL;
472}
473
474
475void Help()
476{
477  printf("Syntax : c2tex in_file [out_file] [-usePascal] [-numbers]\n\n");
478  printf("Default out_file is output.tex.\n");
479  printf("-usePascal     use usePascal comments (* *).\n");
480  printf("-numbers    print line numbers.\n");
481  exit(1);
482}
483
484int main(int argc, char **argv)
485{
486char    s[16384]; // docasny buffer
487
488OptionsC options(argc,argv);
489 
490 if (argc<2) Help();
491 if (argc>=3 && (*argv[2]!='-') )
492   strcpy(s,argv[2]);
493 else
494   strcpy(s,"output.tex");
495 
496 if (options.isOption("-usePascal")) {
497    usePascal=1;
498    comment_1b='(';
499    comment_2b='*';
500    comment_1e='*';
501    comment_2e=')';
502 }
503
504 int numbers;
505 
506 numbers = options.isOption("-numbers");
507 
508 
509  LexanC        lexan;
510  TexC  tex(numbers, s);
511  lexan.open(argv[1]);
512
513  int   t;
514  cout<<tex.numbers;
515
516 
517  while((t=lexan.read(s))!=EOF)
518    {
519      //printf("Type : %d - %s\n",t,s);
520      printf("%s",s);
521      tex.write(t,s);
522      //getchar();
523    }
524
525return 0;
526}
527
528
529
530
531
Note: See TracBrowser for help on using the repository browser.