OpenXcom  1.0
Open-source clone of the original X-Com
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
LocalizedText.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010-2014 OpenXcom Developers.
3  *
4  * This file is part of OpenXcom.
5  *
6  * OpenXcom is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * OpenXcom is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef OPENXCOM_LOCALIZEDTEXT_H
20 #define OPENXCOM_LOCALIZEDTEXT_H
21 
22 #include <string>
23 #include <sstream>
24 
26 
31 #ifndef OX_REQUIRED_RESULT
32 # if defined(__GNUC_) && !defined(__INTEL_COMPILER) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
33 # define OX_REQUIRED_RESULT __attribute__ ((warn_unused_result))
34 # else
35 # define OX_REQUIRED_RESULT
36 # endif
37 #endif
38 namespace OpenXcom
39 {
40 
46 {
47 public:
49  LocalizedText(const std::wstring &);
51  LocalizedText() : _text(L""), _nextArg(1) { /* Empty by design. */ }
53  operator std::wstring const& () const OX_REQUIRED_RESULT;
55  std::string asUTF8() const OX_REQUIRED_RESULT;
57  const wchar_t *c_str() const OX_REQUIRED_RESULT { return _text.c_str(); }
58 
59  // Argument substitution.
61  LocalizedText arg(std::wstring const &) const OX_REQUIRED_RESULT;
63  LocalizedText &arg(std::wstring const &) OX_REQUIRED_RESULT;
65  template <typename T> LocalizedText arg(T) const OX_REQUIRED_RESULT;
67  template <typename T> LocalizedText &arg(T) OX_REQUIRED_RESULT;
68 private:
69  std::wstring _text;
70  unsigned _nextArg;
71  LocalizedText(const std::wstring &, unsigned);
72 };
73 
77 inline LocalizedText::LocalizedText(const std::wstring &text)
78  : _text(text), _nextArg(0)
79 {
80  // Empty by design.
81 }
82 
86 inline LocalizedText::LocalizedText(const std::wstring &text, unsigned replaced)
87  : _text(text), _nextArg(replaced + 1)
88 {
89  // Empty by design.
90 }
91 
96 inline LocalizedText::operator std::wstring const& () const
97 {
98  return _text;
99 }
100 
107 template <typename T>
109 {
110  std::wostringstream os;
111  os << '{' << _nextArg << '}';
112  std::wstring marker(os.str());
113  size_t pos = _text.find(marker);
114  if (std::string::npos == pos)
115  return *this;
116  std::wstring ntext(_text);
117  os.str(L"");
118  os << val;
119  std::wstring tval(os.str());
120  for (/*empty*/ ; std::wstring::npos != pos; pos = ntext.find(marker, pos + tval.length()))
121  {
122  ntext.replace(pos, marker.length(), tval);
123  }
124  return LocalizedText(ntext, _nextArg);
125 }
126 
133 template <typename T>
135 {
136  std::wostringstream os;
137  os << '{' << _nextArg << '}';
138  std::wstring marker(os.str());
139  size_t pos = _text.find(marker);
140  if (std::string::npos != pos)
141  {
142  os.str(L"");
143  os << val;
144  std::wstring tval(os.str());
145  for (/*empty*/ ; std::wstring::npos != pos; pos = _text.find(marker, pos + tval.length()))
146  {
147  _text.replace(pos, marker.length(), tval);
148  }
149  ++_nextArg;
150  }
151  return *this;
152 }
153 
155 inline std::wostream &operator<<(std::wostream &os, const LocalizedText &txt)
156 {
157  os << static_cast<std::wstring const &>(txt);
158  return os;
159 }
160 }
161 
162 #endif
LocalizedText()
Create the empty string.
Definition: LocalizedText.h:51
std::string asUTF8() const OX_REQUIRED_RESULT
Return the UTF-8 representation of this string.
Definition: LocalizedText.cpp:72
#define OX_REQUIRED_RESULT
This is used to enable warning of unused results, to warn the user of costly function calls...
Definition: LocalizedText.h:35
A string that is already translated.
Definition: LocalizedText.h:45
LocalizedText arg(std::wstring const &) const OX_REQUIRED_RESULT
Replace next argument.
Definition: LocalizedText.cpp:30
const wchar_t * c_str() const OX_REQUIRED_RESULT
Get a pointer to underlying wchat_t data.
Definition: LocalizedText.h:57