Changeset - r27466:4d701a19674d
[Not reviewed]
master
0 4 0
Rubidium - 19 months ago 2023-04-30 08:22:14
rubidium@openttd.org
Codechange: use std::string for LangString over C-style strings
4 files changed with 31 insertions and 43 deletions:
0 comments (0 inline, 0 general)
src/game/game_text.cpp
Show inline comments
 
@@ -158,7 +158,7 @@ struct StringNameWriter : HeaderWriter {
 
	{
 
	}
 

	
 
	void WriteStringID(const char *name, int stringid)
 
	void WriteStringID(const std::string &name, int stringid)
 
	{
 
		if (stringid == (int)this->strings.size()) this->strings.emplace_back(name);
 
	}
 
@@ -271,7 +271,7 @@ static void ExtractStringParams(const St
 
		if (ls != nullptr) {
 
			StringParams &param = params.emplace_back();
 
			ParsedCommandStruct pcs;
 
			ExtractCommandString(&pcs, ls->english, false);
 
			ExtractCommandString(&pcs, ls->english.c_str(), false);
 

	
 
			for (const CmdStruct *cs : pcs.cmd) {
 
				if (cs == nullptr) break;
src/strgen/strgen.cpp
Show inline comments
 
@@ -236,7 +236,7 @@ struct HeaderFileWriter : HeaderWriter, 
 
		this->output_stream << "#define TABLE_STRINGS_H\n";
 
	}
 

	
 
	void WriteStringID(const char *name, int stringid)
 
	void WriteStringID(const std::string &name, int stringid)
 
	{
 
		if (prev + 1 != stringid) this->output_stream << "\n";
 
		fmt::print(this->output_stream, "static const StringID {} = 0x{:X};\n", name, stringid);
src/strgen/strgen.h
Show inline comments
 
@@ -23,16 +23,15 @@ struct Case {
 

	
 
/** Information about a single string. */
 
struct LangString {
 
	char *name;            ///< Name of the string.
 
	char *english;         ///< English text.
 
	char *translated;      ///< Translated text.
 
	size_t hash_next;      ///< Next hash entry.
 
	size_t index;          ///< The index in the language file.
 
	int line;              ///< Line of string in source-file.
 
	std::string name;       ///< Name of the string.
 
	std::string english;    ///< English text.
 
	std::string translated; ///< Translated text.
 
	size_t hash_next;       ///< Next hash entry.
 
	size_t index;           ///< The index in the language file.
 
	int line;               ///< Line of string in source-file.
 
	std::vector<Case> translated_cases; ///< Cases of the translation.
 

	
 
	LangString(const char *name, const char *english, size_t index, int line);
 
	~LangString();
 
	LangString(const std::string &name, const std::string &english, size_t index, int line);
 
	void FreeTranslation();
 
};
 

	
 
@@ -93,7 +92,7 @@ struct HeaderWriter {
 
	 * @param name     The name of the string.
 
	 * @param stringid The ID of the string.
 
	 */
 
	virtual void WriteStringID(const char *name, int stringid) = 0;
 
	virtual void WriteStringID(const std::string &name, int stringid) = 0;
 

	
 
	/**
 
	 * Finalise writing the file.
src/strgen/strgen_base.cpp
Show inline comments
 
@@ -51,26 +51,15 @@ Case::Case(int caseidx, const std::strin
 
 * @param index   The index in the string table.
 
 * @param line    The line this string was found on.
 
 */
 
LangString::LangString(const char *name, const char *english, size_t index, int line) :
 
		name(stredup(name)), english(stredup(english)), translated(nullptr),
 
		hash_next(0), index(index), line(line)
 
LangString::LangString(const std::string &name, const std::string &english, size_t index, int line) :
 
		name(name), english(english), hash_next(0), index(index), line(line)
 
{
 
}
 

	
 
/** Free everything we allocated. */
 
LangString::~LangString()
 
{
 
	free(this->name);
 
	free(this->english);
 
	free(this->translated);
 
}
 

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

	
 
	this->translated.clear();
 
	this->translated_cases.clear();
 
}
 

	
 
@@ -140,7 +129,7 @@ LangString *StringData::Find(const char 
 
	while (idx-- > 0) {
 
		LangString *ls = this->strings[idx];
 

	
 
		if (strcmp(ls->name, s) == 0) return ls;
 
		if (ls->name == s) return ls;
 
		idx = ls->hash_next;
 
	}
 
	return nullptr;
 
@@ -179,12 +168,12 @@ uint StringData::Version() const
 
			int argno;
 
			int casei;
 

	
 
			s = ls->name;
 
			s = ls->name.c_str();
 
			hash ^= i * 0x717239;
 
			hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : hash >> 1);
 
			hash = this->VersionHashStr(hash, s + 1);
 

	
 
			s = ls->english;
 
			s = ls->english.c_str();
 
			while ((cs = ParseCommandString(&s, buf, &argno, &casei)) != nullptr) {
 
				if (cs->flags & C_DONTCOUNT) continue;
 

	
 
@@ -630,7 +619,7 @@ const CmdStruct *TranslateCmdForCompare(
 
}
 

	
 

	
 
static bool CheckCommandsMatch(char *a, char *b, const char *name)
 
static bool CheckCommandsMatch(const char *a, const char *b, const char *name)
 
{
 
	/* If we're not translating, i.e. we're compiling the base language,
 
	 * it is pointless to do all these checks as it'll always be correct.
 
@@ -757,18 +746,18 @@ void StringReader::HandleString(char *st
 
			return;
 
		}
 

	
 
		if (ent->translated && casep == nullptr) {
 
		if (!ent->translated.empty() && casep == nullptr) {
 
			StrgenError("String name '{}' is used multiple times", str);
 
			return;
 
		}
 

	
 
		/* make sure that the commands match */
 
		if (!CheckCommandsMatch(s, ent->english, str)) return;
 
		if (!CheckCommandsMatch(s, ent->english.c_str(), str)) return;
 

	
 
		if (casep != nullptr) {
 
			ent->translated_cases.emplace_back(ResolveCaseName(casep, strlen(casep)), s);
 
		} else {
 
			ent->translated = stredup(s);
 
			ent->translated = s;
 
			/* If the string was translated, use the line from the
 
			 * translated language so errors in the translated file
 
			 * are properly referenced to. */
 
@@ -950,7 +939,7 @@ void LanguageWriter::WriteLang(const Str
 

	
 
		for (uint j = 0; j != in_use[tab]; j++) {
 
			const LangString *ls = data.strings[(tab * TAB_SIZE) + j];
 
			if (ls != nullptr && ls->translated == nullptr) _lang.missing++;
 
			if (ls != nullptr && ls->translated.empty()) _lang.missing++;
 
		}
 
	}
 

	
 
@@ -965,7 +954,7 @@ void LanguageWriter::WriteLang(const Str
 
	for (size_t tab = 0; tab < data.tabs; tab++) {
 
		for (uint j = 0; j != in_use[tab]; j++) {
 
			const LangString *ls = data.strings[(tab * TAB_SIZE) + j];
 
			const char *cmdp;
 
			const std::string *cmdp;
 

	
 
			/* For undefined strings, just set that it's an empty string */
 
			if (ls == nullptr) {
 
@@ -973,11 +962,11 @@ void LanguageWriter::WriteLang(const Str
 
				continue;
 
			}
 

	
 
			_cur_ident = ls->name;
 
			_cur_ident = ls->name.c_str();
 
			_cur_line = ls->line;
 

	
 
			/* Produce a message if a string doesn't have a translation. */
 
			if (_show_todo > 0 && ls->translated == nullptr) {
 
			if (_show_todo > 0 && ls->translated.empty()) {
 
				if ((_show_todo & 2) != 0) {
 
					StrgenWarning("'{}' is untranslated", ls->name);
 
				}
 
@@ -988,15 +977,15 @@ void LanguageWriter::WriteLang(const Str
 
			}
 

	
 
			/* Extract the strings and stuff from the english command string */
 
			ExtractCommandString(&_cur_pcs, ls->english, false);
 
			ExtractCommandString(&_cur_pcs, ls->english.c_str(), false);
 

	
 
			if (!ls->translated_cases.empty() || ls->translated != nullptr) {
 
				cmdp = ls->translated;
 
			if (!ls->translated_cases.empty() || !ls->translated.empty()) {
 
				cmdp = &ls->translated;
 
			} else {
 
				cmdp = ls->english;
 
				cmdp = &ls->english;
 
			}
 

	
 
			_translated = cmdp != ls->english;
 
			_translated = cmdp != &ls->english;
 

	
 
			if (!ls->translated_cases.empty()) {
 
				/* Need to output a case-switch.
 
@@ -1023,7 +1012,7 @@ void LanguageWriter::WriteLang(const Str
 
				}
 
			}
 

	
 
			if (cmdp != nullptr) PutCommandString(&buffer, cmdp);
 
			if (!cmdp->empty()) PutCommandString(&buffer, cmdp->c_str());
 

	
 
			this->WriteLength((uint)buffer.size());
 
			this->Write(buffer.data(), buffer.size());
0 comments (0 inline, 0 general)