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 >
Wrap
C/C++ Source or Header
|
1993-03-14
|
4KB
|
130 lines
/* lingua.c -- (C) SichemSoft 1991 -- ASR 930314 */
/* Roghorst 160, 6708 KS Wageningen, Netherlands */
/* utility for language independent applications */
#include <stdio.h>
#include <string.h>
#define UIT_ENCRYPT 53
static FILE *openfile(char *name,char *mode)
{
FILE *fp;
fp=fopen(name,mode);
if (!fp) printf("%s not opened\n",name);
return fp;
} /* openfile */
int main(int argc,char *argv[])
{
FILE *txt,*etf,*header; char fname[81],buf[256],*p,*q;
unsigned long offset=0,filpos=0;
unsigned count=0,lines=0,len=0,array=0,multi=0,l=0,checksum=0;
/* copyright message and arguments check */
puts("Lingua v1.1 - (C)1993 SichemSoft Wageningen Netherlands");
if (argc!=2 && argc!=3) {
puts("lingua <file> [<version>]"); return 1;
}
/* open all files */
strcpy(fname,argv[1]); strupr(fname);
if (!strchr(fname,'.')) strcat(fname,".TXT");
if ((txt=openfile(fname,"r"))==NULL) return 2;
if ((header=openfile("UI_TEXT.H","w"))==NULL) return 2;
strcpy(strchr(fname,'.')+1,"ETF");
if ((etf=openfile(fname,"wb"))==NULL) return 2;
/* start of UI_TEXT.H */
fputs("/* UI_TEXT.H */\n\n",header);
fputs("int ui_loadtext(char *fname,char *vers);\n",header);
fputs("void ui_unloadtext(void);\n",header);
fputs("extern char **ui_text;\n\n",header);
/* start of .ETF (filename+version) */
fprintf(etf,"%s%s\032",fname,argc==3?argv[2]:"");
filpos=ftell(etf);
fwrite(&count,sizeof(count),1,etf);
fwrite(&offset,sizeof(offset),1,etf);
/* count lines, determine offsets and write to .ETF and .H */
while (fgets(buf,255,txt)) {
l=strlen(buf)-1; if (buf[l]=='\n') buf[l]='\0';
lines++;
if (!buf[0] || buf[0]=='#') continue; /* comment */
p=strchr(buf,' '); if (!p) goto fatal;
*p=0; p++; while (*p==' ') p++;
len=strlen(p); if (!strcmp(p,"-")) len=0;
l=strlen(buf)-1;
if (buf[l]=='[') { /* array identifier */
if (l>0) {
buf[l]='\0'; array=1; multi=0;
fprintf(header,"#define %-30s (ui_text+%u)\n",buf,count);
} else if (!array) goto fatal;
fwrite(&offset,sizeof(offset),1,etf);
offset+=len+1; count++;
} else if (buf[l]=='/') { /* multi line text */
if (l>0) {
buf[l]='\0'; multi=1; array=0;
fprintf(header,"#define %-30s ui_text[%u]\n",buf,count);
fwrite(&offset,sizeof(offset),1,etf);
offset+=len+1; count++;
} else {
offset+=len+2; if (!multi) goto fatal;
}
} else { /* normal identifier */
array=0; multi=0;
fprintf(header,"#define %-30s ui_text[%u]\n",buf,count);
fwrite(&offset,sizeof(offset),1,etf);
offset+=len+1; count++;
}
}
/* encrypt lines and write to .ETF */
rewind(txt); multi=0;
while (fgets(buf,255,txt)) {
l=strlen(buf)-1; if (buf[l]=='\n') buf[l]='\0';
if (!buf[0] || buf[0]=='#') continue; /* comment */
p=strchr(buf,' ');
*p=0; p++; while (*p==' ') p++;
len=strlen(p); if (!strcmp(p,"-")) p[0]='\0';
for (q=p; *q=='_'; q++) *q=' ';
for (q=p+len-1; *q=='_'; q--) *q=' ';
l=strlen(buf)-1;
if (buf[l]=='/') {
if (l==0 && multi) {
putc('\r'^UIT_ENCRYPT,etf); putc('\n'^UIT_ENCRYPT,etf);
checksum+='\r'; checksum+='\n';
}
if (l>0) {
if (multi) putc('\0',etf);
multi=1;
}
} else {
if (multi) putc('\0',etf);
multi=0;
}
for (q=p; *q; q++) {
checksum+=(unsigned char)(*q); *q^=UIT_ENCRYPT; putc(*q,etf);
}
if (!multi) putc('\0',etf);
}
if (multi) putc('\0',etf);
/* write checksum and number of lines and bytes to .ETF */
fwrite(&checksum,sizeof(checksum),1,etf);
fseek(etf,filpos,SEEK_SET);
fwrite(&count,sizeof(count),1,etf);
fwrite(&offset,sizeof(offset),1,etf);
/* close all files */
fclose(txt); fclose(etf); fclose(header);
printf("%u items, %lu bytes\n",count,offset);
return 0;
fatal: printf("fatal error in text file at line %d\n",lines);
return 3;
}