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/>.
10 /** @file newgrf_text.h Header of Action 04 "universal holder" structure and functions */
18 #include "strings_type.h"
19 #include "core/flexarray.h"
20 #include "core/smallvec_type.hpp"
21 #include "table/control_codes.h"
23 /** Skip the NFO unicode string marker, if present. */
24 static inline bool skip_nfo_utf8_identifier (const char **str
)
26 /* This character, the thorn, indicates a unicode string to NFO. */
27 if ((*str
)[0] != '\xC3' || (*str
)[1] != '\x9E') return false;
34 * Element of the linked list.
35 * Each of those elements represent the string,
36 * but according to a different lang.
38 struct GRFText
: FlexArray
<char> {
40 const size_t len
; ///< The length of the stored string, used for copying.
41 char text
[]; ///< The actual (translated) text.
45 * Actually construct the GRFText.
46 * @param text The text to store in this GRFText.
47 * @param len The length of the text to store.
49 GRFText (const char *text
, size_t len
) : len (len
)
51 /* We need to use memcpy instead of strcpy due to
52 * the possibility of "choice lists" and therefore
53 * intermediate string terminators. */
54 memcpy (this->text
, text
, len
);
58 * Allocate and assign a new GRFText with the given text.
59 * As these strings can have string terminations in them, e.g.
60 * due to "choice lists" we (sometimes) cannot rely on detecting
61 * the length by means of strlen. Also, if the length is already
62 * known not scanning the whole string is more efficient.
63 * @param text The text to store in the new GRFText.
64 * @param len The length of the text.
66 static GRFText
*create (const char *text
, size_t len
)
68 return new (len
) GRFText (text
, len
);
72 static GRFText
*create (const char *text
, uint32 grfid
, byte langid
,
75 /** Create a GRFText for a given string. */
76 static GRFText
*create (const char *text
)
78 return create (text
, strlen(text
) + 1);
82 * Create a copy of this GRFText.
83 * @return an exact copy of the given text.
85 GRFText
*clone (void) const
87 return GRFText::create (this->text
, this->len
);
92 StringID
AddGRFString(uint32 grfid
, uint16 stringid
, byte langid
, bool new_scheme
, bool allow_newlines
, const char *text_to_add
, StringID def_string
);
93 StringID
GetGRFStringID(uint32 grfid
, StringID stringid
);
94 const char *GetGRFStringPtr(uint16 stringid
);
95 void CleanUpStrings();
96 void SetCurrentGrfLangID(byte language_id
);
98 char *TranslateTTDPatchCodes (uint32 grfid
, uint8 language_id
,
99 bool allow_newlines
, const char *str
,
100 StringControlCode byte80
= SCC_NEWGRF_PRINT_WORD_STRING_ID
);
102 bool CheckGrfLangID(byte lang_id
, byte grf_version
);
104 void StartTextRefStackUsage(const GRFFile
*grffile
, byte numEntries
, const uint32
*values
= NULL
);
105 void StopTextRefStackUsage();
106 void RewindTextRefStack();
107 bool UsingNewGRFTextStack();
108 struct TextRefStack
*CreateTextRefStackBackup();
109 void RestoreTextRefStackBackup(struct TextRefStack
*backup
);
110 uint
RemapNewGRFStringControlCode (uint scc
, stringb
*buf
, const char **str
, int64
*argv
, uint argv_size
, bool modify_argv
);
112 /** Mapping of language data between a NewGRF and OpenTTD. */
114 /** Mapping between NewGRF and OpenTTD IDs. */
116 byte newgrf_id
; ///< NewGRF's internal ID for a case/gender.
117 byte openttd_id
; ///< OpenTTD's internal ID for a case/gender.
120 /* We need a vector and can't use SmallMap due to the fact that for "setting" a
121 * gender of a string or requesting a case for a substring we want to map from
122 * the NewGRF's internal ID to OpenTTD's ID whereas for the choice lists we map
123 * the genders/cases/plural OpenTTD IDs to the NewGRF's internal IDs. In this
124 * case a NewGRF developer/translator might want a different translation for
125 * both cases. Thus we are basically implementing a multi-map. */
126 SmallVector
<Mapping
, 1> gender_map
; ///< Mapping of NewGRF and OpenTTD IDs for genders.
127 SmallVector
<Mapping
, 1> case_map
; ///< Mapping of NewGRF and OpenTTD IDs for cases.
128 int plural_form
; ///< The plural form used for this language.
130 static const LanguageMap
*GetLanguageMap(uint32 grfid
, uint8 language_id
);
133 /** Map of GRFText objects by langid. */
134 struct GRFTextMap
: private std::map
<byte
, GRFText
*> {
135 /** Default constructor. */
136 GRFTextMap() : std::map
<byte
, GRFText
*> ()
140 GRFTextMap (const GRFTextMap
&other
);
144 const GRFText
*get_current (void) const;
147 * Get a C-string from this GRFText-list. If there is a translation
148 * for the current language it is returned, otherwise the default
149 * translation is returned. If there is neither a default nor a
150 * translation for the current language NULL is returned.
152 const char *get_string (void) const
154 const GRFText
*t
= this->get_current();
155 return (t
!= NULL
) ? t
->text
: NULL
;
158 void add (byte langid
, GRFText
*text
);
159 void add (byte langid
, uint32 grfid
, bool allow_newlines
, const char *text
);
162 * Add a GRFText to this list. The text should not contain any
163 * text-codes. The text will be added as a 'default language'-text.
164 * @param text The text to add to the list.
166 void add_default (const char *text
)
168 this->add (0x7F, GRFText::create (text
));
172 #endif /* NEWGRF_TEXT_H */