File: translator.cpp

package info (click to toggle)
sludge 2.2.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,852 kB
  • sloc: cpp: 32,432; sh: 1,237; makefile: 634; xml: 284
file content (359 lines) | stat: -rw-r--r-- 8,498 bytes parent folder | download | duplicates (7)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 *  Translator.cpp
 *  Sludge Dev Kit
 *
 *  Created by Rikard Peterson on 2009-08-06.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>

#include "interface.h"
#include "moreio.h"
#include "helpers.h"
#include "settings.h"
#include "stringarray.h"

#include "translator.h"

#define HEADERLINE	"### SLUDGE Translation File ###"

enum theMode {MODE_ID, MODE_NAME, MODE_STRINGS, MODE_UNKNOWN};

stringArray * splitLangString (const char * inString, const char findCharIn, const splitMode howMany) {
	stringArray * newStringArray = NULL;
	int a, stringLen = strlen (inString), lastStart = 0;
	char findChar = findCharIn;
	
	for (a = 0; a < stringLen; a ++) {
		if (inString[a] == findChar) {
			addToStringArray (newStringArray, inString, lastStart, a, false);
			lastStart = a + 1;
			if (howMany == ONCE) findChar = 0;
		}
	}
	addToStringArray (newStringArray, inString, lastStart, stringLen, false);
	
	return newStringArray;
}


void newFile (transLine ** firstTransLine) {
	transLine * selectedTransLine;
	while (*firstTransLine) {
		selectedTransLine = *firstTransLine;
		*firstTransLine = (*firstTransLine) -> next;
		
		delete selectedTransLine -> transFrom;
		delete selectedTransLine -> transTo;
		delete selectedTransLine;
	}
}


transLine * addLine (char * line, transLine * lastSoFar) {
	stringArray * pair = splitLangString (line, '\t', ONCE);
	
	transLine * nt = new transLine;
		
	trimEdgeSpace (pair->string);
	nt -> transFrom = copyString (pair->string);
	destroyFirst (pair);
	if (pair) {
		trimEdgeSpace (pair->string);
		if (strcmp (pair -> string, "*\t")) {
			nt -> transTo = copyString (pair -> string);
			nt -> type = TYPE_TRANS;
		} else {
			nt -> transTo = NULL;
			nt -> type = TYPE_NEW;
		}
		destroyFirst (pair);
	} else {
		nt -> transTo = NULL;
		nt -> type = TYPE_NONE;
	}
	
	if (lastSoFar) {
		lastSoFar -> next = nt;
	}
	
	nt -> next = NULL;		
	return nt;
}

bool loadTranslationFile (char * fileIn, transLine ** firstTransLine, char **langName, unsigned int *lanID) {
	char * file = copyString (fileIn);
	const char * error = NULL;
	transLine * lastSoFar = NULL;
	newFile (firstTransLine);
	
	FILE * fp = fopen (file, "rt");
	if (fp == NULL) {
		error = "Can't open file for reading";
	} else {
		char * line = readText (fp);
		if (line == NULL) {
			error = "File is empty";
		} else if (strcmp (line, HEADERLINE)) {
			error = "Not a SLUDGE translation file (first line isn't right)";
		} else {
			theMode mode = MODE_UNKNOWN;
			for (;;) {
				delete line;
				line = readText (fp);
				if (line == NULL) break;
				switch (line[0]) {
					case NULL:
						break;
						
					case '[':
						if (strcmp (line, "[ID]") == 0)
							mode = MODE_ID;
						else if (strcmp (line, "[DATA]") == 0)
							mode = MODE_STRINGS;
						else if (strcmp (line, "[NAME]") == 0)
							mode = MODE_NAME;
						else
							mode = MODE_UNKNOWN;
						break;
						
					default:
						switch (mode) {
							case MODE_ID:
							{
								long id = strtol(line, NULL, 10); //stringToInt (line);
								if (id > 0) *lanID = id;
								break;
							}
							case MODE_NAME:
								if (strlen(line) && !(*langName)) {
									(*langName) =  copyString (line);
								}
								break;
								
							case MODE_STRINGS:
								lastSoFar = addLine (line, lastSoFar);
								if (!(*firstTransLine))
									*firstTransLine = lastSoFar;
								break;
						}
				}
			}
		}
		delete line;
	}
	fclose (fp);
	if (error) {
		errorBox (error, file);
	}
	delete file;
	return (error == 0);
}

bool saveTranslationFile (const char * filename, transLine * firstTransLine, const char *langName, unsigned int lan) {
	
	FILE * fp = fopen (filename, "wt");
	
	if (! fp) {
		return false;
	}
	
	fprintf (fp, HEADERLINE"\n\n[ID]\n%i\n\n[NAME]\n%s\n\n[DATA]\n", lan, langName);
	transLine * eachLine = firstTransLine;
	
	while (eachLine) {
		switch (eachLine -> type) {
			case TYPE_NEW:
				fprintf (fp, "%s\t*\t\n", eachLine -> transFrom);
				break;
				
			case TYPE_TRANS:
				fprintf (fp, "%s\t%s\n", eachLine -> transFrom, eachLine -> transTo);
				break;
				
			default:
				fprintf (fp, "%s\n", eachLine -> transFrom);
				break;
		}
		eachLine = eachLine -> next;
	}
	
	fclose (fp);	
	return true;
}


int foundStringInFileDel (char * string, transLine ** firstTransLine) {
	transLine * hunt = *firstTransLine;
	transLine * lastOne = NULL;
	
	trimEdgeSpace (string);
	
	while (hunt) {
		if (strcmp (hunt -> transFrom, string) == 0) {
			hunt -> exists = 1;
			return 0;
		}
		lastOne = hunt;
		hunt = hunt -> next;
	}
	//	errorBox ("Found NEW string", string);
	hunt = addLine(string, lastOne);
	if (!(*firstTransLine))
		*firstTransLine = hunt;
	
	hunt->type = TYPE_NEW;
	hunt->exists = 1;
	delete string;
	return 1;
}


int foundStringInFileEscaped (char * string, transLine **firstTransLine) {
	stringArray * bits = splitLangString (string, '\t', REPEAT);
	char * rebuilt = copyString ("");
	while (bits) {
		char * temp = joinStrings (rebuilt, bits->string);
		delete rebuilt;
		rebuilt = temp;
		destroyFirst (bits);
	}
	return foundStringInFileDel(rebuilt, firstTransLine);
}



int updateFromSource (char * filename, transLine **firstTransLine) {
	int len = strlen (filename);
	if (len < 4) return 0;

	char * last4 = filename + len - 4;
	if (strcmp (last4, ".slu")) return 0;
	
	fixPath (filename, true);
	FILE * source = fopen (filename, "rt");
	if (source == NULL) {
		errorBox ("Can't open source file for reading", filename);
		return 0;
	}
	
	int numChanges = 0;
	
	for (;;) {
		char * wholeLine = readText(source);
		if (wholeLine == NULL) break;
		for (int a = 0; wholeLine[a]; a ++) {
			if (wholeLine[a] == '#') break;	// Comment? Skip it!
			if (wholeLine[a] == '\"') {
				while (wholeLine[a+1] == ' ') a ++;	// No spaces at start, please
				bool escape = false;
				for (int b = a+1; wholeLine[b]; b ++) {
					if (wholeLine[b] == '\\') {
						if (! escape) wholeLine[b] = '\t';		// So we can split the string up on tab later
						escape = ! escape;
					} else if (wholeLine[b] == '\"') {
						if (! escape) {
							if (b != a + 1) {
								wholeLine[b] = 0;
								numChanges += foundStringInFileEscaped (wholeLine + a + 1, firstTransLine);
								wholeLine[b] = '\"';
							}
							a = b;
							break;
						}
						escape = false;
					} else {
						escape = false;
					}
				}
			}
		}
		delete wholeLine;
	}
	
	fclose (source);
	return numChanges;
}

bool updateFromProject (const char * filename, transLine **firstTransLine) {
	getSourceDirFromName (filename);
	gotoSourceDirectory ();	

	FILE * fp = fopen (filename, "rt");
	int totalNew = 0;
	int totalOld = 0;
	if (fp) {
		transLine * hunt = *firstTransLine;
		
		while (hunt) {
			hunt -> exists = 0;
			hunt = hunt -> next;
		}		
		
		char * theLine;
		for (;;) {
			theLine = readText (fp);
			if (theLine == NULL) break;
			if (strcmp (theLine, "[FILES]") == 0) break;
			stringArray * bits = splitLangString (theLine, '=', ONCE);
			if (bits -> next != NULL) {
				if (strcmp (bits -> string, "windowname") == 0 ||
					strcmp (bits -> string, "quitmessage") == 0) {
					totalNew += foundStringInFileDel (copyString (bits -> next -> string), firstTransLine);
				}
			}
			delete theLine;
		}
		if (theLine == NULL) {
			fclose (fp);
			errorBox ("Not a SLUDGE project file", filename);
			return false;
		}
		while (theLine) {
			delete theLine;
			theLine = readText (fp);
			if (theLine) totalNew += updateFromSource (theLine, firstTransLine);
		}
		
		hunt = *firstTransLine;
		transLine *prev = NULL;
		while (hunt) {
			if (! hunt -> exists) {
				//fprintf(stderr, "Removing string: %s\n", hunt->transFrom);
				if (prev) {
					prev->next = hunt->next;
					
					delete hunt -> transFrom;
					delete hunt -> transTo;
					delete hunt;
					hunt = prev;
				} else {
					*firstTransLine = (*firstTransLine) -> next;

					delete hunt -> transFrom;
					delete hunt -> transTo;
					delete hunt;
					hunt = *firstTransLine;					
				}
				totalOld++;
			}
			if (hunt) {
				prev = hunt;
				hunt = hunt -> next;
			}
		}		
		
	}
	if (totalOld) {
		errorBox ("Warning.", "I found unused strings in the translation file. The unused strings are removed.");
	}	else if (! totalNew) {
		errorBox ("Found no new strings in the project that I don't already know about.", "This translation file is up to date! Hooray!");
		return false;
	}
	return true;
}