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

Annotation of /trunk/Src/wptNLS.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 372 - (hide annotations)
Fri Dec 9 10:59:16 2011 UTC (13 years, 2 months ago) by twoaday
File size: 12150 byte(s)
2011-12-09  Timo Schulz  <twoaday@gmx.net>

        * wptNLS.cpp (gettext_set_user_domain): Changed return type to
	void.
		

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 twoaday 372 /* Full file name to the MO file */
68 twoaday 328 char *file_name;
69 twoaday 372 /* Last modification date of the MO file */
70 twoaday 328 time_t modify_time;
71 twoaday 372
72 twoaday 328 char *data;
73     int must_swap;
74     DWORD nstrings;
75     char *mapped;
76     struct string_desc *orig_tab;
77     struct string_desc *trans_tab;
78     DWORD hash_size;
79     DWORD *hash_tab;
80 twoaday 273 };
81 twoaday 328 typedef struct loaded_domain_s *gettext_domain_t;
82 twoaday 273
83     /* List of all available languages. */
84     struct lang_table_s lang_list[] = {
85     {"en", "English", LANG_ENGLISH},
86     {"de", "German", LANG_GERMAN},
87 twoaday 328 {"ru", "Russian", LANG_RUSSIAN},
88     {NULL, NULL, 0}
89 twoaday 273 };
90    
91 twoaday 278
92 twoaday 328 /* The current gettext domain. */
93     static gettext_domain_t the_domain;
94 twoaday 273
95    
96 twoaday 368 /* Return TRUE if the native user language is not English
97     and thus, gettext support is available */
98     static bool
99     gettext_is_required (void)
100     {
101     LANGID lang = GetUserDefaultLangID ();
102     if (PRIMARYLANGID (lang) == LANG_ENGLISH)
103     return false;
104     return true;
105     }
106    
107    
108 twoaday 273 static DWORD
109     do_swap_u32 (DWORD i)
110     {
111     return (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24);
112     }
113    
114    
115     /* The so called `hashpjw' function by P.J. Weinberger
116     [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
117     1986, 1987 Bell Telephone Laboratories, Inc.] */
118     static DWORD
119     hash_string (const char *str_param)
120     {
121     DWORD hval, g;
122     const char *str = str_param;
123    
124     hval = 0;
125     while (*str != '\0') {
126     hval <<= 4;
127     hval += (DWORD) *str++;
128     g = hval & ((DWORD) 0xf << (HASHWORDBITS - 4));
129     if (g != 0) {
130     hval ^= g >> (HASHWORDBITS - 8);
131     hval ^= g;
132     }
133     }
134     return hval;
135     }
136    
137 twoaday 278
138 twoaday 328 static gettext_domain_t
139 twoaday 278 load_domain (const char *filename)
140 twoaday 273 {
141     FILE *fp;
142     size_t size;
143     struct stat st;
144     struct mo_file_header *data = NULL;
145 twoaday 328 gettext_domain_t domain = NULL;
146 twoaday 273 size_t to_read;
147     char *read_ptr;
148    
149 twoaday 278 fp = fopen (filename, "rb");
150     if (!fp)
151     return NULL;
152 twoaday 273 /* we must know about the size of the file */
153 twoaday 328 if (fstat (fileno(fp), &st) ||
154     (size = (size_t)st.st_size) != (size_t)st.st_size ||
155     size < sizeof (struct mo_file_header)) {
156     fclose (fp);
157 twoaday 273 return NULL;
158     }
159    
160 twoaday 278 data = (struct mo_file_header *) malloc (size);
161     if (!data)
162     BUG (0);
163 twoaday 273
164     to_read = size;
165     read_ptr = (char *) data;
166     do {
167 twoaday 328 size_t nb = fread (read_ptr, 1, to_read, fp);
168     if (nb < to_read) {
169 twoaday 273 fclose (fp);
170 twoaday 368 free (data);
171 twoaday 328 return NULL; /* read error */
172 twoaday 273 }
173     read_ptr += nb;
174     to_read -= nb;
175 twoaday 328 } while (to_read > 0);
176 twoaday 273 fclose (fp);
177    
178 twoaday 372 /* Using the magic number we can test whether it really is a message catalog file. */
179 twoaday 328 if (data->magic != MAGIC && data->magic != MAGIC_SWAPPED) {
180 twoaday 273 /* The magic number is wrong: not a message catalog file. */
181 twoaday 328 free (data);
182 twoaday 273 return NULL;
183     }
184    
185 twoaday 328 domain = (struct loaded_domain_s *)calloc (1, sizeof *domain);
186 twoaday 278 if (!domain)
187     BUG (0);
188 twoaday 273 domain->data = (char *) data;
189     domain->must_swap = data->magic != MAGIC;
190    
191     /* Fill in the information about the available tables. */
192 twoaday 328 switch (SWAPIT(domain->must_swap, data->revision)) {
193 twoaday 273 case 0:
194 twoaday 328 domain->nstrings = SWAPIT (domain->must_swap, data->nstrings);
195 twoaday 273 domain->orig_tab = (struct string_desc *)
196 twoaday 328 ((char *) data + SWAPIT (domain->must_swap, data->orig_tab_offset));
197 twoaday 273 domain->trans_tab = (struct string_desc *)
198 twoaday 328 ((char *) data + SWAPIT (domain->must_swap, data->trans_tab_offset));
199     domain->hash_size = SWAPIT (domain->must_swap, data->hash_tab_size);
200 twoaday 273 domain->hash_tab = (DWORD *)
201 twoaday 328 ((char *) data + SWAPIT (domain->must_swap, data->hash_tab_offset));
202 twoaday 273 break;
203    
204     default: /* This is an invalid revision. */
205 twoaday 328 free (data);
206     free (domain);
207 twoaday 273 return NULL;
208     }
209    
210     /* allocate an array to keep track of code page mappings */
211 twoaday 368 domain->mapped = (char *)calloc (1, domain->nstrings);
212 twoaday 278 if (!domain->mapped)
213     BUG (0);
214 twoaday 328
215     domain->file_name = strdup (filename);
216     domain->modify_time = st.st_mtime;
217 twoaday 273 return domain;
218 twoaday 278 }
219 twoaday 273
220    
221 twoaday 328 /* To avoid problems that the current directory is not the
222     * same folder the WinPT.exe is located in, we extract the
223     * module directory.
224     */
225     char*
226     get_module_dir (char *buf, DWORD buflen)
227     {
228     if (buflen < 1 || !GetModuleFileName (glob_hinst, buf, buflen-1))
229     return NULL;
230    
231 twoaday 368 size_t pos = strlen (buf);
232 twoaday 328 while (pos > 0 && buf[--pos] != '\\')
233     ;
234 twoaday 372 buf[pos + 1] = '\0';
235 twoaday 328 return buf;
236     }
237    
238    
239     /* Returns a localisation name for a specific resource (file).
240 twoaday 368 * For instance, lang=DE and file=winpt.chm will become winpt.DE.chm
241 twoaday 328 */
242     char*
243     get_locale_name (const char *file)
244     {
245 twoaday 368 const char *lang = gettext_get_langid ();
246 twoaday 372 char buf[MAX_PATH+1];
247 twoaday 328
248 twoaday 372 char *p = strrchr (file, '.');
249 twoaday 328 if (p == NULL) {
250     p = new char[strlen(file)+strlen(lang)+2+1];
251     sprintf (p, "%s.%s", file, lang);
252     return p;
253     }
254 twoaday 372 char *name = substr (file, 0, p - file);
255     char *ext = substr (file, p - file + 1, strlen(file));
256 twoaday 328 p = get_module_dir (buf, MAX_PATH);
257     if (!p)
258     BUG (NULL);
259     StringBuffer s = p;
260     s = s + name + "." + lang + "." + ext;
261     p = s.getBufferCopy();
262     free_if_alloc (name);
263     free_if_alloc (ext);
264     return p;
265     }
266    
267    
268 twoaday 278 /* Deallocate static resources. */
269     void
270     gettext_free_current_domain (void)
271     {
272 twoaday 328 if (the_domain == NULL)
273 twoaday 278 return;
274     free (the_domain->data);
275     free (the_domain->mapped);
276 twoaday 328 free (the_domain->file_name);
277 twoaday 278 free (the_domain);
278     the_domain = NULL;
279     }
280    
281    
282 twoaday 368 /* Replace and release the old domain and assign a new domain then */
283     static void
284     gettext_update_domain (gettext_domain_t new_dom)
285     {
286     gettext_free_current_domain ();
287     the_domain = new_dom;
288     }
289    
290    
291 twoaday 328 char* get_reg_entry_mo (void);
292    
293 twoaday 368 /* Fallback code which is used when no new NLS file were found.
294     Then we read the MODir registry key and check if a file named
295     'winpt.mo' exists in this directory. */
296 twoaday 328 static gettext_domain_t
297     load_modir_domain (void)
298 twoaday 368 {
299 twoaday 366 char *modir = get_reg_entry_mo ();
300 twoaday 328 if (!modir)
301     return NULL;
302     if (dir_exist_check (modir)) {
303     free_if_alloc (modir);
304     return NULL;
305     }
306 twoaday 366 StringBuffer s = modir;
307 twoaday 328 if (modir[strlen (modir)-1] != '\\')
308     s += "\\";
309     s += "winpt.mo";
310 twoaday 366
311 twoaday 368 gettext_domain_t domain = NULL;
312 twoaday 328 const char *fname = s.getBuffer();
313     if (!file_exist_check (fname))
314     domain = load_domain (fname);
315     free_if_alloc (modir);
316     return domain;
317     }
318    
319    
320 twoaday 368 /* Try to load the user domain, either from
321     the WinPT.exe directory or from the registry */
322 twoaday 372 void
323 twoaday 328 gettext_set_user_domain (void)
324     {
325 twoaday 368 if (!gettext_is_required ())
326 twoaday 372 return;
327 twoaday 368
328 twoaday 328 gettext_domain_t domain = NULL;
329     char *file = get_locale_name ("winpt.mo");
330     if (file && !file_exist_check (file))
331     domain = load_domain (file);
332     else
333     domain = load_modir_domain ();
334     free_if_alloc (file);
335 twoaday 368 gettext_update_domain (domain);
336 twoaday 328 }
337    
338    
339     int
340     gettext_domain_needs_refresh (void)
341     {
342     struct stat st;
343    
344     if (the_domain == NULL || stat (the_domain->file_name, &st))
345     return 0;
346     /* '1' means that the MO file were modified in the mean time
347     and the application should reload the domain. */
348     if (st.st_mtime != the_domain->modify_time)
349     return 1;
350    
351     return 0;
352     }
353    
354 twoaday 273 static const char*
355 twoaday 328 get_string (gettext_domain_t domain, DWORD idx)
356 twoaday 273 {
357     char *p = domain->data + SWAPIT(domain->must_swap,
358     domain->trans_tab[idx].offset);
359 twoaday 328 if (!domain->mapped[idx])
360     domain->mapped[idx] = 1;
361 twoaday 368 /* FIXME: all text is in UTF-8 and need to be converted to
362     the locale charset. But the caller does not free the data and
363     thus we will get a leak */
364 twoaday 366 return (const char *)p;
365 twoaday 273 }
366    
367    
368     const char *
369 twoaday 328 gettext (const char *msgid)
370 twoaday 273 {
371     size_t act = 0;
372     size_t top, bottom;
373 twoaday 328
374 twoaday 366 gettext_domain_t domain = the_domain;
375 twoaday 328 if (domain == NULL)
376 twoaday 273 goto not_found;
377    
378     /* Locate the MSGID and its translation. */
379 twoaday 328 if (domain->hash_size > 2 && domain->hash_tab) {
380 twoaday 273 /* Use the hashing table. */
381     DWORD len = strlen (msgid);
382     DWORD hash_val = hash_string (msgid);
383     DWORD idx = hash_val % domain->hash_size;
384     DWORD incr = 1 + (hash_val % (domain->hash_size - 2));
385     DWORD nstr = SWAPIT (domain->must_swap, domain->hash_tab[idx]);
386    
387 twoaday 328 if (!nstr) /* Hash table entry is empty. */
388 twoaday 273 goto not_found;
389 twoaday 328
390     if (SWAPIT(domain->must_swap,
391 twoaday 273 domain->orig_tab[nstr - 1].length) == len
392 twoaday 328 && !strcmp (msgid,
393     domain->data + SWAPIT (domain->must_swap,
394     domain->orig_tab[nstr - 1].offset)))
395     return get_string (domain, nstr - 1);
396 twoaday 273 for(;;) {
397     if (idx >= domain->hash_size - incr)
398     idx -= domain->hash_size - incr;
399     else
400     idx += incr;
401 twoaday 366 nstr = SWAPIT (domain->must_swap, domain->hash_tab[idx]);
402 twoaday 328 if (!nstr)
403 twoaday 273 goto not_found; /* Hash table entry is empty. */
404    
405 twoaday 328 if (SWAPIT (domain->must_swap,
406     domain->orig_tab[nstr - 1].length) == len
407     && !strcmp (msgid,
408     domain->data + SWAPIT (domain->must_swap,
409     domain->orig_tab[nstr - 1].offset)))
410    
411     return get_string (domain, nstr-1);
412 twoaday 273 } /* NOTREACHED */
413     }
414    
415     /* Now we try the default method: binary search in the sorted
416     array of messages. */
417     bottom = 0;
418     top = domain->nstrings;
419 twoaday 366 while (bottom < top) {
420 twoaday 273 act = (bottom + top) / 2;
421 twoaday 366 int cmp_val = strcmp (msgid, domain->data + SWAPIT(domain->must_swap,
422     domain->orig_tab[act].offset));
423 twoaday 273 if (cmp_val < 0)
424     top = act;
425     else if (cmp_val > 0)
426     bottom = act + 1;
427 twoaday 328 else
428     return get_string (domain, act);
429 twoaday 273 }
430    
431     not_found:
432     return msgid;
433     }
434    
435    
436 twoaday 368
437 twoaday 273 /* Map the user specific language ID to a gettext conform language string.
438 twoaday 368 English "en" is defined as the fallback.
439 twoaday 273 Example: LANG_GERMAN -> "de" */
440     const char*
441 twoaday 278 gettext_get_langid (void)
442 twoaday 273 {
443 twoaday 366 LANGID lang = GetUserDefaultLangID ();
444 twoaday 368
445 twoaday 328 for (int i=0; lang_list[i].id; i++) {
446 twoaday 273 if (PRIMARYLANGID (lang) == lang_list[i].langid)
447     return lang_list[i].id;
448     }
449 twoaday 368
450     return "en";
451 twoaday 273 }
452    
453    
454     /* Take a table with control item IDs and their translation
455     and set each text to the translated value. */
456     void
457 twoaday 366 gettext_localize_dialog (HWND dlg, struct gettext_tab_s *tab, const char *title)
458 twoaday 273 {
459 twoaday 328 for (int i=0; tab[i].trans != NULL; i++)
460 twoaday 273 SetDlgItemText (dlg, tab[i].ctlid, tab[i].trans);
461 twoaday 328 if (title != NULL)
462 twoaday 273 SetWindowText (dlg, title);
463     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26