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