libDwm-0.9.45
DwmStringUtils.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2005-2007, 2016
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
39//---------------------------------------------------------------------------
40
41#ifndef _DWMSTRINGUTILS_HH_
42#define _DWMSTRINGUTILS_HH_
43
44#include <cerrno>
45#include <cstdlib>
46#include <climits>
47#include <iomanip>
48#include <iostream>
49#include <limits>
50#include <sstream>
51#include <stdexcept>
52#include <string>
53#include <typeinfo>
54#include <vector>
55
56#include "DwmTypeName.hh"
57
58namespace Dwm {
59
60 namespace StringUtils {
61
62 //------------------------------------------------------------------------
65 //------------------------------------------------------------------------
66 bool HasSuffix(const std::string & str,
67 const std::string & suffix);
68
69 //------------------------------------------------------------------------
75 //------------------------------------------------------------------------
76 bool StringToVector(const std::string & s, char sep,
77 std::vector<std::string> & result,
78 bool ignoreEmpty = true);
79
80 //------------------------------------------------------------------------
82 //------------------------------------------------------------------------
84 : public std::runtime_error
85 {
86 public:
87 //----------------------------------------------------------------------
89 //----------------------------------------------------------------------
90 BadStringConversion(const std::string & s)
91 : std::runtime_error(s)
92 {}
93 };
94
95 //------------------------------------------------------------------------
97 //------------------------------------------------------------------------
98 template <typename T>
99 bool StringToIntegral(const std::string & s, T & target, int base = 0)
100 {
101 bool rc = false;
102
103 if (std::numeric_limits<T>::is_signed) {
104 // target is signed
105 char *endptr = 0;
106 errno = 0;
107 long long val = strtoll(s.c_str(), &endptr, base);
108 if ((! errno) && (endptr != s.c_str()) &&
109 (val <= static_cast<long long>(std::numeric_limits<T>::max())) &&
110 (val >= static_cast<long long>(std::numeric_limits<T>::min()))) {
111 target = static_cast<T>(val);
112 rc = true;
113 }
114 }
115 else {
116 // target is unsigned
117 char *endptr = 0;
118 errno = 0;
119 unsigned long long val = strtoull(s.c_str(), &endptr, base);
120 if ((! errno) && (endptr != s.c_str()) &&
121 (val <= static_cast<T>(std::numeric_limits<T>::max())) &&
122 (val >= static_cast<T>(std::numeric_limits<T>::min()))) {
123 target = static_cast<T>(val);
124 rc = true;
125 }
126 }
127 return(rc);
128 }
129
130 //------------------------------------------------------------------------
132 //------------------------------------------------------------------------
133 template <typename T>
134 bool StringTo(const std::string & s, T & target)
135 {
136 bool rc = false;
137
138 if (! std::numeric_limits<T>::digits10) {
139 // target is a non-integer type
140 std::istringstream is(s);
141 if ((is >> target)) {
142 rc = true;
143 }
144 }
145 return(rc);
146 }
147
148 //------------------------------------------------------------------------
150 //------------------------------------------------------------------------
151 bool StringTo(const std::string & s, std::string & target);
152
153 //------------------------------------------------------------------------
155 //------------------------------------------------------------------------
156 bool StringTo(const std::string & s, char & target, int base = 0);
157
158 //------------------------------------------------------------------------
160 //------------------------------------------------------------------------
161 bool StringTo(const std::string & s, signed char & target, int base = 0);
162
163 //------------------------------------------------------------------------
165 //------------------------------------------------------------------------
166 bool StringTo(const std::string & s, unsigned char & target,
167 int base = 0);
168
169 //------------------------------------------------------------------------
171 //------------------------------------------------------------------------
172 bool StringTo(const std::string & s, short & target, int base = 0);
173
174 //------------------------------------------------------------------------
176 //------------------------------------------------------------------------
177 bool StringTo(const std::string & s, unsigned short & target,
178 int base = 0);
179
180 //------------------------------------------------------------------------
182 //------------------------------------------------------------------------
183 bool StringTo(const std::string & s, int & target, int base = 0);
184
185 //------------------------------------------------------------------------
187 //------------------------------------------------------------------------
188 bool StringTo(const std::string & s, unsigned int & target, int base = 0);
189
190 //------------------------------------------------------------------------
192 //------------------------------------------------------------------------
193 bool StringTo(const std::string & s, long & target, int base = 0);
194
195 //------------------------------------------------------------------------
197 //------------------------------------------------------------------------
198 bool StringTo(const std::string & s, unsigned long & target,
199 int base = 0);
200
201 //------------------------------------------------------------------------
203 //------------------------------------------------------------------------
204 bool StringTo(const std::string & s, long long & target, int base = 0);
205
206 //------------------------------------------------------------------------
208 //------------------------------------------------------------------------
209 bool StringTo(const std::string & s, unsigned long long & target,
210 int base = 0);
211
212 //------------------------------------------------------------------------
214 //------------------------------------------------------------------------
215 bool StringTo(const std::string & s, float & target);
216
217 //------------------------------------------------------------------------
219 //------------------------------------------------------------------------
220 bool StringTo(const std::string & s, double & target);
221
222 //------------------------------------------------------------------------
224 //------------------------------------------------------------------------
225 bool StringTo(const std::string & s, bool & target);
226
227 //------------------------------------------------------------------------
229 //------------------------------------------------------------------------
230 template <typename T>
231 static T StringTo(const std::string & s)
232 {
233 T x;
234 if (! StringTo(s, x)) {
235 std::ostringstream os;
236 os << "\"" << s << "\" -> " << TypeName(x);
237 throw(BadStringConversion(os.str()));
238 }
239 return(x);
240 }
241
242 //------------------------------------------------------------------------
244 //------------------------------------------------------------------------
245 template <typename T>
246 std::string ToString(const T & x,
247 std::ios_base & (*iom)(std::ios_base &) = std::dec)
248 {
249 std::ostringstream os;
250 os << std::setprecision(500);
251 if (iom)
252 os << iom;
253 os << x;
254 return(os.str());
255 }
256
257
258 } // namespace StringUtils
259
260} // namespace Dwm
261
262#endif // _DWMSTRINGUTILS_HH_
263
264//---------------------------- emacs settings -----------------------------
265// Local Variables:
266// mode: C++
267// tab-width: 2
268// indent-tabs-mode: nil
269// c-basic-offset: 2
270// End:
271//-------------------------------------------------------------------------
bool HasSuffix(const std::string &str, const std::string &suffix)
Returns true if str ends with suffix.
bool StringToVector(const std::string &s, char sep, std::vector< std::string > &result, bool ignoreEmpty=true)
Populates result with substrings from s, where each substring is separated by sep in s.
Hackish support for getting the name of a given object's type, since std::type_info::name() returns a...
std::string TypeName(const T &x)
Returns the name of type T.
Definition DwmTypeName.hh:83
Simple exception class for StringTo().
Definition DwmStringUtils.hh:85