forked from pioneerspacesim/pioneer
/
Lang.cpp
240 lines (183 loc) · 5.6 KB
/
Lang.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "libs.h"
#include <map>
#define MAX_STRING (1024)
namespace Lang {
typedef std::map<std::string,char*> token_map;
static token_map s_tokens;
}
#define DECLARE_STRING(x) \
char x[MAX_STRING]; \
static class _init_class_##x { \
public: \
_init_class_##x() { \
s_tokens.insert(std::make_pair(#x,x)); \
*x = '\0'; \
} \
} _init_##x
#include "Lang.h"
namespace Lang {
static bool _read_pair(FILE *f, const std::string &filename, int *lineno, token_map::iterator *outIter, char **outValue)
{
static char buf[1024];
*outValue = buf;
bool doing_token = true;
errno = 0;
while (fgets(buf, sizeof(buf), f)) {
(*lineno)++;
char *line = buf;
int i = 0;
while (buf[i]) {
Uint32 chr;
int n = conv_mb_to_wc(&chr, &buf[i]);
if (!n) {
fprintf(stderr, "invalid utf-8 character in line %d of '%s'\n", *lineno, filename.c_str());
return false;
}
i += n;
}
// eat leading whitespace
while (*line == ' ' || *line == '\t' || *line == '\r' || *line == '\n') line++;
// skip empty lines
if (!*line) continue;
// skip comment lines
if (*line == '#') continue;
// eat trailing whitespace
char *end = strchr(line, '\0');
while (*(end-1) == ' ' || *(end-1) == '\t' || *(end-1) == '\r' || *(end-1) == '\n') end--;
*end = '\0';
// skip empty lines
if (line == end) continue;
// token line
if (doing_token) {
for (char *c = line; c < end; c++) {
// valid token chars are A-Z0-9_
if (!((*c >= 'A' && *c <= 'Z') || (*c >= '0' && *c <= '9') || (*c == '_'))) {
fprintf(stderr, "unexpected token character '%c' in line %d of '%s'\n", *c, *lineno, filename.c_str());
return false;
}
}
*outIter = s_tokens.find(std::string(line));
if ((*outIter) == s_tokens.end()) {
fprintf(stderr, "unknown token '%s' at line %d of '%s'\n", line, *lineno, filename.c_str());
return false;
}
}
// string line
else {
// eat quotes
if (*line == '"' && *(end-1) == '"') {
line++;
end--;
*end = '\0';
}
// skip empty lines
if (line == end) continue;
*outValue = line;
}
if (doing_token)
doing_token = false;
else
break;
}
if (errno) {
fprintf(stderr, "error reading string file '%s': %s\n", filename.c_str(), strerror(errno));
return false;
}
return true;
}
static void _copy_string(const char *src, char *dest)
{
// copy in one char at a time
int s = 0, d = 0;
while (src[s] != '\0' && d < MAX_STRING-1) {
// turn \n into a real newline
if (src[s] == '\\' && src[s+1] == 'n') {
dest[d++] = '\n';
s += 2;
continue;
}
dest[d++] = src[s++];
}
dest[d] = '\0';
}
bool LoadStrings(const std::string &lang)
{
for (token_map::iterator i = s_tokens.begin(); i != s_tokens.end(); i++)
*((*i).second) = '\0';
std::map<std::string,bool> seen, missing;
token_map::iterator token_iter;
char *value;
std::string filename(PIONEER_DATA_DIR "/lang/English.txt");
FILE *f = fopen(filename.c_str(), "r");
if (!f) {
fprintf(stderr, "couldn't open string file '%s': %s\n", filename.c_str(), strerror(errno));
return false;
}
int lineno = 0;
while (!feof(f)) {
bool success = _read_pair(f, filename, &lineno, &token_iter, &value);
if (!success)
return false;
_copy_string(value, (*token_iter).second);
seen.insert(std::make_pair((*token_iter).first, true));
}
fclose(f);
if (seen.size() != s_tokens.size()) {
fprintf(stderr, "string file '%s' has missing tokens:\n", filename.c_str());
for (token_map::iterator i = s_tokens.begin(); i != s_tokens.end(); i++) {
if (seen.find((*i).first) == seen.end()) {
fprintf(stderr, " %s\n", (*i).first.c_str());
missing.insert(std::make_pair((*i).first, false));
}
}
}
if (lang == "English")
return seen.size() == s_tokens.size();
seen.clear();
filename = std::string(PIONEER_DATA_DIR "/lang/" + lang + ".txt");
f = fopen(filename.c_str(), "r");
if (!f) {
fprintf(stderr, "couldn't open string file '%s': %s\n", filename.c_str(), strerror(errno));
return false;
}
lineno = 0;
while (!feof(f)) {
bool success = _read_pair(f, filename, &lineno, &token_iter, &value);
if (!success)
return false;
_copy_string(value, (*token_iter).second);
seen.insert(std::make_pair((*token_iter).first, true));
std::map<std::string,bool>::iterator i = missing.find((*token_iter).first);
if (i != missing.end())
missing.erase(i);
}
fclose(f);
if (seen.size() != s_tokens.size()) {
fprintf(stderr, "string file '%s' has missing tokens:\n", filename.c_str());
for (token_map::iterator i = s_tokens.begin(); i != s_tokens.end(); i++) {
if (seen.find((*i).first) == seen.end())
fprintf(stderr, " %s\n", (*i).first.c_str());
}
}
if (missing.size() > 0) {
fprintf(stderr, "no strings found for the following tokens:\n");
for (std::map<std::string,bool>::iterator i = missing.begin(); i != missing.end(); i++)
fprintf(stderr, " %s\n", (*i).first.c_str());
return false;
}
return true;
}
std::list<std::string> s_availableLanguages;
void _found_language_file_callback(const std::string &name, const std::string &fullname) {
size_t pos = name.find(".txt");
if (pos == name.npos) return;
if (name.length() - pos != 4) return;
s_availableLanguages.push_back(name.substr(0, pos));
}
const std::list<std::string> &GetAvailableLanguages()
{
s_availableLanguages.clear();
foreach_file_in(PIONEER_DATA_DIR "/lang/", _found_language_file_callback);
return s_availableLanguages;
}
}