forked from theZiz/sparrow3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparrowText.c
292 lines (266 loc) · 7.35 KB
/
sparrowText.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
/* This file is part of sparrow3d.
* Sparrow3d 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.
*
* Sparrow3d 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 Foobar. If not, see <http://www.gnu.org/licenses/>
*
* For feedback and questions about my Files and Projects please mail me,
* Alexander Matthes (Ziz) , zizsdl_at_googlemail.com */
#include "sparrowText.h"
#include "sparrowFile.h"
#include <stdlib.h>
#include <string.h>
Uint16 spDefaultLanguage = SP_LANGUAGE_EN;
spBundlePointer spUberBundle = NULL;
spBundle spMainBundle = {NULL,NULL};
char spErrorTranslation[] = "No translations found";
int spLanguageCount = 0;
Uint16* spLanguageCaption = NULL;
char** spLanguageName = NULL;
int cycled_left_shift(int x,int s)
{
int shift = x << s;
return ((shift) & 255) | ((shift >> 8) & 255);
}
int hash_function(const char* text)
{
int i;
int hash = 0;
for (i = 0; text[i]; i++)
{
hash = hash ^ cycled_left_shift(text[i], i & 7 );
}
return hash;
}
PREFIX spTextPointer spCreateText(const char* caption,spBundlePointer bundle)
{
spTextPointer text = (spTextPointer) malloc(sizeof(spText));
text->caption = (char*)malloc(strlen(caption)+1);
sprintf(text->caption,"%s",caption);
text->firstTranslation = NULL;
text->bundle = NULL;
text->hash = hash_function(caption);
spChangeBundle(text,bundle);
return text;
}
PREFIX void spAddTranslation(spTextPointer text,Uint16 language,const char* translation)
{
spTranslationPointer result = (spTranslationPointer)malloc(sizeof(spTranslation));
result->text = (char*)malloc(strlen(translation)+1);
sprintf(result->text,"%s",translation);
result->language = language;
result->next = text->firstTranslation;
text->firstTranslation = result;
}
PREFIX spTextPointer spCreateTextWithTranslation(const char* caption,spBundlePointer bundle,Uint16 language,const char* translation)
{
spTextPointer text = spCreateText(caption,bundle);
spAddTranslation(text,language,translation);
return text;
}
PREFIX spTextPointer spSearchCaption(spBundlePointer bundle, char* caption)
{
if (bundle == NULL)
bundle = &spMainBundle;
spTextPointer text = bundle->firstText;
while (text)
{
if (strcmp(text->caption,caption) == 0)
return text;
text = text->next;
}
return NULL;
}
PREFIX char* spGetTranslationFromCaption(spBundlePointer bundle, char* caption)
{
return spGetTranslation(spSearchCaption(bundle,caption));
}
PREFIX char* spGetTranslation(spTextPointer text)
{
if (text == NULL)
return spErrorTranslation;
spTranslationPointer translation = text->firstTranslation;
spTranslationPointer lastTranslation = NULL;
while (translation)
{
if (translation->language == spDefaultLanguage)
return translation->text;
lastTranslation = translation;
translation = translation->next;
}
if (lastTranslation)
return lastTranslation->text;
return spErrorTranslation;
}
PREFIX void spChangeBundle(spTextPointer text,spBundlePointer bundle)
{
if (text == NULL)
return;
if (text->bundle)
{
//Deleting from the old bundle
spTextPointer prev = text->prev;
spTextPointer next = text->next;
if (prev)
prev->next = next;
if (next)
next->prev = prev;
}
if (bundle == NULL) //Adding in the main bundle
bundle = &spMainBundle;
//putting in the new bundle at first position
text->bundle = bundle;
text->prev = NULL;
text->next = bundle->firstText;
if (bundle->firstText)
bundle->firstText->prev = text;
bundle->firstText = text;
}
PREFIX spBundlePointer spCreateTextBundle()
{
spBundlePointer bundle = (spBundlePointer)malloc(sizeof(spBundle));
bundle->next = spUberBundle;
spUberBundle = bundle;
bundle->firstText = NULL;
return bundle;
}
PREFIX void spSetDefaultLanguage(Uint16 language)
{
spDefaultLanguage = language;
}
PREFIX void spDeleteBundle(spBundlePointer bundle,int keepText)
{
if (bundle == NULL)
bundle = &spMainBundle;
if (bundle == &spMainBundle && keepText) //The mainBundle is undestroyable!
return;
spTextPointer text = bundle->firstText;
if (keepText)
{
while (text)
{
spTextPointer next = text->next;
spChangeBundle(text,NULL);
text = next;
}
}
else
{
while (text)
{
spTextPointer next = text->next;
spDeleteText(text);
text = next;
}
}
if (bundle != &spMainBundle)
free(bundle);
}
PREFIX void spDeleteText(spTextPointer text)
{
free(text->caption);
while (text->firstTranslation)
{
spTranslationPointer next = text->firstTranslation->next;
free(text->firstTranslation->text);
free(text->firstTranslation);
text->firstTranslation = next;
}
//Deleting from the bundle
spTextPointer prev = text->prev;
spTextPointer next = text->next;
if (prev)
prev->next = next;
if (next)
next->prev = prev;
free(text);
}
PREFIX spBundlePointer spLoadBundle(const char* filename,int own_bundle)
{
spBundlePointer bundle = &spMainBundle;
if (own_bundle)
bundle = spCreateTextBundle();
SDL_RWops *file = SDL_RWFromFile(filename, "rt");
if (!file)
return bundle;
//Loading the file line by line
int end = 0;
spTextPointer text = NULL;
while (!end)
{
char line[SP_TEXT_MAX_READABLE_LINE];
end = spReadOneLine(file,line,SP_TEXT_MAX_READABLE_LINE);
//parsing the line
if (line[0] == '-' && line[1] == '-' && line[2] != 0) //new text
{
char* caption = &(line[3]);
text = spCreateText(caption,bundle);
}
else
if (line[0] == '#' || line[0] == ' ' || line[0] == '\n' || line[0] == 0) {} //comment, empty line
else
if (line[0] != 0 && line[1] != 0 && line[2] != 0) //new translation
{
if (text == NULL)
{
printf("Error: no new text (caption) defined!\n");
break;
}
Uint16 language = line[0]*256+line[1];
char* translation = &(line[3]);
spAddTranslation(text,language,translation);
}
}
SDL_RWclose(file);
return bundle;
}
PREFIX void spReadPossibleLanguages(const char* filename)
{
spBundlePointer tempBundle = spLoadBundle(filename,1);
if (tempBundle == NULL || tempBundle->firstText == NULL || tempBundle->firstText->firstTranslation == NULL)
return;
//Reading the languages and the names
spLanguageCount = 0;
spTranslationPointer translation = tempBundle->firstText->firstTranslation;
while (translation)
{
spLanguageCount++;
translation = translation->next;
}
spLanguageCaption = (Uint16*)malloc(sizeof(Uint16)*spLanguageCount);
spLanguageName = (char**)malloc(sizeof(char*)*spLanguageCount);
translation = tempBundle->firstText->firstTranslation;
int i;
for (i = 0; i < spLanguageCount; i++)
{
spLanguageCaption[i] = translation->language;
spLanguageName[i] = (char*)malloc(strlen(translation->text));
sprintf(spLanguageName[i],"%s",translation->text);
translation = translation->next;
}
spDeleteBundle(tempBundle,0);
}
PREFIX int spGetPossibleLanguagesCount()
{
return spLanguageCount;
}
PREFIX Uint16 spGetPossibleLanguage(int nr)
{
if (nr >=0 && nr < spLanguageCount)
return spLanguageCaption[nr];
return 0;
}
PREFIX char* spGetPossibleLanguageName(int nr)
{
if (nr >=0 && nr < spLanguageCount)
return spLanguageName[nr];
return 0;
}