/[winpt]/trunk/Src/wptNLS.cpp
ViewVC logotype

Annotation of /trunk/Src/wptNLS.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (hide annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 12831 byte(s)


1 twoaday 273 /* wptNLS.cpp - W32 Native Language Support
2 twoaday 328 * Copyright (C) 2001-2003, 2006, 2008, 2009 Timo Schulz
3 twoaday 273 * Copyright (C) 1995-1999 Free Software Foundation, Inc.
4     *
5 twoaday 278 * This code is a stripped down version of simple-gettext.c
6     * written by by Ulrich Drepper from the GPG project.
7 twoaday 273 *
8     * WinPT is free software; you can redistribute it and/or
9     * modify it under the terms of the GNU General Public License
10     * as published by the Free Software Foundation; either version 2
11     * of the License, or (at your option) any later version.
12     *
13     * WinPT is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16     * General Public License for more details.
17     */
18     #ifdef HAVE_CONFIG_H
19     #include <config.h>
20     #endif
21    
22     #include <stdio.h>
23     #include <string.h>
24     #include <sys/stat.h>
25 twoaday 328 #include <time.h>
26 twoaday 273 #include <windows.h>
27    
28     #include "wptNLS.h"
29 twoaday 278 #include "wptTypes.h"
30 twoaday 328 #include "wptUtil.h"
31     #include "wptW32API.h"
32     #include "wptVersion.h"
33     #include "StringBuffer.h"
34 twoaday 273
35     /* The magic number of the GNU message catalog format. */
36     #define MAGIC 0x950412de
37     #define MAGIC_SWAPPED 0xde120495
38    
39     /* Revision number of the currently used .mo (binary) file format. */
40     #define MO_REVISION_NUMBER 0
41    
42     #define SWAPIT(flag, data) ((flag) ? do_swap_u32(data) : (data) )
43    
44    
45     /* We assume to have `unsigned long int' value with at least 32 bits. */
46     #define HASHWORDBITS 32
47    
48     /* Header for binary .mo file format. */
49     struct mo_file_header {
50     DWORD magic; /* The magic number. */
51     DWORD revision; /* The revision number of the file format. */
52     DWORD nstrings; /* The number of strings pairs. */
53     DWORD orig_tab_offset; /* Offset of table with start offsets of original
54     strings. */
55     DWORD trans_tab_offset; /* Offset of table with start offsets of translation
56     strings. */
57     DWORD hash_tab_size; /* Size of hashing table. */
58     DWORD hash_tab_offset; /* Offset of first hashing entry. */
59     };
60    
61     struct string_desc {
62     DWORD length; /* Length of addressed string. */
63     DWORD offset; /* Offset of string in file. */
64     };
65    
66 twoaday 328 struct loaded_domain_s {
67     char *file_name;
68     time_t modify_time;
69     char *data;
70     int must_swap;
71     DWORD nstrings;
72     char *mapped;
73     struct string_desc *orig_tab;
74     struct string_desc *trans_tab;
75     DWORD hash_size;
76     DWORD *hash_tab;
77 twoaday 273 };
78 twoaday 328 typedef struct loaded_domain_s *gettext_domain_t;
79 twoaday 273
80     /* List of all available languages. */
81     struct lang_table_s lang_list[] = {
82     {"en", "English", LANG_ENGLISH},
83     {"de", "German", LANG_GERMAN},
84 twoaday 328 {"ru", "Russian", LANG_RUSSIAN},
85     //{"fr", "French", LANG_FRENCH},
86     //{"jp", "Japanese",LANG_JAPANESE},
87     {NULL, NULL, 0}
88 twoaday 273 };
89    
90 twoaday 278
91 twoaday 328 /* The current gettext domain. */
92     static gettext_domain_t the_domain;
93 twoaday 273
94    
95     static DWORD
96     do_swap_u32 (DWORD i)
97     {
98     return (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24);
99     }
100    
101    
102     /* The so called `hashpjw' function by P.J. Weinberger
103     [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
104     1986, 1987 Bell Telephone Laboratories, Inc.] */
105     static DWORD
106     hash_string (const char *str_param)
107     {
108     DWORD hval, g;
109     const char *str = str_param;
110    
111     hval = 0;
112     while (*str != '\0') {
113     hval <<= 4;
114     hval += (DWORD) *str++;
115     g = hval & ((DWORD) 0xf << (HASHWORDBITS - 4));
116     if (g != 0) {
117     hval ^= g >> (HASHWORDBITS - 8);
118     hval ^= g;
119     }
120     }
121     return hval;
122     }
123    
124 twoaday 278
125 twoaday 273 /* Missing W32 functions. */
126     static char*
127 twoaday 328 w32_stpcpy (char *a, const char *b)
128 twoaday 273 {
129     while (*b)
130     *a++ = *b++;
131     *a = 0;
132     return (char*)a;
133     }
134    
135    
136 twoaday 328 static gettext_domain_t
137 twoaday 278 load_domain (const char *filename)
138 twoaday 273 {
139     FILE *fp;
140     size_t size;
141     struct stat st;
142     struct mo_file_header *data = NULL;
143 twoaday 328 gettext_domain_t domain = NULL;
144 twoaday 273 size_t to_read;
145     char *read_ptr;
146    
147 twoaday 278 fp = fopen (filename, "rb");
148     if (!fp)
149     return NULL;
150 twoaday 273 /* we must know about the size of the file */
151 twoaday 328 if (fstat (fileno(fp), &st) ||
152     (size = (size_t)st.st_size) != (size_t)st.st_size ||
153     size < sizeof (struct mo_file_header)) {
154     fclose (fp);
155 twoaday 273 return NULL;
156     }
157    
158 twoaday 278 data = (struct mo_file_header *) malloc (size);
159     if (!data)
160     BUG (0);
161 twoaday 273
162     to_read = size;
163     read_ptr = (char *) data;
164     do {
165 twoaday 328 size_t nb = fread (read_ptr, 1, to_read, fp);
166     if (nb < to_read) {
167 twoaday 273 fclose (fp);
168     free(data);
169 twoaday 328 return NULL; /* read error */
170 twoaday 273 }
171     read_ptr += nb;
172     to_read -= nb;
173 twoaday 328 } while (to_read > 0);
174 twoaday 273 fclose (fp);
175    
176     /* Using the magic number we can test whether it really is a message
177     * catalog file. */
178 twoaday 328 if (data->magic != MAGIC && data->magic != MAGIC_SWAPPED) {
179 twoaday 273 /* The magic number is wrong: not a message catalog file. */
180 twoaday 328 free (data);
181 twoaday 273 return NULL;
182     }
183    
184 twoaday 328 domain = (struct loaded_domain_s *)calloc (1, sizeof *domain);
185 twoaday 278 if (!domain)
186     BUG (0);
187 twoaday 273 domain->data = (char *) data;
188     domain->must_swap = data->magic != MAGIC;
189    
190     /* Fill in the information about the available tables. */
191 twoaday 328 switch (SWAPIT(domain->must_swap, data->revision)) {
192 twoaday 273 case 0:
193 twoaday 328 domain->nstrings = SWAPIT (domain->must_swap, data->nstrings);
194 twoaday 273 domain->orig_tab = (struct string_desc *)
195 twoaday 328 ((char *) data + SWAPIT (domain->must_swap, data->orig_tab_offset));
196 twoaday 273 domain->trans_tab = (struct string_desc *)
197 twoaday 328 ((char *) data + SWAPIT (domain->must_swap, data->trans_tab_offset));
198     domain->hash_size = SWAPIT (domain->must_swap, data->hash_tab_size);
199 twoaday 273 domain->hash_tab = (DWORD *)
200 twoaday 328 ((char *) data + SWAPIT (domain->must_swap, data->hash_tab_offset));
201 twoaday 273 break;
202    
203     default: /* This is an invalid revision. */
204 twoaday 328 free (data);
205     free (domain);
206 twoaday 273 return NULL;
207     }
208    
209     /* allocate an array to keep track of code page mappings */
210     domain->mapped = (char *)calloc( 1, domain->nstrings );
211 twoaday 278 if (!domain->mapped)
212     BUG (0);
213 twoaday 328
214     domain->file_name = strdup (filename);
215     domain->modify_time = st.st_mtime;
216 twoaday 273 return domain;
217 twoaday 278 }
218 twoaday 273
219    
220 twoaday 328 /* Returns a string representation of the users system language
221     * if we support it, 'en' otherwise.
222     */
223     static const char*
224     get_user_langid (void)
225     {
226     int lang = GetUserDefaultLangID() & 511;
227    
228     for (int i=0; lang_list[i].id != NULL; i++) {
229     if (lang == lang_list[i].langid)
230     return lang_list[i].id;
231     }
232     return "en";
233     }
234    
235    
236     /* To avoid problems that the current directory is not the
237     * same folder the WinPT.exe is located in, we extract the
238     * module directory.
239     */
240     char*
241     get_module_dir (char *buf, DWORD buflen)
242     {
243     int pos;
244    
245     if (buflen < 1 || !GetModuleFileName (glob_hinst, buf, buflen-1))
246     return NULL;
247    
248     pos = strlen (buf);
249     while (pos > 0 && buf[--pos] != '\\')
250     ;
251     buf[pos+1] = '\0';
252     return buf;
253     }
254    
255    
256     /* Returns a localisation name for a specific resource (file).
257     * For instance, lang=DE and file=winpt.chm will become
258     * winpt.DE.chm
259     */
260     char*
261     get_locale_name (const char *file)
262     {
263     const char *lang = get_user_langid();
264     char *name, *ext;
265     char *p, buf[MAX_PATH+1];
266    
267     p = strrchr (file, '.');
268     if (p == NULL) {
269     p = new char[strlen(file)+strlen(lang)+2+1];
270     sprintf (p, "%s.%s", file, lang);
271     return p;
272     }
273     name = substr(file, 0, p-file);
274     ext = substr(file, p-file+1, strlen(file));
275     p = get_module_dir (buf, MAX_PATH);
276     if (!p)
277     BUG (NULL);
278     StringBuffer s = p;
279     s = s + name + "." + lang + "." + ext;
280     p = s.getBufferCopy();
281     free_if_alloc (name);
282     free_if_alloc (ext);
283     return p;
284     }
285    
286    
287 twoaday 278 /* Deallocate static resources. */
288     void
289     gettext_free_current_domain (void)
290     {
291 twoaday 328 if (the_domain == NULL)
292 twoaday 278 return;
293     free (the_domain->data);
294     free (the_domain->mapped);
295 twoaday 328 free (the_domain->file_name);
296 twoaday 278 free (the_domain);
297     the_domain = NULL;
298     }
299    
300    
301 twoaday 328 char* get_reg_entry_mo (void);
302    
303     /* Fallback code which is used when no new NLS file
304     * were found. Then we read the MODir registry key
305     * and check if a file named 'winpt.mo' exists in this
306     * directory.
307     */
308     static gettext_domain_t
309     load_modir_domain (void)
310     {
311     gettext_domain_t domain = NULL;
312     StringBuffer s;
313     char *modir;
314    
315     modir = get_reg_entry_mo ();
316     if (!modir)
317     return NULL;
318     if (dir_exist_check (modir)) {
319     free_if_alloc (modir);
320     return NULL;
321     }
322     s = modir;
323     if (modir[strlen (modir)-1] != '\\')
324     s += "\\";
325     s += "winpt.mo";
326     const char *fname = s.getBuffer();
327     if (!file_exist_check (fname))
328     domain = load_domain (fname);
329     free_if_alloc (modir);
330     return domain;
331     }
332    
333    
334     int
335     gettext_set_user_domain (void)
336     {
337     gettext_domain_t domain = NULL;
338     char *file = get_locale_name ("winpt.mo");
339     if (file && !file_exist_check (file))
340     domain = load_domain (file);
341     else
342     domain = load_modir_domain ();
343     free_if_alloc (file);
344     gettext_free_current_domain ();
345     the_domain = domain;
346     return 0;
347     }
348    
349    
350     int
351     gettext_domain_needs_refresh (void)
352     {
353     struct stat st;
354    
355     if (the_domain == NULL || stat (the_domain->file_name, &st))
356     return 0;
357     /* '1' means that the MO file were modified in the mean time
358     and the application should reload the domain. */
359     if (st.st_mtime != the_domain->modify_time)
360     return 1;
361    
362     return 0;
363     }
364    
365 twoaday 273 /* Set the file used for translations. Pass a NULL to disable translation.
366     A new filename may be set at anytime. */
367     int
368 twoaday 278 gettext_set_file (const char *filename, const char *nls_dir)
369 twoaday 273 {
370 twoaday 328 gettext_domain_t domain = NULL;
371 twoaday 273
372 twoaday 278 if (filename && *filename) {
373     if (filename[1] == ':' && filename[2] == '\\')
374     domain = load_domain (filename); /* absolute path - use it as is */
375 twoaday 273 else { /* relative path - append ".mo" and get dir from the environment */
376 twoaday 278 char *buf, *dir;
377 twoaday 273
378 twoaday 278 dir = strdup (nls_dir);
379     if (!dir)
380     BUG (0);
381     buf= (char *)malloc (strlen (dir) + strlen (filename)+1+3+1);
382     strcpy (w32_stpcpy (w32_stpcpy (
383 twoaday 328 w32_stpcpy (buf, dir), "\\"), filename),".mo");
384 twoaday 278 domain = load_domain(buf);
385     free (buf);
386     free (dir);
387 twoaday 273 }
388 twoaday 278 if (!domain)
389 twoaday 273 return -1;
390     }
391    
392 twoaday 328 gettext_free_current_domain ();
393 twoaday 273 the_domain = domain;
394     return 0;
395     }
396    
397    
398     static const char*
399 twoaday 328 get_string (gettext_domain_t domain, DWORD idx)
400 twoaday 273 {
401     char *p = domain->data + SWAPIT(domain->must_swap,
402     domain->trans_tab[idx].offset);
403 twoaday 328 if (!domain->mapped[idx])
404     domain->mapped[idx] = 1;
405 twoaday 273 return (const char*)p;
406     }
407    
408    
409     const char *
410 twoaday 328 gettext (const char *msgid)
411 twoaday 273 {
412 twoaday 328 gettext_domain_t domain;
413 twoaday 273 size_t act = 0;
414     size_t top, bottom;
415 twoaday 328
416     domain = the_domain;
417     if (domain == NULL)
418 twoaday 273 goto not_found;
419    
420     /* Locate the MSGID and its translation. */
421 twoaday 328 if (domain->hash_size > 2 && domain->hash_tab) {
422 twoaday 273 /* Use the hashing table. */
423     DWORD len = strlen (msgid);
424     DWORD hash_val = hash_string (msgid);
425     DWORD idx = hash_val % domain->hash_size;
426     DWORD incr = 1 + (hash_val % (domain->hash_size - 2));
427     DWORD nstr = SWAPIT (domain->must_swap, domain->hash_tab[idx]);
428    
429 twoaday 328 if (!nstr) /* Hash table entry is empty. */
430 twoaday 273 goto not_found;
431 twoaday 328
432     if (SWAPIT(domain->must_swap,
433 twoaday 273 domain->orig_tab[nstr - 1].length) == len
434 twoaday 328 && !strcmp (msgid,
435     domain->data + SWAPIT (domain->must_swap,
436     domain->orig_tab[nstr - 1].offset)))
437     return get_string (domain, nstr - 1);
438 twoaday 273 for(;;) {
439     if (idx >= domain->hash_size - incr)
440     idx -= domain->hash_size - incr;
441     else
442     idx += incr;
443     nstr = SWAPIT(domain->must_swap, domain->hash_tab[idx]);
444 twoaday 328 if (!nstr)
445 twoaday 273 goto not_found; /* Hash table entry is empty. */
446    
447 twoaday 328 if (SWAPIT (domain->must_swap,
448     domain->orig_tab[nstr - 1].length) == len
449     && !strcmp (msgid,
450     domain->data + SWAPIT (domain->must_swap,
451     domain->orig_tab[nstr - 1].offset)))
452    
453     return get_string (domain, nstr-1);
454 twoaday 273 } /* NOTREACHED */
455     }
456    
457     /* Now we try the default method: binary search in the sorted
458     array of messages. */
459     bottom = 0;
460     top = domain->nstrings;
461 twoaday 328 while (bottom < top) {
462 twoaday 273 int cmp_val;
463    
464     act = (bottom + top) / 2;
465 twoaday 328 cmp_val = strcmp (msgid, domain->data + SWAPIT(domain->must_swap,
466     domain->orig_tab[act].offset));
467 twoaday 273 if (cmp_val < 0)
468     top = act;
469     else if (cmp_val > 0)
470     bottom = act + 1;
471 twoaday 328 else
472     return get_string (domain, act);
473 twoaday 273 }
474    
475     not_found:
476     return msgid;
477     }
478    
479    
480     /* Map the user specific language ID to a gettext conform language string.
481     Example: LANG_GERMAN -> "de" */
482     const char*
483 twoaday 278 gettext_get_langid (void)
484 twoaday 273 {
485     LANGID lang;
486    
487     lang = GetUserDefaultLangID ();
488     if (PRIMARYLANGID (lang) == LANG_ENGLISH)
489     return NULL;
490    
491 twoaday 328 for (int i=0; lang_list[i].id; i++) {
492 twoaday 273 if (PRIMARYLANGID (lang) == lang_list[i].langid)
493     return lang_list[i].id;
494     }
495     return NULL;
496     }
497    
498    
499     /* Take a table with control item IDs and their translation
500     and set each text to the translated value. */
501     void
502     gettext_localize_dialog (HWND dlg, struct gettext_tab_s *tab,
503     const char *title)
504     {
505 twoaday 328 for (int i=0; tab[i].trans != NULL; i++)
506 twoaday 273 SetDlgItemText (dlg, tab[i].ctlid, tab[i].trans);
507 twoaday 328 if (title != NULL)
508 twoaday 273 SetWindowText (dlg, title);
509     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26