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

Contents of /trunk/Src/wptNLS.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 48 - (show annotations)
Mon Oct 31 21:14:11 2005 UTC (19 years, 4 months ago) by werner
File MIME type: text/plain
File size: 9370 byte(s)
More changes.  Compiles again but there are at least gettext issues with
w32-gettext.c.  I can't get a gpg-error build with ENABLE_NLS.

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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26