Debian Codegraph @

terps/scare/sclocale.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o Standard libc ctype.h functions vary their results according to the
24 * currently set locale. We want consistent Windows codepage 1252 or
25 * codepage 1251 (WinLatin1 or WinCyrillic) results. To get this, then,
26 * we have to define the needed functions internally to SCARE.
27 */
28
29 #include <assert.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "scare.h"
35 #include "scprotos.h"
36
37
38 /*
39 * All the ctype boolean and character tables contain 256 elements, one for
40 * each possible sc_char value. This is used to size arrays and to verify
41 * that range setting functions do not overrun array boundaries.
42 */
43 enum { TABLE_SIZE = 256 };
44
45
46 /*
47 * loc_setrange_bool()
48 * loc_setranges_bool()
49 *
50 * Helpers for building ctype tables. Sets all elements from start to end
51 * inclusive to TRUE, and iterate this on a ranges array.
52 */
53 static void
54 loc_setrange_bool (sc_int start, sc_int end, sc_bool table[])
55 {
56 sc_int index_;
57
58 for (index_ = start; index_ <= end; index_++)
59 {
60 assert (index_ > -1 && index_ < TABLE_SIZE);
61 table[index_] = TRUE;
62 }
63 }
64
65 static void
66 loc_setranges_bool (const sc_int ranges[], sc_bool table[])
67 {
68 sc_int index_;
69
70 for (index_ = 0; ranges[index_] > -1; index_ += 2)
71 {
72 assert (ranges[index_] <= ranges[index_ + 1]);
73 loc_setrange_bool (ranges[index_], ranges[index_ + 1], table);
74 }
75 }
76
77
78 /*
79 * loc_setrange_char()
80 * loc_setranges_char()
81 *
82 * Helpers for building ctype conversion tables. Sets all elements from start
83 * to end inclusive to their index value plus the given offset, and iterate
84 * this on a ranges array.
85 */
86 static void
87 loc_setrange_char (sc_int start, sc_int end, sc_int offset, sc_char table[])
88 {
89 sc_int index_;
90
91 for (index_ = start; index_ <= end; index_++)
92 {
93 assert (index_ > -1 && index_ < TABLE_SIZE);
94 assert (index_ + offset > -1 && index_ + offset < TABLE_SIZE);
95 table[index_] = index_ + offset;
96 }
97 }
98
99 static void
100 loc_setranges_char (const sc_int ranges[], sc_char table[])
101 {
102 sc_int index_;
103
104 for (index_ = 0; ranges[index_] > -1; index_ += 3)
105 {
106 assert (ranges[index_] <= ranges[index_ + 1]);
107 loc_setrange_char (ranges[index_],
108 ranges[index_ + 1], ranges[index_ + 2], table);
109 }
110 }
111
112
113 /*
114 * A locale consists of a name, ranges for each table, and signatures for
115 * autodetection based on the game's compilation date. This is the static
116 * data portion of a locale.
117 */
118 enum { RANGES_LENGTH = 32 };
119 enum { SIGNATURE_COUNT = 24, SIGNATURE_LENGTH = 3 };
120 typedef struct
121 {
122 const sc_char *const name;
123 const sc_int isspace_ranges[RANGES_LENGTH];
124 const sc_int isdigit_ranges[RANGES_LENGTH];
125 const sc_int isalpha_ranges[RANGES_LENGTH];
126 const sc_int toupper_ranges[RANGES_LENGTH];
127 const sc_int tolower_ranges[RANGES_LENGTH];
128 const sc_byte signature[SIGNATURE_COUNT][SIGNATURE_LENGTH];
129 } sc_locale_t;
130
131
132 /*
133 * The locale table set is built from a locale using its ranges. There is one
134 * table for each function, and a pointer to the locale used to construct the
135 * table, for synchronization with changed locales. This is the dynamic data
136 * portion of a locale.
137 */
138 typedef struct
139 {
140 const sc_locale_t *locale;
141 sc_bool isspace[TABLE_SIZE];
142 sc_bool isdigit[TABLE_SIZE];
143 sc_bool isalpha[TABLE_SIZE];
144 sc_char toupper[TABLE_SIZE];
145 sc_char tolower[TABLE_SIZE];
146 } sc_locale_table_t;
147
148 /*
149 * Define a single static locale table set. This set re-initializes if it
150 * detects a locale change.
151 */
152 static sc_locale_table_t loc_locale_tables = {NULL, {0}, {0}, {0}, {0}, {0}};
153
154
155 /*
156 * loc_synchronize_tables()
157 * loc_check_tables_synchronized()
158 *
159 * Initialize tables for a locale. And compare the locale tables to a locale
160 * and if not for the same locale, (re-)initialize.
161 */
162 static void
163 loc_synchronize_tables (const sc_locale_t *locale)
164 {
165 /* Clear all tables and the locale pointer. */
166 memset (&loc_locale_tables, 0, sizeof (loc_locale_tables));
167
168 /* Set ranges and attach the new locale. */
169 loc_setranges_bool (locale->isspace_ranges, loc_locale_tables.isspace);
170 loc_setranges_bool (locale->isdigit_ranges, loc_locale_tables.isdigit);
171 loc_setranges_bool (locale->isalpha_ranges, loc_locale_tables.isalpha);
172 loc_setranges_char (locale->toupper_ranges, loc_locale_tables.toupper);
173 loc_setranges_char (locale->tolower_ranges, loc_locale_tables.tolower);
174
175 loc_locale_tables.locale = locale;
176 }
177
178 static void
179 loc_check_tables_synchronized (const sc_locale_t *locale)
180 {
181 if (locale != loc_locale_tables.locale)
182 loc_synchronize_tables (locale);
183 }
184
185
186 /*
187 * Locale for Latin1. The signatures in this locale are null since it is the
188 * default locale; no matching required. Also, none may be practical, as this
189 * locale works for a large number of Western European languages (though in
190 * practice, it seems that only English and French Adrift Latin1 games exist).
191 */
192 static const sc_locale_t LATIN1_LOCALE = {
193 "Latin1",
194 {9,13, 32,32, 160,160, -1},
195 {48,57, -1},
196 {65,90, 97,122, 192,214, 216,246, 248,255, 138,138, 140,140,
197 142,142, 154,154, 156,156, 158,158, 159,159, -1},
198 {0,TABLE_SIZE-1,0, 97,122,-32, 224,246,-32, 248,254,-32, 154,154,-16,
199 156,156,-16, 158,158,-16, 255,255,-96, -1},
200 {0,TABLE_SIZE-1,0, 65,90,32, 192,214,32, 216,222,32, 138,138,16,
201 140,140,16, 142,142,16, 159,159,96, -1},
202 {{0}}
203 };
204
205
206 /*
207 * Locale for Cyrillic. The signatures in this locale are month names in
208 * both mixed case and lowercase Russian Cyrillic.
209 */
210 static const sc_locale_t CYRILLIC_LOCALE = {
211 "Cyrillic",
212 {9,13, 32,32, 160,160, -1},
213 {48,57, -1},
214 {65,90, 97,122, 168,168, 184,184, 175,175, 191,191, 178,179,
215 192,255, -1},
216 {0,TABLE_SIZE-1,0, 97,122,-32, 184,184,-16, 191,191,-16, 179,179,-1,
217 224,255,-32, -1},
218 {0,TABLE_SIZE-1,0, 65,90,32, 168,168,16, 175,175,16, 178,178,1,
219 192,223,32, -1},
220 {{223, 237, 226}, {212, 229, 226}, {204, 224, 240}, {192, 239, 240},
221 {204, 224, 233}, {200, 254, 237}, {200, 254, 235}, {192, 226, 227},
222 {209, 229, 237}, {206, 234, 242}, {205, 238, 255}, {196, 229, 234},
223 {255, 237, 226}, {244, 229, 226}, {236, 224, 240}, {224, 239, 240},
224 {236, 224, 233}, {232, 254, 237}, {232, 254, 235}, {224, 226, 227},
225 {241, 229, 237}, {238, 234, 242}, {237, 238, 255}, {228, 229, 234}}
226 };
227
228
229 /* List of pointers to supported and available locales, NULL terminated. */
230 static const sc_locale_t *const AVAILABLE_LOCALES[] = {
231 &LATIN1_LOCALE,
232 &CYRILLIC_LOCALE,
233 NULL
234 };
235
236 /*
237 * The locale for the game, set below explicitly or on game startup, and
238 * a flag to note if it's been set explicitly, to prevent autodetection from
239 * overwriting a manual setting.
240 */
241 static const sc_locale_t *loc_locale = &LATIN1_LOCALE;
242 static sc_bool loc_is_autodetect_enabled = TRUE;
243
244
245 /*
246 * loc_locate_signature_in_date()
247 *
248 * Checks the format of the input date to ensure it matches the format
249 * "dd [Mm]mm yyyy". Returns the address of the month part of the string, or
250 * NULL if it doesn't match the expected format.
251 */
252 static const sc_char *
253 loc_locate_signature_in_date (const sc_char *date)
254 {
255 sc_int day, year, converted;
256 sc_char signature[SIGNATURE_LENGTH + 1];
257
258 /* Clear signature, and convert using a scanf format. */
259 memset (signature, 0, sizeof (signature));
260 converted = sscanf (date, "%2ld %3[^ 0-9] %4ld", &day, signature, &year);
261
262 /* Valid if we converted three values, and month has three characters. */
263 if (converted == 3 && strlen (signature) == SIGNATURE_LENGTH)
264 return strstr (date, signature);
265 else
266 return NULL;
267 }
268
269
270 /*
271 * loc_compare_locale_signatures()
272 *
273 * Search a locale's signatures for a match with the signature passed in.
274 * Returns TRUE if a match found, FALSE otherwise. Uses memcmp rather than
275 * any strcasecmp() variant because the signatures are in the locale's
276 * codepage, but the locale is not yet (by definition) set.
277 */
278 static sc_bool
279 loc_compare_locale_signatures (const char *signature, const sc_locale_t *locale)
280 {
281 sc_int index_;
282 sc_bool is_matched;
283
284 /* Compare signatures, stopping on the first match found. */
285 is_matched = FALSE;
286 for (index_ = 0; index_ < SIGNATURE_COUNT; index_++)
287 {
288 if (memcmp (locale->signature[index_],
289 signature, sizeof (locale->signature[0])) == 0)
290 {
291 is_matched = TRUE;
292 break;
293 }
294 }
295
296 return is_matched;
297 }
298
299
300 /*
301 * loc_find_matching_locale()
302 *
303 * Look at the incoming date, expected to be in the format "dd [Mm]mm yyyy",
304 * where "[Mm]mm" is a standard month abbreviation of the locale in which the
305 * Generator was run. Match this with locale signatures, and return the
306 * first locale that matches, or NULL if none match.
307 */
308 static const sc_locale_t *
309 loc_find_matching_locale (const sc_char *date,
310 const sc_locale_t *const *locales)
311 {
312 const sc_char *signature;
313 const sc_locale_t *matched = NULL;
314
315 /* Get the month part of date, and if valid, search locale signatures. */
316 signature = loc_locate_signature_in_date (date);
317 if (signature)
318 {
319 const sc_locale_t *const *iterator;
320
321 /* Search for this signature in the locale's signatures. */
322 for (iterator = locales; *iterator; iterator++)
323 {
324 if (loc_compare_locale_signatures (signature, *iterator))
325 {
326 matched = *iterator;
327 break;
328 }
329 }
330 }
331
332 /* Return the matching locale, NULL if none matched. */
333 return matched;
334 }
335
336
337 /*
338 * loc_detect_game_locale()
339 *
340 * Set an autodetected value for the locale based on looking at a game's
341 * compilation date.
342 */
343 void
344 loc_detect_game_locale (sc_prop_setref_t bundle)
345 {
346 assert (bundle);
347
348 /* If an explicit locale has already been set, ignore the call. */
349 if (loc_is_autodetect_enabled)
350 {
351 sc_vartype_t vt_key[1];
352 const sc_char *compile_date;
353 const sc_locale_t *matched;
354
355 /* Read the game's compilation date from the properties. */
356 vt_key[0].string = "CompileDate";
357 compile_date = prop_get_string (bundle, "S<-s", vt_key);
358
359 /* Search for a matching locale based on the game compilation date. */
360 matched = loc_find_matching_locale (compile_date, AVAILABLE_LOCALES);
361
362 /* If a locale matched, set the global locale to it. */
363 if (matched)
364 loc_locale = matched;
365 }
366 }
367
368
369 /*
370 * loc_ascii_tolower()
371 * loc_ascii_strncasecmp()
372 *
373 * The standard sc_strncasecmp() calls sc_tolower(), which is locale specific.
374 * This isn't a particular problem because it will set the Latin1 locale
375 * automatically before continuing. However, since locale names should always
376 * be in ascii anyway, it's slightly safer to just use an ascii-only version
377 * of this function.
378 */
379 static sc_char
380 loc_ascii_tolower (sc_char ch)
381 {
382 return (ch >= 'A' && ch <= 'Z') ? ch - 'A' + 'a' : ch;
383 }
384
385 static sc_int
386 loc_ascii_strncasecmp (const sc_char *s1, const sc_char *s2, sc_int n)
387 {
388 sc_int index_;
389
390 for (index_ = 0; index_ < n; index_++)
391 {
392 sc_int diff;
393
394 diff = loc_ascii_tolower (s1[index_]) - loc_ascii_tolower (s2[index_]);
395 if (diff < 0 || diff > 0)
396 return diff < 0 ? -1 : 1;
397 }
398
399 return 0;
400 }
401
402
403 /*
404 * loc_set_locale()
405 * loc_get_locale()
406 *
407 * Set a locale explicitly from the name passed in, returning TRUE if a locale
408 * matched the name. Get the current locale, which may be the default locale
409 * if none yet set.
410 */
411 sc_bool
412 loc_set_locale (const sc_char *name)
413 {
414 const sc_locale_t *matched = NULL;
415 const sc_locale_t *const *iterator;
416 assert (name);
417
418 /*
419 * Search locales for a matching name, abbreviated if necessary. Stop on
420 * the first match found.
421 */
422 for (iterator = AVAILABLE_LOCALES; *iterator; iterator++)
423 {
424 const sc_locale_t *const locale = *iterator;
425
426 if (loc_ascii_strncasecmp (name, locale->name, strlen (name)) == 0)
427 {
428 matched = locale;
429 break;
430 }
431 }
432
433 /* If matched, set the global locale, and lock out future autodetection. */
434 if (matched)
435 {
436 loc_locale = matched;
437 loc_is_autodetect_enabled = FALSE;
438 }
439
440 return matched ? TRUE : FALSE;
441 }
442
443 const sc_char *
444 loc_get_locale (void)
445 {
446 return loc_locale->name;
447 }
448
449
450 /*
451 * loc_debug_dump_new_line()
452 * loc_debug_dump_bool_table()
453 * loc_debug_dump_char_table()
454 * loc_debug_dump()
455 *
456 * Print out locale tables.
457 */
458 static int
459 loc_debug_dump_new_line (sc_int index_, sc_int count)
460 {
461 return index_ < TABLE_SIZE - 1 && index_ % count == count - 1;
462 }
463
464 static void
465 loc_debug_dump_bool_table (const sc_char *label,
466 sc_int count, const sc_bool table[])
467 {
468 sc_int index_;
469
470 sc_trace ("loc_locale_tables.%s = {\n ", label);
471 for (index_ = 0; index_ < TABLE_SIZE; index_++)
472 {
473 sc_trace ("%s%s", table[index_] ? "T" : "F",
474 loc_debug_dump_new_line (index_, count) ? "\n " : "");
475 }
476 sc_trace ("\n}\n");
477 }
478
479 static void
480 loc_debug_dump_char_table (const sc_char *label,
481 sc_int count, const sc_char table[])
482 {
483 sc_int index_;
484
485 sc_trace ("loc_locale_tables.%s = {\n ", label);
486 for (index_ = 0; index_ < TABLE_SIZE; index_++)
487 {
488 sc_trace ("%02lx%s", (sc_int) (sc_byte) table[index_],
489 loc_debug_dump_new_line (index_, count) ? "\n " : " ");
490 }
491 sc_trace ("\n}\n");
492 }
493
494 void
495 loc_debug_dump (void)
496 {
497 sc_trace ("Locale: debug dump follows...\n");
498
499 loc_check_tables_synchronized (loc_locale);
500 sc_trace ("loc_locale_tables"
501 ".locale->name = %s\n", loc_locale_tables.locale->name);
502
503 loc_debug_dump_bool_table ("isspace", 64, loc_locale_tables.isspace);
504 loc_debug_dump_bool_table ("isdigit", 64, loc_locale_tables.isdigit);
505 loc_debug_dump_bool_table ("isalpha", 64, loc_locale_tables.isalpha);
506 loc_debug_dump_char_table ("toupper", 16, loc_locale_tables.toupper);
507 loc_debug_dump_char_table ("tolower", 16, loc_locale_tables.tolower);
508 }
509
510
511 /*
512 * loc_bool_template()
513 * loc_char_template()
514 *
515 * "Template" functions for locale variant ctype functions. Synchronize
516 * tables to the currently set locale, and return the value from the table.
517 */
518 static sc_bool
519 loc_bool_template (sc_char character, const sc_bool table[])
520 {
521 loc_check_tables_synchronized (loc_locale);
522 return table[(sc_byte) character];
523 }
524
525 static sc_char
526 loc_char_template (sc_char character, const sc_char table[])
527 {
528 loc_check_tables_synchronized (loc_locale);
529 return table[(sc_byte) character];
530 }
531
532
533 /*
534 * sc_isspace()
535 * sc_isalpha()
536 * sc_isdigit()
537 * sc_tolower()
538 * sc_toupper()
539 *
540 * Public entry points into locale variant ctype functions.
541 */
542 sc_bool
543 sc_isspace (sc_char character)
544 {
545 return loc_bool_template (character, loc_locale_tables.isspace);
546 }
547
548 sc_bool
549 sc_isalpha (sc_char character)
550 {
551 return loc_bool_template (character, loc_locale_tables.isalpha);
552 }
553
554 sc_bool
555 sc_isdigit (sc_char character)
556 {
557 return loc_bool_template (character, loc_locale_tables.isdigit);
558 }
559
560 sc_char
561 sc_toupper (sc_char character)
562 {
563 return loc_char_template (character, loc_locale_tables.toupper);
564 }
565
566 sc_char
567 sc_tolower (sc_char character)
568 {
569 return loc_char_template (character, loc_locale_tables.tolower);
570 }