]> icculus.org git repositories - taylor/freespace2.git/blob - src/localization/localize.cpp
const-char warning fixes
[taylor/freespace2.git] / src / localization / localize.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Localization/localize.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  *
16  * $Log$
17  * Revision 1.10  2005/10/03 11:23:14  taylor
18  * don't display error message in FS1 for missing tstrings.tbl, it's ok to be missing in that case
19  *
20  * Revision 1.9  2005/10/01 21:35:51  taylor
21  * various FS1 related changes, removes need to extra fs1.vp file
22  *
23  * Revision 1.8  2005/03/29 02:18:47  taylor
24  * Various 64-bit platform fixes
25  * Fix compiler errors with MAKE_FS1 and fix gr_set_bitmap() too
26  * Make sure that turrets can fire at asteroids for FS1 (needed for a couple missions)
27  * Streaming audio support (big thanks to Pierre Willenbrock!!)
28  * Removed dependance on strings.tbl for FS1 since we don't actually need it now
29  *
30  * Revision 1.7  2004/07/04 11:39:06  taylor
31  * fix missing debrief text, crash on exit, path separator's, warning fixes, no GR_SOFT
32  *
33  * Revision 1.6  2003/06/11 18:30:32  taylor
34  * plug memory leaks
35  *
36  * Revision 1.5  2003/06/03 04:00:40  taylor
37  * Polish language support (Janusz Dziemidowicz)
38  *
39  * Revision 1.4  2003/05/18 03:55:30  taylor
40  * automatic language selection support
41  *
42  * Revision 1.3  2002/06/09 04:41:22  relnev
43  * added copyright header
44  *
45  * Revision 1.2  2002/05/07 03:16:46  theoddone33
46  * The Great Newline Fix
47  *
48  * Revision 1.1.1.1  2002/05/03 03:28:09  root
49  * Initial import.
50  *
51  * 
52  * 91    6/16/00 3:16p Jefff
53  * sim of the year dvd version changes, a few german soty localization
54  * fixes
55  * 
56  * 90    11/02/99 8:20p Jefff
57  * more translation changes/additions
58  * 
59  * 89    11/02/99 3:24p Jefff
60  * added translation functions for a few key instances where english was
61  * showing up
62  * 
63  * 88    10/29/99 10:40p Jefff
64  * mo stirngs
65  * 
66  * 87    10/28/99 2:05a Jefff
67  * safety fix in localization stuff
68  * 
69  * 86    10/26/99 10:53a Jefff
70  * German builds now correctly default to German on first run
71  * 
72  * 85    10/25/99 11:22p Jefff
73  * mo' strings (launcher)
74  * 
75  * 84    10/25/99 5:46p Jefff
76  * Many localization fixes/changes for German builds
77  * 
78  * 83    10/14/99 3:25p Jefff
79  * soo...many....xstrs....
80  * 
81  * 82    10/14/99 2:52p Jefff
82  * localization fixes.  added support for hi-res specific xstr offsets
83  * 
84  * 81    10/13/99 3:58p Jefff
85  * added a bunch of missed XSTRs
86  * 
87  * 80    9/13/99 11:30a Dave
88  * Added checkboxes and functionality for disabling PXO banners as well as
89  * disabling d3d zbuffer biasing.
90  * 
91  * 79    9/08/99 9:59a Jefff
92  * 
93  * 78    9/07/99 4:01p Dave
94  * Fixed up a string.tbl paroblem (self destruct message). Make sure IPX
95  * does everything properly (setting up address when binding). Remove
96  * black rectangle background from UI_INPUTBOX.
97  * 
98  * 77    9/07/99 1:54p Jefff
99  * skip mission strings added
100  * 
101  * 76    9/03/99 10:55a Jefff
102  * xstr_size++
103  * 
104  * 75    9/03/99 1:31a Dave
105  * CD checking by act. Added support to play 2 cutscenes in a row
106  * seamlessly. Fixed super low level cfile bug related to files in the
107  * root directory of a CD. Added cheat code to set campaign mission # in
108  * main hall.
109  * 
110  * 74    8/27/99 12:04a Dave
111  * Campaign loop screen.
112  * 
113  * 73    8/26/99 12:48p Jefff
114  * 
115  * 72    8/22/99 5:53p Dave
116  * Scoring fixes. Added self destruct key. Put callsigns in the logfile
117  * instead of ship designations for multiplayer players.
118  * 
119  * 71    8/22/99 1:55p Dave
120  * Cleaned up host/team-captain leaving code.
121  * 
122  * 70    8/17/99 7:32p Jefff
123  * mo' strings
124  * 
125  * 69    8/16/99 5:15p Dave
126  * Upped string count.
127  * 
128  * 68    8/16/99 4:04p Dave
129  * Big honking checkin.
130  * 
131  * 67    8/11/99 2:09p Jefff
132  * ill give you three guesses.
133  * 
134  * 66    8/10/99 7:57p Jefff
135  * even more strings
136  * 
137  * 65    8/05/99 2:40p Jefff
138  * added string
139  * 
140  * 64    8/02/99 9:13p Dave
141  * Added popup tips.
142  * 
143  * 63    8/02/99 12:01p Jefff
144  * added a string
145  * 
146  * 62    7/29/99 10:47p Dave
147  * Standardized D3D fogging using vertex fog. Shook out Savage 4 bugs.
148  * 
149  * 61    7/29/99 9:44a Jefff
150  * added some strings for ingame objectives screen
151  * 
152  * 60    7/27/99 7:15p Jefff
153  * 3 new interface strings
154  * 
155  * 59    7/27/99 6:53p Dave
156  * Hi-res main hall support.
157  * 
158  * 58    7/24/99 1:54p Dave
159  * Hud text flash gauge. Reworked dead popup to use 4 buttons in red-alert
160  * missions.
161  * 
162  * 57    7/22/99 4:08p Jefff
163  * some new strings
164  * 
165  * 56    7/19/99 7:20p Dave
166  * Beam tooling. Specialized player-killed-self messages. Fixed d3d nebula
167  * pre-rendering.
168  * 
169  * 55    7/19/99 2:13p Dave
170  * Added some new strings for Heiko.
171  * 
172  * 54    7/15/99 2:37p Jefff
173  * 
174  * 53    7/13/99 6:07p Jefff
175  * Added support for localization string offsets.
176  * 
177  * 52    7/09/99 10:32p Dave
178  * Command brief and red alert screens.
179  * 
180  * 51    6/25/99 2:51p Jasons
181  * New string.
182  * 
183  * 50    6/25/99 11:59a Dave
184  * Multi options screen.
185  * 
186  * 49    6/24/99 12:34a Dave
187  * Main options screen.
188  * 
189  * 48    6/19/99 3:58p Dave
190  * More new strings.
191  * 
192  * 47    6/14/99 5:19p Dave
193  * 
194  * 46    6/04/99 11:32a Dave
195  * Added reset text to ship select screen. Fixed minor xstr bug in ui
196  * window
197  * 
198  * 45    6/01/99 6:07p Dave
199  * New loading/pause/please wait bar.
200  * 
201  * 44    6/01/99 4:03p Dave
202  * Changed some popup flag #defines. Upped string count to 1336
203  * 
204  * 43    5/26/99 11:46a Dave
205  * Added ship-blasting lighting and made the randomization of lighting
206  * much more customizable.
207  * 
208  * 42    5/22/99 6:05p Dave
209  * Fixed a few localization # problems.
210  * 
211  * 41    5/21/99 6:45p Dave
212  * Sped up ui loading a bit. Sped up localization disk access stuff. Multi
213  * start game screen, multi password, and multi pxo-help screen.
214  * 
215  * 40    5/17/99 2:52p Dave
216  * Fixed dumb localization problem.
217  * 
218  * 39    5/17/99 10:07a Dave
219  * Updated string count.
220  * 
221  * 38    5/04/99 6:38p Dave
222  * Finished multi join-wait screen.
223  * 
224  * 37    5/04/99 5:20p Dave
225  * Fixed up multiplayer join screen and host options screen. Should both
226  * be at 100% now.
227  * 
228  * 36    5/03/99 11:04p Dave
229  * Most of the way done with the multi join screen.
230  * 
231  * 35    5/03/99 8:32p Dave
232  * New version of multi host options screen.
233  * 
234  * 34    4/09/99 2:28p Dave
235  * Upped Xstring count.
236  * 
237  * 33    4/09/99 2:21p Dave
238  * Multiplayer beta stuff. CD checking.
239  * 
240  * 32    4/08/99 12:50p Neilk
241  * changed number of xstr constant
242  * 
243  * 31    4/07/99 5:54p Dave
244  * Fixes for encryption in updatelauncher.
245  * 
246  * 30    3/24/99 4:05p Dave
247  * Put in support for assigning the player to a specific squadron with a
248  * specific logo. Preliminary work for doing pos/orient checksumming in
249  * multiplayer to reduce bandwidth.
250  * 
251  * 29    3/23/99 3:35p Andsager
252  * Add error check for duplicate string indices
253  * 
254  * 28    3/22/99 2:56p Andsager
255  * Localize Launcher Updater
256  * 
257  * 27    2/23/99 11:18a Andsager
258  * Localize launcher using strings.tbl
259  * 
260  * 26    2/22/99 9:35p Andsager
261  * Add lcl_get_language_name() returns string with current lang.  Added
262  * localization for launcher.
263  * 
264  * 25    2/21/99 6:02p Dave
265  * Fixed standalone WSS packets. 
266  * 
267  * 24    2/17/99 2:11p Dave
268  * First full run of squad war. All freespace and tracker side stuff
269  * works.
270  * 
271  * 23    2/12/99 6:15p Dave
272  * Pre-mission Squad War code is 95% done.
273  * 
274  * 22    2/02/99 7:27p Neilk
275  * implemented lc_close() to free up some string memory when shutting down
276  * 
277  * 21    1/30/99 1:29a Dave
278  * Fixed nebula thumbnail problem. Full support for 1024x768 choose pilot
279  * screen.  Fixed beam weapon death messages.
280  * 
281  * 20    1/29/99 7:56p Neilk
282  * Added new mission log interface
283  * 
284  * 19    1/29/99 4:17p Dave
285  * New interface screens.
286  * 
287  * 18    1/29/99 12:47a Dave
288  * Put in sounds for beam weapon. A bunch of interface screens (tech
289  * database stuff).
290  * 
291  * 17    1/13/99 2:10p Andsager
292  * added string
293  * 
294  * 16    12/14/98 12:13p Dave
295  * Spiffed up xfer system a bit. Put in support for squad logo file xfer.
296  * Need to test now.
297  * 
298  * 15    12/03/98 5:22p Dave
299  * Ported over Freespace 1 multiplayer ships.tbl and weapons.tbl
300  * checksumming.
301  * 
302  * 14    12/02/98 5:47p Dave
303  * Put in interface xstr code. Converted barracks screen to new format.
304  * 
305  * 13    12/01/98 4:46p Dave
306  * Put in targa bitmap support (16 bit).
307  *  
308  * $NoKeywords: $
309  */
310
311
312 #include <ctype.h>
313 #include "cfile.h"
314 #include "localize.h"
315 #include "parselo.h"
316 #include "osregistry.h"
317 #include "encrypt.h"
318 #include "2d.h"
319
320 // ------------------------------------------------------------------------------------------------------------
321 // LOCALIZE DEFINES/VARS
322 //
323
324 // general language/localization data ---------------------
325
326 // current language
327 int Lcl_current_lang = LCL_ENGLISH;
328
329 // language info table
330 typedef struct lang_info {
331         char lang_name[LCL_LANG_NAME_LEN + 1];                          // literal name of the language
332         char lang_ext[LCL_LANG_NAME_LEN + 1];                           // for adding to names on disk access
333 } lang_info;
334
335 lang_info Lcl_languages[LCL_NUM_LANGUAGES] = {
336         { "English",            "" },                                                                   // english
337         { "German",                     "gr" },                                                         // german
338         { "French",                     "fr" },                                                         // french
339         { "Polish",                     "pl" },                                                         // polish
340 };
341
342 //#if defined(GERMAN_BUILD)
343 //#define DEFAULT_LANGUAGE                                                      "German"
344 //#else
345 #define DEFAULT_LANGUAGE                                                        "English"
346 //#endif
347
348 // following is the offset where special characters start in our font
349 #define LCL_SPECIAL_CHARS_FR    164
350 #define LCL_SPECIAL_CHARS_GR    164
351 #define LCL_SPECIAL_CHARS_PL    127
352 #define LCL_SPECIAL_CHARS               127
353 int Lcl_special_chars;
354
355 // use these to replace *_BUILD, values before
356 // only 1 will be active at a time
357 int Lcl_fr = 0;
358 int Lcl_gr = 0 ;
359 int Lcl_pl = 0;
360 int Lcl_english = 1;
361
362
363 // executable string localization data --------------------
364
365 // XSTR_SIZE is the total count of unique XSTR index numbers.  An index is used to
366 // reference an entry in strings.tbl.  This is used for translation of strings from
367 // the english version (in the code) to a foreign version (in the table).  Thus, if you
368 // add a new string to the code, you must assign it a new index.  Use the number below for
369 // that index and increase the number below by one.
370 #define XSTR_SIZE       1570
371
372
373 // struct to allow for strings.tbl-determined x offset
374 // offset is 0 for english, by default
375 typedef struct {
376         char *str;
377         int  offset_x;                          // string offset in 640
378         int  offset_x_hi;                       // string offset in 1024
379 } lcl_xstr;
380
381 //char *Xstr_table[XSTR_SIZE];
382 lcl_xstr Xstr_table[XSTR_SIZE];
383 int Xstr_inited = 0;
384
385
386 // table/mission externalization stuff --------------------
387
388 #define TABLE_STRING_FILENAME                                           "tstrings.tbl"
389 // filename of the file to use when localizing table strings
390 char *Lcl_ext_filename = NULL;
391 CFILE *Lcl_ext_file = NULL;
392
393 // for scanning/parsing tstrings.tbl (from ExStr)
394 #define PARSE_TEXT_STRING_LEN                   2048
395 #define PARSE_ID_STRING_LEN                     128
396 #define TS_SCANNING                                             0                               // scanning for a line of text
397 #define TS_ID_STRING                                            1                               // reading in an id string
398 #define TS_OPEN_QUOTE                                   2                               // looking for an open quote
399 #define TS_STRING                                                       3                               // reading in the text string itself
400 int Ts_current_state = 0;
401 char Ts_text[PARSE_TEXT_STRING_LEN];                            // string we're currently working with
402 char Ts_id_text[PARSE_ID_STRING_LEN];                           // id string we're currently working with
403 int Ts_text_size;
404 int Ts_id_text_size;
405
406 // file pointers for optimized string lookups
407 // some example times for Freespace2 startup with granularities (mostly .tbl files, ~500 strings in the table file, many looked up more than once)
408 // granularity 20                       :               13 secs
409 // granularity 10                       :               11 secs
410 // granularity 5                        :               9 secs
411 // granularity 2                        :               7-8 secs
412 #define LCL_GRANULARITY                                 1                               // how many strings between each pointer (lower granularities should give faster lookup times)
413 #define LCL_MAX_POINTERS                                4500                    // max # of pointers
414 #define LCL_MAX_STRINGS                                 (LCL_GRANULARITY * LCL_MAX_POINTERS)
415 int Lcl_pointers[LCL_MAX_POINTERS];
416 int Lcl_pointer_count = 0;
417
418 #ifdef MAKE_FS1
419 // strings.tbl for FS1, hard coded
420 extern char *FS1_trans[LCL_NUM_LANGUAGES_FS1][LCL_NUM_STRINGS_FS1];
421 #endif
422
423 // ------------------------------------------------------------------------------------------------------------
424 // LOCALIZE FORWARD DECLARATIONS
425 //
426
427 // associate table file externalization with the specified input file
428 void lcl_ext_associate(const char *filename);
429
430 // given a valid XSTR() tag piece of text, extract the string portion, return it in out, nonzero on success
431 int lcl_ext_get_text(char *xstr, char *out);
432
433 // given a valid XSTR() tag piece of text, extract the id# portion, return the value in out, nonzero on success
434 int lcl_ext_get_id(char *xstr, int *out);
435
436 // given a valid XSTR() id#, lookup the string in tstrings.tbl, filling in out if found, nonzero on success
437 int lcl_ext_lookup(char *out, int id);
438
439 // if the char is a valid char for a signed integer value string
440 int lcl_is_valid_numeric_char(char c);
441
442 // sub-parse function for individual lines of tstrings.tbl (from Exstr)
443 // returns : integer with the low bits having the following values :
444 // 0 on fail, 1 on success, 2 if found a matching id/string pair, 3 if end of language has been found
445 // for cases 1 and 2 : the high bit (1<<31) will be set if the parser detected the beginning of a new string id on this line
446 // so be sure to mask this value out to get the low portion of the return value
447 //
448 int lcl_ext_lookup_sub(char *text, char *out, int id);
449
450 // initialize the pointer array into tstrings.tbl (call from lcl_ext_open() ONLY)
451 void lcl_ext_setup_pointers();
452
453
454 // ------------------------------------------------------------------------------------------------------------
455 // LOCALIZE FUNCTIONS
456 //
457
458 // initialize localization, if no language is passed - use the language specified in the registry
459 void lcl_init(int lang_init)
460 {
461         char lang_string[128];
462         const char *ret;
463         int lang, idx;
464
465         // initialize encryption
466         encrypt_init();
467
468         // read the language from the registry
469         if(lang_init < 0){
470                 memset(lang_string, 0, 128);
471
472                 // default to DEFAULT_LANGUAGE (which should be english so we dont have to put German text 
473                 // in tstrings in the #default section
474                 ret = os_config_read_string(NULL, "Language", DEFAULT_LANGUAGE);
475
476                 if(ret == NULL){
477                         Int3();
478                         strcpy(lang_string, DEFAULT_LANGUAGE);
479                 } else {
480                         strcpy(lang_string, ret);
481                 }
482
483                 // look it up
484                 lang = -1;
485                 for(idx=0; idx<LCL_NUM_LANGUAGES; idx++){
486                         if(!stricmp(Lcl_languages[idx].lang_name, lang_string)){
487                                 lang = idx;
488                                 break;
489                         }
490                 }
491                 if(lang < 0){
492                         lang = 0;
493                 }       
494         } else {
495                 Assert((lang_init >= 0) && (lang_init < LCL_NUM_LANGUAGES));
496                 lang = lang_init;
497         }
498
499 #ifdef MAKE_FS1
500         if (lang >= LCL_NUM_LANGUAGES_FS1)
501                 lang = 0;
502 #endif
503
504         // language markers
505         Lcl_pointer_count = 0;
506
507         // associate the table string file
508         lcl_ext_associate(TABLE_STRING_FILENAME);               
509
510         // set the language (this function takes care of setting up file pointers)
511         lcl_set_language(lang);         
512 }
513
514 // added 2.2.99 by NeilK to take care of fs2 launcher memory leaks
515 // shutdown localization
516 void lcl_close()
517 {
518         // if the filename exists, free it up
519         if(Lcl_ext_filename != NULL){
520                 free(Lcl_ext_filename);
521         }
522
523         // free the Xstr_table
524         lcl_xstr_close();
525 };
526
527 // determine what language we're running in, see LCL_* defines above
528 int lcl_get_language()
529 {
530         return Lcl_current_lang;
531 }
532
533 // initialize the xstr table
534 void lcl_xstr_init()
535 {
536 #ifndef MAKE_FS1
537         int i;
538         char chr, buf[4096];
539         char language_tag[512]; 
540         int z, index, rval;
541         char *p_offset = NULL;
542         int offset_lo = 0, offset_hi = 0;
543
544         for (i=0; i<XSTR_SIZE; i++){
545                 Xstr_table[i].str = NULL;
546         }
547
548         if ((rval = setjmp(parse_abort)) != 0) {
549                 mprintf(("Error parsing 'strings.tbl'\nError code = %i.\n", rval));
550         } else {
551                 // make sure localization is NOT running
552                 lcl_ext_close();
553
554                 read_file_text("strings.tbl");
555                 reset_parse();
556
557                 // move down to the proper section              
558                 memset(language_tag, 0, 512);
559                 strcpy(language_tag, "#");
560                 strcat(language_tag, Lcl_languages[Lcl_current_lang].lang_name);
561                 if(skip_to_string(language_tag) != 1){
562                         Error(LOCATION, NOX("Strings.tbl is corrupt"));
563                 }               
564
565                 // parse all the strings in this section of the table
566                 while (!check_for_string("#")) {
567                         int num_offsets_on_this_line = 0;
568
569                         stuff_int(&index);
570                         stuff_string(buf, F_NAME, NULL, 4096);
571
572                         if(Lcl_pl) {
573                             lcl_fix_polish(buf);
574                         }
575
576                         i = strlen(buf);
577                         while (i--) {
578                                 if (!isspace(buf[i])) {
579                                         break;
580                                 }
581                         }
582
583                         // trim unneccesary end of string
584                         if (i >= 0) {
585                                 // Assert(buf[i] == '"');
586                                 if (buf[i] != '"') {
587                                         // probably an offset on this entry
588                                         buf[i+1] = 0;                                           // drop down a null terminator (prolly unnecessary)
589                                         while(!is_white_space(buf[i])) i--;     // back up over the potential offset
590                                         while(is_white_space(buf[i])) i--;      // now back up over intervening spaces
591                                         num_offsets_on_this_line = 1;
592                                         if (buf[i] != '"') {
593                                                 // could have a 2nd offset value (one for 640, one for 1024)
594                                                 // so back up again
595                                                 while(!is_white_space(buf[i])) i--;     // back up over the 2nd offset
596                                                 while(is_white_space(buf[i])) i--;      // now back up over intervening spaces
597                                                 num_offsets_on_this_line = 2;
598                                         }
599
600                                         p_offset = &buf[i+1];                   // get ptr to string section with offset in it
601                                         if (buf[i] != '"') {
602                                                 Error(LOCATION, NOX("Strings.tbl is corrupt"));         // now its an error
603                                         }
604                                 }
605
606                                 buf[i] = 0;
607                         }
608
609                         // copy string into buf
610                         z = 0;
611                         for (i=1; buf[i]; i++) {
612                                 chr = buf[i];
613                                 if (chr == '\\') {
614                                         chr = buf[++i];
615                                         if (chr == 'n') {
616                                                 chr = '\n';
617                                         } else if (chr == 'r') {
618                                                 chr = '\r';
619                                         }
620
621                                 }
622
623                                 buf[z++] = chr;
624                         }
625
626                         // null terminator on buf
627                         buf[z] = 0;
628
629                         // write into Xstr_table
630                         if (index >= 0 && index < XSTR_SIZE) {
631                                 if (Xstr_table[index].str != NULL) {
632                                         Warning(LOCATION, "Strings table index %d used more than once", index);
633                                 }
634                                 Xstr_table[index].str = strdup(buf);
635                         }
636
637                         // read offset information, assume 0 if nonexistant
638                         if (p_offset != NULL) {
639                                 if (sscanf(p_offset, "%d%d", &offset_lo, &offset_hi) < num_offsets_on_this_line) {
640                                         // whatever is in the file ain't a proper offset
641                                         Error(LOCATION, NOX("Strings.tbl is corrupt"));
642                                 }
643                         }
644                         Xstr_table[index].offset_x = offset_lo;
645                         if (num_offsets_on_this_line == 2) {
646                                 Xstr_table[index].offset_x_hi = offset_hi;
647                         } else {
648                                 Xstr_table[index].offset_x_hi = offset_lo;                                              
649                         }
650
651                         // clear out our vars
652                         p_offset = NULL;
653                         offset_lo = 0;
654                         offset_hi = 0;
655                         num_offsets_on_this_line = 0;
656                 }
657         }
658 #else
659         int i;
660
661         Assert(XSTR_SIZE == LCL_NUM_STRINGS_FS1);
662         Assert(Lcl_current_lang < LCL_NUM_LANGUAGES_FS1);
663         
664         for (i=0; i<XSTR_SIZE; i++) {
665                 if ( !strlen(FS1_trans[Lcl_current_lang][i]) ) {
666                         Xstr_table[i].str = NULL;
667                 } else {
668                         Xstr_table[i].str = FS1_trans[Lcl_current_lang][i];
669                 }
670
671                 Xstr_table[i].offset_x = 0;
672                 Xstr_table[i].offset_x_hi = 0;
673         }
674 #endif
675
676         Xstr_inited = 1;
677 }
678
679
680 // free Xstr table
681 void lcl_xstr_close()
682 {
683 #ifndef MAKE_FS1
684         for (int i=0; i<XSTR_SIZE; i++){
685                 if (Xstr_table[i].str != NULL) {
686                         free(Xstr_table[i].str);
687                         Xstr_table[i].str = NULL;
688                 }
689         }
690 #endif
691 }
692
693
694 // set our current language
695 void lcl_set_language(int lang)
696 {
697         Lcl_current_lang = lang;
698
699         nprintf(("General", "Setting language to %s\n", Lcl_languages[lang].lang_name));
700
701         // flag the proper language as being active
702         Lcl_fr = 0;
703         Lcl_gr = 0;
704         Lcl_pl = 0;
705         Lcl_english = 0;
706         switch(lang){
707         case LCL_ENGLISH:
708                 Lcl_english = 1;                
709                 Lcl_special_chars = LCL_SPECIAL_CHARS;
710                 break;
711         case LCL_FRENCH:
712                 Lcl_fr = 1;
713                 Lcl_special_chars = LCL_SPECIAL_CHARS_FR;
714                 break;
715         case LCL_GERMAN:
716                 Lcl_gr = 1;
717                 Lcl_special_chars = LCL_SPECIAL_CHARS_GR;
718                 break;
719         case LCL_POLISH:
720                 Lcl_pl = 1;
721                 Lcl_special_chars = LCL_SPECIAL_CHARS_PL;
722                 break;
723         }
724
725         // set to 0, so lcl_ext_open() knows to reset file pointers
726         Lcl_pointer_count = 0;
727
728         // reset file pointers to the proper language-section
729         if(Lcl_current_lang != LCL_DEFAULT_LANGUAGE){
730                 lcl_ext_setup_pointers();
731         }
732 }
733
734 // maybe add on an appropriate subdirectory when opening a localized file
735 void lcl_add_dir(char *current_path)
736 {
737         char last_char;
738         int path_len;
739
740         // if the disk extension is 0 length, don't add enything
741         if (strlen(Lcl_languages[Lcl_current_lang].lang_ext) <= 0) {
742                 return;
743         }
744
745         // get the length of the string so far
746         path_len = strlen(current_path);
747         if (path_len <= 0) {
748                 return;
749         }
750         
751         // get the current last char
752         last_char = current_path[path_len - 1];
753
754         // if the last char is a slash, just copy in the disk extension
755         if (last_char == DIR_SEPARATOR_CHAR) {
756                 strcat(current_path, Lcl_languages[Lcl_current_lang].lang_ext);
757                 strcat(current_path, DIR_SEPARATOR_STR);
758         } 
759         // otherwise add a slash, then copy in the disk extension
760         else {
761                 strcat(current_path, DIR_SEPARATOR_STR);
762                 strcat(current_path, Lcl_languages[Lcl_current_lang].lang_ext);
763         }
764 }
765
766 // maybe add localized directory to full path with file name when opening a localized file
767 void lcl_add_dir_to_path_with_filename(char *current_path)
768 {
769         char temp[MAX_PATH_LEN];
770
771         // if the disk extension is 0 length, don't add enything
772         if (strlen(Lcl_languages[Lcl_current_lang].lang_ext) <= 0) {
773                 return;
774         }
775
776         // find position of last slash and copy rest of filename (not counting slash) to temp
777         // mark end of current path with '\0', so strcat will work
778         char *last_slash = strrchr(current_path, DIR_SEPARATOR_CHAR);
779         if (last_slash == NULL) {
780                 strcpy(temp, current_path);
781                 current_path[0] = '\0';
782         } else {
783                 strcpy(temp, last_slash+1);
784                 last_slash[1] = '\0';
785         }
786
787         // add extension
788         strcat(current_path, Lcl_languages[Lcl_current_lang].lang_ext);
789         strcat(current_path, DIR_SEPARATOR_STR);
790
791         // copy rest of filename from temp
792         strcat(current_path, temp);
793 }
794
795
796 // externalization of table/mission files ----------------------- 
797
798 // open the externalization file for use during parsing (call before parsing a given file)
799 void lcl_ext_open()
800 {
801         // if the file is already open, do nothing
802         Assert(Lcl_ext_file == NULL);   
803
804         // if we're running in the default language, do nothing
805         if(Lcl_current_lang == LCL_DEFAULT_LANGUAGE){
806                 return;
807         }
808
809 #ifndef MAKE_FS1
810         // otherwise open the file
811         Lcl_ext_file = cfopen(Lcl_ext_filename, "rt");
812         if(Lcl_ext_file == NULL){
813                 return;
814         }
815 #endif
816 }
817
818 // close the externalization file (call after parsing a given file)
819 void lcl_ext_close()
820 {
821         // if the file is not open, do nothing
822         if(Lcl_ext_file == NULL){
823                 return;
824         }
825
826         // if we're running in the default language, do nothing
827         if(Lcl_current_lang == LCL_DEFAULT_LANGUAGE){
828                 return;
829         }
830                 
831         // otherwise close it
832         cfclose(Lcl_ext_file);
833         Lcl_ext_file = NULL;
834 }
835
836 // get the localized version of the string. if none exists, return the original string
837 // valid input to this function includes :
838 // "this is some text"
839 // XSTR("wheeee", -1)
840 // XSTR("whee", 20)
841 // and these should cover all the externalized string cases
842 // fills in id if non-NULL. a value of -2 indicates it is not an external string
843 void lcl_ext_localize(char *in, char *out, int max_len, int *id)
844 {                       
845         char first_four[5];
846         char text_str[2048]="";
847         char lookup_str[2048]="";
848         int str_id;     
849         int str_len;    
850
851         Assert(in);
852         Assert(out);
853
854         // default (non-external string) value
855         if(id != NULL){
856                 *id = -2;
857         }       
858
859         str_len = strlen(in);
860
861         // if the string is < 9 chars, it can't be an XSTR("",) tag, so just copy it
862         if(str_len < 9){
863                 if(str_len > max_len){
864                         error_display(0, "Token too long: [%s].  Length = %i.  Max is %i.\n", in, str_len, max_len);
865                         return;
866                 }               
867                 strcpy(out, in);
868                 if(id != NULL){
869                         *id = -2;
870                 }
871                 return;
872         }
873
874         // otherwise, check to see if it's an XSTR() tag
875         memset(first_four, 0, 5);
876         strncpy(first_four, in, 4);
877         if(stricmp(first_four, "XSTR")){
878                 // NOT an XSTR() tag
879                 if(str_len > max_len){
880                         error_display(0, "Token too long: [%s].  Length = %i.  Max is %i.\n", in, str_len, max_len);
881                         return;
882                 }               
883                 strcpy(out, in);
884                 if(id != NULL){
885                         *id = -2;
886                 }
887                 return;
888         }
889
890         // at this point we _know_ its an XSTR() tag, so split off the strings and id sections          
891         if(!lcl_ext_get_text(in, text_str)){
892                 Int3();
893                 strcpy(out, in);
894                 if(id != NULL){
895                         *id = -1;
896                 }
897                 return;
898         }
899         if(!lcl_ext_get_id(in, &str_id)){
900                 strcpy(out, in);
901                 if(id != NULL){
902                         *id = -1;
903                 }
904                 return;
905         }
906         
907         // if the localization file is not open, or we're running in the default language, return the original string
908         if((Lcl_ext_file == NULL) || (str_id < 0) || (Lcl_current_lang == LCL_DEFAULT_LANGUAGE)){
909                 strcpy(out, text_str);
910                 if(id != NULL){
911                         *id = str_id;
912                 }
913                 return;
914         }               
915
916         // attempt to find the string
917         if(lcl_ext_lookup(lookup_str, str_id)){
918                 // copy to the outgoing string
919                 Assert(strlen(lookup_str) <= (unsigned int)(max_len - 1));
920
921                 if (strlen(lookup_str) > (unsigned int)(max_len-1)) {
922                         // be safe and truncate string to fit
923                         strncpy(out, lookup_str, (size_t) (max_len-1));
924                         out[max_len-1] = '\0';          // ensure null terminator, since strncpy(...) doesnt.
925                 } else {
926                         strcpy(out, lookup_str);
927                 }
928
929         }
930         // otherwise use what we have - probably should Int3() or assert here
931         else {
932                 strcpy(out, text_str);
933         }       
934
935         // set the id #
936         if(id != NULL){
937                 *id = str_id;
938         }
939 }
940
941 // translate the specified string based upon the current language
942 const char *XSTR(const char *str, int index)
943 {
944         if(!Xstr_inited){
945                 return str;
946         }
947
948         // perform a lookup
949         if (index >= 0 && index < XSTR_SIZE) {
950                 if (Xstr_table[index].str){
951                         return Xstr_table[index].str;  // return translation of string
952                 }
953         }
954
955         // can't translate, return original english string
956         return str;
957 }
958
959 // retrieve the offset for a localized string
960 int lcl_get_xstr_offset(int index, int res)
961 {
962         if (res == GR_640) {
963                 return Xstr_table[index].offset_x;
964         } else {
965                 return Xstr_table[index].offset_x_hi;
966         }
967 }
968
969
970 // ------------------------------------------------------------------------------------------------------------
971 // LOCALIZE FORWARD DEFINITIONS
972 //
973
974 // associate table file externalization with the specified input file
975 void lcl_ext_associate(const char *filename)
976 {
977 #ifndef MAKE_FS1
978         // if the filename already exists, free it up
979         if(Lcl_ext_filename != NULL){
980                 free(Lcl_ext_filename);
981         }
982
983         // set the new filename
984         Lcl_ext_filename = strdup(filename);
985 #endif
986 }
987
988 // given a valid XSTR() tag piece of text, extract the string portion, return it in out, nonzero on success
989 int lcl_ext_get_text(char *xstr, char *out)
990 {
991         int str_start, str_end;
992         int str_len;
993         char *p, *p2;
994
995         Assert(xstr != NULL);
996         Assert(out != NULL);
997         str_len = strlen(xstr);
998         
999         // this is some crazy wack-ass code.
1000         // look for the open quote
1001         str_start = str_end = 0;
1002         p = strstr(xstr, "\"");
1003         if(p == NULL){
1004                 error_display(0, "Error parsing XSTR() tag %s\n", xstr);                
1005                 return 0;
1006         } else {
1007                 str_start = p - xstr + 1;               
1008         }
1009         // make sure we're not about to walk past the end of the string
1010         if((p - xstr) >= str_len){
1011                 error_display(0, "Error parsing XSTR() tag %s\n", xstr);                
1012                 return 0;
1013         }
1014
1015         // look for the close quote
1016         p2 = strstr(p+1, "\"");
1017         if(p2 == NULL){
1018                 error_display(0, "Error parsing XSTR() tag %s\n", xstr);                
1019                 return 0;
1020         } else {
1021                 str_end = p2 - xstr;
1022         }
1023
1024         // now that we know the boundaries of the actual string in the XSTR() tag. copy it
1025         memcpy(out, xstr + str_start, str_end - str_start);     
1026
1027         if(Lcl_pl) {
1028             lcl_fix_polish(out);
1029         }
1030
1031         // success
1032         return 1;
1033 }
1034
1035 // given a valid XSTR() tag piece of text, extract the id# portion, return the value in out, nonzero on success
1036 int lcl_ext_get_id(char *xstr, int *out)
1037 {
1038         char *p, *pnext;
1039         int str_len;
1040
1041         Assert(xstr != NULL);
1042         Assert(out != NULL);
1043         
1044         str_len = strlen(xstr);
1045
1046         // find the first quote
1047         p = strstr(xstr, "\"");
1048         if(p == NULL){
1049                 error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr);
1050                 return 0;
1051         }
1052         // make sure we're not about to walk off the end of the string
1053         if((p - xstr) >= str_len){
1054                 error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr);
1055                 return 0;
1056         }
1057         p++;
1058
1059         // continue searching until we find the close quote
1060         while(1){
1061                 pnext = strstr(p, "\"");
1062                 if(pnext == NULL){
1063                         error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr);
1064                         return 0;
1065                 }
1066
1067                 // if the previous char is a \, we know its not the "end-of-string" quote
1068                 if(*(pnext - 1) != '\\'){
1069                         p = pnext;
1070                         break;
1071                 }
1072
1073                 // continue
1074                 p = pnext;
1075         }
1076
1077         // search until we find a ,     
1078         pnext = strstr(p, ",");
1079         if(pnext == NULL){
1080                 error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr);
1081                 return 0;
1082         }
1083         // make sure we're not about to walk off the end of the string
1084         if((pnext - xstr) >= str_len){
1085                 error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr);
1086                 return 0;
1087         }
1088         pnext++;
1089         
1090         // now get the id string
1091         p = pnext;
1092         pnext = strtok(p, ")");
1093         if(pnext == NULL){
1094                 error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr);
1095                 return 0;
1096         }
1097
1098         // get the value and we're done
1099         *out = atoi(pnext);
1100
1101         // success
1102         return 1;
1103 }
1104
1105 // given a valid XSTR() id#, lookup the string in tstrings.tbl, filling in out if found, nonzero on success
1106 int lcl_ext_lookup(char *out, int id)
1107 {
1108         char text[1024];
1109         int ret;
1110         int pointer;
1111         
1112         Assert(Lcl_pointer_count >= 0);
1113         Assert(Lcl_pointers[0] >= 0);
1114         Assert(Lcl_pointers[Lcl_pointer_count - 1] >= 0);
1115         Assert(Lcl_ext_file != NULL);
1116         Assert(id >= 0);
1117
1118         // seek to the closest pointer <= the id# we're looking for
1119         pointer = id / LCL_GRANULARITY;
1120         cfseek(Lcl_ext_file, Lcl_pointers[pointer], CF_SEEK_SET);
1121
1122         // reset parsing vars and go to town
1123         Ts_current_state = TS_SCANNING;
1124         Ts_id_text_size = 0;
1125         Ts_text_size = 0;
1126         memset(Ts_text, 0, PARSE_TEXT_STRING_LEN);
1127         memset(Ts_id_text, 0, PARSE_ID_STRING_LEN);
1128         while((cftell(Lcl_ext_file) < Lcl_pointers[Lcl_pointer_count - 1]) && cfgets(text, 1024, Lcl_ext_file)){
1129                 ret = lcl_ext_lookup_sub(text, out, id);
1130                         
1131                 // run the line parse function          
1132                 switch(ret & 0x0fffffff){
1133                 // error
1134                 case 0 :
1135                         Int3();                 // should never get here - it means the string doens't exist in the table!!
1136                         return 0;
1137
1138                 // success parsing the line - please continue
1139                 case 1 :
1140                         break;
1141
1142                 // found a matching string/id pair
1143                 case 2 :                        
1144                         // success
1145                         if (Lcl_gr) {
1146                                 // this is because tstrings.tbl reads in as ANSI for some reason
1147                                 // opening tstrings with "rb" mode didnt seem to help, so its now still "rt" like before
1148                                 lcl_fix_umlauts(out, LCL_TO_ASCII);             
1149                         }       
1150                         return 1;
1151
1152                 // end of language found
1153                 case 3 :
1154                         Int3();                 // should never get here - it means the string doens't exist in the table!!
1155                         return 0;               
1156                 }
1157         }
1158         
1159         Int3();                 // should never get here - it means the string doens't exist in the table!!
1160         return 0;
1161 }
1162
1163 // sub-parse function for individual lines of tstrings.tbl (from Exstr)
1164 // returns : integer with the low bits having the following values :
1165 // 0 on fail, 1 on success, 2 if found a matching id/string pair, 3 if end of language has been found
1166 // for cases 1 and 2 : the high bit (1<<31) will be set if the parser detected the beginning of a new string id on this line
1167 //
1168 int lcl_ext_lookup_sub(char *text, char *out, int id)
1169 {
1170         char *front;                    // front of the line
1171         char *p;                                        // current ptr
1172         int len = strlen(text);
1173         int count;      
1174         char text_copy[1024];   
1175         char *tok;
1176         int found_new_string_id = 0;
1177
1178         front = text;
1179         p = text;
1180         count = 0;                      
1181         while(count < len){
1182                 // do something useful
1183                 switch(Ts_current_state){               
1184                 // scanning for a line of text
1185                 case TS_SCANNING:
1186                         // if the first word is #end, we're done with the file altogether
1187                         strcpy(text_copy, text);
1188                         tok = strtok(text_copy, " \n");
1189                         if((tok != NULL) && !stricmp(tok, "#end")){
1190                                 return 3;
1191                         }
1192                         // if its a commented line, skip it
1193                         else if((text[0] == ';') || (text[0] == ' ') || (text[0] == '\n')){
1194                                 return 1;
1195                         }
1196                         // otherwise we should have an ID #, so stuff it and move to the proper state
1197                         else {
1198                                 if(lcl_is_valid_numeric_char(*p)){
1199                                         memset(Ts_id_text, 0, PARSE_ID_STRING_LEN);
1200                                         Ts_id_text_size = 0;
1201                                         Ts_id_text[Ts_id_text_size++] = *p;
1202                                         Ts_current_state = TS_ID_STRING;
1203
1204                                         found_new_string_id = 1;
1205                                 }
1206                                 // error
1207                                 else {
1208                                         Int3();
1209                                         return 0;
1210                                 }
1211                         }
1212                         break;
1213
1214                 // scanning in an id string
1215                 case TS_ID_STRING:
1216                         // if we have another valid char
1217                         if(lcl_is_valid_numeric_char(*p)){
1218                                 Ts_id_text[Ts_id_text_size++] = *p;
1219                         }
1220                         // if we found a comma, our id# is finished, look for the open quote
1221                         else if(*p == ','){
1222                                 Ts_current_state = TS_OPEN_QUOTE;
1223                         } else {
1224                                 Int3();
1225                                 return 0;
1226                         }
1227                         break;
1228
1229                 case TS_OPEN_QUOTE:
1230                         // valid space or an open quote
1231                         if((*p == ' ') || (*p == '\"')){
1232                                 if(*p == '\"'){
1233                                         memset(Ts_text, 0, PARSE_TEXT_STRING_LEN);
1234                                         Ts_text_size = 0;
1235                                         Ts_current_state = TS_STRING;
1236                                 }
1237                         } else {
1238                                 Int3();
1239                                 return 0;
1240                         }
1241                         break;
1242
1243                 case TS_STRING:
1244                         // if we have an end quote, we need to look for a comma
1245                         if((*p == '\"') /*&& (Ts_text_size > 0)*/ && (Ts_text[Ts_text_size - 1] != '\\')){
1246                                 // we're now done - we have a string
1247                                 Ts_current_state = TS_SCANNING;
1248
1249                                 // if the id#'s match, copy the string and return "string found"
1250                                 if((atoi(Ts_id_text) == id) && (out != NULL)){
1251                                         strcpy(out, Ts_text);
1252
1253                                         return found_new_string_id ? (1<<1) | (1<<31) : (1<<1);                                 
1254                                 }
1255                                 
1256                                 // otherwise, just continue parsing                             
1257                                 return found_new_string_id ? (1<<0) | (1<<31) : (1<<0);
1258                         } 
1259                         // otherwise add to the string
1260                         else {
1261                                 Ts_text[Ts_text_size++] = *p;
1262                         }                                                                               
1263                         break;
1264                 }               
1265
1266                 // if we have a newline, return success, we're done with this line
1267                 if(*p == '\n'){
1268                         return found_new_string_id ? (1<<0) | (1<<31) : (1<<0);
1269                 }
1270
1271                 // next char in the line
1272                 p++;
1273                 count++;
1274         }       
1275
1276         // success
1277         return found_new_string_id ? (1<<0) | (1<<31) : (1<<0);
1278 }
1279
1280 // if the char is a valid char for a signed integer value
1281 int lcl_is_valid_numeric_char(char c)
1282 {
1283         return ( (c == '-') || (c == '0') || (c == '1') || (c == '2') || (c == '3') || (c == '4') ||
1284                                 (c == '5') || (c == '6') || (c == '7') || (c == '8') || (c == '9') ) ? 1 : 0;
1285 }
1286
1287 // initialize the pointer array into tstrings.tbl (call from lcl_ext_open() ONLY)
1288 void lcl_ext_setup_pointers()
1289 {
1290         char language_string[128];
1291         char line[1024];
1292         char *tok;      
1293         int string_count;
1294         int ret;
1295         int found_start = 0;
1296
1297         // open the localization file
1298         lcl_ext_open();
1299         if(Lcl_ext_file == NULL){
1300 #ifndef MAKE_FS1
1301                 error_display(0, "Error opening externalization file! File likely does not exist or could not be found");
1302 #endif
1303                 return;
1304         }
1305
1306         // seek to the currently active language
1307         memset(language_string, 0, 128);
1308         strcpy(language_string, "#");
1309         if(!stricmp(DEFAULT_LANGUAGE, Lcl_languages[Lcl_current_lang].lang_name)){
1310                 strcat(language_string, "default");
1311         } else {
1312                 strcat(language_string, Lcl_languages[Lcl_current_lang].lang_name);
1313         }
1314         memset(line, 0, 1024);
1315
1316         // reset seek variables and begin               
1317         Lcl_pointer_count = 0;
1318         while(cfgets(line, 1024, Lcl_ext_file)){
1319                 tok = strtok(line, " \n");
1320                 if(tok == NULL){
1321                         continue;                       
1322                 }
1323                 
1324                 // if the language matches, we're good to start parsing strings
1325                 if(!stricmp(language_string, tok)){
1326                         found_start = 1;                        
1327                         break;
1328                 }               
1329         }
1330
1331         // if we didn't find the language specified, error
1332         if(found_start <= 0){
1333                 error_display(0, "Could not find specified langauge in tstrings.tbl!\n");
1334                 lcl_ext_close();
1335                 return;
1336         }
1337
1338         string_count = 0;       
1339         while(cfgets(line, 1024, Lcl_ext_file)){
1340                 ret = lcl_ext_lookup_sub(line, NULL, -1);
1341
1342                 // do stuff
1343                 switch(ret & 0x0fffffff){
1344                 // error
1345                 case 0 :
1346                         lcl_ext_close();
1347                         return;         
1348
1349                 // end of language found
1350                 case 3 :
1351                         // mark one final pointer
1352                         Lcl_pointers[Lcl_pointer_count++] = cftell(Lcl_ext_file) - strlen(line) - 1;
1353                         lcl_ext_close();
1354                         return;
1355                 }
1356
1357                 // the only other case we care about is the beginning of a new id#
1358                 if(ret & (1<<31)){              
1359                         if((string_count % LCL_GRANULARITY) == 0){
1360                                 // mark the pointer down
1361                                 Lcl_pointers[Lcl_pointer_count++] = cftell(Lcl_ext_file) - strlen(line) - 1;
1362
1363                                 // if we're out of pointer slots
1364                                 if(Lcl_pointer_count >= LCL_MAX_POINTERS){
1365                                         error_display(0, "Out of pointer for tstrings.tbl lookup. Please increment LCL_MAX_POINTERS in localize.cpp");
1366                                         lcl_ext_close();
1367                                         return;
1368                                 }
1369                         }
1370                         // increment string count
1371                         string_count++;                 
1372                 }
1373         }
1374
1375         // should never get here. we should always be exiting through case 3 (end of language section) of the above switch
1376         // statement
1377         Int3();
1378         lcl_ext_close();
1379 }
1380
1381 void lcl_get_language_name(char *lang_name)
1382 {
1383         Assert(LCL_NUM_LANGUAGES == 3);
1384
1385         strcpy(lang_name, Lcl_languages[Lcl_current_lang].lang_name);
1386 }
1387
1388 // converts german umlauted chars from ASCII to ANSI
1389 // so they appear in the launcher
1390 // how friggin lame is this
1391 // pass in a null terminated string, foo!
1392 // returns ptr to string you sent in
1393 char* lcl_fix_umlauts(char *str, int which_way)
1394 {
1395         int i=0;
1396
1397         if (which_way == LCL_TO_ANSI) {
1398                 // moving to ANSI charset
1399                 // run thru string and perform appropriate conversions
1400                 while (str[i] != '\0') {
1401                         switch (str[i]) {
1402                         case '\x81':
1403                                 // lower umlaut u
1404                                 str[i] = '\xFC';
1405                                 break;
1406                         case '\x84':
1407                                 // lower umlaut a
1408                                 str[i] = '\xE4';
1409                                 break;
1410                         case '\x94':
1411                                 // lower umlaut o
1412                                 str[i] = '\xF6';
1413                                 break;
1414                         case '\x9A':
1415                                 // upper umlaut u
1416                                 str[i] = '\xDC';
1417                                 break;
1418                         case '\x8E':
1419                                 // upper umlaut a
1420                                 str[i] = '\xC4';
1421                                 break;
1422                         case '\x99':
1423                                 // upper umlaut o
1424                                 str[i] = '\xD6';
1425                                 break;
1426                         case '\xE1':
1427                                 // beta-lookin thing that means "ss"
1428                                 str[i] = '\xDF';
1429                                 break;
1430                         }
1431
1432                         i++;
1433                 }
1434         } else {
1435                 // moving to ASCII charset
1436                 // run thru string and perform appropriate conversions
1437                 while (str[i] != '\0') {
1438                         switch (str[i]) {
1439                         case '\xFC':
1440                                 // lower umlaut u
1441                                 str[i] = '\x81';
1442                                 break;
1443                         case '\xE4':
1444                                 // lower umlaut a
1445                                 str[i] = '\x84';
1446                                 break;
1447                         case '\xF6':
1448                                 // lower umlaut o
1449                                 str[i] = '\x94';
1450                                 break;
1451                         case '\xDC':
1452                                 // upper umlaut u
1453                                 str[i] = '\x9A';
1454                                 break;
1455                         case '\xC4':
1456                                 // upper umlaut a
1457                                 str[i] = '\x8E';
1458                                 break;
1459                         case '\xD6':
1460                                 // upper umlaut o
1461                                 str[i] = '\x99';
1462                                 break;
1463                         case '\xDF':
1464                                 // beta-lookin thing that means "ss"
1465                                 str[i] = '\xE1';
1466                                 break;
1467                         }
1468
1469                         i++;
1470                 }
1471         }
1472
1473         return str;
1474 }
1475
1476 // convert some of the polish characters
1477 void lcl_fix_polish(char *str)
1478 {
1479     for(;*str;str++) {
1480         if(*str == '\xA2') *str = '\xF3';
1481         else if(*str == '\x88') *str = '\xEA';
1482     }
1483 }
1484
1485 // ------------------------------------------------------------------
1486 // lcl_translate_wep_name()
1487 //
1488 // For displaying weapon names in german version
1489 // since we cant actually just change them outright.
1490 //
1491 void lcl_translate_wep_name(char *name)
1492 {
1493         if (!strcmp(name, "Morning Star")) {    
1494                 strcpy(name, "Morgenstern");
1495         } else if (!strcmp(name, "MorningStar")) {
1496                 strcpy(name, "Morgenstern D");
1497         } else if (!strcmp(name, "UD-8 Kayser")) {
1498                 strcpy(name, "Kayserstrahl");
1499         } else if (!strcmp(name, "UD-D Kayser")) {
1500                 strcpy(name, "Kayserstrahl");
1501         }
1502 }
1503
1504 // ------------------------------------------------------------------
1505 // lcl_translate_brief_icon_name()
1506 //
1507 // For displaying ship names in german version
1508 // since we cant actually just change them outright.
1509 //
1510 void lcl_translate_brief_icon_name(char *name)
1511 {
1512         char *pos;
1513         char buf[128];
1514
1515         if (!stricmp(name, "Subspace Portal")) {        
1516                 strcpy(name, "Subraum Portal");
1517
1518         } else if (!stricmp(name, "Alpha Wing")) {
1519                 strcpy(name, "Alpha");
1520
1521         } else if (!stricmp(name, "Beta Wing")) {
1522                 strcpy(name, "Beta");
1523
1524         } else if (!stricmp(name, "Zeta Wing")) {
1525                 strcpy(name, "Zeta");
1526
1527         } else if (!stricmp(name, "Capella Node")) {
1528                 strcpy(name, "Capella");
1529
1530         } else if (!stricmp(name, "Hostile")) {
1531                 strcpy(name, "Gegner");
1532
1533         } else if (!stricmp(name, "Hostile Craft")) {
1534                 strcpy(name, "Gegner");
1535
1536         } else if (!stricmp(name, "Rebel Wing")) {
1537                 strcpy(name, "Rebellen");
1538
1539         } else if (!stricmp(name, "Rebel Fleet")) {
1540                 strcpy(name, "Rebellenflotte");
1541
1542         } else if (!stricmp(name, "Sentry Gun")) {
1543                 strcpy(name, "Gesch\x81tz");
1544
1545         } else if (!stricmp(name, "Cargo")) {
1546                 strcpy(name, "Fracht");
1547
1548         } else if (!stricmp(name, "Knossos Device")) {
1549                 strcpy(name, "Knossosger\x84t");
1550         
1551         } else if (!stricmp(name, "Support")) {
1552                 strcpy(name, "Versorger");
1553
1554         } else if (!stricmp(name, "Unknown")) {
1555                 strcpy(name, "Unbekannt");
1556
1557         } else if (!stricmp(name, "Instructor")) {
1558                 strcpy(name, "Ausbilder");
1559         
1560         } else if (!stricmp(name, "Jump Node")) {
1561                 strcpy(name, "Sprungknoten");
1562
1563         } else if (!stricmp(name, "Escort")) {
1564                 strcpy(name, "Geleitschutz");
1565
1566         } else if (!stricmp(name, "Asteroid Field")) {
1567                 strcpy(name, "Asteroidenfeld");
1568
1569         } else if (!stricmp(name, "Enif Station")) {
1570                 strcpy(name, "Station Enif");
1571
1572         } else if (!stricmp(name, "Rally Point")) {
1573                 strcpy(name, "Sammelpunkt");
1574
1575         } else if ((pos = strstr(name, "Transport")) != NULL) {
1576                 pos += 9;               // strlen of "transport"
1577                 strcpy(buf, "Transporter");
1578                 strcat(buf, pos);
1579                 strcpy(name, buf);
1580
1581         } else if ((pos = strstr(name, "Jump Node")) != NULL) {
1582                 pos += 9;               // strlen of "jump node"
1583                 strcpy(buf, "Sprungknoten");
1584                 strcat(buf, pos);
1585                 strcpy(name, buf);
1586         
1587         } else if (!stricmp(name, "Orion under repair")) {
1588                 strcpy(name, "Orion wird repariert");
1589
1590         // SOTY-specific ones below!
1591         
1592         } else if (!stricmp(name, "Wayfarer Station")) {
1593                 strcpy(name, "Station Wayfarer");
1594         } else if (!stricmp(name, "Enemy")) {
1595                 strcpy(name, "Gegner");
1596         } else if (!stricmp(name, "Supply Depot")) {
1597                 strcpy(name, "Nachschubdepot");
1598         } else if (!stricmp(name, "Fighter Escort")) {
1599                 strcpy(name, "Jagdschutz");
1600         } else if (!stricmp(name, "Shivans")) {
1601                 strcpy(name, "Shivaner");
1602         } else if (!stricmp(name, "NTF Base of Operations")) {
1603                 strcpy(name, "NTF-Operationsbasis");
1604         } else if (!stricmp(name, "NTF Bombers")) {
1605                 strcpy(name, "NTF-Bomber");
1606         } else if (!stricmp(name, "NTF Fighters")) {
1607                 strcpy(name, "NTF-J\x84ger");
1608         } else if (!stricmp(name, "Sentry")) {
1609                 strcpy(name, "Sperrgesch\x81tz");
1610         } else if (!stricmp(name, "Cargo Containers")) {
1611                 strcpy(name, "Frachtbeh\x84lter");
1612         } else if (!stricmp(name, "NTF Reinforcements")) {
1613                 strcpy(name, "NTF-Verst\x84rkungen");
1614         } else if (!stricmp(name, "NTF Base")) {
1615                 strcpy(name, "NTF-St\x81tzpunkt");
1616         } else if (!stricmp(name, "Refugee Convoy")) {
1617                 strcpy(name, "Fl\x81""chtlingskonvoi");
1618         } else if (!stricmp(name, "Food Convoy")) {
1619                 strcpy(name, "Nachschubkonvoi");
1620         } else if (!stricmp(name, "Governor's Shuttle")) {
1621                 strcpy(name, "F\x84hre des Gouverneurs");
1622         } else if (!stricmp(name, "GTVA Patrol")) {
1623                 strcpy(name, "GTVA-Patrouille");
1624         } else if (!stricmp(name, "Escort fighters")) {
1625                 strcpy(name, "Geleitschutz");
1626         } else if (!stricmp(name, "Nagada Outpost")) {
1627                 strcpy(name, "Nagada-Aussenposten");
1628         } else if (!stricmp(name, "Fighters")) {
1629                 strcpy(name, "J\x84ger");
1630         } else if (!stricmp(name, "Bombers")) {
1631                 strcpy(name, "Bomber");
1632         } else if (!stricmp(name, "Enemy Destroyers")) {
1633                 strcpy(name, "Feindliche Zerst\x94rer");
1634         } else if (!stricmp(name, "Ross 128 System")) {
1635                 strcpy(name, "System Ross 128");
1636         } else if (!stricmp(name, "Knossos Station")) {
1637                 strcpy(name, "Knossos-Station");
1638         } else if (!stricmp(name, "Transporters")) {
1639                 strcpy(name, "Transporter");
1640         } else if (!stricmp(name, "Pirates?")) {
1641                 strcpy(name, "Piraten?");
1642         } else if (!stricmp(name, "Escorts")) {
1643                 strcpy(name, "Geleitschutz");
1644         } else if (!stricmp(name, "Shivan Fighters")) {
1645                 strcpy(name, "J\x84ger");
1646         } else if (!stricmp(name, "Shivan Territory")) {
1647                 strcpy(name, "Shivaner");
1648         }
1649 }
1650
1651 // ------------------------------------------------------------------
1652 // lcl_translate_brief_icon_name_pl()
1653 //
1654 // For displaying ship names in polish version
1655 // since we cant actually just change them outright.
1656 //
1657 void lcl_translate_brief_icon_name_pl(char *name)
1658 {
1659         char *pos;
1660         char buf[128];
1661
1662         if (!stricmp(name, "Subspace Portal")) {        
1663                 strcpy(name, "Portal podprz.");
1664
1665         } else if (!stricmp(name, "Alpha Wing")) {
1666                 strcpy(name, "Alfa");
1667
1668         } else if (!stricmp(name, "Beta Wing")) {
1669                 strcpy(name, "Beta");
1670
1671         } else if (!stricmp(name, "Zeta Wing")) {
1672                 strcpy(name, "Zeta");
1673
1674         } else if (!stricmp(name, "Capella Node")) {
1675                 strcpy(name, "Capella");
1676
1677         } else if (!stricmp(name, "Hostile")) {
1678                 strcpy(name, "Wr\xF3g");
1679
1680         } else if (!stricmp(name, "Hostile Craft")) {
1681                 strcpy(name, "Wr\xF3g");
1682
1683         } else if (!stricmp(name, "Rebel Wing")) {
1684                 strcpy(name, "Rebelianci");
1685
1686         } else if (!stricmp(name, "Rebel Fleet")) {
1687                 strcpy(name, "Flota Rebelii");
1688
1689         } else if (!stricmp(name, "Sentry Gun")) {
1690                 strcpy(name, "Dzia\xB3o str.");
1691
1692         } else if (!stricmp(name, "Cargo")) {
1693                 strcpy(name, "\xA3\x61\x64unek");
1694
1695         } else if (!stricmp(name, "Knossos Device")) {
1696                 strcpy(name, "Urz. Knossos");
1697         
1698         } else if (!stricmp(name, "Support")) {
1699                 strcpy(name, "Wsparcie");
1700
1701         } else if (!stricmp(name, "Unknown")) {
1702                 strcpy(name, "Nieznany");
1703
1704         } else if (!stricmp(name, "Instructor")) {
1705                 strcpy(name, "Instruktor");
1706         
1707         } else if (!stricmp(name, "Jump Node")) {
1708                 strcpy(name, "W\xEAze\xB3 skokowy");
1709
1710         } else if (!stricmp(name, "Escort")) {
1711                 strcpy(name, "Eskorta");
1712
1713         } else if (!stricmp(name, "Asteroid Field")) {
1714                 strcpy(name, "Pole asteroid");
1715
1716         } else if (!stricmp(name, "Enif Station")) {
1717                 strcpy(name, "Stacja Enif");
1718
1719         } else if (!stricmp(name, "Rally Point")) {
1720                 strcpy(name, "Pkt zborny");
1721
1722         } else if ((pos = strstr(name, "Transport")) != NULL) {
1723                 pos += 9;               // strlen of "transport"
1724                 strcpy(buf, "Transporter");
1725                 strcat(buf, pos);
1726                 strcpy(name, buf);
1727
1728         } else if ((pos = strstr(name, "Jump Node")) != NULL) {
1729                 pos += 9;               // strlen of "jump node"
1730                 strcpy(buf, "W\xEAze\xB3 skokowy");
1731                 strcat(buf, pos);
1732                 strcpy(name, buf);
1733         
1734         } else if (!stricmp(name, "Orion under repair")) {
1735                 strcpy(name, "Naprawiany Orion");
1736         }
1737 }
1738
1739 // ------------------------------------------------------------------
1740 // lcl_translate_ship_name()
1741 //
1742 // For displaying ship names in german version in the briefing
1743 // since we cant actually just change them outright.
1744 //
1745 void lcl_translate_ship_name(char *name)
1746 {
1747         if (!strcmp(name, "GTDR Amazon Advanced")) {    
1748                 strcpy(name, "GTDR Amazon VII");
1749         } 
1750 }
1751
1752 // ------------------------------------------------------------------
1753 // lcl_translate_targetbox_name()
1754 //
1755 // For displaying ship names in german version in the targetbox
1756 // since we cant actually just change them outright.
1757 //
1758 void lcl_translate_targetbox_name(char *name)
1759 {
1760         char *pos;
1761         char buf[128];
1762         
1763         if ((pos = strstr(name, "Sentry")) != NULL) {
1764                 pos += 6;               // strlen of "sentry"
1765                 strcpy(buf, "Sperrgesch\x81tz");
1766                 strcat(buf, pos);
1767                 strcpy(name, buf);
1768
1769         } else if ((pos = strstr(name, "Support")) != NULL) {
1770                 pos += 7;               // strlen of "support"
1771                 strcpy(buf, "Versorger");
1772                 strcat(buf, pos);
1773                 strcpy(name, buf);
1774
1775         } else if ((pos = strstr(name, "Unknown")) != NULL) {
1776                 pos += 7;               // strlen of "unknown"
1777                 strcpy(buf, "Unbekannt");
1778                 strcat(buf, pos);
1779                 strcpy(name, buf);
1780
1781         } else if ((pos = strstr(name, "Drone")) != NULL) {
1782                 pos += 5;               // strlen of "drone"
1783                 strcpy(buf, "Drohne");
1784                 strcat(buf, pos);
1785                 strcpy(name, buf);
1786
1787         } else if ((pos = strstr(name, "Jump Node")) != NULL) {
1788                 pos += 9;               // strlen of "jump node"
1789                 strcpy(buf, "Sprungknoten");
1790                 strcat(buf, pos);
1791                 strcpy(name, buf);
1792
1793         } else if (!stricmp(name, "Instructor")) {
1794                 strcpy(name, "Ausbilder");
1795
1796         } else if (!stricmp(name, "NTF Vessel")) {
1797                 strcpy(name, "NTF-Schiff");
1798
1799         } else if (!stricmp(name, "Enif Station")) {
1800                 strcpy(name, "Station Enif");
1801         }
1802 }
1803
1804 // ------------------------------------------------------------------
1805 // lcl_translate_targetbox_name_pl()
1806 //
1807 // For displaying ship names in polish version in the targetbox
1808 // since we cant actually just change them outright.
1809 //
1810 void lcl_translate_targetbox_name_pl(char *name)
1811 {
1812         char *pos;
1813         char buf[128];
1814         
1815         if ((pos = strstr(name, "Sentry")) != NULL) {
1816                 pos += 6;               // strlen of "sentry"
1817                 strcpy(buf, "Stra\xBFnik");
1818                 strcat(buf, pos);
1819                 strcpy(name, buf);
1820
1821         } else if ((pos = strstr(name, "Support")) != NULL) {
1822                 pos += 7;               // strlen of "support"
1823                 strcpy(buf, "Wsparcie");
1824                 strcat(buf, pos);
1825                 strcpy(name, buf);
1826
1827         } else if ((pos = strstr(name, "Unknown")) != NULL) {
1828                 pos += 7;               // strlen of "unknown"
1829                 strcpy(buf, "Nieznany");
1830                 strcat(buf, pos);
1831                 strcpy(name, buf);
1832
1833         } else if ((pos = strstr(name, "Drone")) != NULL) {
1834                 pos += 5;               // strlen of "drone"
1835                 strcpy(buf, "Sonda");
1836                 strcat(buf, pos);
1837                 strcpy(name, buf);
1838
1839         } else if ((pos = strstr(name, "Jump Node")) != NULL) {
1840                 pos += 9;               // strlen of "jump node"
1841                 strcpy(buf, "W\xEAze\xB3 skokowy");
1842                 strcat(buf, pos);
1843                 strcpy(name, buf);
1844
1845         } else if (!stricmp(name, "Instructor")) {
1846                 strcpy(name, "Instruktor");
1847
1848         } else if (!stricmp(name, "NTF Vessel")) {
1849                 strcpy(name, "Okr\xEAt NTF");
1850
1851         } else if (!stricmp(name, "Enif Station")) {
1852                 strcpy(name, "Stacja Enif");
1853         }
1854 }