-
Notifications
You must be signed in to change notification settings - Fork 8
/
slang.c
309 lines (278 loc) · 7.42 KB
/
slang.c
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright (C) 2000,2001 Florian Sander
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
static struct slang_header *slang_find(struct slang_header *, char *);
#include "slang_text.c"
#include "slang_multitext.c"
#include "slang_ids.c"
#ifndef SLANG_NOTYPES
#include "slang_types.c"
#endif
#include "slang_duration.c"
#ifndef SLANG_NOFACTS
#include "slang_facts_places.c"
#include "slang_facts.c"
#endif
#include "slang_chanlang.c"
struct slang_header {
struct slang_header *next;
char *lang;
char *desc;
struct slang_id *ids;
#ifndef SLANG_NOTYPES
struct slang_type *types;
#endif
struct slang_duration *durations;
};
static void slang_glob_init()
{
glob_slang_cmd_list = NULL;
}
static int slang_glob_expmem()
{
return slang_commands_list_expmem(glob_slang_cmd_list);
}
static void slang_glob_free()
{
slang_commands_list_free(glob_slang_cmd_list);
glob_slang_cmd_list = NULL;
}
static struct slang_header *slang_create(struct slang_header *list, char *lang, char *desc)
{
struct slang_header *nslang, *l;
Assert(lang);
debug2("Creating language '%s' starting by %" PRIdPTR, lang, list);
for (nslang = list; nslang; nslang = nslang->next)
if (!strcasecmp(nslang->lang, lang))
return list;
nslang = nmalloc(sizeof(struct slang_header));
nslang->next = NULL;
nslang->desc = NULL;
nslang->lang = nmalloc(strlen(lang) + 1);
strcpy(nslang->lang, lang);
nslang->desc = nmalloc(strlen(desc) + 1);
strcpy(nslang->desc, desc);
nslang->ids = NULL;
#ifndef SLANG_NOTYPES
nslang->types = NULL;
#endif
nslang->durations = NULL;
for (l = list; l && l->next; l = l->next);
if (l)
l->next = nslang;
else {
Assert(!list);
list = nslang;
}
return list;
}
static int slang_expmem(struct slang_header *what)
{
int size = 0;
while (what) {
size += sizeof(struct slang_header);
size += strlen(what->lang) + 1;
size += strlen(what->desc) + 1;
size += slang_id_expmem(what->ids);
#ifndef SLANG_NOTYPES
size += slang_type_expmem(what->types);
#endif
size += slang_duration_expmem(what->durations);
what = what->next;
}
return size;
}
static void slang_free(struct slang_header *what)
{
struct slang_header *next;
while (what) {
next = what->next;
slang_id_free(what->ids);
#ifndef SLANG_NOTYPES
slang_type_free(what->types);
#endif
slang_duration_free(what->durations);
nfree(what->lang);
nfree(what->desc);
nfree(what);
what = next;
}
}
static int slang_load(struct slang_header *slang, char *filename)
{
FILE *f;
char *buffer, *s;
char *cmd, *sid, *strtol_ret;
#ifndef SLANG_NOTYPES
char *type;
#endif
int line, id;
Assert(slang);
putlog(LOG_MISC, "*", "Loading language \"%s\" from %s...", slang->lang, filename);
f = fopen(filename, "r");
if (!f) {
putlog(LOG_MISC, "*", "Couldn't open slangfile \"%s\"!", filename);
return 0;
}
buffer = nmalloc(2000);
line = 0;
while (!feof(f)) {
s = buffer;
if (fgets(s, 2000, f)) {
line++;
// at first, kill those stupid line feeds and carriage returns...
if (s[strlen(s) - 1] == '\n')
s[strlen(s) - 1] = 0;
if (s[strlen(s) - 1] == '\r')
s[strlen(s) - 1] = 0;
if (!s[0])
continue;
cmd = newsplit(&s);
if (!strcasecmp(cmd, "T")) {
#ifndef SLANG_NOTYPES
type = newsplit(&s);
slang->types = slang_type_add(slang->types, type, s);
#endif
} else if (!strcasecmp(cmd, "D")) {
sid = newsplit(&s);
id = strtol(sid, &strtol_ret, 10);
if (strtol_ret == sid) {
putlog(LOG_MISC, "*", "ERROR in slangfile \"%s\", line %d: %s is not a valid "
"duration index!", filename, line, sid);
continue;
}
slang->durations = slang_duration_add(slang->durations, id, s);
} else {
id = strtol(cmd, &strtol_ret, 10);
if (strtol_ret == cmd)
continue;
slang->ids = slang_id_add(slang->ids, id, s);
}
}
}
fclose(f);
nfree(buffer);
return 1;
}
static struct slang_header *slang_find(struct slang_header *where, char *language)
{
struct slang_header *slang = NULL;
// at first, search for the specified language
for (slang = where; slang; slang = slang->next)
if (!strcasecmp(slang->lang, language))
return slang;
// oops... language seems to be invalid. Let's find the default.
Assert(default_slang != NULL);
for (slang = where; slang; slang = slang->next)
if (!strcasecmp(slang->lang, default_slang))
return slang;
// default_slang wasn't found either? *sigh*
// Let's return the first known language then.
return where;
}
#ifndef SLANG_NOVALIDATE
/* slang_valid():
* check if the given language is a valid one
*/
static int slang_valid(struct slang_header *where, char *language)
{
struct slang_header *slang = NULL;
for (slang = where; slang; slang = slang->next)
if (!strcasecmp(slang->lang, language))
return 1;
return 0;
}
#endif
static char getslang_error[17];
static char *getslang(int id)
{
char *text;
if (!glob_slang) {
putlog(LOG_MISC, "*", "WARNING! No language selected! (getslang())");
return "NOLANG";
}
text = slang_id_get(glob_slang->ids, id);
if (!text) {
snprintf(getslang_error, sizeof(getslang_error), "SLANG%d", id);
return getslang_error;
}
return text;
}
static char *getdur(int idx)
{
char *text;
Assert((idx >= 0) && (idx < DURATIONS));
if (!glob_slang) {
putlog(LOG_MISC, "*", "WARNING! No language selected! (getdur())");
return "NOLANG";
}
text = slang_duration_get(glob_slang->durations, idx);
if (!text) {
snprintf(getslang_error, sizeof(getslang_error), "DUR%d", idx);
return getslang_error;
}
return text;
}
#ifndef SLANG_NOTYPES
static char *getslangtype(char *type)
{
char *stype;
if (!glob_slang) {
putlog(LOG_MISC, "*", "WARNING! No language selected! (getslangtype())");
return "NOLANG";
}
stype = slang_type_get(glob_slang->types, type);
if (stype)
return stype;
else
return type;
}
static int slangtypetoi(char *slangtype)
{
char *type;
if (!glob_slang) {
putlog(LOG_MISC, "*", "WARNING! No language selected! (slangtypetoi())");
return T_ERROR;
}
type = slang_type_slang2type(glob_slang->types, slangtype);
if (type) {
debug1("type: %s", type);
return typetoi(type);
} else
return typetoi(slangtype);
}
#endif
#ifndef SLANG_NOGETALL
static char *getslang_first(int id)
{
char *text;
if (!glob_slang) {
putlog(LOG_MISC, "*", "WARNING! No language selected! (getslang())");
return "NOLANG";
}
text = slang_id_get_first(glob_slang->ids, id);
if (!text) {
snprintf(getslang_error, sizeof(getslang_error), "SLANG%d", id);
return getslang_error;
}
return text;
}
static char *getslang_next()
{
return slang_id_get_next();
}
#endif