Translations update
[openttd/fttd.git] / src / newgrf_text.h
blobb6b68b33fc0c768935d2f0b8c74881e933965800
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file newgrf_text.h Header of Action 04 "universal holder" structure and functions */
12 #ifndef NEWGRF_TEXT_H
13 #define NEWGRF_TEXT_H
15 #include <map>
17 #include "string.h"
18 #include "strings_type.h"
19 #include "core/smallvec_type.hpp"
20 #include "table/control_codes.h"
22 /** This character, the thorn ('รพ'), indicates a unicode string to NFO. */
23 static const WChar NFO_UTF8_IDENTIFIER = 0x00DE;
26 /**
27 * Element of the linked list.
28 * Each of those elements represent the string,
29 * but according to a different lang.
31 struct GRFText {
32 public:
33 /**
34 * Allocate and assign a new GRFText with the given text.
35 * As these strings can have string terminations in them, e.g.
36 * due to "choice lists" we (sometimes) cannot rely on detecting
37 * the length by means of strlen. Also, if the length is already
38 * known not scanning the whole string is more efficient.
39 * @param text The text to store in the new GRFText.
40 * @param len The length of the text.
42 static GRFText *create (const char *text, size_t len)
44 return new (len) GRFText (text, len);
47 /** Create a GRFText for a given string. */
48 static GRFText *create (const char *text)
50 return create (text, strlen(text) + 1);
53 /**
54 * Create a copy of this GRFText.
55 * @return an exact copy of the given text.
57 GRFText *clone (void) const
59 return GRFText::create (this->text, this->len);
62 /**
63 * Free the memory we allocated.
64 * @param p memory to free.
66 void operator delete (void *p)
68 free (p);
71 private:
72 /**
73 * Actually construct the GRFText.
74 * @param text_ The text to store in this GRFText.
75 * @param len_ The length of the text to store.
77 GRFText (const char *text_, size_t len_) : len(len_)
79 /* We need to use memcpy instead of strcpy due to
80 * the possibility of "choice lists" and therefore
81 * intermediate string terminators. */
82 memcpy (this->text, text_, len);
85 /**
86 * Allocate memory for this class.
87 * @param size the size of the instance
88 * @param extra the extra memory for the text
89 * @return the requested amount of memory for both the instance and the text
91 void *operator new (size_t size, size_t extra)
93 return xmalloc (size + extra);
96 /**
97 * Helper allocation function to disallow something.
98 * Don't allow simple 'news'; they wouldn't have enough memory.
99 * @param size the amount of space not to allocate.
101 void *operator new (size_t size) DELETED;
103 public:
104 size_t len; ///< The length of the stored string, used for copying.
105 char text[]; ///< The actual (translated) text.
109 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid, bool new_scheme, bool allow_newlines, const char *text_to_add, StringID def_string);
110 StringID GetGRFStringID(uint32 grfid, uint16 stringid);
111 const char *GetGRFStringPtr(uint16 stringid);
112 void CleanUpStrings();
113 void SetCurrentGrfLangID(byte language_id);
114 char *TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, bool allow_newlines, const char *str, int *olen = NULL, StringControlCode byte80 = SCC_NEWGRF_PRINT_WORD_STRING_ID);
116 bool CheckGrfLangID(byte lang_id, byte grf_version);
118 void StartTextRefStackUsage(const GRFFile *grffile, byte numEntries, const uint32 *values = NULL);
119 void StopTextRefStackUsage();
120 void RewindTextRefStack();
121 bool UsingNewGRFTextStack();
122 struct TextRefStack *CreateTextRefStackBackup();
123 void RestoreTextRefStackBackup(struct TextRefStack *backup);
124 uint RemapNewGRFStringControlCode (uint scc, stringb *buf, const char **str, int64 *argv, uint argv_size, bool modify_argv);
126 /** Mapping of language data between a NewGRF and OpenTTD. */
127 struct LanguageMap {
128 /** Mapping between NewGRF and OpenTTD IDs. */
129 struct Mapping {
130 byte newgrf_id; ///< NewGRF's internal ID for a case/gender.
131 byte openttd_id; ///< OpenTTD's internal ID for a case/gender.
134 /* We need a vector and can't use SmallMap due to the fact that for "setting" a
135 * gender of a string or requesting a case for a substring we want to map from
136 * the NewGRF's internal ID to OpenTTD's ID whereas for the choice lists we map
137 * the genders/cases/plural OpenTTD IDs to the NewGRF's internal IDs. In this
138 * case a NewGRF developer/translator might want a different translation for
139 * both cases. Thus we are basically implementing a multi-map. */
140 SmallVector<Mapping, 1> gender_map; ///< Mapping of NewGRF and OpenTTD IDs for genders.
141 SmallVector<Mapping, 1> case_map; ///< Mapping of NewGRF and OpenTTD IDs for cases.
142 int plural_form; ///< The plural form used for this language.
144 int GetMapping(int newgrf_id, bool gender) const;
145 int GetReverseMapping(int openttd_id, bool gender) const;
146 static const LanguageMap *GetLanguageMap(uint32 grfid, uint8 language_id);
149 /** Map of GRFText objects by langid. */
150 struct GRFTextMap : private std::map <byte, GRFText *> {
151 /** Default constructor. */
152 GRFTextMap() : std::map <byte, GRFText *> ()
156 GRFTextMap (const GRFTextMap &other);
158 ~GRFTextMap();
160 const GRFText *get_current (void) const;
163 * Get a C-string from this GRFText-list. If there is a translation
164 * for the current language it is returned, otherwise the default
165 * translation is returned. If there is neither a default nor a
166 * translation for the current language NULL is returned.
168 const char *get_string (void) const
170 const GRFText *t = this->get_current();
171 return (t != NULL) ? t->text : NULL;
174 void add (byte langid, GRFText *text);
175 void add (byte langid, uint32 grfid, bool allow_newlines, const char *text);
178 * Add a GRFText to this list. The text should not contain any
179 * text-codes. The text will be added as a 'default language'-text.
180 * @param text The text to add to the list.
182 void add_default (const char *text)
184 this->add (0x7F, GRFText::create (text));
188 #endif /* NEWGRF_TEXT_H */