Subversion Repositories Games.Chess Giants

Rev

Rev 1 | Rev 56 | 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
   {
30
      MessageBox (NULL, L"Chess Giants was unable to load its data files.\nThe game cannot start.\n\nPlease reinstall this program to fix the problem.", L"Chess Giants", MB_ICONERROR | MB_OK);
31
      return (false);
32
   }
33
 
34
   // read the number of known localized texts and mallocate enough space for them
35
   text_count = INIFile_GetNumberOfEntries (inifile, L"texts");
36
   texts = (text_t *) SAFE_malloc (text_count, sizeof (text_t), true);
37
 
38
   // now read all of them
39
   for (text_index = 0; text_index < text_count; text_index++)
40
   {
41
      text = &texts[text_index]; // quick access to text
42
 
43
      id_string = INIFile_GetEntryName (inifile, L"texts", text_index); // read this entry
44
      if (id_string == NULL)
45
      {
46
         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);
47
         return (false); // consistency check
48
      }
49
 
50
      text->id_string = _wcsdup (id_string); // have a copy of ID string and save it
51
      text->localized_string = (wchar_t *) SAFE_malloc (65536, sizeof (wchar_t), false); // allocate big space for localized string...
52
      wcscpy_s (text->localized_string, 65536, INIFile_ReadEntryAsString (inifile, L"texts", text->id_string, text->id_string)); // ... read it...
53
      text->localized_string = (wchar_t *) SAFE_realloc (text->localized_string, 65536, wcslen (text->localized_string) + 1, sizeof (wchar_t), false); // and resize correctly
54
 
55
      // conventionally, all texts whose ID end in "FileFilter" are file filters. Convert them for Windows API.
11 pmbaty 56
      // Reminder: INI file key entries are LOWERCASE.
57
      if (wcsistr (text->id_string, L"FileFilter") != NULL)
1 pmbaty 58
      {
59
         filter_char = text->localized_string; // convert vertical bars into string terminator characters
60
         while (*filter_char != 0) { if (*filter_char == L'|') *filter_char = 0; filter_char++; }
61
      }
62
   }
63
 
64
   // close the INI file
65
   INIFile_FreeINIFile (inifile);
66
   return (true); // finished loading texts
67
}
68
 
69
 
70
void LocalizedTexts_Shutdown (void)
71
{
72
   // this function unloads all the contents of the localized texts array, freeing
73
   // the associated memory space, and resets the text_count variable to zero.
74
 
75
   int text_index;
76
 
77
   // cycle through all the localized texts and free their mallocated space
78
   for (text_index = 0; text_index < text_count; text_index++)
79
   {
80
      SAFE_free ((void **) &texts[text_index].id_string);
81
      SAFE_free ((void **) &texts[text_index].localized_string);
82
   }
83
 
84
   text_count = 0; // reset the localized texts count
85
   return; // finished unloading texts
86
}
87
 
88
 
89
wchar_t *LocalizedTexts_GetLocalizedTextFor (wchar_t *id_string)
90
{
91
   // this function returns the localized text corresponding to a given id string
92
 
93
   int text_index;
94
 
95
   // cycle through all the localized texts and find the one we want
96
   // NOTE: case-insensitive comparison mandatory, for dictionary entries are lowercase.
97
   for (text_index = 0; text_index < text_count; text_index++)
98
      if (_wcsicmp (texts[text_index].id_string, id_string) == 0)
99
         return (texts[text_index].localized_string); // when found, return a pointer to it
100
 
101
   Debug_Log (L"===WARNING: translation missing for string ID [%s]!===\n", id_string);
102
   return (id_string); // when not found, return the ID string itself
103
}