(svn r23575) -Codechange: split class definition and implementation of base strgen classes

This commit is contained in:
rubidium 2011-12-17 17:25:50 +00:00
parent 9c34bc52db
commit c97b2a5224
3 changed files with 456 additions and 419 deletions

View File

@ -42,7 +42,7 @@ RES := $(shell mkdir -p $(BIN_DIR)/lang )
all: table/strings.h $(LANGS)
strgen.o: $(SRC_DIR)/strgen/strgen.cpp endian_host.h $(SRC_DIR)/table/control_codes.h $(SRC_DIR)/table/strgen_tables.h
strgen.o: $(SRC_DIR)/strgen/strgen.cpp $(SRC_DIR)/strgen/strgen.h endian_host.h $(SRC_DIR)/table/control_codes.h $(SRC_DIR)/table/strgen_tables.h
$(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.cpp=%.cpp)'
$(Q)$(CXX_BUILD) $(CFLAGS_BUILD) -DSTRGEN -c -o $@ $<

View File

@ -17,6 +17,8 @@
#include "../misc/getoptdata.h"
#include "../table/control_codes.h"
#include "strgen.h"
#include <stdarg.h>
#include <exception>
@ -49,40 +51,23 @@ static int _errors, _warnings, _show_todo;
static const ptrdiff_t MAX_COMMAND_PARAM_SIZE = 100; ///< Maximum size of every command block, not counting the name of the command itself
static const CmdStruct *ParseCommandString(const char **str, char *param, int *argno, int *casei);
/** Container for the different cases of a string. */
struct Case {
int caseidx; ///< The index of the case.
char *string; ///< The translation of the case.
Case *next; ///< The next, chained, case.
/**
* Create a new case.
* @param caseidx The index of the case.
* @param string The translation of the case.
* @param next The next chained case.
*/
Case(int caseidx, const char *string, Case *next) :
Case::Case(int caseidx, const char *string, Case *next) :
caseidx(caseidx), string(strdup(string)), next(next)
{
}
/** Free everything we allocated. */
~Case()
Case::~Case()
{
free(this->string);
delete this->next;
}
};
/** Information about a single string. */
struct LangString {
char *name; ///< Name of the string.
char *english; ///< English text.
char *translated; ///< Translated text.
uint16 hash_next; ///< Next hash entry.
uint16 index; ///< The index in the language file.
int line; ///< Line of string in source-file.
Case *translated_case; ///< Cases of the translation.
/**
* Create a new string.
@ -91,14 +76,14 @@ struct LangString {
* @param index The index in the string table.
* @param line The line this string was found on.
*/
LangString(const char *name, const char *english, int index, int line) :
LangString::LangString(const char *name, const char *english, int index, int line) :
name(strdup(name)), english(strdup(english)), translated(NULL),
hash_next(0), index(index), line(line), translated_case(NULL)
{
}
/** Free everything we allocated. */
~LangString()
LangString::~LangString()
{
free(this->name);
free(this->english);
@ -107,7 +92,7 @@ struct LangString {
}
/** Free all data related to the translation. */
void FreeTranslation()
void LangString::FreeTranslation()
{
free(this->translated);
this->translated = NULL;
@ -115,23 +100,12 @@ struct LangString {
delete this->translated_case;
this->translated_case = NULL;
}
};
/** Information about the currently known strings. */
struct StringData {
static const uint STRINGS_IN_TAB = 2048;
LangString **strings; ///< Array of all known strings.
uint16 *hash_heads; ///< Hash table for the strings.
size_t tabs; ///< The number of 'tabs' of strings.
size_t max_strings; ///< The maxmimum number of strings.
int next_string_id; ///< The next string ID to allocate.
/**
* Create a new string data container.
* @param max_strings The maximum number of strings.
*/
StringData(size_t tabs = 32) : tabs(tabs), max_strings(tabs * STRINGS_IN_TAB)
StringData::StringData(size_t tabs) : tabs(tabs), max_strings(tabs * STRINGS_IN_TAB)
{
this->strings = CallocT<LangString *>(max_strings);
this->hash_heads = CallocT<uint16>(max_strings);
@ -139,7 +113,7 @@ struct StringData {
}
/** Free everything we allocated. */
~StringData()
StringData::~StringData()
{
for (size_t i = 0; i < this->max_strings; i++) delete this->strings[i];
free(this->strings);
@ -147,7 +121,7 @@ struct StringData {
}
/** Free all data related to the translation. */
void FreeTranslation()
void StringData::FreeTranslation()
{
for (size_t i = 0; i < this->max_strings; i++) {
LangString *ls = this->strings[i];
@ -160,7 +134,7 @@ struct StringData {
* @param s The string to hash.
* @return The hashed string.
*/
uint HashStr(const char *s) const
uint StringData::HashStr(const char *s) const
{
uint hash = 0;
for (; *s != '\0'; s++) hash = ROL(hash, 3) ^ *s;
@ -172,7 +146,7 @@ struct StringData {
* @param s The name of the string.
* @param ls The string to add.
*/
void Add(const char *s, LangString *ls)
void StringData::Add(const char *s, LangString *ls)
{
uint hash = this->HashStr(s);
ls->hash_next = this->hash_heads[hash];
@ -186,7 +160,7 @@ struct StringData {
* @param s The string name to search on.
* @return The LangString or NULL if it is not known.
*/
LangString *Find(const char *s)
LangString *StringData::Find(const char *s)
{
int idx = this->hash_heads[this->HashStr(s)];
@ -205,7 +179,7 @@ struct StringData {
* @param s The string hash.
* @return The new hash.
*/
uint VersionHashStr(uint hash, const char *s) const
uint StringData::VersionHashStr(uint hash, const char *s) const
{
for (; *s != '\0'; s++) {
hash = ROL(hash, 3) ^ *s;
@ -218,7 +192,7 @@ struct StringData {
* Make a hash of the file to get a unique "version number"
* @return The version number.
*/
uint Version() const
uint StringData::Version() const
{
uint hash = 0;
@ -254,13 +228,12 @@ struct StringData {
* Count the number of tab elements that are in use.
* @param tab The tab to count the elements of.
*/
uint CountInUse(uint tab) const
uint StringData::CountInUse(uint tab) const
{
int i;
for (i = STRINGS_IN_TAB; --i >= 0;) if (this->strings[(tab * STRINGS_IN_TAB) + i] != NULL) break;
return i + 1;
}
};
static LanguagePackHeader _lang; ///< Header information about a language.
@ -661,13 +634,6 @@ static const CmdStruct *ParseCommandString(const char **str, char *param, int *a
return cmd;
}
/** Helper for reading strings. */
struct StringReader {
StringData &data; ///< The data to fill during reading.
const char *file; ///< The file we are reading.
bool master; ///< Are we reading the master file?
bool translation; ///< Are we reading a translation, implies !master. However, the base translation will have this false.
/**
* Prepare reading.
* @param data The data to fill during reading.
@ -675,43 +641,17 @@ struct StringReader {
* @param master Are we reading the master file?
* @param translation Are we reading a translation?
*/
StringReader(StringData &data, const char *file, bool master, bool translation) :
StringReader::StringReader(StringData &data, const char *file, bool master, bool translation) :
data(data), file(strdup(file)), master(master), translation(translation)
{
}
/** Make sure the right reader gets freed. */
virtual ~StringReader()
StringReader::~StringReader()
{
free(file);
}
/**
* Read a single line from the source of strings.
* @param buffer The buffer to read the data in to.
* @param size The size of the buffer.
* @return The buffer, or NULL if at the end of the file.
*/
virtual char *ReadLine(char *buffer, size_t size) = 0;
/**
* Handle the pragma of the file.
* @param str The pragma string to parse.
*/
virtual void HandlePragma(char *str) = 0;
/**
* Handle reading a single string.
* @param str The string to handle.
*/
void HandleString(char *str);
/**
* Start parsing the file.
*/
virtual void ParseFile();
};
/** A reader that simply reads using fopen. */
struct FileStringReader : StringReader {
FILE *fh; ///< The file we are reading.
@ -1120,29 +1060,11 @@ struct FileWriter {
}
};
/** 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.
* @param data The data about the string.
*/
virtual void Finalise(const StringData &data) = 0;
/** Especially destroy the subclasses. */
virtual ~HeaderWriter() {};
/**
* Write the header information.
* @param data The data about the string.
*/
void WriteHeader(const StringData &data)
void HeaderWriter::WriteHeader(const StringData &data)
{
int last = 0;
for (size_t i = 0; i < data.max_strings; i++) {
@ -1154,7 +1076,6 @@ struct HeaderWriter {
this->WriteStringID("STR_LAST_STRINGID", last);
}
};
struct HeaderFileWriter : HeaderWriter, FileWriter {
/** The real file name we eventually want to write to. */
@ -1287,35 +1208,11 @@ static void PutCommandString(Buffer *buffer, const char *str)
}
}
/** Base class for all language writers. */
struct LanguageWriter {
/**
* Write the header metadata. The multi-byte integers are already converted to
* the little endian format.
* @param header The header to write.
*/
virtual void WriteHeader(const LanguagePackHeader *header) = 0;
/**
* Write a number of bytes.
* @param buffer The buffer to write.
* @param length The amount of byte to write.
*/
virtual void Write(const byte *buffer, size_t length) = 0;
/**
* Finalise writing the file.
*/
virtual void Finalise() = 0;
/** Especially destroy the subclasses. */
virtual ~LanguageWriter() {}
/**
* Write the length as a simple gamma.
* @param length The number to write.
*/
void WriteLength(uint length)
void LanguageWriter::WriteLength(uint length)
{
char buffer[2];
int offs = 0;
@ -1334,7 +1231,7 @@ struct LanguageWriter {
* Actually write the language.
* @param data The data about the string.
*/
void WriteLang(const StringData &data)
void LanguageWriter::WriteLang(const StringData &data)
{
uint *in_use = AllocaM(uint, data.tabs);
for (size_t tab = 0; tab < data.tabs; tab++) {
@ -1434,7 +1331,6 @@ struct LanguageWriter {
}
}
}
};
/** Class for writing a language to disk. */
struct LanguageFileWriter : LanguageWriter, FileWriter {

141
src/strgen/strgen.h Normal file
View File

@ -0,0 +1,141 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file strgen.h Structures related to strgen. */
#ifndef STRGEN_H
#define STRGEN_H
/** Container for the different cases of a string. */
struct Case {
int caseidx; ///< The index of the case.
char *string; ///< The translation of the case.
Case *next; ///< The next, chained, case.
Case(int caseidx, const char *string, Case *next);
~Case();
};
/** Information about a single string. */
struct LangString {
char *name; ///< Name of the string.
char *english; ///< English text.
char *translated; ///< Translated text.
uint16 hash_next; ///< Next hash entry.
uint16 index; ///< The index in the language file.
int line; ///< Line of string in source-file.
Case *translated_case; ///< Cases of the translation.
LangString(const char *name, const char *english, int index, int line);
~LangString();
void FreeTranslation();
};
/** Information about the currently known strings. */
struct StringData {
static const uint STRINGS_IN_TAB = 2048;
LangString **strings; ///< Array of all known strings.
uint16 *hash_heads; ///< Hash table for the strings.
size_t tabs; ///< The number of 'tabs' of strings.
size_t max_strings; ///< The maxmimum number of strings.
int next_string_id; ///< The next string ID to allocate.
StringData(size_t tabs = 32);
~StringData();
void FreeTranslation();
uint HashStr(const char *s) const;
void Add(const char *s, LangString *ls);
LangString *Find(const char *s);
uint VersionHashStr(uint hash, const char *s) const;
uint Version() const;
uint CountInUse(uint tab) const;
};
/** Helper for reading strings. */
struct StringReader {
StringData &data; ///< The data to fill during reading.
const char *file; ///< The file we are reading.
bool master; ///< Are we reading the master file?
bool translation; ///< Are we reading a translation, implies !master. However, the base translation will have this false.
StringReader(StringData &data, const char *file, bool master, bool translation);
virtual ~StringReader();
void HandleString(char *str);
/**
* Read a single line from the source of strings.
* @param buffer The buffer to read the data in to.
* @param size The size of the buffer.
* @return The buffer, or NULL if at the end of the file.
*/
virtual char *ReadLine(char *buffer, size_t size) = 0;
/**
* Handle the pragma of the file.
* @param str The pragma string to parse.
*/
virtual void HandlePragma(char *str) = 0;
/**
* Start parsing the file.
*/
virtual void ParseFile();
};
/** Base class for writing the header, i.e. the STR_XXX to numeric value. */
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.
* @param data The data about the string.
*/
virtual void Finalise(const StringData &data) = 0;
/** Especially destroy the subclasses. */
virtual ~HeaderWriter() {};
void WriteHeader(const StringData &data);
};
/** Base class for all language writers. */
struct LanguageWriter {
/**
* Write the header metadata. The multi-byte integers are already converted to
* the little endian format.
* @param header The header to write.
*/
virtual void WriteHeader(const LanguagePackHeader *header) = 0;
/**
* Write a number of bytes.
* @param buffer The buffer to write.
* @param length The amount of byte to write.
*/
virtual void Write(const byte *buffer, size_t length) = 0;
/**
* Finalise writing the file.
*/
virtual void Finalise() = 0;
/** Especially destroy the subclasses. */
virtual ~LanguageWriter() {}
void WriteLength(uint length);
void WriteLang(const StringData &data);
};
#endif /* STRGEN_H */