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