Subversion Repositories wimsdev

Rev

Rev 8100 | Rev 8149 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 1
/*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
2
 *
3
 *  This program is free software; you can redistribute it and/or modify
4
 *  it under the terms of the GNU General Public License as published by
5
 *  the Free Software Foundation; either version 2 of the License, or
6
 *  (at your option) any later version.
7
 *
8
 *  This program is distributed in the hope that it will be useful,
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 *  GNU General Public License for more details.
12
 *
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
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 */
17
 
7676 bpr 18
/* Sort dictionary */
10 reyssat 19
 
20
/*************** Customization: change values hereafter ****************/
21
 
7676 bpr 22
/* limit of dictionary entries */
10 reyssat 23
#define entrylim 512*1024
7676 bpr 24
/* limit of dictionary length */
25
#define diclim 32*1024*1024
26
/* separation character */
10 reyssat 27
char sepchar=':', grpchar=0;
28
 
29
/***************** Nothing should need change hereafter *****************/
30
 
8123 bpr 31
#include "suffix.h"
10 reyssat 32
#include "../wims.h"
8100 bpr 33
#include "../Lib/libwims.h"
10 reyssat 34
 
35
char inpbuf[MAX_LINELEN+1], outbuf[2*MAX_LINELEN+2];
36
char *dicbuf;
37
char dicname[1024], suffixname[1024];
38
 
39
struct entry {
40
    char *original;
41
    char *replace;
42
} entry[entrylim];
43
int entrycount;
44
 
45
int nocase=0, hassuffix=0, leaveline=0;
46
int entrycount, ocount;
47
 
8100 bpr 48
/*
10 reyssat 49
void *xmalloc(size_t n)
50
{
51
    void *p;
52
    p=malloc(n);
53
    if(p==NULL) exit(1);
54
    return p;
55
}
8100 bpr 56
*/
7676 bpr 57
/* Points to the end of the word */
8100 bpr 58
/*char *find_word_end(char *p)
10 reyssat 59
{
60
    int i;
61
    for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
62
    return p;
63
}
8100 bpr 64
*/
7676 bpr 65
/* Strips leading spaces */
8100 bpr 66
/*char *find_word_start(char *p)
10 reyssat 67
{
68
    int i;
69
    for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
70
    return p;
71
}
8100 bpr 72
*/
7676 bpr 73
/* strip trailing spaces; return string end. */
8100 bpr 74
char *strip_trailing_spaces2(char *p)
10 reyssat 75
{
76
    char *pp;
77
    if(*p==0) return p;
78
    for(pp=p+strlen(p)-1; pp>=p && isspace(*pp); *(pp--)=0);
79
    return pp;
80
}
81
 
82
int compare(const void *s1, const void *s2)
83
{
84
    const struct entry *p1, *p2;
85
    p1=s1; p2=s2;
86
    if(nocase) return strcasecmp(p1->original,p2->original);
87
    else return strcmp(p1->original,p2->original);
88
}
89
 
90
void sortdic(void)
91
{
92
    qsort(entry,entrycount,sizeof(entry[0]),compare);
93
}
94
 
8100 bpr 95
/* modify a string. Bufferlen must be at least MAX_LINELEN */
96
void string_modify3(char *start, char *bad_beg, char *bad_end, char *good,...)
10 reyssat 97
{
98
    char buf[MAX_LINELEN+1];
99
    va_list vp;
7676 bpr 100
 
10 reyssat 101
    va_start(vp,good);
102
    vsnprintf(buf,sizeof(buf),good,vp); va_end(vp);
103
    if(strlen(start)-(bad_end-bad_beg)+strlen(buf)>=MAX_LINELEN)
7676 bpr 104
      return; /* this is an error situation. */
10 reyssat 105
    strcat(buf,bad_end);
3718 reyssat 106
    ovlstrcpy(bad_beg,buf);
10 reyssat 107
}
108
 
7676 bpr 109
/* change all spaces into ' ', and collapse multiple occurences */
8100 bpr 110
void singlespace2(char *p)
10 reyssat 111
{
112
    char *pp, *p2;
113
    for(pp=p;*pp;pp++) {
7676 bpr 114
      if(!isspace(*pp)) continue;
115
      if(leaveline) {
116
          if(*pp==13) ovlstrcpy(pp,pp+1);
117
          if(*pp=='\n') {
118
            pp++;
119
            gopt: for(p2=pp; isspace(*p2) && *p2!='\n'; p2++);
120
            if(p2>pp) ovlstrcpy(pp,p2); pp--;
121
          }
122
          else {
123
            pp++; if(!isspace(*pp) || *pp=='\n') continue;
124
            goto gopt;
125
          }
126
      }
127
      else {
128
          if(*pp!=' ') *pp=' ';
129
          pp++; if(!isspace(*pp)) continue;
130
          for(p2=pp;isspace(*p2);p2++);
131
          ovlstrcpy(pp,p2); pp--;
132
      }
10 reyssat 133
    }
134
}
135
 
7676 bpr 136
/* Prepare dictionary */
10 reyssat 137
void prepare_dic(void)
138
{
139
    int i;
140
    FILE *dicf;
141
    char *p1, *p2, *pp;
142
    long int flen;
143
 
144
    entrycount=0;
145
    dicf=fopen(dicname,"r"); if(dicf==NULL) return;
146
    fseek(dicf,0,SEEK_END);flen=ftell(dicf); fseek(dicf,0,SEEK_SET);
147
    if(flen>diclim) return;
148
    dicbuf=xmalloc(2*flen+1024);flen=fread(dicbuf,1,flen,dicf);
149
    fclose(dicf);
150
    if(flen>0 && flen<diclim) dicbuf[flen]=0;
151
    else return;
152
    for(i=0,p1=dicbuf;p1!=NULL && *p1!=0 && i<entrylim;p1=p2) {
7676 bpr 153
      p2=strchr(p1+1,'\n'); if(p2>p1) *p2++=0;
154
      pp=strchr(p1,sepchar); if(pp==NULL) continue;
155
      *pp++=0;
8100 bpr 156
      strip_trailing_spaces2(p1); strip_trailing_spaces2(pp);
157
      singlespace2(p1);
7676 bpr 158
      p1=find_word_start(p1); pp=find_word_start(pp);
159
      if(*p1==0) continue;
160
      entry[i].original=p1; entry[i].replace=pp; i++;
10 reyssat 161
    }
162
    entrycount=i;
163
}
164
 
165
void output(void)
166
{
167
    int i;
168
    FILE *f;
169
 
170
    ocount=0;
171
    strcat(dicname,".sorted");
172
    f=fopen(dicname,"w"); if(f==NULL) return;
173
    for(i=0;i<entrycount;i++) {
7676 bpr 174
      if(i>0 && strcmp(entry[i].original,entry[i-1].original)==0
175
         && strcmp(entry[i].replace,entry[i-1].replace)==0)
176
        continue;
177
      if(grpchar!=0) {
178
          if(i>0 && strcmp(entry[i].original,entry[i-1].original)==0)
179
            fprintf(f,"%c%s",grpchar, entry[i].replace);
180
          else {
181
            if(i>0) fprintf(f,"\n");
182
            fprintf(f,"%s%c%s",entry[i].original,sepchar,entry[i].replace);
183
            ocount++;
184
          }
185
 
186
      }
187
      else {
188
          fprintf(f,"%s%c%s\n",entry[i].original,sepchar,entry[i].replace);
189
          ocount++;
190
      }
10 reyssat 191
    }
192
    if(grpchar!=0) fprintf(f,"\n");
193
    fclose(f);
194
}
195
 
196
int main(int argc, char *argv[])
197
{
198
    char *ss, *gr;
199
    if(argc<2) return -1;
7676 bpr 200
 
10 reyssat 201
    ss=getenv("dicsort_separator");
202
    if(ss!=NULL && *ss!=0) sepchar=*ss;
203
    gr=getenv("dicsort_grouping");
204
    if(gr!=NULL && *gr!=0) grpchar=*gr;
205
    snprintf(dicname,sizeof(dicname)-128,"%s",argv[1]); prepare_dic();
206
    if(argc>2) {
7676 bpr 207
      snprintf(suffixname,sizeof(suffixname),"%s",argv[2]);
208
      suffix_dic(suffixname); hassuffix=1;
10 reyssat 209
    }
210
    else suffixname[0]=hassuffix=0;
211
    sortdic(); output();
212
    printf("%s: sorted %d entries.\n",dicname, ocount);
213
    return 0;
214
}
215