home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / parser / lingua / lingua.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-13  |  3.0 KB  |  97 lines

  1. /* lingua.c -- (C) SichemSoft 1991 -- ASR 920613  */
  2. /* Roghorst 160, 6708 KS Wageningen,  Netherlands */
  3. /* utility for language independent applications  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. #define UIT_ENCRYPT 53
  9.  
  10. static FILE *openfile(char *name,char *mode)
  11. {
  12.    FILE *fp;
  13.  
  14.    fp=fopen(name,mode);
  15.    if (!fp) printf("%s not opened\n",name);
  16.    return fp;
  17. } /* openfile */
  18.  
  19. int main(int argc,char *argv[])
  20. {
  21.    FILE *txt,*etf,*header; char fname[81];
  22.    unsigned long offset=0,filpos=0;
  23.    unsigned count=0,len=0,array=0,l=0;
  24.    char buf[256],*p,*q;
  25.  
  26.    if (argc!=2 && argc!=3) {
  27.       puts("lingua <file> [<version>]"); return 1;
  28.    }
  29.  
  30.    /* open all files */
  31.    strcpy(fname,argv[1]); strupr(fname);
  32.    if (!strchr(fname,'.')) strcat(fname,".TXT");
  33.    if ((txt=openfile(fname,"r"))==NULL) return 2;
  34.    if ((header=openfile("UI_TEXT.H","w"))==NULL) return 2;
  35.    strcpy(strchr(fname,'.')+1,"ETF");
  36.    if ((etf=openfile(fname,"wb"))==NULL) return 2;
  37.  
  38.    /* start of UI_TEXT.H */
  39.    fputs("/* UI_TEXT.H */\n\n",header);
  40.    fputs("void ui_loadtext(char *fname,char *vers);\n",header);
  41.    fputs("void ui_unloadtext(void);\n",header);
  42.    fputs("extern char **ui_text;\n\n",header);
  43.  
  44.    /* start of .ETF (filename+version) */
  45.    fprintf(etf,"%s%s\032",fname,argc==3?argv[2]:"");
  46.    filpos=ftell(etf);
  47.    fwrite(&count,sizeof(count),1,etf);
  48.    fwrite(&offset,sizeof(offset),1,etf);
  49.  
  50.    /* count lines, determine offsets and write to .ETF and .H */
  51.    while (fgets(buf,255,txt)) {
  52.       if (buf[0]=='#') continue; /* comment */
  53.       p=strchr(buf,' '); if (!p) goto fatal;
  54.       *p=0; p++; while (*p==' ') p++;
  55.       len=strlen(p); p[len-1]='\0'; if (!strcmp(p,"-")) len=1;
  56.       l=strlen(buf)-1;
  57.       if (buf[l]=='[') { /* array identifier */
  58.          if (l>1) {
  59.             if (array) goto fatal; array=1; buf[l]='\0';
  60.             fprintf(header,"#define %-30s (ui_text+%u)\n",buf,count);
  61.          } else if (!array) goto fatal;
  62.       } else { /* normal identifier */
  63.          if (array) array=0;
  64.          fprintf(header,"#define %-30s ui_text[%u]\n",buf,count);
  65.       }
  66.       fwrite(&offset,sizeof(offset),1,etf);
  67.       offset+=len; count++;
  68.    }
  69.  
  70.    /* encrypt lines and write to .ETF */
  71.    rewind(txt);
  72.    while (fgets(buf,255,txt)) {
  73.       if (buf[0]=='#') continue; /* comment */
  74.       p=strchr(buf,' ');
  75.       *p=0; p++; while (*p==' ') p++;
  76.       len=strlen(p); p[len-1]='\0'; if (!strcmp(p,"-")) p[0]='\0';
  77.       for (q=p; *q=='_'; q++) *q=' ';
  78.       for (q=p+len-2; *q=='_'; q--) *q=' ';
  79.       for (q=p; *q; q++) *q^=UIT_ENCRYPT;
  80.       fprintf(etf,p); putc('\0',etf);
  81.    }
  82.  
  83.    /* write number of lines and bytes to .ETF */
  84.    fseek(etf,filpos,SEEK_SET);
  85.    fwrite(&count,sizeof(count),1,etf);
  86.    fwrite(&offset,sizeof(offset),1,etf);
  87.  
  88.    /* close all files */
  89.    fclose(txt); fclose(etf); fclose(header);
  90.    printf("%u items, %lu bytes\n",count,offset);
  91.  
  92.    return 0;
  93.  
  94. fatal: puts("fatal in text file");
  95.    return 3;
  96. }
  97.