Changeset - r18716:adddcf514d59
[Not reviewed]
master
0 1 0
rubidium - 12 years ago 2011-12-17 17:03:38
rubidium@openttd.org
(svn r23574) -Codechange/Feature-ish: allow converting multiple translations with the same master language instance in a single strgen run
1 file changed with 43 insertions and 21 deletions:
0 comments (0 inline, 0 general)
src/strgen/strgen.cpp
Show inline comments
 
@@ -102,12 +102,22 @@ struct LangString {
 
	{
 
		free(this->name);
 
		free(this->english);
 
		free(this->translated);
 
		delete this->translated_case;
 
	}
 

	
 
	/** Free all data related to the translation. */
 
	void FreeTranslation()
 
	{
 
		free(this->translated);
 
		this->translated = NULL;
 

	
 
		delete this->translated_case;
 
		this->translated_case = NULL;
 
	}
 
};
 

	
 
/** Information about the currently known strings. */
 
struct StringData {
 
	static const uint STRINGS_IN_TAB = 2048;
 

	
 
@@ -133,12 +143,21 @@ struct StringData {
 
	{
 
		for (size_t i = 0; i < this->max_strings; i++) delete this->strings[i];
 
		free(this->strings);
 
		free(this->hash_heads);
 
	}
 

	
 
	/** Free all data related to the translation. */
 
	void FreeTranslation()
 
	{
 
		for (size_t i = 0; i < this->max_strings; i++) {
 
			LangString *ls = this->strings[i];
 
			if (ls != NULL) ls->FreeTranslation();
 
		}
 
	}
 

	
 
	/**
 
	 * Create a hash of the string for finding them back quickly.
 
	 * @param s The string to hash.
 
	 * @return The hashed string.
 
	 */
 
	uint HashStr(const char *s) const
 
@@ -1014,12 +1033,13 @@ static void rstrip(char *buf)
 
	buf[i] = '\0';
 
}
 

	
 
void StringReader::ParseFile()
 
{
 
	char buf[2048];
 
	_warnings = _errors = 0;
 

	
 
	_translation = this->master || this->translation;
 
	_file = this->file;
 

	
 
	/* For each new file we parse, reset the genders, and language codes. */
 
	MemSetT(&_lang, 0);
 
@@ -1618,47 +1638,49 @@ int CDECL main(int argc, char *argv[])
 
			ottd_mkdir(dest_dir);
 
			mkpath(pathbuf, lengthof(pathbuf), dest_dir, "strings.h");
 

	
 
			HeaderFileWriter writer(pathbuf);
 
			writer.WriteHeader(data);
 
			writer.Finalise(data);
 
		} else if (mgo.numleft == 1) {
 
		} else if (mgo.numleft >= 1) {
 
			char *r;
 

	
 
			mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt");
 

	
 
			StringData data;
 
			/* parse master file and check if target file is correct */
 
			FileStringReader master_reader(data, pathbuf, true, false);
 
			master_reader.ParseFile();
 

	
 
			const char *translation = replace_pathsep(mgo.argv[0]);
 
			const char *file = strrchr(translation, PATHSEPCHAR);
 
			FileStringReader translation_reader(data, translation, false, file == NULL || strcmp(file + 1, "english.txt") != 0);
 
			translation_reader.ParseFile(); // target file
 
			if (_errors != 0) return 1;
 
			for (int i = 0; i < mgo.numleft; i++) {
 
				data.FreeTranslation();
 

	
 
			/* get the targetfile, strip any directories and append to destination path */
 
			r = strrchr(mgo.argv[0], PATHSEPCHAR);
 
			mkpath(pathbuf, lengthof(pathbuf), dest_dir, (r != NULL) ? &r[1] : mgo.argv[0]);
 
				const char *translation = replace_pathsep(mgo.argv[i]);
 
				const char *file = strrchr(translation, PATHSEPCHAR);
 
				FileStringReader translation_reader(data, translation, false, file == NULL || strcmp(file + 1, "english.txt") != 0);
 
				translation_reader.ParseFile(); // target file
 
				if (_errors != 0) return 1;
 

	
 
				/* get the targetfile, strip any directories and append to destination path */
 
				r = strrchr(mgo.argv[i], PATHSEPCHAR);
 
				mkpath(pathbuf, lengthof(pathbuf), dest_dir, (r != NULL) ? &r[1] : mgo.argv[i]);
 

	
 
			/* rename the .txt (input-extension) to .lng */
 
			r = strrchr(pathbuf, '.');
 
			if (r == NULL || strcmp(r, ".txt") != 0) r = strchr(pathbuf, '\0');
 
			ttd_strlcpy(r, ".lng", (size_t)(r - pathbuf));
 
				/* rename the .txt (input-extension) to .lng */
 
				r = strrchr(pathbuf, '.');
 
				if (r == NULL || strcmp(r, ".txt") != 0) r = strchr(pathbuf, '\0');
 
				ttd_strlcpy(r, ".lng", (size_t)(r - pathbuf));
 

	
 
			LanguageFileWriter writer(pathbuf);
 
			writer.WriteLang(data);
 
			writer.Finalise();
 
				LanguageFileWriter writer(pathbuf);
 
				writer.WriteLang(data);
 
				writer.Finalise();
 

	
 
			/* if showing warnings, print a summary of the language */
 
			if ((_show_todo & 2) != 0) {
 
				fprintf(stdout, "%d warnings and %d errors for %s\n", _warnings, _errors, pathbuf);
 
				/* if showing warnings, print a summary of the language */
 
				if ((_show_todo & 2) != 0) {
 
					fprintf(stdout, "%d warnings and %d errors for %s\n", _warnings, _errors, pathbuf);
 
				}
 
			}
 
		} else {
 
			fprintf(stderr, "Invalid arguments\n");
 
		}
 
	} catch (...) {
 
		return 2;
 
	}
 

	
 
	return 0;
0 comments (0 inline, 0 general)