/************************************************************************** c2tex - Konverze C++ do TeXu Jiri Bittner, Cerven 1996 **************************************************************************/ #include #include #include #include #include #define KEYWORD1 1 // kody pro jednotlive znacky #define KEYWORD2 2 #define SYMBOL 3 #define COMMENT 4 #define NORMAL 5 #define WORD 10 #define S_LEN 256 int usePascal=0; char comment_1b='/',comment_2b='*',comment_1e='*',comment_2e='/'; class BaseC { }; typedef double Real; class OptionsC : public BaseC { public: int number; char **strings; // OptionsC() { number=0; } OptionsC(int n,char **a) { init(n,a); } void init(int n,char **s) { number=n; strings=s; } int isOption(char *string); int isOption(char *string,int *number); int getParam(char *s,int n=1); int getOptionValue(char *string,int *result); int getOptionValue(char *string,Real *r); int getOptionValue(char *string,char *result); }; class LexanC: public BaseC { public: FILE *file; int nsep,nkey[2]; char *separators; char **key[2]; LexanC(){init("default.lex");} int isSeparator(int c){ return strchr(separators,c)!=NULL; } int isKey(char *s); int init(const char *lexname); int open(const char *filename); int readWord(FILE *f,char *string); int read(char *string); void close(){fclose(file);} virtual ostream& operator>>(ostream &s) { s<' : fprintf(file,"\\>"); break; case '~' : fprintf(file,"\\~{}"); break; default : fputc(*sp,file); } } return 1; } int LexanC::isKey(char *s) { int i,j; for (i=0;i<2;i++) for(j=0;j2) { c=s[i-2]; fseek(file,-1,SEEK_CUR); s[i-2]=0; return type; } fgets(s+i,S_LEN,file); c=fgetc(file); return COMMENT; } else if(c==comment_2b) // line comment { if (i>2) { c=s[i-2]; fseek(file,-1,SEEK_CUR); s[i-2]=0; return type; } c=fgetc(file); if(c!=EOF) { s[i++]=c; int count=1; do{ c=fgetc(file); if(c==EOF) break; s[i++]=c; if (s[i-2]==comment_1e && s[i-1]==comment_2e) count--; if (s[i-2]==comment_1b &&s[i-1]==comment_2b) count++; } while(count!=0); } s[i]=0; c=fgetc(file); return COMMENT; } } else wasSlash=(c==comment_1b); } else if (c=='`') { if (i>=1) { s[i]=0; return type; } c=fgetc(file); while(c!='`') { if (c==EOF) break; s[i++]=c; c=fgetc(file); } s[i]=0; c=fgetc(file); return WORD; } else { if (type==EOF) type=WORD; break; } } while((c=fgetc(file))!=EOF); if (c!=EOF) if (type==WORD) { do { s[i++]=c; c=fgetc(file); if (c==EOF) break; } while(!isSeparator(c)); s[i]=0; int k=isKey(s); if (k==0) return NORMAL; return k; } s[i]=0; return type; } int TexC::init(const char *filename) { file=fopen(filename,"wt"); fprintf(file,"\\input default.mac\n"); return file!=NULL; } void Help() { printf("Syntax : c2tex in_file [out_file] [-usePascal] [-numbers]\n\n"); printf("Default out_file is output.tex.\n"); printf("-usePascal use usePascal comments (* *).\n"); printf("-numbers print line numbers.\n"); exit(1); } int main(int argc, char **argv) { char s[16384]; // docasny buffer OptionsC options(argc,argv); if (argc<2) Help(); if (argc>=3 && (*argv[2]!='-') ) strcpy(s,argv[2]); else strcpy(s,"output.tex"); if (options.isOption("-usePascal")) { usePascal=1; comment_1b='('; comment_2b='*'; comment_1e='*'; comment_2e=')'; } int numbers; numbers = options.isOption("-numbers"); LexanC lexan; TexC tex(numbers, s); lexan.open(argv[1]); int t; cout<