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

Contents of /trunk/Src/wptNLS.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 176 - (show annotations)
Mon Feb 13 09:38:03 2006 UTC (19 years ago) by twoaday
File MIME type: text/plain
File size: 10173 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26