Changeset - r18708:683e5888dbd3
[Not reviewed]
master
0 1 0
rubidium - 13 years ago 2011-12-17 12:19:22
rubidium@openttd.org
(svn r23566) -Fix (r23565): hopefully fix MSVC compilation error
1 file changed with 1 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/strgen/strgen.cpp
Show inline comments
 
@@ -885,190 +885,189 @@ bool CompareFiles(const char *n1, const 
 
	size_t l1, l2;
 
	do {
 
		char b1[4096];
 
		char b2[4096];
 
		l1 = fread(b1, 1, sizeof(b1), f1);
 
		l2 = fread(b2, 1, sizeof(b2), f2);
 

	
 
		if (l1 != l2 || memcmp(b1, b2, l1)) {
 
			fclose(f2);
 
			fclose(f1);
 
			return false;
 
		}
 
	} while (l1 != 0);
 

	
 
	fclose(f2);
 
	fclose(f1);
 
	return true;
 
}
 

	
 
/** Base class for writing data to disk. */
 
struct FileWriter {
 
	FILE *fh;             ///< The file handle we're writing to.
 
	const char *filename; ///< The file name we're writing to.
 

	
 
	/**
 
	 * Open a file to write to.
 
	 * @param filename The file to open.
 
	 */
 
	FileWriter(const char *filename)
 
	{
 
		this->filename = strdup(filename);
 
		this->fh = fopen(this->filename, "wb");
 

	
 
		if (this->fh == NULL) {
 
			error("Could not open %s", this->filename);
 
		}
 
	}
 

	
 
	/** Finalise the writing. */
 
	void Finalise()
 
	{
 
		fclose(this->fh);
 
		this->fh = NULL;
 
	}
 

	
 
	/** Make sure the file is closed. */
 
	virtual ~FileWriter()
 
	{
 
		printf("close %s %p\n", this->filename, this->fh);
 
		/* If we weren't closed an exception was thrown, so remove the termporary file. */
 
		if (fh != NULL) {
 
			fclose(this->fh);
 
			unlink(this->filename);
 
		}
 
		free(this->filename);
 
	}
 
};
 

	
 
/** Base class for writing the header. */
 
struct HeaderWriter {
 
	/**
 
	 * Write the string ID.
 
	 * @param name     The name of the string.
 
	 * @param stringid The ID of the string.
 
	 */
 
	virtual void WriteStringID(const char *name, int stringid) = 0;
 

	
 
	/**
 
	 * Finalise writing the file.
 
	 */
 
	virtual void Finalise() = 0;
 

	
 
	/** Especially destroy the subclasses. */
 
	virtual ~HeaderWriter() {};
 

	
 
	/** Write the header information. */
 
	void WriteHeader()
 
	{
 
		int last = 0;
 
		for (int i = 0; i != lengthof(_strings); i++) {
 
			if (_strings[i] != NULL) {
 
				this->WriteStringID(_strings[i]->name, i);
 
				last = i;
 
			}
 
		}
 

	
 
		this->WriteStringID("STR_LAST_STRINGID", last);
 
	}
 
};
 

	
 
struct HeaderFileWriter : HeaderWriter, FileWriter {
 
	/** The real file name we eventually want to write to. */
 
	const char *real_filename;
 
	/** The previous string ID that was printed. */
 
	int prev;
 

	
 
	/**
 
	 * Open a file to write to.
 
	 * @param filename The file to open.
 
	 */
 
	HeaderFileWriter(const char *filename) : FileWriter("tmp.xxx"),
 
		real_filename(strdup(filename)), prev(0)
 
	{
 
		fprintf(this->fh, "/* This file is automatically generated. Do not modify */\n\n");
 
		fprintf(this->fh, "#ifndef TABLE_STRINGS_H\n");
 
		fprintf(this->fh, "#define TABLE_STRINGS_H\n");
 
	}
 

	
 
	void WriteStringID(const char *name, int stringid)
 
	{
 
		if (prev + 1 != stringid) fprintf(this->fh, "\n");
 
		fprintf(this->fh, "static const StringID %s = 0x%X;\n", name, stringid);
 
		prev = stringid;
 
	}
 

	
 
	void Finalise()
 
	{
 
		/* Find the plural form with the most amount of cases. */
 
		int max_plural_forms = 0;
 
		for (uint i = 0; i < lengthof(_plural_forms); i++) {
 
			max_plural_forms = max(max_plural_forms, _plural_forms[i].plural_count);
 
		}
 

	
 
		fprintf(this->fh,
 
			"\n"
 
			"static const uint LANGUAGE_PACK_VERSION     = 0x%X;\n"
 
			"static const uint LANGUAGE_MAX_PLURAL       = %d;\n"
 
			"static const uint LANGUAGE_MAX_PLURAL_FORMS = %d;\n\n",
 
			(uint)_hash, (uint)lengthof(_plural_forms), max_plural_forms
 
		);
 

	
 
		fprintf(this->fh, "#endif /* TABLE_STRINGS_H */\n");
 

	
 
		this->FileWriter::Finalise();
 

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

	
 
static int TranslateArgumentIdx(int argidx, int offset)
 
{
 
	int sum;
 

	
 
	if (argidx < 0 || (uint)argidx >= lengthof(_cur_pcs.cmd)) {
 
		error("invalid argidx %d", argidx);
 
	}
 
	const CmdStruct *cs = _cur_pcs.cmd[argidx];
 
	if (cs != NULL && cs->consumes <= offset) {
 
		error("invalid argidx offset %d:%d", argidx, offset);
 
	}
 

	
 
	if (_cur_pcs.cmd[argidx] == NULL) {
 
		error("no command for this argidx %d", argidx);
 
	}
 

	
 
	for (int i = sum = 0; i < argidx; i++) {
 
		const CmdStruct *cs = _cur_pcs.cmd[i];
 

	
 
		sum += (cs != NULL) ? cs->consumes : 1;
 
	}
 

	
 
	return sum + offset;
 
}
 

	
 
static void PutArgidxCommand()
 
{
 
	PutUtf8(SCC_ARG_INDEX);
 
	PutByte(TranslateArgumentIdx(_cur_argidx));
 
}
 

	
 

	
 
static void PutCommandString(const char *str)
 
{
 
	_cur_argidx = 0;
 

	
 
	while (*str != '\0') {
 
		/* Process characters as they are until we encounter a { */
 
		if (*str != '{') {
 
			PutByte(*str++);
 
			continue;
 
		}
0 comments (0 inline, 0 general)