OpenXcom  1.0
Open-source clone of the original X-Com
LocalizedText.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * Copyright 2010-2016 OpenXcom Developers.
4  *
5  * This file is part of OpenXcom.
6  *
7  * OpenXcom is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * OpenXcom is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
19  */
20 #include <string>
21 #include <sstream>
22 
24 
29 #ifndef OX_REQUIRED_RESULT
30 # if defined(__GNUC_) && !defined(__INTEL_COMPILER) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
31 # define OX_REQUIRED_RESULT __attribute__ ((warn_unused_result))
32 # else
33 # define OX_REQUIRED_RESULT
34 # endif
35 #endif
36 namespace OpenXcom
37 {
38 
44 {
45 public:
47  LocalizedText(const std::wstring &);
49  LocalizedText() : _nextArg(1) { /* Empty by design. */ }
51  operator std::wstring const&() const OX_REQUIRED_RESULT;
53  std::string asUTF8() const OX_REQUIRED_RESULT;
55  const wchar_t *c_str() const OX_REQUIRED_RESULT { return _text.c_str(); }
56 
57  // Argument substitution.
59  LocalizedText arg(const std::wstring &) const OX_REQUIRED_RESULT;
60  LocalizedText &arg(const std::wstring &) OX_REQUIRED_RESULT;
61  LocalizedText arg(const std::string &) const OX_REQUIRED_RESULT;
62  LocalizedText &arg(const std::string &) OX_REQUIRED_RESULT;
63  template <typename T> LocalizedText arg(T) const OX_REQUIRED_RESULT;
64  template <typename T> LocalizedText &arg(T) OX_REQUIRED_RESULT;
65 private:
66  std::wstring _text;
67  unsigned _nextArg;
68  LocalizedText(const std::wstring &, unsigned);
69 };
70 
74 inline LocalizedText::LocalizedText(const std::wstring &text)
75  : _text(text), _nextArg(0)
76 {
77  // Empty by design.
78 }
79 
83 inline LocalizedText::LocalizedText(const std::wstring &text, unsigned replaced)
84  : _text(text), _nextArg(replaced + 1)
85 {
86  // Empty by design.
87 }
88 
93 inline LocalizedText::operator std::wstring const&() const
94 {
95  return _text;
96 }
97 
104 template <typename T>
106 {
107  std::wostringstream os;
108  os << '{' << _nextArg << '}';
109  std::wstring marker(os.str());
110  size_t pos = _text.find(marker);
111  if (std::string::npos == pos)
112  return *this;
113  std::wstring ntext(_text);
114  os.str(L"");
115  os << val;
116  std::wstring tval(os.str());
117  for (/*empty*/ ; std::wstring::npos != pos; pos = ntext.find(marker, pos + tval.length()))
118  {
119  ntext.replace(pos, marker.length(), tval);
120  }
121  return LocalizedText(ntext, _nextArg);
122 }
123 
130 template <typename T>
132 {
133  std::wostringstream os;
134  os << '{' << _nextArg << '}';
135  std::wstring marker(os.str());
136  size_t pos = _text.find(marker);
137  if (std::string::npos != pos)
138  {
139  os.str(L"");
140  os << val;
141  std::wstring tval(os.str());
142  for (/*empty*/ ; std::wstring::npos != pos; pos = _text.find(marker, pos + tval.length()))
143  {
144  _text.replace(pos, marker.length(), tval);
145  }
146  ++_nextArg;
147  }
148  return *this;
149 }
150 
152 inline std::wostream &operator<<(std::wostream &os, const LocalizedText &txt)
153 {
154  os << static_cast<std::wstring const &>(txt);
155  return os;
156 }
157 
158 }
LocalizedText()
Create the empty string.
Definition: LocalizedText.h:49
std::string asUTF8() const OX_REQUIRED_RESULT
Return the UTF-8 representation of this string.
Definition: LocalizedText.cpp:82
#define OX_REQUIRED_RESULT
This is used to enable warning of unused results, to warn the user of costly function calls...
Definition: LocalizedText.h:33
A string that is already translated.
Definition: LocalizedText.h:43
LocalizedText arg(const std::wstring &) 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:55
Definition: BaseInfoState.cpp:40