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

Contents of /trunk/Src/wptNLS.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 205 - (show annotations)
Thu Apr 27 12:46:03 2006 UTC (18 years, 10 months ago) by twoaday
File MIME type: text/plain
File size: 10219 byte(s)
2006-04-27  Timo Schulz  <ts@g10code.de>
                                                                                
        * wptKeyManager.cpp (km_get_key_ptr): New.
        * wptListview.cpp (listview_get_item_text): Drop utf8 support.
        * wptKeyCache.cpp (keycache_decode_uids): New.
        (free_native_uids): New.
        * wptKeyEdit.cpp (uid_inf_colon_handler): Do utf8 decodig here.
                                                                                
2006-04-26  Timo Schulz  <ts@g10code.de>
                                                                                
        * wptKeylist.cpp (get_keyid_from_fpr): New.
        * wptDecryptClipDlg.cpp (clip_decrypt_dlg): Use it here.
        * wptVerifyList.cpp (verlist_add_sig): Likewise.


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 {NULL, 0}
87 };
88
89 /* The current domain. */
90 static struct loaded_domain *the_domain;
91
92
93 static DWORD
94 do_swap_u32 (DWORD i)
95 {
96 return (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24);
97 }
98
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 DWORD
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 }
122
123 /* Missing W32 functions. */
124 static char*
125 w32_stpcpy (char *a,const char *b)
126 {
127 while (*b)
128 *a++ = *b++;
129 *a = 0;
130 return (char*)a;
131 }
132
133
134
135 static struct loaded_domain *
136 load_domain( const char *filename )
137 {
138 FILE *fp;
139 size_t size;
140 struct stat st;
141 struct mo_file_header *data = NULL;
142 struct loaded_domain *domain = NULL;
143 size_t to_read;
144 char *read_ptr;
145
146 fp = fopen( filename, "rb" );
147 if( !fp )
148 return NULL; /* can't open the file */
149 /* we must know about the size of the file */
150 if( fstat( fileno(fp ), &st )
151 || (size = (size_t)st.st_size) != (size_t)st.st_size
152 || size < sizeof (struct mo_file_header) ) {
153 fclose( fp );
154 return NULL;
155 }
156
157 data = (struct mo_file_header *) malloc( size );
158 if( !data ) {
159 fclose( fp );
160 return NULL; /* out of memory */
161 }
162
163 to_read = size;
164 read_ptr = (char *) data;
165 do {
166 size_t nb = fread( read_ptr, 1, to_read, fp );
167 if( nb < to_read ) {
168 fclose (fp);
169 free(data);
170 return NULL; /* read error */
171
172 }
173 read_ptr += nb;
174 to_read -= nb;
175 } while( to_read > 0 );
176 fclose (fp);
177
178 /* Using the magic number we can test whether it really is a message
179 * catalog file. */
180 if( data->magic != MAGIC && data->magic != MAGIC_SWAPPED ) {
181 /* The magic number is wrong: not a message catalog file. */
182 free( data );
183 return NULL;
184 }
185
186 domain = (struct loaded_domain *)calloc( 1, sizeof *domain );
187 if( !domain ) {
188 free( data );
189 return NULL;
190 }
191 domain->data = (char *) data;
192 domain->must_swap = data->magic != MAGIC;
193
194 /* Fill in the information about the available tables. */
195 switch( SWAPIT(domain->must_swap, data->revision) ) {
196 case 0:
197 domain->nstrings = SWAPIT(domain->must_swap, data->nstrings);
198 domain->orig_tab = (struct string_desc *)
199 ((char *) data + SWAPIT(domain->must_swap, data->orig_tab_offset));
200 domain->trans_tab = (struct string_desc *)
201 ((char *) data + SWAPIT(domain->must_swap, data->trans_tab_offset));
202 domain->hash_size = SWAPIT(domain->must_swap, data->hash_tab_size);
203 domain->hash_tab = (DWORD *)
204 ((char *) data + SWAPIT(domain->must_swap, data->hash_tab_offset));
205 break;
206
207 default: /* This is an invalid revision. */
208 free( data );
209 free( domain );
210 return NULL;
211 }
212
213 /* allocate an array to keep track of code page mappings */
214 domain->mapped = (char *)calloc( 1, domain->nstrings );
215 if( !domain->mapped ) {
216 free( data );
217 free( domain );
218 return NULL;
219 }
220
221 return domain;
222 } /* load_domain */
223
224
225 /* Set the file used for translations. Pass a NULL to disable translation.
226 A new filename may be set at anytime. */
227 int
228 set_gettext_file( const char *filename, const char *nls_dir )
229 {
230 struct loaded_domain *domain = NULL;
231
232 if( filename && *filename ) {
233 if( filename[0] == '/'
234 || ( isalpha(filename[0])
235 && filename[1] == ':'
236 && (filename[2] == '/' || filename[2] == '\\') )
237 ) {
238 /* absolute path - use it as is */
239 domain = load_domain( filename );
240 }
241 else { /* relative path - append ".mo" and get dir from the environment */
242 char *buf = NULL;
243 char *dir;
244
245 dir = strdup( nls_dir );
246 if( dir && (buf= (char *)malloc(strlen(dir)+strlen(filename)+1+3+1)) ) {
247 strcpy(w32_stpcpy(w32_stpcpy(w32_stpcpy( buf, dir),"/"), filename),".mo");
248 domain = load_domain( buf );
249 free(buf);
250 }
251 free(dir);
252 }
253 if( !domain )
254 return -1;
255 }
256
257 if( the_domain ) {
258 free( the_domain->data );
259 free( the_domain->mapped );
260 free( the_domain );
261 the_domain = NULL;
262 }
263 the_domain = domain;
264 return 0;
265 }
266
267
268 static const char*
269 get_string( struct loaded_domain *domain, DWORD idx )
270 {
271 char *p = domain->data + SWAPIT(domain->must_swap,
272 domain->trans_tab[idx].offset);
273 if( !domain->mapped[idx] ) {
274 domain->mapped[idx] = 1;
275 }
276 return (const char*)p;
277 }
278
279
280 const char *
281 gettext( const char *msgid )
282 {
283 struct loaded_domain *domain;
284 size_t act = 0;
285 size_t top, bottom;
286
287 if (!(domain = the_domain))
288 goto not_found;
289
290 /* Locate the MSGID and its translation. */
291 if( domain->hash_size > 2 && domain->hash_tab ) {
292 /* Use the hashing table. */
293 DWORD len = strlen (msgid);
294 DWORD hash_val = hash_string (msgid);
295 DWORD idx = hash_val % domain->hash_size;
296 DWORD incr = 1 + (hash_val % (domain->hash_size - 2));
297 DWORD nstr = SWAPIT (domain->must_swap, domain->hash_tab[idx]);
298
299 if ( !nstr ) /* Hash table entry is empty. */
300 goto not_found;
301
302
303 if( SWAPIT(domain->must_swap,
304 domain->orig_tab[nstr - 1].length) == len
305 && !strcmp( msgid,
306 domain->data + SWAPIT(domain->must_swap,
307 domain->orig_tab[nstr - 1].offset)) )
308 return get_string( domain, nstr - 1 );
309 for(;;) {
310 if (idx >= domain->hash_size - incr)
311 idx -= domain->hash_size - incr;
312 else
313 idx += incr;
314 nstr = SWAPIT(domain->must_swap, domain->hash_tab[idx]);
315 if( !nstr )
316 goto not_found; /* Hash table entry is empty. */
317
318 if ( SWAPIT(domain->must_swap,
319 domain->orig_tab[nstr - 1].length) == len
320 && !strcmp (msgid,
321 domain->data + SWAPIT(domain->must_swap,
322 domain->orig_tab[nstr - 1].offset)))
323
324 return get_string( domain, nstr-1 );
325 } /* NOTREACHED */
326 }
327
328 /* Now we try the default method: binary search in the sorted
329 array of messages. */
330 bottom = 0;
331 top = domain->nstrings;
332 while( bottom < top ) {
333 int cmp_val;
334
335 act = (bottom + top) / 2;
336 cmp_val = strcmp(msgid, domain->data + SWAPIT(domain->must_swap,
337 domain->orig_tab[act].offset));
338 if (cmp_val < 0)
339 top = act;
340 else if (cmp_val > 0)
341 bottom = act + 1;
342 else
343 return get_string( domain, act );
344 }
345
346 not_found:
347 return msgid;
348 } /* gettext */
349
350
351 /* Map the user specific language ID to a gettext conform language string.
352 Example: LANG_GERMAN -> "de" */
353 const char*
354 get_gettext_langid (void)
355 {
356 LANGID lang;
357 int i;
358
359 lang = GetUserDefaultLangID ();
360 if (PRIMARYLANGID (lang) == LANG_ENGLISH)
361 return NULL;
362
363 for (i=0; lang_list[i].id; i++) {
364 if (PRIMARYLANGID (lang) == lang_list[i].langid)
365 return lang_list[i].id;
366 }
367 return NULL;
368 }
369
370
371 /* Take a table with control item IDs and their translation
372 and set each text to the translated value. */
373 void
374 gettext_localize_dialog (HWND dlg, struct gettext_tab_s *tab,
375 const char *title)
376 {
377 int i;
378
379 for (i=0; tab[i].trans != NULL; i++)
380 SetDlgItemText (dlg, tab[i].ctlid, tab[i].trans);
381 if (title)
382 SetWindowText (dlg, title);
383 }
384
385
386 /* XXX: this has to be placed in a .c file but not here */
387 #include <shlobj.h>
388
389 void _SHFree (void *p)
390 {
391 IMalloc *pm;
392 SHGetMalloc(&pm);
393 if (pm) {
394 pm->lpVtbl->Free(pm,p);
395 pm->lpVtbl->Release(pm);
396 }
397 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26