Debian Codegraph @

Descent3/localization.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/localization.cpp $
21 * $Revision: 18 $
22 * $Date: 11/05/99 4:05p $
23 * $Author: Jeff $
24 *
25 * Localization implementation
26 *
27 * $Log: /DescentIII/main/localization.cpp $
28 *
29 * 18 11/05/99 4:05p Jeff
30 * fixed bug with 'retry english' closing the file and then trying to read
31 * it
32 *
33 * 17 10/12/99 11:04a Jeff
34 * load english string table if foreign strings are not found
35 *
36 * 16 5/05/99 4:33p Jeff
37 * fixed Linux filename fix when opening file for second time
38 *
39 * 15 5/04/99 8:50p Jeff
40 * safety precautions
41 *
42 * 14 4/26/99 5:17p Jeff
43 * loading string tables is filename friendly (for Linux)...filename must
44 * be all lowercase
45 *
46 * 13 4/14/99 2:51a Jeff
47 * fixed some case mismatched #includes
48 *
49 * 12 2/22/99 3:19p Jeff
50 * added function to get current language
51 *
52 * 11 2/11/99 11:02p Jeff
53 * fixed bug with aux string table loading
54 *
55 * 10 2/11/99 10:35p Jeff
56 * added support for multiple languages in string table files
57 *
58 * 9 1/19/99 1:26p Jeff
59 * handled \r
60 *
61 * 8 1/18/99 12:03p Jeff
62 * return true if 0 strings are loaded from a correct string table, not
63 * false
64 *
65 * 7 1/13/99 4:05p Jeff
66 * when loading a script dll, a string table is looked for and
67 * automatically loaded and passed to the newly loaded dll on init for
68 * use.
69 *
70 * 6 6/26/98 4:52p Jeff
71 * fixed stupid
72 *
73 * 5 6/26/98 4:00p Jeff
74 * added parsing of escape characters for CreateStringTable()
75 *
76 * 4 6/17/98 3:28p Jeff
77 *
78 * 3 6/16/98 10:37a Jeff
79 * fixed bug in growstring class, parse escape chars
80 *
81 * 2 6/12/98 5:55p Jeff
82 * initial creation
83 *
84 * $NoKeywords: $
85 */
86
87 #include <stdlib.h>
88 #include <string.h>
89 #include <stdio.h>
90 #include <ctype.h>
91 #include "game.h"
92 #include "descent.h"
93 #include "mono.h"
94 #include "cfile.h"
95 #include "localization.h"
96 #include "mem.h"
97 #include "ddio.h"
98
99 struct tLangTag {
100 const char *tag;
101 int length;
102 };
103
104 tLangTag Language_tags[] = {
105 {"!=!", -1}, // English
106 {"!G!", -1}, // German
107 {"!S!", -1}, // Spanish
108 {"!I!", -1}, // Italian
109 {"!F!", -1}, // French
110 };
111 int Num_languages = sizeof(Language_tags) / sizeof(tLangTag);
112
113 // The following data, in the anonymous namespace, are static to this file
114 namespace {
115 int Localization_language = -1;
116
117 int String_table_size = 0;
118 char **String_table = NULL;
119
120 // list of the string table files, they will be loaded in the order they are listed
121 const char *String_table_list[] = {"D3.STR", NULL};
122
123 const char *_Error_string = "!!ERROR MISSING STRING!!";
124 const char *_Empty_string = "\0";
125 } // namespace
126
127 void Localization_SetLanguage(int type) {
128 ASSERT(type >= 0 && type < Num_languages);
129 Localization_language = type;
130 }
131
132 int Localization_GetLanguage(void) { return Localization_language; }
133
134 #define COMMENT_TAG "!/!" // This line is to be ignored
135
136 #define STAG_CONTINUE -1 // this line is just a continuation of the last line
137 #define STAG_EMPTY -2 // empty line
138 #define STAG_COMMENT -3 // comment line
139 // 0 -> Num_languages means it's the start of a string that begins with that language
140
141 #define MAX_LINE_LENGTH 1024
142 #define MAX_STRING_LENGTH 8 * MAX_LINE_LENGTH
143 #define MAX_TAG_LENGTH 3
144
145 int GetTotalStringCount(void);
146 int LoadStringFile(const char *filename, int starting_offset);
147 int8_t _parse_line_information(char *line);
148 char *_parse_string_tag(char *buffer);
149 char *_parse_escape_chars(char *buffer);
150
151 // Call this to load up the string tables into memory
152 // Returns the number of strings loaded, if this is 0, then the program MUST not continue
153 int LoadStringTables(void) {
154 static bool called = false;
155 int old_language;
156
157 if (called) {
158 // Only call this guy once
159 Int3();
160 return 0;
161 }
162 called = true;
163
164 old_language = Localization_language;
165
166 int string_count = GetTotalStringCount();
167 if (string_count == 0) {
168 // attempt to load english version of language, so there is something
169 if (Localization_language != LANGUAGE_ENGLISH) {
170 Localization_language = LANGUAGE_ENGLISH;
171 string_count = GetTotalStringCount();
172 }
173
174 if (string_count == 0) {
175 return 0;
176 }
177 }
178
179 String_table_size = 0;
180
181 // malloc our array of char *
182 String_table = (char **)mem_malloc(sizeof(char *) * string_count);
183 if (!String_table) {
184 Localization_language = old_language;
185 return 0;
186 }
187
188 for (int tcount = 0; tcount < string_count; tcount++)
189 String_table[tcount] = NULL;
190
191 int runcount = 0;
192 int temp;
193 int index = 0;
194 while (String_table_list[index]) {
195 temp = LoadStringFile(String_table_list[index], runcount);
196 if (temp == 0) {
197 Localization_language = old_language;
198 return 0;
199 }
200 if (runcount + temp > string_count) {
201 Localization_language = old_language;
202 return 0;
203 }
204 runcount += temp;
205 index++;
206 }
207
208 if (runcount == 0) {
209 Localization_language = old_language;
210 return 0;
211 }
212
213 String_table_size = runcount;
214 Localization_language = old_language;
215
216 atexit(FreeStringTables);
217 return String_table_size;
218 }
219
220 // Deallocates all the memory used for the string tables
221 void FreeStringTables(void) {
222 DestroyStringTable(String_table, String_table_size);
223 String_table = NULL;
224 }
225
226 const char *GetStringFromTable(int index) {
227 if ((index < 0) || (index >= String_table_size))
228 return _Error_string;
229
230 if (!String_table[index])
231 return _Empty_string;
232
233 return String_table[index];
234 }
235
236 void FixFilenameCase(const char *filename, char *newfile) {
237 char path[_MAX_PATH], file[_MAX_FNAME], ext[_MAX_EXT];
238 ddio_SplitPath(filename, path, file, ext);
239
240 char *p;
241
242 p = file;
243 while (*p) {
244 *p = tolower(*p);
245 p++;
246 };
247 p = ext;
248 while (*p) {
249 *p = tolower(*p);
250 p++;
251 };
252
253 strcat(file, ext);
254
255 if (strlen(path) > 0)
256 ddio_MakePath(newfile, path, file, NULL);
257 else
258 strcpy(newfile, file);
259 }
260
261 // Given a filename, pointer to a char * array and a pointer to an int,
262 // it will load the string table and fill in the information
263 // returns true on success
264 bool CreateStringTable(const char *filename, char ***table, int *size) {
265 ASSERT(filename);
266 ASSERT(Localization_language != -1);
267 if (!filename) {
268 if (table)
269 *table = NULL;
270 if (size)
271 *size = 0;
272 return false;
273 }
274 CFILE *file;
275
276 char fname[_MAX_PATH];
277 FixFilenameCase(filename, fname);
278 file = cfopen(fname, "rt");
279 if (!file) {
280 if (table)
281 *table = NULL;
282 if (size)
283 *size = 0;
284 return false;
285 }
286
287 int old_language, scount;
288 char tempbuffer[MAX_STRING_LENGTH];
289
290 old_language = Localization_language;
291
292 try_english:
293
294 // first found out how many strings total in the file
295 scount = 0;
296
297 while (!cfeof(file)) {
298 cf_ReadString(tempbuffer, MAX_LINE_LENGTH + 1, file);
299 if (_parse_line_information(tempbuffer) == Localization_language)
300 scount++;
301 }
302 cfclose(file);
303
304 if (scount == 0) {
305 // no strings found, if we are searching a non-English version, then
306 // load english versions
307 if (Localization_language != LANGUAGE_ENGLISH) {
308 Localization_language = LANGUAGE_ENGLISH;
309 // open the file again
310 file = cfopen(fname, "rt");
311 goto try_english;
312 }
313
314 // no strings found
315 Localization_language = old_language;
316 mprintf(0, "Localization: Warning, 0 strings found in %s\n", filename);
317 *table = NULL;
318 *size = 0;
319 return true;
320 }
321
322 *size = scount;
323 char **strtable;
324
325 // malloc our array of char *
326 *table = (char **)mem_malloc(sizeof(char *) * scount);
327 if (!*table) {
328 if (table)
329 *table = NULL;
330 if (size)
331 *size = 0;
332 Localization_language = old_language;
333 return false;
334 }
335
336 strtable = *table;
337 for (int tcount = 0; tcount < scount; tcount++)
338 strtable[tcount] = NULL;
339
340 // now load the strings
341 file = cfopen(fname, "rt");
342 if (!file) {
343 if (table)
344 *table = NULL;
345 if (size)
346 *size = 0;
347 Localization_language = old_language;
348 return false;
349 }
350
351 bool reading_string = false;
352 scount = 0;
353
354 GrowString string;
355
356 int line_info;
357
358 while (!cfeof(file)) {
359 cf_ReadString(tempbuffer, MAX_STRING_LENGTH, file);
360 if (scount >= 198)
361 scount = scount;
362
363 line_info = _parse_line_information(tempbuffer);
364
365 switch (line_info) {
366 case STAG_CONTINUE:
367 if (reading_string) {
368 // we need to add on to the working buffer
369 string += _parse_escape_chars(tempbuffer);
370 }
371 break;
372 case STAG_EMPTY:
373 // empty line !ACK! What should I do...do a new line char or skip?...skip for now
374 break;
375 case STAG_COMMENT:
376 // ignore this line
377 if (reading_string) {
378 // ok we're done with the string, finish it and put it in the string table
379 string.GetString(&strtable[scount]);
380 scount++;
381 string.Destroy();
382 }
383 reading_string = false;
384 break;
385 default: {
386 if (line_info >= 0 && line_info < Num_languages) {
387 if (reading_string) {
388 // ok we're done with the string, finish it and put it in the string table
389 string.GetString(&strtable[scount]);
390 scount++;
391 string.Destroy();
392 reading_string = false;
393 }
394
395 // see if we should read this string
396 if (line_info == Localization_language) {
397 reading_string = true;
398 // start filling in the buffer
399 string.Destroy();
400 string = _parse_escape_chars(_parse_string_tag(tempbuffer));
401 }
402
403 } else {
404 Int3(); // get Jeff
405 reading_string = false;
406 }
407 } break;
408 } // end switch
409 } // end while
410
411 if (reading_string) {
412 // we're at the end of the file and we're reading, so but it in the string table
413 string.GetString(&strtable[scount]);
414 scount++;
415 string.Destroy();
416 reading_string = false;
417 }
418
419 cfclose(file);
420
421 mprintf(0, "String Table (%s) loaded with %d strings\n", filename, *size);
422 Localization_language = old_language;
423
424 return (scount == (*size));
425 }
426
427 // Given a string table and it's count of strings, it will free up it's memory
428 void DestroyStringTable(char **table, int size) {
429 if ((size > 0) && (table)) {
430 for (int i = 0; i < size; i++) {
431 if (table[i])
432 mem_free(table[i]);
433 }
434 }
435
436 if (table)
437 mem_free(table);
438 }
439
440 // returns the total number of strings in all the string table files
441 // returns 0 on error
442 int GetTotalStringCount(void) {
443 int scount = 0;
444 int findex = 0;
445 CFILE *file;
446 char tempbuffer[MAX_LINE_LENGTH + 1];
447 ASSERT(Localization_language != -1);
448
449 while (String_table_list[findex]) {
450 // open the file up
451 char fname[_MAX_PATH];
452 FixFilenameCase(String_table_list[findex], fname);
453 file = cfopen(fname, "rt");
454 if (!file)
455 return 0;
456
457 while (!cfeof(file)) {
458 cf_ReadString(tempbuffer, MAX_LINE_LENGTH + 1, file);
459 if (_parse_line_information(tempbuffer) == Localization_language)
460 scount++;
461 }
462
463 cfclose(file);
464 findex++;
465 }
466 return scount;
467 }
468
469 // Loads a string table file, returns number of strings read if everything went ok,else 0
470 int LoadStringFile(const char *filename, int starting_offset) {
471 ASSERT(filename);
472 ASSERT(Localization_language != -1);
473 if (!filename)
474 return 0;
475
476 CFILE *file;
477 char fname[_MAX_PATH];
478 FixFilenameCase(filename, fname);
479 file = cfopen(fname, "rt");
480 if (!file)
481 return 0;
482
483 int scount = 0;
484 char buffer[MAX_STRING_LENGTH];
485
486 bool reading_string = false;
487
488 GrowString string;
489 int line_info;
490
491 while (!cfeof(file)) {
492 cf_ReadString(buffer, MAX_STRING_LENGTH, file);
493 if (scount >= 198)
494 scount = scount;
495
496 line_info = _parse_line_information(buffer);
497
498 switch (line_info) {
499 case STAG_CONTINUE:
500 if (reading_string) {
501 // we need to add on to the working buffer
502 string += _parse_escape_chars(buffer);
503 }
504 break;
505 case STAG_EMPTY:
506 // empty line !ACK! What should I do...do a new line char or skip?...skip for now
507 break;
508 case STAG_COMMENT:
509 // ignore this line
510 if (reading_string) {
511 // ok we're done with the string, finish it and put it in the string table
512 string.GetString(&String_table[scount + starting_offset]);
513 scount++;
514 string.Destroy();
515 }
516 reading_string = false;
517 break;
518 default: {
519 if (line_info >= 0 && line_info < Num_languages) {
520 if (reading_string) {
521 // ok we're done with the string, finish it and put it in the string table
522 string.GetString(&String_table[scount + starting_offset]);
523 scount++;
524 string.Destroy();
525 reading_string = false;
526 }
527
528 // see if we should read this string
529 if (line_info == Localization_language) {
530 reading_string = true;
531 // start filling in the buffer
532 string.Destroy();
533 string = _parse_escape_chars(_parse_string_tag(buffer));
534 }
535
536 } else {
537 Int3(); // get Jeff
538 reading_string = false;
539 }
540 } break;
541 } // end switch
542 } // end while
543
544 if (reading_string) {
545 // we're at the end of the file and we're reading, so but it in the string table
546 string.GetString(&String_table[scount + starting_offset]);
547 scount++;
548 string.Destroy();
549 reading_string = false;
550 }
551
552 cfclose(file);
553 return scount;
554 }
555
556 // returns STAG_* information about the line
557 int8_t _parse_line_information(char *line) {
558 for (int i = 0; i < Num_languages; i++) {
559 if (Language_tags[i].length == -1)
560 Language_tags[i].length = strlen(Language_tags[i].tag);
561
562 if (!strncmp(line, Language_tags[i].tag, Language_tags[i].length))
563 return i;
564 }
565
566 if (!strncmp(line, COMMENT_TAG, strlen(COMMENT_TAG)))
567 return STAG_COMMENT;
568 if (strlen(line) == 0)
569 return STAG_EMPTY;
570 return STAG_CONTINUE;
571 }
572
573 // parses a string_tag out
574 char *_parse_string_tag(char *buffer) {
575 int8_t i = _parse_line_information(buffer);
576
577 switch (i) {
578 case STAG_CONTINUE:
579 case STAG_EMPTY:
580 return buffer;
581 break;
582 case STAG_COMMENT:
583 return buffer + (strlen(COMMENT_TAG));
584 break;
585 default:
586 if (i >= 0 && i < Num_languages) {
587 // we have a specific language...
588 return buffer + (Language_tags[i].length);
589 }
590 Int3(); // Get Jeff
591 break;
592 }
593 return buffer;
594 }
595
596 // parses out escape chars...like /t,/n
597 char *_parse_escape_chars(char *buffer) {
598 char tempbuffer[MAX_STRING_LENGTH];
599 int t_index, b_index;
600
601 t_index = 0;
602 b_index = 0;
603
604 // search for a /
605 while (buffer[b_index] != '\0') {
606 // see if we are at a backslash
607 if (buffer[b_index] != '\\') {
608 tempbuffer[t_index] = buffer[b_index];
609 t_index++;
610 b_index++;
611 } else {
612 // we are at a backslash, so determine what to do
613 b_index++;
614 switch (toupper(buffer[b_index])) {
615 case 'T':
616 tempbuffer[t_index] = '\t';
617 break;
618 case 'N':
619 tempbuffer[t_index] = '\n';
620 break;
621 case 'R':
622 tempbuffer[t_index] = '\r';
623 break;
624 case '\"':
625 tempbuffer[t_index] = '\"';
626 break;
627 case '\\':
628 tempbuffer[t_index] = '\\';
629 break;
630 default:
631 // see if we have a number
632 if ((buffer[b_index] >= '0') && (buffer[b_index] <= '9')) {
633 // we have a number, so parse the value
634 uint8_t value = 0;
635 while ((buffer[b_index] >= '0') && (buffer[b_index] <= '9')) {
636 value = (value * 10) + buffer[b_index] - '0';
637 b_index++;
638 }
639 // adjust b_index
640 b_index--;
641 tempbuffer[t_index] = value;
642 }
643 break;
644 }
645 b_index++;
646 t_index++;
647 }
648 }
649 tempbuffer[t_index] = '\0';
650 strcpy(buffer, tempbuffer);
651 return buffer;
652 }
653
654 GrowString::GrowString() {
655 root.string_data = NULL;
656 root.next = NULL;
657 curr = &root;
658 }
659 GrowString::~GrowString() { Destroy(); }
660
661 void GrowString::operator+=(char *str) {
662 if (!str)
663 return;
664 if (root.string_data) {
665 tbufferinfo *node;
666 node = (tbufferinfo *)mem_malloc(sizeof(tbufferinfo));
667 if (!node)
668 return;
669 node->string_data = (char *)mem_malloc(strlen(str) + 2);
670 if (!node->string_data) {
671 mem_free(node);
672 return;
673 }
674 sprintf(node->string_data, "\n%s", str);
675 curr->next = node;
676 node->next = NULL;
677 curr = node;
678 } else {
679 root.string_data = (char *)mem_malloc(strlen(str) + 1);
680 if (!root.string_data)
681 return;
682 strcpy(root.string_data, str);
683 root.next = NULL;
684 }
685 }
686
687 void GrowString::Destroy(void) {
688 if (root.string_data)
689 mem_free(root.string_data);
690 root.string_data = NULL;
691
692 tbufferinfo *c, *next;
693 c = next = root.next;
694 while (c) {
695 next = c->next;
696 if (c->string_data)
697 mem_free(c->string_data);
698 mem_free(c);
699 c = next;
700 }
701 root.next = NULL;
702 root.string_data = NULL;
703 curr = &root;
704 }
705
706 GrowString GrowString::operator+(char *str) {
707 *this += str;
708 return *this;
709 }
710
711 GrowString GrowString::operator+(GrowString &gs) {
712 char *str = NULL;
713 gs.GetString(&str);
714 *this += str;
715 if (str)
716 mem_free(str);
717 return *this;
718 }
719
720 void GrowString::operator+=(GrowString &gs) {
721 char *str = NULL;
722 gs.GetString(&str);
723 *this += str;
724 if (str)
725 mem_free(str);
726 }
727
728 void GrowString::operator=(char *str) {
729 Destroy();
730 *this += str;
731 }
732
733 void GrowString::operator=(GrowString &gs) {
734 char *str = NULL;
735 gs.GetString(&str);
736 *this = str;
737 if (str)
738 mem_free(str);
739 }
740
741 void GrowString::GetString(char **str) {
742 *str = NULL;
743 int size = Length();
744 if (size == -1)
745 return;
746
747 tbufferinfo *next;
748 next = root.next;
749
750 *str = (char *)mem_malloc(size + 1);
751
752 strcpy(*str, root.string_data);
753 next = root.next;
754 while (next) {
755 if (next->string_data) {
756 strcat(*str, next->string_data);
757 }
758 next = next->next;
759 }
760 }
761
762 int GrowString::Length(void) {
763 if (!root.string_data)
764 return -1;
765
766 tbufferinfo *next;
767 next = root.next;
768 int size = strlen(root.string_data);
769 while (next) {
770 if (next->string_data)
771 size += strlen(next->string_data);
772 next = next->next;
773 }
774 return size;
775 }