Changeset - r14532:b3eed94f4dfd
[Not reviewed]
master
0 1 0
rubidium - 14 years ago 2010-02-12 23:45:25
rubidium@openttd.org
(svn r19113) -Change: [strgen] Remove the partially generated language file if compilation fails.
1 file changed with 42 insertions and 28 deletions:
0 comments (0 inline, 0 general)
src/strgen/strgen.cpp
Show inline comments
 
@@ -45,11 +45,13 @@ struct Case {
 
	Case *next;
 
};
 

	
 
static bool _masterlang;
 
static bool _translated;
 
static bool _translation; ///< Is the current file actually a translation or not
 
static const char *_file = "(unknown file)";
 
static int _cur_line;
 
static bool _masterlang;                     ///< Whether we are loading the master language
 
static bool _translated;                     ///< Whether the current language is not the master language
 
static bool _translation;                    ///< Is the current file actually a translation or not
 
static const char *_file = "(unknown file)"; ///< The filename of the input, so we can refer to it in errors/warnings
 
static FILE *_output_file = NULL;            ///< The file we are currently writing output to
 
static const char *_output_filename = NULL;  ///< The filename of the output, so we can delete it if compilation fails
 
static int _cur_line;                        ///< The current line we're parsing in the input file
 
static int _errors, _warnings, _show_todo;
 

	
 
struct LangString {
 
@@ -175,6 +177,11 @@ void NORETURN CDECL error(const char *s,
 
	vsnprintf(buf, lengthof(buf), s, va);
 
	va_end(va);
 
	fprintf(stderr, "%s" LINE_NUM_FMT ": FATAL: %s\n", _file, _cur_line, buf);
 
	/* We were writing output to a file, remove it. */
 
	if (_output_file != NULL) {
 
		fclose(_output_file);
 
		unlink(_output_filename);
 
	}
 
	exit(1);
 
}
 

	
 
@@ -916,44 +923,47 @@ static void WriteStringsH(const char *fi
 
{
 
	int next = -1;
 

	
 
	FILE *out = fopen("tmp.xxx", "w");
 
	if (out == NULL) error("can't open tmp.xxx");
 
	_output_filename = "tmp.xxx";
 
	_output_file = fopen(_output_filename, "w");
 
	if (_output_file == NULL) error("can't open tmp.xxx");
 

	
 
	fprintf(out, "/* This file is automatically generated. Do not modify */\n\n");
 
	fprintf(out, "#ifndef TABLE_STRINGS_H\n");
 
	fprintf(out, "#define TABLE_STRINGS_H\n");
 
	fprintf(_output_file, "/* This file is automatically generated. Do not modify */\n\n");
 
	fprintf(_output_file, "#ifndef TABLE_STRINGS_H\n");
 
	fprintf(_output_file, "#define TABLE_STRINGS_H\n");
 

	
 
	for (int i = 0; i != lengthof(_strings); i++) {
 
		if (_strings[i] != NULL) {
 
			if (next != i) fprintf(out, "\n");
 
			fprintf(out, "static const StringID %s = 0x%X;\n", _strings[i]->name, i);
 
			if (next != i) fprintf(_output_file, "\n");
 
			fprintf(_output_file, "static const StringID %s = 0x%X;\n", _strings[i]->name, i);
 
			next = i + 1;
 
		}
 
	}
 

	
 
	fprintf(out, "\nstatic const StringID STR_LAST_STRINGID = 0x%X;\n", next - 1);
 
	fprintf(_output_file, "\nstatic const StringID STR_LAST_STRINGID = 0x%X;\n", next - 1);
 

	
 
	fprintf(out,
 
	fprintf(_output_file,
 
		"\nenum {\n"
 
		"\tLANGUAGE_PACK_IDENT = 0x474E414C, // Big Endian value for 'LANG' (LE is 0x 4C 41 4E 47)\n"
 
		"\tLANGUAGE_PACK_VERSION = 0x%X,\n"
 
		"};\n", (uint)_hash
 
	);
 

	
 
	fprintf(out, "\n#endif /* TABLE_STRINGS_H */\n");
 

	
 
	fclose(out);
 
	fprintf(_output_file, "\n#endif /* TABLE_STRINGS_H */\n");
 

	
 
	if (CompareFiles("tmp.xxx", filename)) {
 
	fclose(_output_file);
 
	_output_file = NULL;
 

	
 
	if (CompareFiles(_output_filename, filename)) {
 
		/* files are equal. tmp.xxx is not needed */
 
		unlink("tmp.xxx");
 
		unlink(_output_filename);
 
	} else {
 
		/* else rename tmp.xxx into filename */
 
#if defined(WIN32) || defined(WIN64)
 
		unlink(filename);
 
#endif
 
		if (rename("tmp.xxx", filename) == -1) error("rename() failed");
 
		if (rename(_output_filename, filename) == -1) error("rename() failed");
 
	}
 
	_output_filename = NULL;
 
}
 

	
 
static int TranslateArgumentIdx(int argidx, int offset)
 
@@ -1047,8 +1057,9 @@ static void WriteLangfile(const char *fi
 
	uint in_use[32];
 
	LanguagePackHeader hdr;
 

	
 
	FILE *f = fopen(filename, "wb");
 
	if (f == NULL) error("can't open %s", filename);
 
	_output_filename = filename;
 
	_output_file = fopen(filename, "wb");
 
	if (_output_file == NULL) error("can't open %s", filename);
 

	
 
	memset(&hdr, 0, sizeof(hdr));
 
	for (int i = 0; i != 32; i++) {
 
@@ -1072,7 +1083,7 @@ static void WriteLangfile(const char *fi
 
	strecpy(hdr.digit_group_separator_currency, _lang_digit_group_separator_currency, lastof(hdr.digit_group_separator_currency));
 
	strecpy(hdr.digit_decimal_separator, _lang_digit_decimal_separator, lastof(hdr.digit_decimal_separator));
 

	
 
	fwrite(&hdr, sizeof(hdr), 1, f);
 
	fwrite(&hdr, sizeof(hdr), 1, _output_file);
 

	
 
	for (int i = 0; i != 32; i++) {
 
		for (uint j = 0; j != in_use[i]; j++) {
 
@@ -1082,7 +1093,7 @@ static void WriteLangfile(const char *fi
 

	
 
			/* For undefined strings, just set that it's an empty string */
 
			if (ls == NULL) {
 
				WriteLength(f, 0);
 
				WriteLength(_output_file, 0);
 
				continue;
 
			}
 

	
 
@@ -1146,14 +1157,17 @@ static void WriteLangfile(const char *fi
 

	
 
			if (cmdp != NULL) PutCommandString(cmdp);
 

	
 
			WriteLength(f, _put_pos);
 
			fwrite(_put_buf, 1, _put_pos, f);
 
			WriteLength(_output_file, _put_pos);
 
			fwrite(_put_buf, 1, _put_pos, _output_file);
 
			_put_pos = 0;
 
		}
 
	}
 

	
 
	fputc(0, f);
 
	fclose(f);
 
	fputc(0, _output_file);
 
	fclose(_output_file);
 

	
 
	_output_file = NULL;
 
	_output_filename = NULL;
 
}
 

	
 
/** Multi-OS mkdirectory function */
0 comments (0 inline, 0 general)