home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / LINGU11A / LINGUA.C < prev    next >
C/C++ Source or Header  |  1993-03-14  |  4KB  |  130 lines

  1. /* lingua.c -- (C) SichemSoft 1991 -- ASR 930314 */
  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],buf[256],*p,*q;
  22.     unsigned long offset=0,filpos=0;
  23.     unsigned count=0,lines=0,len=0,array=0,multi=0,l=0,checksum=0;
  24.  
  25.     /* copyright message and arguments check */
  26.     puts("Lingua v1.1 - (C)1993 SichemSoft Wageningen Netherlands");
  27.     if (argc!=2 && argc!=3) {
  28.         puts("lingua <file> [<version>]"); return 1;
  29.     }
  30.  
  31.     /* open all files */
  32.     strcpy(fname,argv[1]); strupr(fname);
  33.     if (!strchr(fname,'.')) strcat(fname,".TXT");
  34.     if ((txt=openfile(fname,"r"))==NULL) return 2;
  35.     if ((header=openfile("UI_TEXT.H","w"))==NULL) return 2;
  36.     strcpy(strchr(fname,'.')+1,"ETF");
  37.     if ((etf=openfile(fname,"wb"))==NULL) return 2;
  38.  
  39.     /* start of UI_TEXT.H */
  40.     fputs("/* UI_TEXT.H */\n\n",header);
  41.     fputs("int ui_loadtext(char *fname,char *vers);\n",header);
  42.     fputs("void ui_unloadtext(void);\n",header);
  43.     fputs("extern char **ui_text;\n\n",header);
  44.  
  45.     /* start of .ETF (filename+version) */
  46.     fprintf(etf,"%s%s\032",fname,argc==3?argv[2]:"");
  47.     filpos=ftell(etf);
  48.     fwrite(&count,sizeof(count),1,etf);
  49.     fwrite(&offset,sizeof(offset),1,etf);
  50.  
  51.     /* count lines, determine offsets and write to .ETF and .H */
  52.     while (fgets(buf,255,txt)) {
  53.         l=strlen(buf)-1; if (buf[l]=='\n') buf[l]='\0';
  54.         lines++;
  55.         if (!buf[0] || buf[0]=='#') continue; /* comment */
  56.         p=strchr(buf,' '); if (!p) goto fatal;
  57.         *p=0; p++; while (*p==' ') p++;
  58.         len=strlen(p); if (!strcmp(p,"-")) len=0;
  59.         l=strlen(buf)-1;
  60.         if (buf[l]=='[') { /* array identifier */
  61.             if (l>0) {
  62.                 buf[l]='\0'; array=1; multi=0;
  63.                 fprintf(header,"#define %-30s (ui_text+%u)\n",buf,count);
  64.             } else if (!array) goto fatal;
  65.             fwrite(&offset,sizeof(offset),1,etf);
  66.             offset+=len+1; count++;
  67.         } else if (buf[l]=='/') { /* multi line text */
  68.             if (l>0) {
  69.                 buf[l]='\0'; multi=1; array=0;
  70.                 fprintf(header,"#define %-30s ui_text[%u]\n",buf,count);
  71.                 fwrite(&offset,sizeof(offset),1,etf);
  72.                 offset+=len+1; count++;
  73.             } else {
  74.                 offset+=len+2; if (!multi) goto fatal;
  75.             }
  76.         } else { /* normal identifier */
  77.             array=0; multi=0;
  78.             fprintf(header,"#define %-30s ui_text[%u]\n",buf,count);
  79.             fwrite(&offset,sizeof(offset),1,etf);
  80.             offset+=len+1; count++;
  81.         }
  82.     }
  83.  
  84.     /* encrypt lines and write to .ETF */
  85.     rewind(txt); multi=0;
  86.     while (fgets(buf,255,txt)) {
  87.         l=strlen(buf)-1; if (buf[l]=='\n') buf[l]='\0';
  88.         if (!buf[0] || buf[0]=='#') continue; /* comment */
  89.         p=strchr(buf,' ');
  90.         *p=0; p++; while (*p==' ') p++;
  91.         len=strlen(p); if (!strcmp(p,"-")) p[0]='\0';
  92.         for (q=p; *q=='_'; q++) *q=' ';
  93.         for (q=p+len-1; *q=='_'; q--) *q=' ';
  94.         l=strlen(buf)-1;
  95.         if (buf[l]=='/') {
  96.             if (l==0 && multi) {
  97.                 putc('\r'^UIT_ENCRYPT,etf); putc('\n'^UIT_ENCRYPT,etf);
  98.                 checksum+='\r'; checksum+='\n';
  99.             }
  100.             if (l>0) {
  101.                 if (multi) putc('\0',etf);
  102.                 multi=1;
  103.             }
  104.         } else {
  105.             if (multi) putc('\0',etf);
  106.             multi=0;
  107.         }
  108.         for (q=p; *q; q++) {
  109.             checksum+=(unsigned char)(*q); *q^=UIT_ENCRYPT; putc(*q,etf);
  110.         }
  111.         if (!multi) putc('\0',etf);
  112.     }
  113.     if (multi) putc('\0',etf);
  114.  
  115.     /* write checksum and number of lines and bytes to .ETF */
  116.     fwrite(&checksum,sizeof(checksum),1,etf);
  117.     fseek(etf,filpos,SEEK_SET);
  118.     fwrite(&count,sizeof(count),1,etf);
  119.     fwrite(&offset,sizeof(offset),1,etf);
  120.  
  121.     /* close all files */
  122.     fclose(txt); fclose(etf); fclose(header);
  123.     printf("%u items, %lu bytes\n",count,offset);
  124.  
  125.     return 0;
  126.  
  127. fatal: printf("fatal error in text file at line %d\n",lines);
  128.     return 3;
  129. }
  130.