]> icculus.org git repositories - taylor/freespace2.git/blob - src/localization/localize.cpp
warnings (clang): unused-but-set-variable
[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 const 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(!SDL_strcasecmp(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                 SDL_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                                 // SDL_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         SDL_assert(XSTR_SIZE == LCL_NUM_STRINGS_FS1);
662         SDL_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 = (char *)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         SDL_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         SDL_assert(in);
852         SDL_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(SDL_strcasecmp(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                 SDL_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         SDL_assert(xstr != NULL);
996         SDL_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         SDL_assert(xstr != NULL);
1042         SDL_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         SDL_assert(Lcl_pointer_count >= 0);
1113         SDL_assert(Lcl_pointers[0] >= 0);
1114         SDL_assert(Lcl_pointers[Lcl_pointer_count - 1] >= 0);
1115         SDL_assert(Lcl_ext_file != NULL);
1116         SDL_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 *p;                                        // current ptr
1171         int len = strlen(text);
1172         int count;      
1173         char text_copy[1024];   
1174         char *tok;
1175         int found_new_string_id = 0;
1176
1177         p = text;
1178         count = 0;                      
1179         while(count < len){
1180                 // do something useful
1181                 switch(Ts_current_state){               
1182                 // scanning for a line of text
1183                 case TS_SCANNING:
1184                         // if the first word is #end, we're done with the file altogether
1185                         strcpy(text_copy, text);
1186                         tok = strtok(text_copy, " \n");
1187                         if((tok != NULL) && !SDL_strcasecmp(tok, "#end")){
1188                                 return 3;
1189                         }
1190                         // if its a commented line, skip it
1191                         else if((text[0] == ';') || (text[0] == ' ') || (text[0] == '\n')){
1192                                 return 1;
1193                         }
1194                         // otherwise we should have an ID #, so stuff it and move to the proper state
1195                         else {
1196                                 if(lcl_is_valid_numeric_char(*p)){
1197                                         memset(Ts_id_text, 0, PARSE_ID_STRING_LEN);
1198                                         Ts_id_text_size = 0;
1199                                         Ts_id_text[Ts_id_text_size++] = *p;
1200                                         Ts_current_state = TS_ID_STRING;
1201
1202                                         found_new_string_id = 1;
1203                                 }
1204                                 // error
1205                                 else {
1206                                         Int3();
1207                                         return 0;
1208                                 }
1209                         }
1210                         break;
1211
1212                 // scanning in an id string
1213                 case TS_ID_STRING:
1214                         // if we have another valid char
1215                         if(lcl_is_valid_numeric_char(*p)){
1216                                 Ts_id_text[Ts_id_text_size++] = *p;
1217                         }
1218                         // if we found a comma, our id# is finished, look for the open quote
1219                         else if(*p == ','){
1220                                 Ts_current_state = TS_OPEN_QUOTE;
1221                         } else {
1222                                 Int3();
1223                                 return 0;
1224                         }
1225                         break;
1226
1227                 case TS_OPEN_QUOTE:
1228                         // valid space or an open quote
1229                         if((*p == ' ') || (*p == '\"')){
1230                                 if(*p == '\"'){
1231                                         memset(Ts_text, 0, PARSE_TEXT_STRING_LEN);
1232                                         Ts_text_size = 0;
1233                                         Ts_current_state = TS_STRING;
1234                                 }
1235                         } else {
1236                                 Int3();
1237                                 return 0;
1238                         }
1239                         break;
1240
1241                 case TS_STRING:
1242                         // if we have an end quote, we need to look for a comma
1243                         if((*p == '\"') /*&& (Ts_text_size > 0)*/ && (Ts_text[Ts_text_size - 1] != '\\')){
1244                                 // we're now done - we have a string
1245                                 Ts_current_state = TS_SCANNING;
1246
1247                                 // if the id#'s match, copy the string and return "string found"
1248                                 if((atoi(Ts_id_text) == id) && (out != NULL)){
1249                                         strcpy(out, Ts_text);
1250
1251                                         return found_new_string_id ? (1<<1) | (1<<31) : (1<<1);                                 
1252                                 }
1253                                 
1254                                 // otherwise, just continue parsing                             
1255                                 return found_new_string_id ? (1<<0) | (1<<31) : (1<<0);
1256                         } 
1257                         // otherwise add to the string
1258                         else {
1259                                 Ts_text[Ts_text_size++] = *p;
1260                         }                                                                               
1261                         break;
1262                 }               
1263
1264                 // if we have a newline, return success, we're done with this line
1265                 if(*p == '\n'){
1266                         return found_new_string_id ? (1<<0) | (1<<31) : (1<<0);
1267                 }
1268
1269                 // next char in the line
1270                 p++;
1271                 count++;
1272         }       
1273
1274         // success
1275         return found_new_string_id ? (1<<0) | (1<<31) : (1<<0);
1276 }
1277
1278 // if the char is a valid char for a signed integer value
1279 int lcl_is_valid_numeric_char(char c)
1280 {
1281         return ( (c == '-') || (c == '0') || (c == '1') || (c == '2') || (c == '3') || (c == '4') ||
1282                                 (c == '5') || (c == '6') || (c == '7') || (c == '8') || (c == '9') ) ? 1 : 0;
1283 }
1284
1285 // initialize the pointer array into tstrings.tbl (call from lcl_ext_open() ONLY)
1286 void lcl_ext_setup_pointers()
1287 {
1288         char language_string[128];
1289         char line[1024];
1290         char *tok;      
1291         int string_count;
1292         int ret;
1293         int found_start = 0;
1294
1295         // open the localization file
1296         lcl_ext_open();
1297         if(Lcl_ext_file == NULL){
1298 #ifndef MAKE_FS1
1299                 error_display(0, "Error opening externalization file! File likely does not exist or could not be found");
1300 #endif
1301                 return;
1302         }
1303
1304         // seek to the currently active language
1305         memset(language_string, 0, 128);
1306         strcpy(language_string, "#");
1307         if(!SDL_strcasecmp(DEFAULT_LANGUAGE, Lcl_languages[Lcl_current_lang].lang_name)){
1308                 strcat(language_string, "default");
1309         } else {
1310                 strcat(language_string, Lcl_languages[Lcl_current_lang].lang_name);
1311         }
1312         memset(line, 0, 1024);
1313
1314         // reset seek variables and begin               
1315         Lcl_pointer_count = 0;
1316         while(cfgets(line, 1024, Lcl_ext_file)){
1317                 tok = strtok(line, " \n");
1318                 if(tok == NULL){
1319                         continue;                       
1320                 }
1321                 
1322                 // if the language matches, we're good to start parsing strings
1323                 if(!SDL_strcasecmp(language_string, tok)){
1324                         found_start = 1;                        
1325                         break;
1326                 }               
1327         }
1328
1329         // if we didn't find the language specified, error
1330         if(found_start <= 0){
1331                 error_display(0, "Could not find specified langauge in tstrings.tbl!\n");
1332                 lcl_ext_close();
1333                 return;
1334         }
1335
1336         string_count = 0;       
1337         while(cfgets(line, 1024, Lcl_ext_file)){
1338                 ret = lcl_ext_lookup_sub(line, NULL, -1);
1339
1340                 // do stuff
1341                 switch(ret & 0x0fffffff){
1342                 // error
1343                 case 0 :
1344                         lcl_ext_close();
1345                         return;         
1346
1347                 // end of language found
1348                 case 3 :
1349                         // mark one final pointer
1350                         Lcl_pointers[Lcl_pointer_count++] = cftell(Lcl_ext_file) - strlen(line) - 1;
1351                         lcl_ext_close();
1352                         return;
1353                 }
1354
1355                 // the only other case we care about is the beginning of a new id#
1356                 if(ret & (1<<31)){              
1357                         if((string_count % LCL_GRANULARITY) == 0){
1358                                 // mark the pointer down
1359                                 Lcl_pointers[Lcl_pointer_count++] = cftell(Lcl_ext_file) - strlen(line) - 1;
1360
1361                                 // if we're out of pointer slots
1362                                 if(Lcl_pointer_count >= LCL_MAX_POINTERS){
1363                                         error_display(0, "Out of pointer for tstrings.tbl lookup. Please increment LCL_MAX_POINTERS in localize.cpp");
1364                                         lcl_ext_close();
1365                                         return;
1366                                 }
1367                         }
1368                         // increment string count
1369                         string_count++;                 
1370                 }
1371         }
1372
1373         // should never get here. we should always be exiting through case 3 (end of language section) of the above switch
1374         // statement
1375         Int3();
1376         lcl_ext_close();
1377 }
1378
1379 void lcl_get_language_name(char *lang_name)
1380 {
1381         SDL_assert(LCL_NUM_LANGUAGES == 3);
1382
1383         strcpy(lang_name, Lcl_languages[Lcl_current_lang].lang_name);
1384 }
1385
1386 // converts german umlauted chars from ASCII to ANSI
1387 // so they appear in the launcher
1388 // how friggin lame is this
1389 // pass in a null terminated string, foo!
1390 // returns ptr to string you sent in
1391 char* lcl_fix_umlauts(char *str, int which_way)
1392 {
1393         int i=0;
1394
1395         if (which_way == LCL_TO_ANSI) {
1396                 // moving to ANSI charset
1397                 // run thru string and perform appropriate conversions
1398                 while (str[i] != '\0') {
1399                         switch (str[i]) {
1400                         case '\x81':
1401                                 // lower umlaut u
1402                                 str[i] = '\xFC';
1403                                 break;
1404                         case '\x84':
1405                                 // lower umlaut a
1406                                 str[i] = '\xE4';
1407                                 break;
1408                         case '\x94':
1409                                 // lower umlaut o
1410                                 str[i] = '\xF6';
1411                                 break;
1412                         case '\x9A':
1413                                 // upper umlaut u
1414                                 str[i] = '\xDC';
1415                                 break;
1416                         case '\x8E':
1417                                 // upper umlaut a
1418                                 str[i] = '\xC4';
1419                                 break;
1420                         case '\x99':
1421                                 // upper umlaut o
1422                                 str[i] = '\xD6';
1423                                 break;
1424                         case '\xE1':
1425                                 // beta-lookin thing that means "ss"
1426                                 str[i] = '\xDF';
1427                                 break;
1428                         }
1429
1430                         i++;
1431                 }
1432         } else {
1433                 // moving to ASCII charset
1434                 // run thru string and perform appropriate conversions
1435                 while (str[i] != '\0') {
1436                         switch (str[i]) {
1437                         case '\xFC':
1438                                 // lower umlaut u
1439                                 str[i] = '\x81';
1440                                 break;
1441                         case '\xE4':
1442                                 // lower umlaut a
1443                                 str[i] = '\x84';
1444                                 break;
1445                         case '\xF6':
1446                                 // lower umlaut o
1447                                 str[i] = '\x94';
1448                                 break;
1449                         case '\xDC':
1450                                 // upper umlaut u
1451                                 str[i] = '\x9A';
1452                                 break;
1453                         case '\xC4':
1454                                 // upper umlaut a
1455                                 str[i] = '\x8E';
1456                                 break;
1457                         case '\xD6':
1458                                 // upper umlaut o
1459                                 str[i] = '\x99';
1460                                 break;
1461                         case '\xDF':
1462                                 // beta-lookin thing that means "ss"
1463                                 str[i] = '\xE1';
1464                                 break;
1465                         }
1466
1467                         i++;
1468                 }
1469         }
1470
1471         return str;
1472 }
1473
1474 // convert some of the polish characters
1475 void lcl_fix_polish(char *str)
1476 {
1477     for(;*str;str++) {
1478         if(*str == '\xA2') *str = '\xF3';
1479         else if(*str == '\x88') *str = '\xEA';
1480     }
1481 }
1482
1483 // ------------------------------------------------------------------
1484 // lcl_translate_wep_name()
1485 //
1486 // For displaying weapon names in german version
1487 // since we cant actually just change them outright.
1488 //
1489 void lcl_translate_wep_name(char *name)
1490 {
1491         if (!strcmp(name, "Morning Star")) {    
1492                 strcpy(name, "Morgenstern");
1493         } else if (!strcmp(name, "MorningStar")) {
1494                 strcpy(name, "Morgenstern D");
1495         } else if (!strcmp(name, "UD-8 Kayser")) {
1496                 strcpy(name, "Kayserstrahl");
1497         } else if (!strcmp(name, "UD-D Kayser")) {
1498                 strcpy(name, "Kayserstrahl");
1499         }
1500 }
1501
1502 // ------------------------------------------------------------------
1503 // lcl_translate_brief_icon_name()
1504 //
1505 // For displaying ship names in german version
1506 // since we cant actually just change them outright.
1507 //
1508 void lcl_translate_brief_icon_name(char *name)
1509 {
1510         char *pos;
1511         char buf[128];
1512
1513         if (!SDL_strcasecmp(name, "Subspace Portal")) { 
1514                 strcpy(name, "Subraum Portal");
1515
1516         } else if (!SDL_strcasecmp(name, "Alpha Wing")) {
1517                 strcpy(name, "Alpha");
1518
1519         } else if (!SDL_strcasecmp(name, "Beta Wing")) {
1520                 strcpy(name, "Beta");
1521
1522         } else if (!SDL_strcasecmp(name, "Zeta Wing")) {
1523                 strcpy(name, "Zeta");
1524
1525         } else if (!SDL_strcasecmp(name, "Capella Node")) {
1526                 strcpy(name, "Capella");
1527
1528         } else if (!SDL_strcasecmp(name, "Hostile")) {
1529                 strcpy(name, "Gegner");
1530
1531         } else if (!SDL_strcasecmp(name, "Hostile Craft")) {
1532                 strcpy(name, "Gegner");
1533
1534         } else if (!SDL_strcasecmp(name, "Rebel Wing")) {
1535                 strcpy(name, "Rebellen");
1536
1537         } else if (!SDL_strcasecmp(name, "Rebel Fleet")) {
1538                 strcpy(name, "Rebellenflotte");
1539
1540         } else if (!SDL_strcasecmp(name, "Sentry Gun")) {
1541                 strcpy(name, "Gesch\x81tz");
1542
1543         } else if (!SDL_strcasecmp(name, "Cargo")) {
1544                 strcpy(name, "Fracht");
1545
1546         } else if (!SDL_strcasecmp(name, "Knossos Device")) {
1547                 strcpy(name, "Knossosger\x84t");
1548         
1549         } else if (!SDL_strcasecmp(name, "Support")) {
1550                 strcpy(name, "Versorger");
1551
1552         } else if (!SDL_strcasecmp(name, "Unknown")) {
1553                 strcpy(name, "Unbekannt");
1554
1555         } else if (!SDL_strcasecmp(name, "Instructor")) {
1556                 strcpy(name, "Ausbilder");
1557         
1558         } else if (!SDL_strcasecmp(name, "Jump Node")) {
1559                 strcpy(name, "Sprungknoten");
1560
1561         } else if (!SDL_strcasecmp(name, "Escort")) {
1562                 strcpy(name, "Geleitschutz");
1563
1564         } else if (!SDL_strcasecmp(name, "Asteroid Field")) {
1565                 strcpy(name, "Asteroidenfeld");
1566
1567         } else if (!SDL_strcasecmp(name, "Enif Station")) {
1568                 strcpy(name, "Station Enif");
1569
1570         } else if (!SDL_strcasecmp(name, "Rally Point")) {
1571                 strcpy(name, "Sammelpunkt");
1572
1573         } else if ((pos = strstr(name, "Transport")) != NULL) {
1574                 pos += 9;               // strlen of "transport"
1575                 strcpy(buf, "Transporter");
1576                 strcat(buf, pos);
1577                 strcpy(name, buf);
1578
1579         } else if ((pos = strstr(name, "Jump Node")) != NULL) {
1580                 pos += 9;               // strlen of "jump node"
1581                 strcpy(buf, "Sprungknoten");
1582                 strcat(buf, pos);
1583                 strcpy(name, buf);
1584         
1585         } else if (!SDL_strcasecmp(name, "Orion under repair")) {
1586                 strcpy(name, "Orion wird repariert");
1587
1588         // SOTY-specific ones below!
1589         
1590         } else if (!SDL_strcasecmp(name, "Wayfarer Station")) {
1591                 strcpy(name, "Station Wayfarer");
1592         } else if (!SDL_strcasecmp(name, "Enemy")) {
1593                 strcpy(name, "Gegner");
1594         } else if (!SDL_strcasecmp(name, "Supply Depot")) {
1595                 strcpy(name, "Nachschubdepot");
1596         } else if (!SDL_strcasecmp(name, "Fighter Escort")) {
1597                 strcpy(name, "Jagdschutz");
1598         } else if (!SDL_strcasecmp(name, "Shivans")) {
1599                 strcpy(name, "Shivaner");
1600         } else if (!SDL_strcasecmp(name, "NTF Base of Operations")) {
1601                 strcpy(name, "NTF-Operationsbasis");
1602         } else if (!SDL_strcasecmp(name, "NTF Bombers")) {
1603                 strcpy(name, "NTF-Bomber");
1604         } else if (!SDL_strcasecmp(name, "NTF Fighters")) {
1605                 strcpy(name, "NTF-J\x84ger");
1606         } else if (!SDL_strcasecmp(name, "Sentry")) {
1607                 strcpy(name, "Sperrgesch\x81tz");
1608         } else if (!SDL_strcasecmp(name, "Cargo Containers")) {
1609                 strcpy(name, "Frachtbeh\x84lter");
1610         } else if (!SDL_strcasecmp(name, "NTF Reinforcements")) {
1611                 strcpy(name, "NTF-Verst\x84rkungen");
1612         } else if (!SDL_strcasecmp(name, "NTF Base")) {
1613                 strcpy(name, "NTF-St\x81tzpunkt");
1614         } else if (!SDL_strcasecmp(name, "Refugee Convoy")) {
1615                 strcpy(name, "Fl\x81""chtlingskonvoi");
1616         } else if (!SDL_strcasecmp(name, "Food Convoy")) {
1617                 strcpy(name, "Nachschubkonvoi");
1618         } else if (!SDL_strcasecmp(name, "Governor's Shuttle")) {
1619                 strcpy(name, "F\x84hre des Gouverneurs");
1620         } else if (!SDL_strcasecmp(name, "GTVA Patrol")) {
1621                 strcpy(name, "GTVA-Patrouille");
1622         } else if (!SDL_strcasecmp(name, "Escort fighters")) {
1623                 strcpy(name, "Geleitschutz");
1624         } else if (!SDL_strcasecmp(name, "Nagada Outpost")) {
1625                 strcpy(name, "Nagada-Aussenposten");
1626         } else if (!SDL_strcasecmp(name, "Fighters")) {
1627                 strcpy(name, "J\x84ger");
1628         } else if (!SDL_strcasecmp(name, "Bombers")) {
1629                 strcpy(name, "Bomber");
1630         } else if (!SDL_strcasecmp(name, "Enemy Destroyers")) {
1631                 strcpy(name, "Feindliche Zerst\x94rer");
1632         } else if (!SDL_strcasecmp(name, "Ross 128 System")) {
1633                 strcpy(name, "System Ross 128");
1634         } else if (!SDL_strcasecmp(name, "Knossos Station")) {
1635                 strcpy(name, "Knossos-Station");
1636         } else if (!SDL_strcasecmp(name, "Transporters")) {
1637                 strcpy(name, "Transporter");
1638         } else if (!SDL_strcasecmp(name, "Pirates?")) {
1639                 strcpy(name, "Piraten?");
1640         } else if (!SDL_strcasecmp(name, "Escorts")) {
1641                 strcpy(name, "Geleitschutz");
1642         } else if (!SDL_strcasecmp(name, "Shivan Fighters")) {
1643                 strcpy(name, "J\x84ger");
1644         } else if (!SDL_strcasecmp(name, "Shivan Territory")) {
1645                 strcpy(name, "Shivaner");
1646         }
1647 }
1648
1649 // ------------------------------------------------------------------
1650 // lcl_translate_brief_icon_name_pl()
1651 //
1652 // For displaying ship names in polish version
1653 // since we cant actually just change them outright.
1654 //
1655 void lcl_translate_brief_icon_name_pl(char *name)
1656 {
1657         char *pos;
1658         char buf[128];
1659
1660         if (!SDL_strcasecmp(name, "Subspace Portal")) { 
1661                 strcpy(name, "Portal podprz.");
1662
1663         } else if (!SDL_strcasecmp(name, "Alpha Wing")) {
1664                 strcpy(name, "Alfa");
1665
1666         } else if (!SDL_strcasecmp(name, "Beta Wing")) {
1667                 strcpy(name, "Beta");
1668
1669         } else if (!SDL_strcasecmp(name, "Zeta Wing")) {
1670                 strcpy(name, "Zeta");
1671
1672         } else if (!SDL_strcasecmp(name, "Capella Node")) {
1673                 strcpy(name, "Capella");
1674
1675         } else if (!SDL_strcasecmp(name, "Hostile")) {
1676                 strcpy(name, "Wr\xF3g");
1677
1678         } else if (!SDL_strcasecmp(name, "Hostile Craft")) {
1679                 strcpy(name, "Wr\xF3g");
1680
1681         } else if (!SDL_strcasecmp(name, "Rebel Wing")) {
1682                 strcpy(name, "Rebelianci");
1683
1684         } else if (!SDL_strcasecmp(name, "Rebel Fleet")) {
1685                 strcpy(name, "Flota Rebelii");
1686
1687         } else if (!SDL_strcasecmp(name, "Sentry Gun")) {
1688                 strcpy(name, "Dzia\xB3o str.");
1689
1690         } else if (!SDL_strcasecmp(name, "Cargo")) {
1691                 strcpy(name, "\xA3\x61\x64unek");
1692
1693         } else if (!SDL_strcasecmp(name, "Knossos Device")) {
1694                 strcpy(name, "Urz. Knossos");
1695         
1696         } else if (!SDL_strcasecmp(name, "Support")) {
1697                 strcpy(name, "Wsparcie");
1698
1699         } else if (!SDL_strcasecmp(name, "Unknown")) {
1700                 strcpy(name, "Nieznany");
1701
1702         } else if (!SDL_strcasecmp(name, "Instructor")) {
1703                 strcpy(name, "Instruktor");
1704         
1705         } else if (!SDL_strcasecmp(name, "Jump Node")) {
1706                 strcpy(name, "W\xEAze\xB3 skokowy");
1707
1708         } else if (!SDL_strcasecmp(name, "Escort")) {
1709                 strcpy(name, "Eskorta");
1710
1711         } else if (!SDL_strcasecmp(name, "Asteroid Field")) {
1712                 strcpy(name, "Pole asteroid");
1713
1714         } else if (!SDL_strcasecmp(name, "Enif Station")) {
1715                 strcpy(name, "Stacja Enif");
1716
1717         } else if (!SDL_strcasecmp(name, "Rally Point")) {
1718                 strcpy(name, "Pkt zborny");
1719
1720         } else if ((pos = strstr(name, "Transport")) != NULL) {
1721                 pos += 9;               // strlen of "transport"
1722                 strcpy(buf, "Transporter");
1723                 strcat(buf, pos);
1724                 strcpy(name, buf);
1725
1726         } else if ((pos = strstr(name, "Jump Node")) != NULL) {
1727                 pos += 9;               // strlen of "jump node"
1728                 strcpy(buf, "W\xEAze\xB3 skokowy");
1729                 strcat(buf, pos);
1730                 strcpy(name, buf);
1731         
1732         } else if (!SDL_strcasecmp(name, "Orion under repair")) {
1733                 strcpy(name, "Naprawiany Orion");
1734         }
1735 }
1736
1737 // ------------------------------------------------------------------
1738 // lcl_translate_ship_name()
1739 //
1740 // For displaying ship names in german version in the briefing
1741 // since we cant actually just change them outright.
1742 //
1743 void lcl_translate_ship_name(char *name)
1744 {
1745         if (!strcmp(name, "GTDR Amazon Advanced")) {    
1746                 strcpy(name, "GTDR Amazon VII");
1747         } 
1748 }
1749
1750 // ------------------------------------------------------------------
1751 // lcl_translate_targetbox_name()
1752 //
1753 // For displaying ship names in german version in the targetbox
1754 // since we cant actually just change them outright.
1755 //
1756 void lcl_translate_targetbox_name(char *name)
1757 {
1758         char *pos;
1759         char buf[128];
1760         
1761         if ((pos = strstr(name, "Sentry")) != NULL) {
1762                 pos += 6;               // strlen of "sentry"
1763                 strcpy(buf, "Sperrgesch\x81tz");
1764                 strcat(buf, pos);
1765                 strcpy(name, buf);
1766
1767         } else if ((pos = strstr(name, "Support")) != NULL) {
1768                 pos += 7;               // strlen of "support"
1769                 strcpy(buf, "Versorger");
1770                 strcat(buf, pos);
1771                 strcpy(name, buf);
1772
1773         } else if ((pos = strstr(name, "Unknown")) != NULL) {
1774                 pos += 7;               // strlen of "unknown"
1775                 strcpy(buf, "Unbekannt");
1776                 strcat(buf, pos);
1777                 strcpy(name, buf);
1778
1779         } else if ((pos = strstr(name, "Drone")) != NULL) {
1780                 pos += 5;               // strlen of "drone"
1781                 strcpy(buf, "Drohne");
1782                 strcat(buf, pos);
1783                 strcpy(name, buf);
1784
1785         } else if ((pos = strstr(name, "Jump Node")) != NULL) {
1786                 pos += 9;               // strlen of "jump node"
1787                 strcpy(buf, "Sprungknoten");
1788                 strcat(buf, pos);
1789                 strcpy(name, buf);
1790
1791         } else if (!SDL_strcasecmp(name, "Instructor")) {
1792                 strcpy(name, "Ausbilder");
1793
1794         } else if (!SDL_strcasecmp(name, "NTF Vessel")) {
1795                 strcpy(name, "NTF-Schiff");
1796
1797         } else if (!SDL_strcasecmp(name, "Enif Station")) {
1798                 strcpy(name, "Station Enif");
1799         }
1800 }
1801
1802 // ------------------------------------------------------------------
1803 // lcl_translate_targetbox_name_pl()
1804 //
1805 // For displaying ship names in polish version in the targetbox
1806 // since we cant actually just change them outright.
1807 //
1808 void lcl_translate_targetbox_name_pl(char *name)
1809 {
1810         char *pos;
1811         char buf[128];
1812         
1813         if ((pos = strstr(name, "Sentry")) != NULL) {
1814                 pos += 6;               // strlen of "sentry"
1815                 strcpy(buf, "Stra\xBFnik");
1816                 strcat(buf, pos);
1817                 strcpy(name, buf);
1818
1819         } else if ((pos = strstr(name, "Support")) != NULL) {
1820                 pos += 7;               // strlen of "support"
1821                 strcpy(buf, "Wsparcie");
1822                 strcat(buf, pos);
1823                 strcpy(name, buf);
1824
1825         } else if ((pos = strstr(name, "Unknown")) != NULL) {
1826                 pos += 7;               // strlen of "unknown"
1827                 strcpy(buf, "Nieznany");
1828                 strcat(buf, pos);
1829                 strcpy(name, buf);
1830
1831         } else if ((pos = strstr(name, "Drone")) != NULL) {
1832                 pos += 5;               // strlen of "drone"
1833                 strcpy(buf, "Sonda");
1834                 strcat(buf, pos);
1835                 strcpy(name, buf);
1836
1837         } else if ((pos = strstr(name, "Jump Node")) != NULL) {
1838                 pos += 9;               // strlen of "jump node"
1839                 strcpy(buf, "W\xEAze\xB3 skokowy");
1840                 strcat(buf, pos);
1841                 strcpy(name, buf);
1842
1843         } else if (!SDL_strcasecmp(name, "Instructor")) {
1844                 strcpy(name, "Instruktor");
1845
1846         } else if (!SDL_strcasecmp(name, "NTF Vessel")) {
1847                 strcpy(name, "Okr\xEAt NTF");
1848
1849         } else if (!SDL_strcasecmp(name, "Enif Station")) {
1850                 strcpy(name, "Stacja Enif");
1851         }
1852 }