Repository navigation
Expand file tree
/
Copy pathdic.cpp
More file actions
executable file
·474 lines (427 loc) · 11.1 KB
/
Copy pathdic.cpp
File metadata and controls
executable file
·474 lines (427 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
** dic.cpp --- Hash dictionary
**
** Copyright (C) 2000, 2001 Bilingual Emacspeak Project <inoue@argv.org>
**
** Last modified: $Date: 2002/05/05 16:44:16 $ by $Author: inoue $
** Keywords: Emacs, Emacspeak, speech, Linux
**
** This file is part of BEP (Bilingual Emacspeak Project)
** <http://www.argv.org/bep/>
** This file is originally written by Gary Bishop, and modified by BEP
** to make a bilingual, Japanese and English, DECTalk-like speech server
** running under Windows.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* デバッグ情報/エラーメッセージの出力をやめるには、下の行をコメントにします。 */
/* #define DEBUG_DIC*/
#include "dic.h"
/* グローバル変数 */
/* for dic(N)ChangeWordX() */
static char DicDummyCont='\0';
/* 関数dicGetKey
bufの中を読み、key_contSepかcommentSepが
出てきたら'\0'を入れ、次の配列番号を返す。*/
int dicGetKey(Dictionary *dicp,char buf[])
{
int i;
for(i=0;;i++)
{
if((buf[i]==dicp->commentSep)||(buf[i]=='\0')) /*contがない場合*/
{
buf[i]='\0';
dicp->eor_flg=1;
break;
}
else if(buf[i]==dicp->key_contSep) /*contがある場合*/
{
buf[i]='\0';
break;
}
}
return(i+1);
}
/*関数dicGetCont
bufの中を読み、commentSepか'\0'が出てきたら
'¥0'を入れ、次の配列番号を返す。*/
int dicGetCont(Dictionary *dicp,char buf[],int ofset)
{
int i;
for(i=ofset;;i++)
{
if((buf[i]==dicp->commentSep)||(buf[i]=='\0'))
{
buf[i]='\0';
dicp->eor_flg=1;
break;
}
}
return(i+1);
}
/* 関数dicProcessHeader
ヘッダーの取り扱い
ヘッダーで定義したコメント開始記号(comment)と
区切り記号(separator)を変数commentSepとkey_contSepに
代入し、ヘッダーの行数を返す。ヘッダーは省略も可能。
ヘッダがないときは0、ヘッダが異常のときは-1を返す。
ヘッダーとは、例として
%{
comment # (デフォルトはタブ)
separator , (デフォルトはスペース)
%}
とすると、
key,content#comment
という辞書形式になる。 */
int dicProcessHeader(Dictionary *dicp,FILE *pfp)
{
int sep=0,lineCnt=0;
char buf[MaxLineLength];
buf[0]='\0';
while(buf[0]=='\0')
{
fgets(buf,MaxLineLength,pfp);
dicGetKey(dicp, buf);
}
if(!str_eq(buf,HeaderBeg))
{
fseek(pfp, 0, SEEK_SET);
#ifdef DEBUG_DIC
fprintf(stderr, "dictionary %s header processing is not required\n", dicp->dicFileName);
#endif
return(0);
}
if(!str_eq(buf,HeaderEnd))
while(1)
{
fgets(buf,MaxLineLength,pfp);
lineCnt++;
sep=dicGetKey(dicp, buf);
if(str_eq(buf,"comment"))
dicp->commentSep=buf[sep];
else if(str_eq(buf,"separator"))
dicp->key_contSep=buf[sep];
else if(str_eq(buf,HeaderEnd))
{
lineCnt--;
break;
}
else
{
#ifdef DEBUG_DIC
fprintf(stderr, "%s: invalid header\n", dicp->dicFileName);
#endif
return -1;
}
}
return(lineCnt);
}
/*関数dicKeyHash
キーを得て、ハッシュ値を返す。*/
unsigned int dicKeyHash(char *name)
{
unsigned int hashVal;
for(hashVal=0;*name;name++) /*ハッシュ値の計算*/
hashVal=(*name+31*hashVal)%HashPrime;
return(hashVal);
}
/*関数dicNKeyHash
キーを得て、ハッシュ値を返す。長さ指定バージョン*/
unsigned int dicNKeyHash(char *name, int length)
{
unsigned int hashVal=0;
char *endp;
for(endp=name+length;name<endp;name++) /*ハッシュ値の計算*/
hashVal=(*name+31*hashVal)%HashPrime;
return(hashVal);
}
/* 関数 dicFreeRecords()
ポインタで示されたdicRecord構造体と、それに連なるリストを再帰的に削除*/
void dicFreeRecords(dicRecord *recp)
{
if (recp == NULL)
return;
free(recp->key);
free(recp->cont);
dicFreeRecords(recp->next);
free(recp);
return;
}
/*関数dicLoadDictionary
keyとcontをハッシュ構造の中に置く。
辞書とファイル名を引数とする。*/
/* 返値: 0=正常終了 1=メモリ関係エラー 2=ファイルオープンエラー
3=ヘッダ形式の異常 */
int dicLoadDictionary(Dictionary *dicp,char *fileName)
{
int sep, sep2, i,hashNum, llen;
char buf[MaxLineLength];
dicRecord *tmp;
FILE *fp;
sep = sep2 = 0;
for(i=0;i<HashPrime;i++) /*ハッシュテーブルの初期化*/
dicp->dicBase[i]=NULL;
dicp->commentSep=TAB;
dicp->key_contSep=SPACE;
dicp->eor_flg=0;
dicp->entryNum=0;
dicp->dicFileName=NULL;
if((dicp->dicFileName=(char *)malloc(sizeof(char)*strlen(fileName)+1))
==NULL)
{
#ifdef DEBUG_DIC
fprintf(stderr, "memory allocation error\n");
#endif
return 1;
}
strcpy(dicp->dicFileName,fileName);
#ifdef MSDOS
if((fp=fopen(dicp->dicFileName,"rt"))==NULL)
#else
if((fp=fopen(dicp->dicFileName,"r"))==NULL)
#endif
{
#ifdef DEBUG_DIC
fprintf(stderr, "file %s can not be opend\n",dicp->dicFileName);
#endif
return 2;
}
if(dicProcessHeader(dicp, fp)<0) return 3;
while(fgets(buf,MaxLineLength,fp)!=NULL)
{
llen=strlen(buf);
if(llen>0) buf[--llen]='\0'; /* 最後の不要な'\n'を削除 */
if(llen>0 && buf[llen-1] == '\r')
buf[--llen] = '\0'; /* 不要なCRがあれば削除 */
if(llen==0 || buf[0]==dicp->commentSep || buf[0]==dicp->key_contSep)
continue; /* 意味のない行なら無視 */
dicp->eor_flg=0;
sep=dicGetKey(dicp, buf);
hashNum=dicKeyHash(buf);
if((tmp=(dicRecord *)malloc(sizeof(dicRecord)))==NULL)
{
#ifdef DEBUG_DIC
fprintf(stderr, "%s: object generation error\n", dicp->dicFileName);
#endif
return 1;
}
tmp->key=NULL; /* 初期化 */
tmp->cont=NULL;
tmp->attr = 0;
tmp->next=NULL;
if((tmp->key=(char *)malloc(sizeof(char)*(strlen(buf)+1)))
==NULL)
{
#ifdef DEBUG_DIC
fprintf(stderr, "memory allocation error\n");
#endif
return 1;
}
strcpy(tmp->key,buf);
if(!(dicp->eor_flg))
{
sep2 = dicGetKey(dicp,&buf[sep]);
if((tmp->cont=(char *)malloc(sizeof(char)*(strlen
(&buf[sep])+1)))==NULL)
{
#ifdef DEBUG_DIC
fprintf(stderr, "memory allocation error\n");
#endif
return 1;
}
strcpy(tmp->cont,&buf[sep]);
}
/* read attribute */
if (!(dicp->eor_flg)) {
dicGetCont(dicp, buf, sep + sep2);
tmp->attr = atoi(&buf[sep + sep2]);
}
tmp->next=dicp->dicBase[hashNum];
dicp->dicBase[hashNum]=tmp;
dicp->entryNum++;
}
fclose(fp);
return 0;
}
/* 関数dicUnloadDictionary()
辞書のために確保したメモリを解放する。*/
void dicUnloadDictionary(Dictionary *dicp)
{
int i;
/* ハッシュテーブルの分繰り返す。 */
for (i = 0; i < HashPrime; i++) {
dicFreeRecords(dicp->dicBase[i]);
dicp->dicBase[i] = NULL;
}
free(dicp->dicFileName);
dicp->dicFileName = NULL;
return;
}
/*関数dicHashSearch
keyを得て、辞書を検索する。
存在したらそのkeyを探すポインタを返す。
存在しなかったらNULLを返す。*/
dicRecord *dicHashSearch(Dictionary *dicp,char *key)
{
int hashNum; /*ハッシュ値*/
dicRecord *tmp;
hashNum=dicKeyHash(key);
if(dicp->dicBase[hashNum]==NULL)
return(NULL);
else if(str_eq(dicp->dicBase[hashNum]->key,key))
return(dicp->dicBase[hashNum]);
else
{
tmp=dicp->dicBase[hashNum];
while(tmp!=NULL)
{
if(str_eq(tmp->key,key))
break;
tmp=tmp->next;
}
return(tmp);
}
}
/* 関数dicNHashSearch: dicHashSearchの長さ指定バージョン
第3引数にkeyから照合に利用する長さを指定 */
dicRecord *dicNHashSearch(Dictionary *dicp,char *key, int length)
{
int hashNum; /*ハッシュ値*/
dicRecord *tmp;
hashNum=dicNKeyHash(key, length);
if(dicp->dicBase[hashNum]==NULL)
return(NULL);
else if(strn_eq(dicp->dicBase[hashNum]->key,key,length)
&& (int)strlen(dicp->dicBase[hashNum]->key) == length)
return(dicp->dicBase[hashNum]);
else
{
tmp=dicp->dicBase[hashNum];
while(tmp!=NULL)
{
if(strn_eq(tmp->key,key,length)
&& (int)strlen(tmp->key) == length)
break;
tmp=tmp->next;
}
return(tmp);
}
}
/*関数dicChangeWord
keyを得て、検索したkeyのcontを返す。
ヒットしたレコードのcontが空ならNULLを返す。 */
char *dicChangeWord(Dictionary *dicp,char *key)
{
dicRecord *tmp;
tmp=dicHashSearch(dicp,key);
if(tmp==NULL || tmp->cont==NULL)
return(NULL);
return(tmp->cont);
}
/* 関数dicNChangeWord: dicChangeWord 長さ指定バージョン */
char *dicNChangeWord(Dictionary *dicp,char *key,int length)
{
dicRecord *tmp;
tmp=dicNHashSearch(dicp,key,length);
if(tmp==NULL || tmp->cont==NULL)
return(NULL);
return(tmp->cont);
}
/*関数dicChangeWordX
keyを得て、検索したkeyのcontを返す。NULLを返す代わりに
エラーメッセージを出し、""の入った場所へのポインタを返す。*/
char *dicChangeWordX(Dictionary *dicp,char *key)
{
dicRecord *tmp;
tmp=dicHashSearch(dicp,key);
if(tmp==NULL || tmp->cont==NULL) {
#ifdef DEBUG_DIC
fprintf(stderr, "\ndicChangeWordX: cont not found: %s\n", key);
#endif
return (&DicDummyCont);
}
return(tmp->cont);
}
char *dicNChangeWordX(Dictionary *dicp,char *key,int length)
{
dicRecord *tmp;
tmp=dicNHashSearch(dicp,key,length);
if(tmp==NULL || tmp->cont == NULL) {
#ifdef DEBUG_DIC
int i;
fprintf(stderr, "\ndicNChangeWordX: cont not found: ");
for(i=0; i<length; i++)
fputc(key[i], stderr);
fputc('\n', stderr);
#endif
return(&DicDummyCont);
}
return(tmp->cont);
}
/* 関数dicSearchNext dicHashSearchで見つかったレコードのポインタとキー
を受け取り、同一のキーを持つレコードをリスト内で探索する。 */
dicRecord *dicSearchNext(dicRecord *recp, char *key)
{
dicRecord *tmp;
if(recp==NULL) return(NULL);
tmp=recp->next;
while(tmp!=NULL) {
if(str_eq(tmp->key, key)) break;
tmp=tmp->next;
}
return(tmp);
}
/* 関数dicNSearchNext: dicSearchNext 長さ指定バージョン */
dicRecord *dicNSearchNext(dicRecord *recp, char *key, int length)
{
dicRecord *tmp;
if(recp==NULL) return(NULL);
tmp=recp->next;
while(tmp!=NULL) {
if(strn_eq(tmp->key, key, length)) break;
tmp=tmp->next;
}
return(tmp);
}
/* 関数dicSearchPair
キーと内容のペアを受け取り、辞書内を探索して同じペアがあればその
dicRecordのポインタを、なければNULLを返す。 */
dicRecord *dicSearchPair(Dictionary *dicp, char *key, char *cont)
{
dicRecord *tmp;
tmp=dicHashSearch(dicp, key);
while(tmp!=NULL)
{
if((tmp->cont!=NULL) && str_eq(cont, tmp->cont)) break;
else tmp=dicSearchNext(tmp, key);
}
return(tmp);
}
/* 関数dicNSearchPair
キーと内容のペアを受け取り、辞書内を探索して同じペアがあればその
dicRecordのポインタを、なければNULLを返す。
キーと内容のそれぞれに長さを指定するバージョン */
dicRecord *dicNSearchPair(Dictionary *dicp, char *key, char *cont,
int length1, int length2)
{
dicRecord *tmp;
tmp=dicNHashSearch(dicp, key, length1);
while(tmp!=NULL)
{
if((tmp->cont!=NULL) && strn_eq(cont, tmp->cont, length2)) break;
else tmp=dicNSearchNext(tmp, key, length1);
}
return(tmp);
}