2 Text Conversion With Guessing
3 (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
10 typedef unsigned char uchar;
12 uchar dest[256],flag[256],line[4096],ans[256];
16 #define UNPRINTABLE(c) (c<32 && c != 10 && c != 9 || c >= 0x80 && c < 0xa0)
18 void putxc(uchar c,int flg)
20 if (!flg && flag[c] || flg && UNPRINTABLE(c)) printf("[%d]",c);
21 else putchar(flg ? c : dest[c]);
24 void halfcontext(uchar *c,uchar *stop,int flg)
26 while (*c && c != stop)
30 void context(uchar *c)
32 halfcontext(line,c,1);
34 halfcontext(c+1,NULL,0);
37 void main(int ac,char **av)
39 if (ac != 3 && ac != 4)
41 printf("Syntax: guesstrans <from> <to> [<table>]\n");
46 for (c=0;c<256;c++) dest[c]=c;
47 for (c=0;c<32;c++) flag[c]=1;
48 for (c=128;c<256;c++) flag[c]=1;
49 flag[13]=0; flag[10]=0; flag[9]=0;
54 if (f=fopen(av[3],"r"))
56 if (fread(dest,1,256,f) != 256)
59 fprintf(stderr,"Error reading translation table!\n");
62 if (fread(flag,1,256,f) != 256) goto failed;
66 if (!(ii=fopen(av[1],"r")))
68 fprintf(stderr,"Unable to read %s\n",av[1]);
71 if (!(oo=fopen(av[2],"w")))
73 fprintf(stderr,"Unable to write %s\n",av[2]);
76 while (fgets(line,4096,ii))
79 if (c = strchr(line, '\r'))
81 if (!strchr(line, '\n'))
89 if (*ans && *ans != '\n')
92 if (*ans == '\\') dest[*c]=atol(ans+1);
101 if (changed && ac == 4)
104 if (f=fopen(av[3],"w"))
106 if (fwrite(dest,1,256,f) != 256) goto wrfail;
107 if (fwrite(flag,1,256,f) != 256) goto wrfail;
112 fprintf(stderr,"Error writing conversion table!\n");