1 |
/* wptNLS.cpp - W32 Native Language Support |
2 |
* Copyright (C) 2001, 2002, 2003, 2006 Timo Schulz |
3 |
* Copyright (C) 1995-1999 Free Software Foundation, Inc. |
4 |
* |
5 |
* This code based on the simple-gettext.c code from the GnuPG |
6 |
* by Ulrich Drepper. |
7 |
* |
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 |
* You should have received a copy of the GNU General Public License |
19 |
* along with WinPT; if not, write to the Free Software Foundation, |
20 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
#ifdef HAVE_CONFIG_H |
24 |
#include <config.h> |
25 |
#endif |
26 |
|
27 |
#include <stdio.h> |
28 |
#include <string.h> |
29 |
#include <ctype.h> |
30 |
#include <errno.h> |
31 |
#include <sys/types.h> |
32 |
#include <sys/stat.h> |
33 |
#include <windows.h> |
34 |
|
35 |
#include "wptNLS.h" |
36 |
|
37 |
|
38 |
/* The magic number of the GNU message catalog format. */ |
39 |
#define MAGIC 0x950412de |
40 |
#define MAGIC_SWAPPED 0xde120495 |
41 |
|
42 |
/* Revision number of the currently used .mo (binary) file format. */ |
43 |
#define MO_REVISION_NUMBER 0 |
44 |
|
45 |
#define SWAPIT(flag, data) ((flag) ? do_swap_u32(data) : (data) ) |
46 |
|
47 |
|
48 |
/* We assume to have `unsigned long int' value with at least 32 bits. */ |
49 |
#define HASHWORDBITS 32 |
50 |
|
51 |
/* Header for binary .mo file format. */ |
52 |
struct mo_file_header { |
53 |
DWORD magic; /* The magic number. */ |
54 |
DWORD revision; /* The revision number of the file format. */ |
55 |
DWORD nstrings; /* The number of strings pairs. */ |
56 |
DWORD orig_tab_offset; /* Offset of table with start offsets of original |
57 |
strings. */ |
58 |
DWORD trans_tab_offset; /* Offset of table with start offsets of translation |
59 |
strings. */ |
60 |
DWORD hash_tab_size; /* Size of hashing table. */ |
61 |
DWORD hash_tab_offset; /* Offset of first hashing entry. */ |
62 |
}; |
63 |
|
64 |
struct string_desc { |
65 |
DWORD length; /* Length of addressed string. */ |
66 |
DWORD offset; /* Offset of string in file. */ |
67 |
}; |
68 |
|
69 |
struct loaded_domain { |
70 |
char *data; |
71 |
int must_swap; |
72 |
DWORD nstrings; |
73 |
char *mapped; |
74 |
struct string_desc *orig_tab; |
75 |
struct string_desc *trans_tab; |
76 |
DWORD hash_size; |
77 |
DWORD *hash_tab; |
78 |
}; |
79 |
|
80 |
/* List of all available languages. */ |
81 |
struct lang_table_s lang_list[] = { |
82 |
{"en", "English", LANG_ENGLISH}, |
83 |
{"de", "German", LANG_GERMAN}, |
84 |
{"fr", "French", LANG_FRENCH}, |
85 |
{"jp", "Japanese", LANG_JAPANESE}, |
86 |
{"pt_BR", "Portuguese (Brazil)", SUBLANG_PORTUGUESE_BRAZILIAN}, |
87 |
{NULL, 0} |
88 |
}; |
89 |
|
90 |
/* The current domain. */ |
91 |
static struct loaded_domain *the_domain; |
92 |
|
93 |
|
94 |
static DWORD |
95 |
do_swap_u32 (DWORD i) |
96 |
{ |
97 |
return (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24); |
98 |
} |
99 |
|
100 |
|
101 |
/* The so called `hashpjw' function by P.J. Weinberger |
102 |
[see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools, |
103 |
1986, 1987 Bell Telephone Laboratories, Inc.] */ |
104 |
|
105 |
static DWORD |
106 |
hash_string (const char *str_param) |
107 |
{ |
108 |
unsigned long int hval, g; |
109 |
const char *str = str_param; |
110 |
|
111 |
hval = 0; |
112 |
while (*str != '\0') { |
113 |
hval <<= 4; |
114 |
hval += (unsigned long int) *str++; |
115 |
g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4)); |
116 |
if (g != 0) { |
117 |
hval ^= g >> (HASHWORDBITS - 8); |
118 |
hval ^= g; |
119 |
} |
120 |
} |
121 |
return hval; |
122 |
} |
123 |
|
124 |
/* Missing W32 functions. */ |
125 |
static char* |
126 |
w32_stpcpy (char *a,const char *b) |
127 |
{ |
128 |
while (*b) |
129 |
*a++ = *b++; |
130 |
*a = 0; |
131 |
return (char*)a; |
132 |
} |
133 |
|
134 |
|
135 |
|
136 |
static struct loaded_domain * |
137 |
load_domain( const char *filename ) |
138 |
{ |
139 |
FILE *fp; |
140 |
size_t size; |
141 |
struct stat st; |
142 |
struct mo_file_header *data = NULL; |
143 |
struct loaded_domain *domain = NULL; |
144 |
size_t to_read; |
145 |
char *read_ptr; |
146 |
|
147 |
fp = fopen( filename, "rb" ); |
148 |
if( !fp ) |
149 |
return NULL; /* can't open the file */ |
150 |
/* we must know about the size of the file */ |
151 |
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 |
return NULL; |
156 |
} |
157 |
|
158 |
data = (struct mo_file_header *) malloc( size ); |
159 |
if( !data ) { |
160 |
fclose( fp ); |
161 |
return NULL; /* out of memory */ |
162 |
} |
163 |
|
164 |
to_read = size; |
165 |
read_ptr = (char *) data; |
166 |
do { |
167 |
size_t nb = fread( read_ptr, 1, to_read, fp ); |
168 |
if( nb < to_read ) { |
169 |
fclose (fp); |
170 |
free(data); |
171 |
return NULL; /* read error */ |
172 |
|
173 |
} |
174 |
read_ptr += nb; |
175 |
to_read -= nb; |
176 |
} while( to_read > 0 ); |
177 |
fclose (fp); |
178 |
|
179 |
/* Using the magic number we can test whether it really is a message |
180 |
* catalog file. */ |
181 |
if( data->magic != MAGIC && data->magic != MAGIC_SWAPPED ) { |
182 |
/* The magic number is wrong: not a message catalog file. */ |
183 |
free( data ); |
184 |
return NULL; |
185 |
} |
186 |
|
187 |
domain = (struct loaded_domain *)calloc( 1, sizeof *domain ); |
188 |
if( !domain ) { |
189 |
free( data ); |
190 |
return NULL; |
191 |
} |
192 |
domain->data = (char *) data; |
193 |
domain->must_swap = data->magic != MAGIC; |
194 |
|
195 |
/* Fill in the information about the available tables. */ |
196 |
switch( SWAPIT(domain->must_swap, data->revision) ) { |
197 |
case 0: |
198 |
domain->nstrings = SWAPIT(domain->must_swap, data->nstrings); |
199 |
domain->orig_tab = (struct string_desc *) |
200 |
((char *) data + SWAPIT(domain->must_swap, data->orig_tab_offset)); |
201 |
domain->trans_tab = (struct string_desc *) |
202 |
((char *) data + SWAPIT(domain->must_swap, data->trans_tab_offset)); |
203 |
domain->hash_size = SWAPIT(domain->must_swap, data->hash_tab_size); |
204 |
domain->hash_tab = (DWORD *) |
205 |
((char *) data + SWAPIT(domain->must_swap, data->hash_tab_offset)); |
206 |
break; |
207 |
|
208 |
default: /* This is an invalid revision. */ |
209 |
free( data ); |
210 |
free( domain ); |
211 |
return NULL; |
212 |
} |
213 |
|
214 |
/* allocate an array to keep track of code page mappings */ |
215 |
domain->mapped = (char *)calloc( 1, domain->nstrings ); |
216 |
if( !domain->mapped ) { |
217 |
free( data ); |
218 |
free( domain ); |
219 |
return NULL; |
220 |
} |
221 |
|
222 |
return domain; |
223 |
} /* load_domain */ |
224 |
|
225 |
|
226 |
/* Set the file used for translations. Pass a NULL to disable translation. |
227 |
A new filename may be set at anytime. */ |
228 |
int |
229 |
set_gettext_file( const char *filename, const char *nls_dir ) |
230 |
{ |
231 |
struct loaded_domain *domain = NULL; |
232 |
|
233 |
if( filename && *filename ) { |
234 |
if( filename[0] == '/' |
235 |
|| ( isalpha(filename[0]) |
236 |
&& filename[1] == ':' |
237 |
&& (filename[2] == '/' || filename[2] == '\\') ) |
238 |
) { |
239 |
/* absolute path - use it as is */ |
240 |
domain = load_domain( filename ); |
241 |
} |
242 |
else { /* relative path - append ".mo" and get dir from the environment */ |
243 |
char *buf = NULL; |
244 |
char *dir; |
245 |
|
246 |
dir = strdup( nls_dir ); |
247 |
if( dir && (buf= (char *)malloc(strlen(dir)+strlen(filename)+1+3+1)) ) { |
248 |
strcpy(w32_stpcpy(w32_stpcpy(w32_stpcpy( buf, dir),"/"), filename),".mo"); |
249 |
domain = load_domain( buf ); |
250 |
free(buf); |
251 |
} |
252 |
free(dir); |
253 |
} |
254 |
if( !domain ) |
255 |
return -1; |
256 |
} |
257 |
|
258 |
if( the_domain ) { |
259 |
free( the_domain->data ); |
260 |
free( the_domain->mapped ); |
261 |
free( the_domain ); |
262 |
the_domain = NULL; |
263 |
} |
264 |
the_domain = domain; |
265 |
return 0; |
266 |
} |
267 |
|
268 |
|
269 |
static const char* |
270 |
get_string( struct loaded_domain *domain, DWORD idx ) |
271 |
{ |
272 |
char *p = domain->data + SWAPIT(domain->must_swap, |
273 |
domain->trans_tab[idx].offset); |
274 |
if( !domain->mapped[idx] ) { |
275 |
domain->mapped[idx] = 1; |
276 |
} |
277 |
return (const char*)p; |
278 |
} |
279 |
|
280 |
|
281 |
const char * |
282 |
gettext( const char *msgid ) |
283 |
{ |
284 |
struct loaded_domain *domain; |
285 |
size_t act = 0; |
286 |
size_t top, bottom; |
287 |
|
288 |
if (!(domain = the_domain)) |
289 |
goto not_found; |
290 |
|
291 |
/* Locate the MSGID and its translation. */ |
292 |
if( domain->hash_size > 2 && domain->hash_tab ) { |
293 |
/* Use the hashing table. */ |
294 |
DWORD len = strlen (msgid); |
295 |
DWORD hash_val = hash_string (msgid); |
296 |
DWORD idx = hash_val % domain->hash_size; |
297 |
DWORD incr = 1 + (hash_val % (domain->hash_size - 2)); |
298 |
DWORD nstr = SWAPIT (domain->must_swap, domain->hash_tab[idx]); |
299 |
|
300 |
if ( !nstr ) /* Hash table entry is empty. */ |
301 |
goto not_found; |
302 |
|
303 |
|
304 |
if( SWAPIT(domain->must_swap, |
305 |
domain->orig_tab[nstr - 1].length) == len |
306 |
&& !strcmp( msgid, |
307 |
domain->data + SWAPIT(domain->must_swap, |
308 |
domain->orig_tab[nstr - 1].offset)) ) |
309 |
return get_string( domain, nstr - 1 ); |
310 |
for(;;) { |
311 |
if (idx >= domain->hash_size - incr) |
312 |
idx -= domain->hash_size - incr; |
313 |
else |
314 |
idx += incr; |
315 |
nstr = SWAPIT(domain->must_swap, domain->hash_tab[idx]); |
316 |
if( !nstr ) |
317 |
goto not_found; /* Hash table entry is empty. */ |
318 |
|
319 |
if ( SWAPIT(domain->must_swap, |
320 |
domain->orig_tab[nstr - 1].length) == len |
321 |
&& !strcmp (msgid, |
322 |
domain->data + SWAPIT(domain->must_swap, |
323 |
domain->orig_tab[nstr - 1].offset))) |
324 |
|
325 |
return get_string( domain, nstr-1 ); |
326 |
} /* NOTREACHED */ |
327 |
} |
328 |
|
329 |
/* Now we try the default method: binary search in the sorted |
330 |
array of messages. */ |
331 |
bottom = 0; |
332 |
top = domain->nstrings; |
333 |
while( bottom < top ) { |
334 |
int cmp_val; |
335 |
|
336 |
act = (bottom + top) / 2; |
337 |
cmp_val = strcmp(msgid, domain->data + SWAPIT(domain->must_swap, |
338 |
domain->orig_tab[act].offset)); |
339 |
if (cmp_val < 0) |
340 |
top = act; |
341 |
else if (cmp_val > 0) |
342 |
bottom = act + 1; |
343 |
else |
344 |
return get_string( domain, act ); |
345 |
} |
346 |
|
347 |
not_found: |
348 |
return msgid; |
349 |
} /* gettext */ |
350 |
|
351 |
|
352 |
/* Map the user specific language ID to a gettext conform language string. |
353 |
Example: LANG_GERMAN -> "de" */ |
354 |
const char* |
355 |
get_gettext_langid (void) |
356 |
{ |
357 |
LANGID lang; |
358 |
int i; |
359 |
|
360 |
lang = GetUserDefaultLangID (); |
361 |
if (PRIMARYLANGID (lang) == LANG_ENGLISH) |
362 |
return NULL; |
363 |
|
364 |
for (i=0; lang_list[i].id; i++) { |
365 |
if (PRIMARYLANGID (lang) == lang_list[i].langid) |
366 |
return lang_list[i].id; |
367 |
} |
368 |
return NULL; |
369 |
} |
370 |
|
371 |
|
372 |
/* Take a table with control item IDs and their translation |
373 |
and set each text to the translated value. */ |
374 |
void |
375 |
gettext_localize_dialog (HWND dlg, struct gettext_tab_s *tab, |
376 |
const char *title) |
377 |
{ |
378 |
int i; |
379 |
|
380 |
for (i=0; tab[i].trans != NULL; i++) |
381 |
SetDlgItemText (dlg, tab[i].ctlid, tab[i].trans); |
382 |
if (title) |
383 |
SetWindowText (dlg, title); |
384 |
} |
385 |
|
386 |
|
387 |
/* XXX: this has to be placed in a .c file but not here */ |
388 |
#include <shlobj.h> |
389 |
|
390 |
void _SHFree (void *p) |
391 |
{ |
392 |
IMalloc *pm; |
393 |
SHGetMalloc(&pm); |
394 |
if (pm) { |
395 |
pm->lpVtbl->Free(pm,p); |
396 |
pm->lpVtbl->Release(pm); |
397 |
} |
398 |
} |