Subversion Repositories wimsdev

Rev

Rev 3808 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3808 Rev 8161
Line 13... Line 13...
13
 *  You should have received a copy of the GNU General Public License
13
 *  You should have received a copy of the GNU General Public License
14
 *  along with this program; if not, write to the Free Software
14
 *  along with this program; if not, write to the Free Software
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 */
16
 */
17
 
17
 
18
        /* Versatile translation according to a dictionary */
18
/* Versatile translation according to a dictionary */
-
 
19
 
-
 
20
#include "symtext.h"
19
 
21
 
20
char inpbuf[MAX_LINELEN+1], troutbuf[2*MAX_LINELEN+2];
22
char inpbuf[MAX_LINELEN+1], troutbuf[2*MAX_LINELEN+2];
21
struct entry {
-
 
22
    unsigned char *original, *replace;
-
 
23
    int olen,earlier;
-
 
24
} entry[MAX_DICENTRIES];
23
struct entry entry[MAX_DICENTRIES];
25
int entrycount=0;
24
int entrycount=0;
26
 
25
 
27
struct dic {
-
 
28
    char name[MAX_FNAME+1];
-
 
29
    char unknown[256];
-
 
30
    char *buf;
-
 
31
    int unknown_type;
-
 
32
    int start;
-
 
33
    int len;
-
 
34
} dic[MAX_DICS];
26
struct dic dic[MAX_DICS];
35
int diccnt;
27
int diccnt;
36
int transdic, macrodic;
28
int transdic, macrodic;
37
 
29
 
38
enum {
-
 
39
    unk_delete, unk_leave, unk_replace
-
 
40
};
-
 
41
 
-
 
42
int compare(struct entry *e, const char *s2)
30
int compare(struct entry *e, const char *s2)
43
{
31
{
44
    int k;
32
    int k;
45
    k=strncmp((char*)e->original, (char*)s2, e->olen);
33
    k=strncmp((char*)e->original, (char*)s2, e->olen);
46
    if(k==0 && isalnum(*(s2+e->olen))) return -1;
34
    if(k==0 && isalnum(*(s2+e->olen))) return -1;
47
    else return k;
35
    else return k;
48
}
36
}
49
 
37
 
50
        /* searches a list. Returns index if found, -1 if nomatch.
38
        /* searches a list. Returns index if found, -1 if nomatch.
51
         * Uses binary search, list must be sorted. */
39
         * Uses binary search, list must be sorted. */
52
int search_dic(struct entry *list, int items, size_t item_size, const char *str)
40
int search_dic(struct entry *list, int items, size_t item_size, const char *str)
53
{
41
{
54
    int i1,i2,j,k,t,t1;
42
    int i1,i2,j,k,t,t1;
55
    unsigned char c;
43
    unsigned char c;
Line 63... Line 51...
63
    if(k>0) for(i1=0,i2=j;i2>i1+1;) {
51
    if(k>0) for(i1=0,i2=j;i2>i1+1;) {
64
        j=i1+(i2-i1)/2;
52
        j=i1+(i2-i1)/2;
65
        k=list[j].original[0]-c; if(k==0) k=compare(list+j,str);
53
        k=list[j].original[0]-c; if(k==0) k=compare(list+j,str);
66
        if(k==0) goto more;
54
        if(k==0) goto more;
67
        if(k>0) {i2=j; continue;}
55
        if(k>0) {i2=j; continue;}
68
        if(k<0) {i1=j; continue;}      
56
        if(k<0) {i1=j; continue;}
69
    }
57
    }
70
    if(k>0) {j--;k=compare(list+j,str);}
58
    if(k>0) {j--;k=compare(list+j,str);}
71
    more:
59
    more:
72
    if((t=list[j].earlier)<0) {
60
    if((t=list[j].earlier)<0) {
73
        if(k==0) return j; else return -1;
61
        if(k==0) return j; else return -1;
74
    }
62
    }
75
    if(compare(entry+t,str)!=0) return -1;
63
    if(compare(entry+t,str)!=0) return -1;
76
    for(j=t1=t,k=0;j<items+(list-entry) && entry[j].earlier==t1 && (k=compare(entry+j,str))<=0; j++)
64
    for(j=t1=t,k=0;j<items+(list-entry) && entry[j].earlier==t1 && (k=compare(entry+j,str))<=0; j++)
77
      if(k==0) t=j;
65
      if(k==0) t=j;
78
    return t-(list-entry);
66
    return t-(list-entry);
79
}
67
}
80
 
68
 
81
#include "suffix.c"
-
 
82
 
-
 
83
        /* Prepare dictionary */
69
/* Prepare dictionary */
84
struct dic *prepare_dic(char *fname)
70
struct dic *prepare_dic(char *fname)
85
{
71
{
86
    int i,l;
72
    int i,l;
87
    struct dic *thisdic;
73
    struct dic *thisdic;
88
    FILE *dicf;
74
    FILE *dicf;
89
    char *p1, *p2, *pp;
75
    char *p1, *p2, *pp;
90
    char tbuf[256], buf[MAX_LINELEN+1];
76
    char tbuf[256], buf[MAX_LINELEN+1];
91
    long int flen;
77
    long int flen;
92
   
78
 
93
    if(diccnt>=MAX_DICS) error("too_many_dictionaries");
79
    if(diccnt>=MAX_DICS) error("too_many_dictionaries");
94
    thisdic=dic+diccnt; diccnt++;
80
    thisdic=dic+diccnt; diccnt++;
95
    thisdic->len=0;
81
    thisdic->len=0;
96
    thisdic->start=entrycount;
82
    thisdic->start=entrycount;
97
    snprintf(thisdic->name,sizeof(thisdic->name),"%s",fname);
83
    snprintf(thisdic->name,sizeof(thisdic->name),"%s",fname);
Line 115... Line 101...
115
                fname,entry[i-1].original,p1);
101
                fname,entry[i-1].original,p1);
116
        if(i>entrycount && strcmp((char*)entry[i-1].original,p1)==0)
102
        if(i>entrycount && strcmp((char*)entry[i-1].original,p1)==0)
117
          error("duplication_in_dictionary %s: %s.\n",
103
          error("duplication_in_dictionary %s: %s.\n",
118
                fname,p1);
104
                fname,p1);
119
        entry[i].original=(unsigned char*)p1;
105
        entry[i].original=(unsigned char*)p1;
120
        entry[i].replace=(unsigned char*)pp;
106
        entry[i].replace=(unsigned char*)pp;
121
        entry[i].olen=l=strlen(p1); entry[i].earlier=-1;
107
        entry[i].olen=l=strlen(p1); entry[i].earlier=-1;
122
        if(i>0) {
108
        if(i>0) {
123
            int l1,l2;
109
            int l1,l2;
124
            l1=entry[i-1].earlier; if(l1>=0) l2=entry[l1].olen;
110
            l1=entry[i-1].earlier; if(l1>=0) l2=entry[l1].olen;
125
            else {l2=entry[i-1].olen;l1=i-1;}
111
            else {l2=entry[i-1].olen;l1=i-1;}
126
            if(l>l2 && isspace(p1[l2])
112
            if(l>l2 && isspace(p1[l2])
127
               && strncmp((char*)entry[l1].original,p1,l2)==0)
113
               && strncmp((char*)entry[l1].original,p1,l2)==0)
128
              entry[i].earlier=entry[i-1].earlier=l1;
114
              entry[i].earlier=entry[i-1].earlier=l1;
129
        }
115
        }
130
        i++;
116
        i++;
131
    }
117
    }
132
    thisdic->len=i-entrycount;
118
    thisdic->len=i-entrycount;
Line 145... Line 131...
145
    if(debug) fprintf(stderr,"Dictionary %d: %s, %d entries.\n",
131
    if(debug) fprintf(stderr,"Dictionary %d: %s, %d entries.\n",
146
                      diccnt,fname,thisdic->len);
132
                      diccnt,fname,thisdic->len);
147
    return thisdic;
133
    return thisdic;
148
}
134
}
149
 
135
 
150
        /* make the translation. */
136
/* make the translation. */
151
void _translate(char *p, int i)
137
void _translate(char *p, int i)
152
{
138
{
153
    char *p1, *p2, *pp;
139
    char *p1, *p2, *pp;
154
    int t;
140
    int t;
155
 
141