diff options
author | rubidium <rubidium@openttd.org> | 2011-12-17 17:25:50 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2011-12-17 17:25:50 +0000 |
commit | c97b2a5224e350380e5f31b0612bf057d1a689ab (patch) | |
tree | 84ddcbe0e211c085bbff14eb5e17e13491ca0729 /src/strgen/strgen.h | |
parent | 9c34bc52dbae216678aaf1f5d05a28540266e307 (diff) | |
download | openttd-c97b2a5224e350380e5f31b0612bf057d1a689ab.tar.xz |
(svn r23575) -Codechange: split class definition and implementation of base strgen classes
Diffstat (limited to 'src/strgen/strgen.h')
-rw-r--r-- | src/strgen/strgen.h | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/src/strgen/strgen.h b/src/strgen/strgen.h new file mode 100644 index 000000000..80737f6eb --- /dev/null +++ b/src/strgen/strgen.h @@ -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 */ |