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::string &);
49  LocalizedText() : _nextArg(1) { /* Empty by design. */ }
51  operator std::string const&() const OX_REQUIRED_RESULT;
53  const char *c_str() const OX_REQUIRED_RESULT { return _text.c_str(); }
54 
55  // Argument substitution.
57  LocalizedText arg(const std::string &) const OX_REQUIRED_RESULT;
58  LocalizedText &arg(const std::string &) OX_REQUIRED_RESULT;
59  template <typename T> LocalizedText arg(T) const OX_REQUIRED_RESULT;
60  template <typename T> LocalizedText &arg(T) OX_REQUIRED_RESULT;
61 private:
62  std::string _text;
63  unsigned _nextArg;
64  LocalizedText(const std::string &, unsigned);
65 };
66 
70 inline LocalizedText::LocalizedText(const std::string &text)
71  : _text(text), _nextArg(0)
72 {
73  // Empty by design.
74 }
75 
79 inline LocalizedText::LocalizedText(const std::string &text, unsigned replaced)
80  : _text(text), _nextArg(replaced + 1)
81 {
82  // Empty by design.
83 }
84 
89 inline LocalizedText::operator std::string const&() const
90 {
91  return _text;
92 }
93 
100 template <typename T>
102 {
103  std::ostringstream os;
104  os << '{' << _nextArg << '}';
105  std::string marker(os.str());
106  size_t pos = _text.find(marker);
107  if (std::string::npos == pos)
108  return *this;
109  std::string ntext(_text);
110  os.str("");
111  os << val;
112  std::string tval(os.str());
113  for (/*empty*/ ; std::string::npos != pos; pos = ntext.find(marker, pos + tval.length()))
114  {
115  ntext.replace(pos, marker.length(), tval);
116  }
117  return LocalizedText(ntext, _nextArg);
118 }
119 
126 template <typename T>
128 {
129  std::ostringstream os;
130  os << '{' << _nextArg << '}';
131  std::string marker(os.str());
132  size_t pos = _text.find(marker);
133  if (std::string::npos != pos)
134  {
135  os.str("");
136  os << val;
137  std::string tval(os.str());
138  for (/*empty*/ ; std::string::npos != pos; pos = _text.find(marker, pos + tval.length()))
139  {
140  _text.replace(pos, marker.length(), tval);
141  }
142  ++_nextArg;
143  }
144  return *this;
145 }
146 
148 inline std::ostream &operator<<(std::ostream &os, const LocalizedText &txt)
149 {
150  os << static_cast<std::string const &>(txt);
151  return os;
152 }
153 
154 }
LocalizedText()
Create the empty string.
Definition: LocalizedText.h:49
#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::string &) const OX_REQUIRED_RESULT
Replace next argument.
Definition: LocalizedText.cpp:30
const char * c_str() const OX_REQUIRED_RESULT
Get a pointer to underlying char data.
Definition: LocalizedText.h:53
Definition: BaseInfoState.cpp:40