]> mj.ucw.cz Git - misc.git/blob - guesstrans.c
Added pstopsfix.
[misc.git] / guesstrans.c
1 /*
2 Text Conversion With Guessing
3 (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 typedef unsigned char uchar;
11
12 uchar dest[256],flag[256],line[4096],ans[256];
13 int changed;
14
15 FILE *ii,*oo;
16 #define UNPRINTABLE(c) (c<32 && c != 10 && c != 9 || c >= 0x80 && c < 0xa0)
17
18 void putxc(uchar c,int flg)
19 {
20         if (!flg && flag[c] || flg && UNPRINTABLE(c)) printf("[%d]",c);
21         else putchar(flg ? c : dest[c]);
22 }
23
24 void halfcontext(uchar *c,uchar *stop,int flg)
25 {
26         while (*c && c != stop)
27                 putxc(*c++,flg);
28 }
29
30 void context(uchar *c)
31 {
32         halfcontext(line,c,1);
33         printf("<%d>",*c);
34         halfcontext(c+1,NULL,0);
35 }
36
37 void main(int ac,char **av)
38 {
39         if (ac != 3 && ac != 4)
40                 {
41                 printf("Syntax: guesstrans <from> <to> [<table>]\n");
42                 exit(0);
43                 }
44         {
45         int c;
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;
50         }
51         if (ac == 4)
52                 {
53                 FILE *f;
54                         if (f=fopen(av[3],"r"))
55                                 {
56                                 if (fread(dest,1,256,f) != 256)
57                                         {
58                                         failed:
59                                                 fprintf(stderr,"Error reading translation table!\n");
60                                                 exit(1);
61                                         }
62                                 if (fread(flag,1,256,f) != 256) goto failed;
63                                 fclose(f);
64                                 }
65                 }
66         if (!(ii=fopen(av[1],"r")))
67                 {
68                 fprintf(stderr,"Unable to read %s\n",av[1]);
69                 exit(1);
70                 }
71         if (!(oo=fopen(av[2],"w")))
72                 {
73                 fprintf(stderr,"Unable to write %s\n",av[2]);
74                 exit(1);
75                 }
76         while (fgets(line,4096,ii))
77                 {
78                 uchar *c;
79                 if (c = strchr(line, '\r'))
80                         *c = 0;
81                 if (!strchr(line, '\n'))
82                         strcat(line, "\n");
83                 for(c=line;*c;c++)
84                         {
85                         if (flag[*c])
86                                 {
87                                 context(c);
88                                 gets(ans);
89                                 if (*ans && *ans != '\n')
90                                         {
91                                         flag[*c]=0;
92                                         if (*ans == '\\') dest[*c]=atol(ans+1);
93                                         else dest[*c]=*ans;
94                                         changed=1;
95                                         }
96                                 }
97                         *c=dest[*c];
98                         }
99                 fputs(line,oo);
100                 }
101         if (changed && ac == 4)
102                 {
103                 FILE *f;
104                         if (f=fopen(av[3],"w"))
105                                 {
106                                 if (fwrite(dest,1,256,f) != 256) goto wrfail;
107                                 if (fwrite(flag,1,256,f) != 256) goto wrfail;
108                                 }
109                         else
110                                 {
111                                 wrfail:
112                                 fprintf(stderr,"Error writing conversion table!\n");
113                                 exit(1);
114                                 }
115                 }
116 }