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

Annotation of /trunk/Src/wptNLS.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 193 - (hide annotations)
Sat Apr 1 12:36:35 2006 UTC (18 years, 11 months ago) by twoaday
File MIME type: text/plain
File size: 10222 byte(s)
2006-03-31  Timo Schulz  <ts@g10code.de>
 
        * wptCommonDlg.cpp (nls_load_langlist): New.
        (nsl_set_language): New.
        (nls_dlg_proc): New.
        (select_language): New. Allow user to select the language.
        * wptNLS.c (get_gettext_langid): Updated available languages.
        * WinPT.cpp (WinMain): Allow to select the languag on first
        start in non-installer environments.
        * wptVerifyList.cpp (verlist_build): Simplified.
        (verlist_add_sig_log): Likewise.
        * wptListview.cpp (listview_set_column_width,
        listview_get_selected_item): New.
        * wptKeyManager.cpp (gpg_clip_export): Merged into..
        (km_clip_export): ..this function.


1 werner 36 /* wptNLS.cpp - W32 Native Language Support
2 twoaday 176 * Copyright (C) 2001, 2002, 2003, 2006 Timo Schulz
3 werner 36 * 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 twoaday 176
38 werner 36 /* 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 twoaday 193 #define SWAPIT(flag, data) ((flag) ? do_swap_u32(data) : (data) )
46 werner 36
47 twoaday 193
48     /* We assume to have `unsigned long int' value with at least 32 bits. */
49     #define HASHWORDBITS 32
50    
51     /* The so called `hashpjw' function by P.J. Weinberger
52     [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
53     1986, 1987 Bell Telephone Laboratories, Inc.] */
54    
55 werner 36 /* Header for binary .mo file format. */
56     struct mo_file_header {
57 twoaday 193 DWORD magic; /* The magic number. */
58     DWORD revision; /* The revision number of the file format. */
59     DWORD nstrings; /* The number of strings pairs. */
60     DWORD orig_tab_offset; /* Offset of table with start offsets of original
61     strings. */
62     DWORD trans_tab_offset; /* Offset of table with start offsets of translation
63     strings. */
64     DWORD hash_tab_size; /* Size of hashing table. */
65     DWORD hash_tab_offset; /* Offset of first hashing entry. */
66 werner 36 };
67    
68     struct string_desc {
69 twoaday 193 DWORD length; /* Length of addressed string. */
70     DWORD offset; /* Offset of string in file. */
71 werner 36 };
72    
73     struct loaded_domain {
74     char *data;
75     int must_swap;
76 twoaday 193 DWORD nstrings;
77 werner 36 char *mapped;
78     struct string_desc *orig_tab;
79     struct string_desc *trans_tab;
80 twoaday 193 DWORD hash_size;
81     DWORD *hash_tab;
82 werner 36 };
83    
84 twoaday 193 /* List of all available languages. */
85     struct lang_table_s lang_list[] = {
86     {"en", "English", LANG_ENGLISH},
87     {"de", "German", LANG_GERMAN},
88     {"fr", "French", LANG_FRENCH},
89     {"jp", "Japanese", LANG_JAPANESE},
90     {NULL, 0}
91     };
92    
93     /* The current domain. */
94 werner 36 static struct loaded_domain *the_domain;
95    
96 twoaday 193
97     static DWORD
98     do_swap_u32 (DWORD i)
99 werner 36 {
100 twoaday 193 return (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24);
101 werner 36 }
102    
103    
104 twoaday 193 /* Missing W32 functions. */
105     static char*
106     w32_stpcpy (char *a,const char *b)
107     {
108     while (*b)
109     *a++ = *b++;
110     *a = 0;
111     return (char*)a;
112     }
113 werner 36
114    
115    
116 twoaday 193 static DWORD
117     hash_string (const char *str_param)
118 werner 36 {
119     unsigned long int hval, g;
120     const char *str = str_param;
121    
122     hval = 0;
123     while (*str != '\0') {
124     hval <<= 4;
125     hval += (unsigned long int) *str++;
126     g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4));
127     if (g != 0) {
128     hval ^= g >> (HASHWORDBITS - 8);
129     hval ^= g;
130     }
131     }
132     return hval;
133 twoaday 193 }
134 werner 36
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 twoaday 193 || (size = (size_t)st.st_size) != (size_t)st.st_size
153 werner 36 || 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 twoaday 193 size_t nb = fread( read_ptr, 1, to_read, fp );
168 werner 36 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 twoaday 193 domain->hash_tab = (DWORD *)
205 werner 36 ((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 twoaday 193 /* Set the file used for translations. Pass a NULL to disable translation.
227     A new filename may be set at anytime. */
228 werner 36 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 twoaday 69 strcpy(w32_stpcpy(w32_stpcpy(w32_stpcpy( buf, dir),"/"), filename),".mo");
249 werner 36 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 twoaday 193 }
267 werner 36
268    
269     static const char*
270 twoaday 193 get_string( struct loaded_domain *domain, DWORD idx )
271 werner 36 {
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 twoaday 193 }
279 werner 36
280 twoaday 193
281 werner 36 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 twoaday 193 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 werner 36
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 twoaday 176
352 twoaday 193 /* Map the user specific language ID to a gettext conform language string.
353 twoaday 76 Example: LANG_GERMAN -> "de" */
354     const char*
355     get_gettext_langid (void)
356     {
357     LANGID lang;
358     int i;
359 twoaday 176
360 twoaday 76 lang = GetUserDefaultLangID ();
361 twoaday 193 if (PRIMARYLANGID (lang) == LANG_ENGLISH)
362     return NULL;
363 twoaday 176
364 twoaday 193 for (i=0; lang_list[i].id; i++) {
365     if (PRIMARYLANGID (lang) == lang_list[i].langid)
366     return lang_list[i].id;
367 twoaday 76 }
368     return NULL;
369     }
370 werner 36
371 twoaday 76
372 twoaday 176 /* 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 werner 36 /* 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     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26