Debian Codegraph @

texk/mendexk/sort.c

raw
1 #include "mendex.h"
2 #include "qsort.h"
3
4 #include "exkana.h"
5 #include "exvar.h"
6
7 int sym,number,alpha,kana;
8
9 static int wcomp(const void *p, const void *q);
10 static int pcomp(const void *p, const void *q);
11 static int ordering(char *buff);
12
13 /* sort index */
14 void wsort(struct index *ind, int num)
15 {
16 int i,order;
17
18 for (order=1,i=0;;i++) {
19 switch (character_order[i]) {
20 case '\0':
21 goto BREAK;
22 break;
23
24 case 'S':
25 case 's':
26 sym=order++;
27 break;
28
29 case 'N':
30 case 'n':
31 number=order++;
32 break;
33
34 case 'E':
35 case 'e':
36 alpha=order++;
37 break;
38
39 case 'J':
40 case 'j':
41 kana=order++;
42 break;
43
44 default:
45 verb_printf(efp,"\nWarning: Illegal input for character_order (%c).",character_order[i]);
46 break;
47 }
48 }
49
50 BREAK:
51
52 if (sym==0) sym=order++;
53 if (number==0) number=order++;
54 if (alpha==0) alpha=order++;
55 if (kana==0) kana=order++;
56
57 qsort(ind,num,sizeof(struct index),wcomp);
58 }
59
60 /* compare for sorting index */
61 static int wcomp(const void *p, const void *q)
62 {
63 int i, j, prechar = 0;
64 const struct index *index1 = p, *index2 = q;
65 char ch1, ch2;
66 char *str1, *str2;
67
68 scount++;
69
70 for (j=0;j<3;j++) {
71
72 /* check level */
73 if (((*index1).words==j)&&((*index2).words!=j)) return -1;
74 else if (((*index1).words!=j)&&((*index2).words==j)) return 1;
75
76 for(i=0;;i++) {
77
78 str1=&((*index1).dic[j][i]);
79 str2=&((*index2).dic[j][i]);
80 ch1=(*index1).dic[j][i];
81 ch2=(*index2).dic[j][i];
82
83 /* even */
84 if ((ch1=='\0')&&(ch2=='\0')) break;
85
86 /* index1 is shorter */
87 if (ch1=='\0') return -1;
88
89 /* index2 is shorter */
90 if (ch2=='\0') return 1;
91
92 /* priority */
93 if ((priority!=0)&&(i>0)) {
94 if (prechar==0) {
95 if ((japanese(str1))
96 &&(!japanese(str2)))
97 return -1;
98
99 if ((japanese(str2))
100 &&(!japanese(str1)))
101 return 1;
102 }
103 else {
104 if ((japanese(str1))
105 &&(!japanese(str2)))
106 return 1;
107
108 if ((japanese(str2))
109 &&(!japanese(str1)))
110 return -1;
111 }
112 }
113
114 /* compare group */
115 if (ordering(str1)<ordering(str2))
116 return -1;
117
118 if (ordering(str1)>ordering(str2))
119 return 1;
120
121 /* symbol pattern */
122 if ((!numeric(str1))&&(numeric(str2)))
123 return -1;
124
125 if ((!numeric(str2))&&(numeric(str1)))
126 return 1;
127
128 /* simple compare */
129 if ((unsigned char)(*index1).dic[j][i]<(unsigned char)(*index2).dic[j][i]) return -1;
130 else if ((unsigned char)(*index1).dic[j][i]>(unsigned char)(*index2).dic[j][i]) return 1;
131
132 /* 2byte character */
133 if (((unsigned char)(*index1).dic[j][i]>=0x80)&&((unsigned char)(*index2).dic[j][i]>=0x80)) {
134 prechar=1;
135 i++;
136 if ((unsigned char)(*index1).dic[j][i]<(unsigned char)(*index2).dic[j][i]) return -1;
137 else if ((unsigned char)(*index1).dic[j][i]>(unsigned char)(*index2).dic[j][i]) return 1;
138 }
139 else prechar=0;
140 }
141
142 /* compare index */
143 for (i=0;;i++) {
144 if (((*index1).idx[j][i]=='\0')&&((*index2).idx[j][i]=='\0')) break;
145 else if ((*index1).idx[j][i]=='\0') return -1;
146 else if ((*index2).idx[j][i]=='\0') return 1;
147 else if ((unsigned char)(*index1).idx[j][i]<(unsigned char)(*index2).idx[j][i]) return -1;
148 else if ((unsigned char)(*index1).idx[j][i]>(unsigned char)(*index2).idx[j][i]) return 1;
149 }
150 }
151 return 0;
152 }
153
154 /* sort page */
155 void pagesort(struct index *ind, int num)
156 {
157 int i,j;
158 struct page *buff;
159
160 for (i=0;i<num;i++) {
161 if (ind[i].num==0) continue;
162
163 buff=xmalloc(sizeof(struct page)*(ind[i].num+1));
164 for (j=0;j<=ind[i].num;j++) {
165 buff[j]=ind[i].p[j];
166 }
167 qsort(buff,ind[i].num+1,sizeof(struct page),pcomp);
168 for (j=0;j<=ind[i].num;j++) {
169 ind[i].p[j]=buff[j];
170 }
171 free(buff);
172 }
173 }
174
175 /* compare for sorting page */
176 static int pcomp(const void *p, const void *q)
177 {
178 int i,j,cc=0,num1,num2;
179 char buff[16],*p0,*p1;
180 const struct page *page1 = p, *page2 = q;
181
182 scount++;
183
184 for (i=0;i<PAGE_COMPOSIT_DEPTH;i++) {
185 if ((page1->attr[i]<0)&&(page2->attr[i]<0)) return 0;
186 else if ((page1->attr[i]<0)&&(page2->attr[i]>=0)) return -1;
187 else if ((page2->attr[i]<0)&&(page1->attr[i]>=0)) return 1;
188
189 if (page1->attr[i]>page2->attr[i]) return 1;
190 if (page1->attr[i]<page2->attr[i]) return -1;
191
192 p0=&page1->page[cc];
193 p1=strstr(p0, page_compositor);
194 j=p1 ? p1-p0 : strlen(p0);
195 strncpy(buff,p0,j);
196 buff[j]='\0';
197 num1=pnumconv(buff,page1->attr[i]);
198
199 p0=&page2->page[cc];
200 p1=strstr(p0, page_compositor);
201 j=p1 ? p1-p0 : strlen(p0);
202 strncpy(buff,p0,j);
203 buff[j]='\0';
204 num2=pnumconv(buff,page2->attr[i]);
205
206 if (num1>num2) return 1;
207 else if (num1<num2) return -1;
208
209 if (page1->enc[0]=='(' || page2->enc[0]==')') return -1;
210 if (page1->enc[0]==')' || page2->enc[0]=='(') return 1;
211
212 if (p1) cc+=j+strlen(page_compositor);
213 else return 0;
214 }
215
216 return 0;
217 }
218
219 static int ordering(char *buff)
220 {
221 if ((unsigned char)(*buff)<0x80) {
222 if (alphabet(buff)) return alpha;
223 else if (numeric(buff)) return number;
224 else return sym;
225 }
226 else {
227 if (japanese(buff)) return kana;
228 else return sym;
229 }
230 }
231
232 int alphanumeric(char *c)
233 {
234 if (((*c>='A')&&(*c<='Z'))||((*c>='a')&&(*c<='z'))||((*c>='0')&&(*c<='9')))
235 return 1;
236 else return 0;
237 }
238
239 int alphabet(char *c)
240 {
241 if (((*c>='A')&&(*c<='Z'))||((*c>='a')&&(*c<='z'))) return 1;
242 else return 0;
243 }
244
245 int numeric(char *c)
246 {
247 if ((*c>='0')&&(*c<='9')) return 1;
248 else return 0;
249 }
250
251 int japanese(char *buff)
252 {
253 if (strncmp(buff,HIRATOP,2)>=0) return 1;
254 else return 0;
255 }
256
257 int chkcontinue(struct page *p, int num)
258 {
259 int i,j,cc=0,num1,num2,k1,k2;
260 char buff1[16],buff2[16],*p0,*p1;
261
262 for (i=0;i<PAGE_COMPOSIT_DEPTH;i++) {
263 if ((p[num].attr[i]<0)&&(p[num+1].attr[i]<0)) return 1;
264 else if (p[num].attr[i]!=p[num+1].attr[i]) return 0;
265
266 p0=&p[num].page[cc];
267 p1=strstr(p0, page_compositor);
268 if (p1) {
269 j=p1-p0;
270 k1=j;
271 } else {
272 j=strlen(p0);
273 k1=0;
274 }
275 strncpy(buff1,p0,j);
276 buff1[j]='\0';
277 num1=pnumconv(buff1,p[num].attr[i]);
278
279 p0=&p[num+1].page[cc];
280 p1=strstr(p0, page_compositor);
281 if (p1) {
282 j=p1-p0;
283 k2=j;
284 } else {
285 j=strlen(p0);
286 k2=0;
287 }
288 strncpy(buff2,p0,j);
289 buff2[j]='\0';
290 num2=pnumconv(buff2,p[num+1].attr[i]);
291
292 if (k1>0 || k2>0) {
293 if (k1!=k2) return 0;
294 if (strcmp(buff1,buff2)) return 0;
295 cc+=k1+strlen(page_compositor);
296 continue;
297 }
298
299 if (num1==num2 || num1+1==num2) return 1;
300 else return 0;
301 }
302
303 return 1;
304 }