The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
util.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2005 - 2016 by Philippe Plantier <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * String-routines - Templates for lexical_cast & lexical_cast_default.
18  */
19 
20 #include "util.hpp"
21 
22 #include <cstdlib>
23 template<>
24 size_t lexical_cast<size_t, const std::string&>(const std::string& a)
25 {
26  char* endptr;
27  size_t res = strtoul(a.c_str(), &endptr, 10);
28 
29  if (a.empty() || *endptr != '\0') {
30  throw bad_lexical_cast();
31  } else {
32  return res;
33  }
34 }
35 
36 #ifndef MSVC_DO_UNIT_TESTS
37 template<>
38 size_t lexical_cast<size_t, const char*>(const char* a)
39 {
40  char* endptr;
41  size_t res = strtoul(a, &endptr, 10);
42 
43  if (*a == '\0' || *endptr != '\0') {
44  throw bad_lexical_cast();
45  } else {
46  return res;
47  }
48 }
49 #endif
50 template<>
51 size_t lexical_cast_default<size_t, const std::string&>(const std::string& a, size_t def)
52 {
53  if(a.empty()) {
54  return def;
55  }
56 
57  char* endptr;
58  size_t res = strtoul(a.c_str(), &endptr, 10);
59 
60  if (*endptr != '\0') {
61  return def;
62  } else {
63  return res;
64  }
65 }
66 template<>
67 size_t lexical_cast_default<size_t, const char*>(const char* a, size_t def)
68 {
69  if(*a == '\0') {
70  return def;
71  }
72 
73  char* endptr;
74  size_t res = strtoul(a, &endptr, 10);
75 
76  if (*endptr != '\0') {
77  return def;
78  } else {
79  return res;
80  }
81 }
82 template<>
83 long lexical_cast<long, const std::string&>(const std::string& a)
84 {
85  char* endptr;
86  long res = strtol(a.c_str(), &endptr, 10);
87 
88  if (a.empty() || *endptr != '\0') {
89  throw bad_lexical_cast();
90  } else {
91  return res;
92  }
93 }
94 
95 template<>
96 long lexical_cast<long, const char*>(const char* a)
97 {
98  char* endptr;
99  long res = strtol(a, &endptr, 10);
100 
101  if (*a == '\0' || *endptr != '\0') {
102  throw bad_lexical_cast();
103  } else {
104  return res;
105  }
106 }
107 template<>
108 long lexical_cast_default<long, const std::string&>(const std::string& a, long def)
109 {
110  if(a.empty()) {
111  return def;
112  }
113 
114  char* endptr;
115  long res = strtol(a.c_str(), &endptr, 10);
116 
117  if (*endptr != '\0') {
118  return def;
119  } else {
120  return res;
121  }
122 }
123 template<>
124 long lexical_cast_default<long, const char*>(const char* a, long def)
125 {
126  if(*a == '\0') {
127  return def;
128  }
129 
130  char* endptr;
131  long res = strtol(a, &endptr, 10);
132 
133  if (*endptr != '\0') {
134  return def;
135  } else {
136  return res;
137  }
138 }
139 template<>
141 {
142  char* endptr;
143  int res = strtol(a.c_str(), &endptr, 10);
144 
145  if (a.empty() || *endptr != '\0') {
146  throw bad_lexical_cast();
147  } else {
148  return res;
149  }
150 }
151 
152 #ifndef MSVC_DO_UNIT_TESTS
153 template<>
154 int lexical_cast<int, const char*>(const char* a)
155 {
156  char* endptr;
157  int res = strtol(a, &endptr, 10);
158 
159  if (*a == '\0' || *endptr != '\0') {
160  throw bad_lexical_cast();
161  } else {
162  return res;
163  }
164 }
165 #endif
166 
167 template<>
168 int lexical_cast_default<int, const std::string&>(const std::string& a, int def)
169 {
170  if(a.empty()) {
171  return def;
172  }
173 
174  char* endptr;
175  int res = strtol(a.c_str(), &endptr, 10);
176 
177  if (*endptr != '\0') {
178  return def;
179  } else {
180  return res;
181  }
182 }
183 
184 template<>
185 int lexical_cast_default<int, const char*>(const char* a, int def)
186 {
187  if(*a == '\0') {
188  return def;
189  }
190 
191  char* endptr;
192  int res = strtol(a, &endptr, 10);
193 
194  if (*endptr != '\0') {
195  return def;
196  } else {
197  return res;
198  }
199 }
200 
201 template<>
202 double lexical_cast<double, const std::string&>(const std::string& a)
203 {
204  char* endptr;
205  double res = strtod(a.c_str(), &endptr);
206 
207  if (a.empty() || *endptr != '\0') {
208  throw bad_lexical_cast();
209  } else {
210  return res;
211  }
212 }
213 
214 template<>
215 double lexical_cast<double, const char*>(const char* a)
216 {
217  char* endptr;
218  double res = strtod(a, &endptr);
219 
220  if (*a == '\0' || *endptr != '\0') {
221  throw bad_lexical_cast();
222  } else {
223  return res;
224  }
225 }
226 
227 template<>
228 double lexical_cast_default<double, const std::string&>(const std::string& a, double def)
229 {
230  char* endptr;
231  double res = strtod(a.c_str(), &endptr);
232 
233  if (a.empty() || *endptr != '\0') {
234  return def;;
235  } else {
236  return res;
237  }
238 }
239 
240 template<>
241 double lexical_cast_default<double, const char*>(const char* a, double def)
242 {
243  char* endptr;
244  double res = strtod(a, &endptr);
245 
246  if (*a == '\0' || *endptr != '\0') {
247  return def;
248  } else {
249  return res;
250  }
251 }
252 
253 template<>
254 float lexical_cast<float, const std::string&>(const std::string& a)
255 {
256  char* endptr;
257  float res = static_cast<float>(strtod(a.c_str(), &endptr));
258 
259  if (a.empty() || *endptr != '\0') {
260  throw bad_lexical_cast();
261  } else {
262  return res;
263  }
264 }
265 
266 template<>
267 float lexical_cast<float, const char*>(const char* a)
268 {
269  char* endptr;
270  float res = static_cast<float>(strtod(a, &endptr));
271 
272  if (*a == '\0' || *endptr != '\0') {
273  throw bad_lexical_cast();
274  } else {
275  return res;
276  }
277 }
278 template<>
279 float lexical_cast_default<float, const std::string&>(const std::string& a, float def)
280 {
281  char* endptr;
282  float res = static_cast<float>(strtod(a.c_str(), &endptr));
283 
284  if (a.empty() || *endptr != '\0') {
285  return def;;
286  } else {
287  return res;
288  }
289 }
290 
291 template<>
292 float lexical_cast_default<float, const char*>(const char* a, float def)
293 {
294  char* endptr;
295  float res = static_cast<float>(strtod(a, &endptr));
296 
297  if (*a == '\0' || *endptr != '\0') {
298  return def;
299  } else {
300  return res;
301  }
302 }
303 
int lexical_cast_default< int, const char * >(const char *a, int def)
Definition: util.cpp:185
To lexical_cast(From value)
Lexical cast converts one type to another.
size_t lexical_cast_default< size_t, const char * >(const char *a, size_t def)
Definition: util.cpp:67
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
long lexical_cast_default< long, const char * >(const char *a, long def)
Definition: util.cpp:124
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
Templates and utility-routines for strings and numbers.
GLuint res
Definition: glew.h:9258
float lexical_cast_default< float, const char * >(const char *a, float def)
Definition: util.cpp:292
double lexical_cast_default< double, const char * >(const char *a, double def)
Definition: util.cpp:241
Thrown when a lexical_cast fails.
GLsizei const GLcharARB ** string
Definition: glew.h:4503