Subversion Repositories Games.Chess Giants

Rev

Rev 11 | Rev 59 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
// localizedtexts.cpp
2
 
3
#include "common.h"
4
 
5
 
11 pmbaty 6
bool LocalizedTexts_Init (const wchar_t *fmt, ...)
1 pmbaty 7
{
8
   // this function opens and parses the INI file containing the localized texts and fills the
9
   // global text_t structure.
10
 
11 pmbaty 11
   static wchar_t lngfile_pathname[MAX_PATH];
12
   va_list argptr;
1 pmbaty 13
   void *inifile;
14
   text_t *text;
15
   int text_index;
16
   wchar_t *id_string;
17
   wchar_t *filter_char;
18
 
11 pmbaty 19
   // concatenate all the arguments in one string
20
   va_start (argptr, fmt);
21
   wvsprintf (lngfile_pathname, fmt, argptr);
22
   va_end (argptr);
23
 
1 pmbaty 24
   // open the INI file
11 pmbaty 25
   inifile = INIFile_LoadINIFile (lngfile_pathname);
1 pmbaty 26
 
27
   // consistency check
28
   if (inifile == NULL)
29
      return (false);
30
 
31
   // read the number of known localized texts and mallocate enough space for them
32
   text_count = INIFile_GetNumberOfEntries (inifile, L"texts");
33
   texts = (text_t *) SAFE_malloc (text_count, sizeof (text_t), true);
34
 
35
   // now read all of them
36
   for (text_index = 0; text_index < text_count; text_index++)
37
   {
38
      text = &texts[text_index]; // quick access to text
39
 
40
      id_string = INIFile_GetEntryName (inifile, L"texts", text_index); // read this entry
41
      if (id_string == NULL)
42
      {
43
         MessageBox (NULL, L"There is an inconsistency in Chess Giants's language file.\nThe game cannot start.\n\nPlease reinstall this program to fix the problem.", L"Chess Giants", MB_ICONERROR | MB_OK);
44
         return (false); // consistency check
45
      }
46
 
47
      text->id_string = _wcsdup (id_string); // have a copy of ID string and save it
48
      text->localized_string = (wchar_t *) SAFE_malloc (65536, sizeof (wchar_t), false); // allocate big space for localized string...
49
      wcscpy_s (text->localized_string, 65536, INIFile_ReadEntryAsString (inifile, L"texts", text->id_string, text->id_string)); // ... read it...
50
      text->localized_string = (wchar_t *) SAFE_realloc (text->localized_string, 65536, wcslen (text->localized_string) + 1, sizeof (wchar_t), false); // and resize correctly
51
 
52
      // conventionally, all texts whose ID end in "FileFilter" are file filters. Convert them for Windows API.
11 pmbaty 53
      // Reminder: INI file key entries are LOWERCASE.
54
      if (wcsistr (text->id_string, L"FileFilter") != NULL)
1 pmbaty 55
      {
56
         filter_char = text->localized_string; // convert vertical bars into string terminator characters
57
         while (*filter_char != 0) { if (*filter_char == L'|') *filter_char = 0; filter_char++; }
58
      }
59
   }
60
 
61
   // close the INI file
62
   INIFile_FreeINIFile (inifile);
63
   return (true); // finished loading texts
64
}
65
 
66
 
67
void LocalizedTexts_Shutdown (void)
68
{
69
   // this function unloads all the contents of the localized texts array, freeing
70
   // the associated memory space, and resets the text_count variable to zero.
71
 
72
   int text_index;
73
 
74
   // cycle through all the localized texts and free their mallocated space
75
   for (text_index = 0; text_index < text_count; text_index++)
76
   {
77
      SAFE_free ((void **) &texts[text_index].id_string);
78
      SAFE_free ((void **) &texts[text_index].localized_string);
79
   }
80
 
81
   text_count = 0; // reset the localized texts count
82
   return; // finished unloading texts
83
}
84
 
85
 
86
wchar_t *LocalizedTexts_GetLocalizedTextFor (wchar_t *id_string)
87
{
88
   // this function returns the localized text corresponding to a given id string
89
 
90
   int text_index;
91
 
92
   // cycle through all the localized texts and find the one we want
93
   // NOTE: case-insensitive comparison mandatory, for dictionary entries are lowercase.
94
   for (text_index = 0; text_index < text_count; text_index++)
95
      if (_wcsicmp (texts[text_index].id_string, id_string) == 0)
96
         return (texts[text_index].localized_string); // when found, return a pointer to it
97
 
98
   Debug_Log (L"===WARNING: translation missing for string ID [%s]!===\n", id_string);
99
   return (id_string); // when not found, return the ID string itself
100
}