Mercurial > freewnn
comparison Wnn/etc/msg.c @ 0:bbc77ca4def5
initial import
author | Yoshiki Yazawa <yaz@cc.rim.or.jp> |
---|---|
date | Thu, 13 Dec 2007 04:30:14 +0900 |
parents | |
children | ed4bb01eb317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:bbc77ca4def5 |
---|---|
1 /* | |
2 * $Id: msg.c,v 1.13 2005/04/10 15:26:37 aonoto Exp $ | |
3 */ | |
4 | |
5 /* | |
6 * FreeWnn is a network-extensible Kana-to-Kanji conversion system. | |
7 * This file is part of FreeWnn. | |
8 * | |
9 * Copyright Kyoto University Research Institute for Mathematical Sciences | |
10 * 1987, 1988, 1989, 1990, 1991, 1992 | |
11 * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999 | |
12 * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992 | |
13 * Copyright FreeWnn Project 1999, 2000, 2002 | |
14 * | |
15 * Maintainer: FreeWnn Project <freewnn@tomo.gr.jp> | |
16 * | |
17 * This library is free software; you can redistribute it and/or | |
18 * modify it under the terms of the GNU Lesser General Public | |
19 * License as published by the Free Software Foundation; either | |
20 * version 2 of the License, or (at your option) any later version. | |
21 * | |
22 * This library is distributed in the hope that it will be useful, | |
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
25 * Lesser General Public License for more details. | |
26 * | |
27 * You should have received a copy of the GNU Lesser General Public | |
28 * License along with this library; if not, write to the | |
29 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
30 * Boston, MA 02111-1307, USA. | |
31 */ | |
32 | |
33 /* | |
34 struct msg_cat msg_open(name, nlspath, lang) | |
35 char *name; | |
36 char *nlspath; | |
37 char *lang; | |
38 | |
39 char * msg_get(cd, id, s, lang) | |
40 struct msg_cat cd; | |
41 int id; | |
42 char *s; | |
43 | |
44 void msg_close(cd) | |
45 struct msg_cat cd; | |
46 | |
47 format of message file | |
48 <message id>\t<message> | |
49 */ | |
50 #ifdef HAVE_CONFIG_H | |
51 # include <config.h> | |
52 #endif | |
53 | |
54 #include <stdio.h> | |
55 #if STDC_HEADERS | |
56 # include <stdlib.h> | |
57 # include <string.h> | |
58 #else | |
59 # if HAVE_MALLOC_H | |
60 # include <malloc.h> | |
61 # endif | |
62 # if HAVE_STRINGS_H | |
63 # include <strings.h> | |
64 # endif | |
65 #endif /* STDC_HEADERS */ | |
66 | |
67 #include "commonhd.h" | |
68 #include "wnn_os.h" | |
69 #include "msg.h" | |
70 | |
71 static char * | |
72 getlang (lang) | |
73 char *lang; | |
74 { | |
75 static char tmp[32]; | |
76 char *p; | |
77 int i; | |
78 | |
79 if (lang == NULL || *lang == '\0') | |
80 { | |
81 #ifdef HAS_SETLOCALE | |
82 lang = setlocale (LC_ALL, NULL); | |
83 if (lang == NULL || *lang == '\0') | |
84 #endif | |
85 { | |
86 lang = getenv ("LC_MESSAGES"); | |
87 if (lang == NULL || *lang == '\0') | |
88 { | |
89 lang = getenv ("LANG"); | |
90 if (lang == NULL || *lang == '\0') | |
91 { | |
92 lang = DEF_LANG; | |
93 } | |
94 } | |
95 } | |
96 } | |
97 for (i = 0, p = lang; *p && *p != '.'; i++, p++) | |
98 { | |
99 tmp[i] = *p; | |
100 } | |
101 tmp[i] = '\0'; | |
102 return (tmp); | |
103 /* | |
104 return(lang); | |
105 */ | |
106 } | |
107 | |
108 static int | |
109 _search (id, bd) | |
110 int *id; | |
111 struct msg_bd *bd; | |
112 { | |
113 return (*id - bd->msg_id); | |
114 } | |
115 | |
116 static void | |
117 _escape (op, ip) | |
118 register char *op, *ip; | |
119 { | |
120 for (; *ip != 0; ip++, op++) | |
121 { | |
122 if (*ip == '\\') | |
123 { | |
124 switch (*++ip) | |
125 { | |
126 case 'n': | |
127 *op = '\n'; | |
128 break; | |
129 case 't': | |
130 *op = '\t'; | |
131 break; | |
132 case 'b': | |
133 *op = '\b'; | |
134 break; | |
135 case 'r': | |
136 *op = '\r'; | |
137 break; | |
138 case 'f': | |
139 *op = '\f'; | |
140 break; | |
141 case 'v': | |
142 *op = '\v'; | |
143 break; | |
144 case '0': | |
145 *op = 0; | |
146 break; | |
147 /* | |
148 case 'a': | |
149 *op = '\a'; | |
150 break; | |
151 case 'e': | |
152 case 'E': | |
153 case 'o': | |
154 case 'd': | |
155 case 'x': | |
156 break; | |
157 */ | |
158 default: | |
159 *op = *ip; | |
160 break; | |
161 } | |
162 } | |
163 else | |
164 { | |
165 if (*ip == '\n') | |
166 { | |
167 *op = '\0'; | |
168 } | |
169 else | |
170 { | |
171 *op = *ip; | |
172 } | |
173 } | |
174 } | |
175 *op = 0; | |
176 } | |
177 | |
178 static char * | |
179 get_msg_bd (cd, id) | |
180 struct msg_cat *cd; | |
181 int id; | |
182 { | |
183 register struct msg_bd *bd; | |
184 if (cd->msg_bd == 0 || cd->msg_cnt == 0) | |
185 return (NULL); | |
186 bd = (struct msg_bd *) bsearch (&id, cd->msg_bd, cd->msg_cnt, sizeof (struct msg_bd), _search); | |
187 if (bd == NULL) | |
188 return (NULL); | |
189 return (bd->msg); | |
190 } | |
191 | |
192 /* expand | |
193 %N: the value of the name parameter passed to msg_open() | |
194 %L: the value of LANG | |
195 %l: the language element from LANG | |
196 %t: the territory element from LANG | |
197 %c: the codeset element from LANG | |
198 %%: a single % charctor | |
199 */ | |
200 static int | |
201 expand (op, ip, name, lang) | |
202 register char *op, *ip, *name, *lang; | |
203 { | |
204 if (!ip || !*ip) | |
205 return (-1); | |
206 for (; *ip != 0; ip++) | |
207 { | |
208 if (*ip == '%') | |
209 { | |
210 switch (*++ip) | |
211 { | |
212 case 'N': | |
213 if (!name || !*name) | |
214 return (-1); | |
215 strcpy (op, name); | |
216 op += strlen (name); | |
217 break; | |
218 case 'L': | |
219 if (!lang || !*lang) | |
220 return (-1); | |
221 strcpy (op, lang); | |
222 op += strlen (lang); | |
223 break; | |
224 /* | |
225 case 'l': | |
226 strcpy(op, language); | |
227 op += strlen(language); | |
228 break; | |
229 case 't': | |
230 strcpy(op, terr); | |
231 op += strlen(terr); | |
232 break; | |
233 case 'c': | |
234 strcpy(op, code); | |
235 op += strlen(code); | |
236 break; | |
237 case '%': | |
238 strcpy(op, "%"); | |
239 op += strlen("%"); | |
240 break; | |
241 */ | |
242 default: | |
243 break; | |
244 } | |
245 } | |
246 else | |
247 { | |
248 *op = *ip; | |
249 op++; | |
250 } | |
251 } | |
252 *op = '\0'; | |
253 return (0); | |
254 } | |
255 | |
256 | |
257 struct msg_cat * | |
258 msg_open (name, nlspath, lang) | |
259 char *name; | |
260 char *nlspath; | |
261 char *lang; | |
262 { | |
263 struct msg_cat *cd; | |
264 | |
265 char fn[MAXPATHLEN]; | |
266 FILE *fp; | |
267 char data[1024]; | |
268 char save[1024]; | |
269 int msg_cnt = 0; | |
270 int msg_byte = 0; | |
271 register char *dp; | |
272 register struct msg_bd *bd; | |
273 register char *msg, *l; | |
274 | |
275 l = getlang (lang); | |
276 if (name && *name == '/') | |
277 { | |
278 strcpy (fn, name); | |
279 } | |
280 else | |
281 { | |
282 if (expand (fn, nlspath, name, l) == -1) | |
283 { | |
284 return (NULL); | |
285 } | |
286 } | |
287 | |
288 if (!(cd = (struct msg_cat *) malloc (sizeof (struct msg_cat)))) | |
289 return (NULL); | |
290 | |
291 strcpy (cd->name, name); | |
292 strcpy (cd->lang, l); | |
293 strcpy (cd->nlspath, nlspath); | |
294 cd->nextp = NULL; | |
295 cd->msg_cnt = 0; | |
296 | |
297 if ((fp = fopen (fn, "r")) == NULL) | |
298 { | |
299 /* message file not found */ | |
300 cd->msg_bd = 0; | |
301 return (cd); | |
302 } | |
303 for (;;) | |
304 { | |
305 /* first: count bytes */ | |
306 if (fgets (data, 1024, fp) == NULL) | |
307 break; | |
308 if (*data == '#') | |
309 continue; /* comment */ | |
310 for (dp = data; *dp && *dp != '\t'; dp++); /* msg_id:message\n */ | |
311 if (*dp == '\0') | |
312 continue; | |
313 dp++; | |
314 msg_byte += strlen (dp); | |
315 msg_cnt++; | |
316 } | |
317 rewind (fp); | |
318 | |
319 cd->msg_cnt = msg_cnt; | |
320 if (!(bd = cd->msg_bd = (struct msg_bd *) malloc ((sizeof (struct msg_bd)) * msg_cnt + msg_byte + 1))) | |
321 { | |
322 fclose (fp); | |
323 free (cd); | |
324 return (NULL); | |
325 } | |
326 msg = (char *) bd + (sizeof (struct msg_bd)) * msg_cnt; | |
327 | |
328 for (;;) | |
329 { | |
330 /* second : get message */ | |
331 if (fgets (data, 1024, fp) == NULL) | |
332 break; | |
333 if (*data == '#') | |
334 continue; /* comment */ | |
335 for (dp = data; *dp && *dp != '\t'; dp++); /* msg_id:message\n */ | |
336 if (*dp == '\0') | |
337 continue; | |
338 *dp = 0; | |
339 dp++; | |
340 bd->msg_id = atoi (data); | |
341 bd->msg = msg; | |
342 bd++; | |
343 _escape (save, dp); | |
344 strcpy (msg, save); | |
345 msg += strlen (save); | |
346 *msg = 0; | |
347 msg++; | |
348 } | |
349 fclose (fp); | |
350 return (cd); | |
351 } | |
352 | |
353 char * | |
354 msg_get (catd, id, msg, lang) | |
355 struct msg_cat *catd; | |
356 int id; | |
357 char *msg; | |
358 register char *lang; | |
359 { | |
360 register struct msg_cat *cd; | |
361 register char *msg_bd; | |
362 | |
363 if (catd == 0) | |
364 goto error; | |
365 cd = catd; | |
366 if (lang == 0 || *lang == '\0') | |
367 { | |
368 lang = cd->lang; | |
369 } | |
370 else | |
371 { | |
372 for (;; cd = cd->nextp) | |
373 { | |
374 if (strcmp (lang, cd->lang) == 0) | |
375 break; | |
376 if (cd->nextp == 0) | |
377 { | |
378 cd->nextp = msg_open (cd->name, cd->nlspath, lang); | |
379 cd = cd->nextp; | |
380 break; | |
381 } | |
382 } | |
383 } | |
384 | |
385 if (msg_bd = get_msg_bd (cd, id)) | |
386 return (msg_bd); | |
387 error: | |
388 if (msg != 0 && *msg != '\0') | |
389 return (msg); | |
390 { | |
391 static char ret[128]; | |
392 sprintf (ret, "mes_id = %d: %s", id, DEF_MSG); | |
393 return (ret); | |
394 } | |
395 } | |
396 | |
397 void | |
398 msg_close (cd) | |
399 register struct msg_cat *cd; | |
400 { | |
401 if (cd->nextp) | |
402 msg_close (cd->nextp); | |
403 if (cd->msg_bd) | |
404 free (cd->msg_bd); | |
405 if (cd) | |
406 free (cd); | |
407 } | |
408 | |
409 #ifdef not_use | |
410 /* test */ | |
411 main () | |
412 { | |
413 struct msg_cat *cd; | |
414 | |
415 cd = msg_open ("msg", "%L", "ja_JP"); | |
416 | |
417 printf (msg_get (cd, 5, "message not found\n", "ja_JP"), 555); | |
418 printf (msg_get (cd, 6, "message not found\n", "zh_CN")); | |
419 printf (msg_get (cd, -1, "", "ja_JP"), 555); | |
420 printf (msg_get (cd, 2, "message not found\n", "ja_JP"), "abc"); | |
421 printf (msg_get (cd, 100, "message not found\n", "zh_CN"), "abc"); | |
422 } | |
423 #endif /* not_use */ |