StringTable cleanup and improvements
cleaned up function interface to avoid referencing the file system in the worker functions. replaced StringTable's operators with functions.. The main reason is that these are far easier to look up when browsing the source. This also fixes a premature translation in SBARINFO that was done in the parsing stage, not the printing stage.
This commit is contained in:
parent
f4a42dae1d
commit
ebd4ebf298
50 changed files with 245 additions and 246 deletions
|
|
@ -47,29 +47,29 @@
|
|||
//
|
||||
//==========================================================================
|
||||
|
||||
void FStringTable::LoadStrings (FileSys::FileSystem& fileSystem_, const char *language)
|
||||
void FStringTable::LoadStrings (FileSys::FileSystem& fileSystem, const char *language)
|
||||
{
|
||||
int lastlump, lump;
|
||||
|
||||
fileSystem = &fileSystem_;
|
||||
allStrings.Clear();
|
||||
lastlump = 0;
|
||||
while ((lump = fileSystem->FindLump("LMACROS", &lastlump)) != -1)
|
||||
while ((lump = fileSystem.FindLump("LMACROS", &lastlump)) != -1)
|
||||
{
|
||||
readMacros(lump);
|
||||
auto lumpdata = fileSystem.ReadFile(lump);
|
||||
readMacros(lumpdata.string(), lumpdata.size());
|
||||
}
|
||||
|
||||
lastlump = 0;
|
||||
while ((lump = fileSystem->FindLump ("LANGUAGE", &lastlump)) != -1)
|
||||
while ((lump = fileSystem.FindLump ("LANGUAGE", &lastlump)) != -1)
|
||||
{
|
||||
auto lumpdata = fileSystem->ReadFile(lump);
|
||||
auto lumpdata = fileSystem.ReadFile(lump);
|
||||
auto filenum = fileSystem.GetFileContainer(lump);
|
||||
|
||||
if (!ParseLanguageCSV(lump, lumpdata.string(), lumpdata.size()))
|
||||
LoadLanguage (lump, lumpdata.string(), lumpdata.size());
|
||||
if (!ParseLanguageCSV(filenum, lumpdata.string(), lumpdata.size()))
|
||||
LoadLanguage (filenum, lumpdata.string(), lumpdata.size());
|
||||
}
|
||||
UpdateLanguage(language);
|
||||
allMacros.Clear();
|
||||
fileSystem = nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -159,13 +159,12 @@ TArray<TArray<FString>> FStringTable::parseCSV(const char* buffer, size_t size)
|
|||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FStringTable::readMacros(int lumpnum)
|
||||
bool FStringTable::readMacros(const char* buffer, size_t size)
|
||||
{
|
||||
auto lumpdata = fileSystem->ReadFile(lumpnum);
|
||||
auto data = parseCSV(lumpdata.string(), lumpdata.size());
|
||||
auto data = parseCSV(buffer, size);
|
||||
|
||||
allMacros.Clear();
|
||||
for (unsigned i = 1; i < data.Size(); i++)
|
||||
for (unsigned i = 1; i < size; i++)
|
||||
{
|
||||
auto macroname = data[i][0];
|
||||
FName name = macroname.GetChars();
|
||||
|
|
@ -187,7 +186,7 @@ bool FStringTable::readMacros(int lumpnum)
|
|||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FStringTable::ParseLanguageCSV(int lumpnum, const char* buffer, size_t size)
|
||||
bool FStringTable::ParseLanguageCSV(int filenum, const char* buffer, size_t size)
|
||||
{
|
||||
if (size < 11) return false;
|
||||
if (strnicmp(buffer, "default,", 8) && strnicmp(buffer, "identifier,", 11 )) return false;
|
||||
|
|
@ -258,14 +257,14 @@ bool FStringTable::ParseLanguageCSV(int lumpnum, const char* buffer, size_t size
|
|||
FName strName = row[labelcol].GetChars();
|
||||
if (hasDefaultEntry)
|
||||
{
|
||||
DeleteForLabel(lumpnum, strName);
|
||||
DeleteForLabel(filenum, strName);
|
||||
}
|
||||
for (auto &langentry : langrows)
|
||||
{
|
||||
auto str = row[langentry.first];
|
||||
if (str.Len() > 0)
|
||||
{
|
||||
InsertString(lumpnum, langentry.second, strName, str);
|
||||
InsertString(filenum, langentry.second, strName, str);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -409,11 +408,10 @@ void FStringTable::DeleteString(int langid, FName label)
|
|||
//
|
||||
//==========================================================================
|
||||
|
||||
void FStringTable::DeleteForLabel(int lumpnum, FName label)
|
||||
void FStringTable::DeleteForLabel(int filenum, FName label)
|
||||
{
|
||||
decltype(allStrings)::Iterator it(allStrings);
|
||||
decltype(allStrings)::Pair *pair;
|
||||
auto filenum = fileSystem->GetFileContainer(lumpnum);
|
||||
|
||||
while (it.NextPair(pair))
|
||||
{
|
||||
|
|
@ -432,10 +430,10 @@ void FStringTable::DeleteForLabel(int lumpnum, FName label)
|
|||
//
|
||||
//==========================================================================
|
||||
|
||||
void FStringTable::InsertString(int lumpnum, int langid, FName label, const FString &string)
|
||||
void FStringTable::InsertString(int filenum, int langid, FName label, const FString &string)
|
||||
{
|
||||
const char *strlangid = (const char *)&langid;
|
||||
TableElement te = { fileSystem->GetFileContainer(lumpnum), { string, string, string, string } };
|
||||
TableElement te = { filenum, { string, string, string, string } };
|
||||
ptrdiff_t index;
|
||||
while ((index = te.strings[0].IndexOf("@[")) >= 0)
|
||||
{
|
||||
|
|
@ -579,7 +577,7 @@ bool FStringTable::exists(const char *name)
|
|||
//
|
||||
//==========================================================================
|
||||
|
||||
const char *FStringTable::GetString(const char *name, uint32_t *langtable, int gender) const
|
||||
const char *FStringTable::CheckString(const char *name, uint32_t *langtable, int gender) const
|
||||
{
|
||||
if (name == nullptr || *name == 0)
|
||||
{
|
||||
|
|
@ -590,15 +588,22 @@ const char *FStringTable::GetString(const char *name, uint32_t *langtable, int g
|
|||
FName nm(name, true);
|
||||
if (nm != NAME_None)
|
||||
{
|
||||
TableElement* bestItem = nullptr;
|
||||
for (auto map : currentLanguageSet)
|
||||
{
|
||||
auto item = map.second->CheckKey(nm);
|
||||
if (item)
|
||||
{
|
||||
if (bestItem && bestItem->filenum > item->filenum)
|
||||
{
|
||||
// prioritize content from later files, even if the language doesn't fully match.
|
||||
// This is mainly for Dehacked content.
|
||||
continue;
|
||||
}
|
||||
if (langtable) *langtable = map.first;
|
||||
auto c = item->strings[gender].GetChars();
|
||||
if (c && *c == '$' && c[1] == '$')
|
||||
return GetString(c + 2, langtable, gender);
|
||||
c = CheckString(c + 2, langtable, gender);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
|
@ -608,7 +613,7 @@ const char *FStringTable::GetString(const char *name, uint32_t *langtable, int g
|
|||
|
||||
//==========================================================================
|
||||
//
|
||||
// Finds a string by name in a given language
|
||||
// Finds a string by name in a given language without attempting any substitution
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
|
|
@ -655,9 +660,9 @@ bool FStringTable::MatchDefaultString(const char *name, const char *content) con
|
|||
//
|
||||
//==========================================================================
|
||||
|
||||
const char *FStringTable::operator() (const char *name) const
|
||||
const char *FStringTable::GetString(const char *name) const
|
||||
{
|
||||
const char *str = operator[] (name);
|
||||
const char *str = CheckString(name, nullptr);
|
||||
return str ? str : name;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -95,20 +95,15 @@ public:
|
|||
|
||||
const char *GetLanguageString(const char *name, uint32_t langtable, int gender = -1) const;
|
||||
bool MatchDefaultString(const char *name, const char *content) const;
|
||||
const char *GetString(const char *name, uint32_t *langtable, int gender = -1) const;
|
||||
const char *operator() (const char *name) const; // Never returns NULL
|
||||
const char* operator() (const FString& name) const { return operator()(name.GetChars()); }
|
||||
const char *operator[] (const char *name) const
|
||||
{
|
||||
return GetString(name, nullptr);
|
||||
}
|
||||
const char *CheckString(const char *name, uint32_t *langtable = nullptr, int gender = -1) const;
|
||||
const char* GetString(const char* name) const;
|
||||
const char* GetString(const FString& name) const { return GetString(name.GetChars()); }
|
||||
bool exists(const char *name);
|
||||
|
||||
void InsertString(int lumpnum, int langid, FName label, const FString& string);
|
||||
void InsertString(int filenum, int langid, FName label, const FString& string);
|
||||
|
||||
private:
|
||||
|
||||
FileSys::FileSystem* fileSystem;
|
||||
FString activeLanguage;
|
||||
StringMacroMap allMacros;
|
||||
LangMap allStrings;
|
||||
|
|
@ -116,11 +111,11 @@ private:
|
|||
|
||||
void LoadLanguage (int lumpnum, const char* buffer, size_t size);
|
||||
TArray<TArray<FString>> parseCSV(const char* buffer, size_t size);
|
||||
bool ParseLanguageCSV(int lumpnum, const char* buffer, size_t size);
|
||||
bool ParseLanguageCSV(int filenum, const char* buffer, size_t size);
|
||||
|
||||
bool readMacros(int lumpnum);
|
||||
bool readMacros(const char* buffer, size_t size);
|
||||
void DeleteString(int langid, FName label);
|
||||
void DeleteForLabel(int lumpnum, FName label);
|
||||
void DeleteForLabel(int filenum, FName label);
|
||||
|
||||
static size_t ProcessEscapes (char *str);
|
||||
public:
|
||||
|
|
@ -138,7 +133,7 @@ public:
|
|||
|
||||
const char* localize(const char* str)
|
||||
{
|
||||
return *str == '$' ? operator()(str + 1) : str;
|
||||
return *str == '$' ? GetString(str + 1) : str;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue