@@ -104,18 +104,15 @@ inline bool convert::stringIsNumberWithErrorMessage(const char* v, const NumberT
104
104
std::cerr << v << " is not " ;
105
105
switch (type)
106
106
{
107
- case NumberType::FLOAT:
108
- {
107
+ case NumberType::FLOAT: {
109
108
std::cerr << " a float" ;
110
109
break ;
111
110
}
112
- case NumberType::INTEGER:
113
- {
111
+ case NumberType::INTEGER: {
114
112
std::cerr << " a signed integer" ;
115
113
break ;
116
114
}
117
- case NumberType::UNSIGNED_INTEGER:
118
- {
115
+ case NumberType::UNSIGNED_INTEGER: {
119
116
std::cerr << " an unsigned integer" ;
120
117
break ;
121
118
}
@@ -184,79 +181,79 @@ inline bool convert::fromString<long double>(const char* v, long double& dest)
184
181
}
185
182
186
183
template <>
187
- inline bool convert::fromString<unsigned int >(const char * v, unsigned int & dest)
184
+ inline bool convert::fromString<uint64_t >(const char * v, uint64_t & dest)
188
185
{
189
186
if (!stringIsNumberWithErrorMessage (v, NumberType::UNSIGNED_INTEGER))
190
187
{
191
188
return false ;
192
189
}
193
190
194
- auto call = makeSmartC (strtoul , ReturnMode::PRE_DEFINED_ERROR_CODE, {ULONG_MAX }, {}, v, nullptr , 10 );
191
+ auto call = makeSmartC (strtoull , ReturnMode::PRE_DEFINED_ERROR_CODE, {ULLONG_MAX }, {}, v, nullptr , 10 );
195
192
if (call.hasErrors ())
196
193
{
197
194
return false ;
198
195
}
199
196
200
- if (call.getReturnValue () > UINT_MAX )
197
+ if (call.getReturnValue () > std::numeric_limits< uint64_t >:: max () )
201
198
{
202
- std::cerr << call.getReturnValue () << " too large, unsigned integer overflow" << std::endl;
199
+ std::cerr << call.getReturnValue () << " too large, uint64_t overflow" << std::endl;
203
200
return false ;
204
201
}
205
202
206
- dest = static_cast <unsigned int >(call.getReturnValue ());
203
+ dest = static_cast <uint64_t >(call.getReturnValue ());
207
204
return true ;
208
205
}
209
206
210
207
template <>
211
- inline bool convert::fromString<unsigned short >(const char * v, unsigned short & dest)
208
+ inline bool convert::fromString<uint32_t >(const char * v, uint32_t & dest)
212
209
{
213
210
if (!stringIsNumberWithErrorMessage (v, NumberType::UNSIGNED_INTEGER))
214
211
{
215
212
return false ;
216
213
}
217
214
218
- auto call = makeSmartC (strtoul , ReturnMode::PRE_DEFINED_ERROR_CODE, {ULONG_MAX }, {}, v, nullptr , 10 );
215
+ auto call = makeSmartC (strtoull , ReturnMode::PRE_DEFINED_ERROR_CODE, {ULLONG_MAX }, {}, v, nullptr , 10 );
219
216
if (call.hasErrors ())
220
217
{
221
218
return false ;
222
219
}
223
220
224
- if (call.getReturnValue () > USHRT_MAX )
221
+ if (call.getReturnValue () > std::numeric_limits< uint32_t >:: max () )
225
222
{
226
- std::cerr << call.getReturnValue () << " too large, unsigned short overflow" << std::endl;
223
+ std::cerr << call.getReturnValue () << " too large, uint32_t overflow" << std::endl;
227
224
return false ;
228
225
}
229
226
230
- dest = static_cast <unsigned short >(call.getReturnValue ());
227
+ dest = static_cast <uint32_t >(call.getReturnValue ());
231
228
return true ;
232
229
}
233
230
234
231
template <>
235
- inline bool convert::fromString<short >(const char * v, short & dest)
232
+ inline bool convert::fromString<uint16_t >(const char * v, uint16_t & dest)
236
233
{
237
- if (!stringIsNumberWithErrorMessage (v, NumberType::INTEGER ))
234
+ if (!stringIsNumberWithErrorMessage (v, NumberType::UNSIGNED_INTEGER ))
238
235
{
239
236
return false ;
240
237
}
241
238
242
- auto call = makeSmartC (strtol , ReturnMode::PRE_DEFINED_ERROR_CODE, {LONG_MAX, LONG_MIN }, {}, v, nullptr , 10 );
239
+ auto call = makeSmartC (strtoul , ReturnMode::PRE_DEFINED_ERROR_CODE, {ULONG_MAX }, {}, v, nullptr , 10 );
243
240
if (call.hasErrors ())
244
241
{
245
242
return false ;
246
243
}
247
244
248
- if (call.getReturnValue () > SHRT_MAX || call. getReturnValue () < SHRT_MIN )
245
+ if (call.getReturnValue () > std::numeric_limits< uint16_t >:: max () )
249
246
{
250
- std::cerr << call.getReturnValue () << " too large, short integer overflow" << std::endl;
247
+ std::cerr << call.getReturnValue () << " too large, uint16_t overflow" << std::endl;
251
248
return false ;
252
249
}
253
250
254
- dest = static_cast <short >(call.getReturnValue ());
251
+ dest = static_cast <uint16_t >(call.getReturnValue ());
255
252
return true ;
256
253
}
257
254
258
255
template <>
259
- inline bool convert::fromString<unsigned long >(const char * v, unsigned long & dest)
256
+ inline bool convert::fromString<uint8_t >(const char * v, uint8_t & dest)
260
257
{
261
258
if (!stringIsNumberWithErrorMessage (v, NumberType::UNSIGNED_INTEGER))
262
259
{
@@ -269,54 +266,68 @@ inline bool convert::fromString<unsigned long>(const char* v, unsigned long& des
269
266
return false ;
270
267
}
271
268
272
- dest = call.getReturnValue ();
269
+ if (call.getReturnValue () > std::numeric_limits<uint8_t >::max ())
270
+ {
271
+ std::cerr << call.getReturnValue () << " too large, uint8_t overflow" << std::endl;
272
+ return false ;
273
+ }
274
+
275
+ dest = static_cast <uint8_t >(call.getReturnValue ());
273
276
return true ;
274
277
}
275
278
276
279
template <>
277
- inline bool convert::fromString<unsigned long long >(const char * v, unsigned long long & dest)
280
+ inline bool convert::fromString<int64_t >(const char * v, int64_t & dest)
278
281
{
279
- if (!stringIsNumberWithErrorMessage (v, NumberType::UNSIGNED_INTEGER ))
282
+ if (!stringIsNumberWithErrorMessage (v, NumberType::INTEGER ))
280
283
{
281
284
return false ;
282
285
}
283
286
284
- auto call = makeSmartC (strtoull , ReturnMode::PRE_DEFINED_ERROR_CODE, {ULLONG_MAX }, {}, v, nullptr , 10 );
287
+ auto call = makeSmartC (strtoll , ReturnMode::PRE_DEFINED_ERROR_CODE, {LLONG_MAX, LLONG_MIN }, {}, v, nullptr , 10 );
285
288
if (call.hasErrors ())
286
289
{
287
290
return false ;
288
291
}
289
292
290
- dest = call.getReturnValue ();
293
+ if (call.getReturnValue () > std::numeric_limits<int64_t >::max ()
294
+ || call.getReturnValue () < std::numeric_limits<int64_t >::min ())
295
+ {
296
+ std::cerr << call.getReturnValue () << " is out of range, int64_t overflow" << std::endl;
297
+ return false ;
298
+ }
299
+
300
+ dest = static_cast <int64_t >(call.getReturnValue ());
291
301
return true ;
292
302
}
293
303
294
304
template <>
295
- inline bool convert::fromString<int >(const char * v, int & dest)
305
+ inline bool convert::fromString<int32_t >(const char * v, int32_t & dest)
296
306
{
297
307
if (!stringIsNumberWithErrorMessage (v, NumberType::INTEGER))
298
308
{
299
309
return false ;
300
310
}
301
311
302
- auto call = makeSmartC (strtol , ReturnMode::PRE_DEFINED_ERROR_CODE, {LONG_MAX, LONG_MIN }, {}, v, nullptr , 10 );
312
+ auto call = makeSmartC (strtoll , ReturnMode::PRE_DEFINED_ERROR_CODE, {LLONG_MAX, LLONG_MIN }, {}, v, nullptr , 10 );
303
313
if (call.hasErrors ())
304
314
{
305
315
return false ;
306
316
}
307
317
308
- if (call.getReturnValue () < INT_MIN || call.getReturnValue () > INT_MAX)
318
+ if (call.getReturnValue () > std::numeric_limits<int32_t >::max ()
319
+ || call.getReturnValue () < std::numeric_limits<int32_t >::min ())
309
320
{
310
- std::cerr << call.getReturnValue () << " too large, integer overflow! " << std::endl;
321
+ std::cerr << call.getReturnValue () << " is out of range, int32_t overflow" << std::endl;
311
322
return false ;
312
323
}
313
324
314
- dest = static_cast <int >(call.getReturnValue ());
325
+ dest = static_cast <int32_t >(call.getReturnValue ());
315
326
return true ;
316
327
}
317
328
318
329
template <>
319
- inline bool convert::fromString<long >(const char * v, long & dest)
330
+ inline bool convert::fromString<int16_t >(const char * v, int16_t & dest)
320
331
{
321
332
if (!stringIsNumberWithErrorMessage (v, NumberType::INTEGER))
322
333
{
@@ -329,25 +340,39 @@ inline bool convert::fromString<long>(const char* v, long& dest)
329
340
return false ;
330
341
}
331
342
332
- dest = call.getReturnValue ();
343
+ if (call.getReturnValue () > std::numeric_limits<int16_t >::max ()
344
+ || call.getReturnValue () < std::numeric_limits<int16_t >::min ())
345
+ {
346
+ std::cerr << call.getReturnValue () << " is out of range, int16_t overflow" << std::endl;
347
+ return false ;
348
+ }
349
+
350
+ dest = static_cast <int16_t >(call.getReturnValue ());
333
351
return true ;
334
352
}
335
353
336
354
template <>
337
- inline bool convert::fromString<long long >(const char * v, long long & dest)
355
+ inline bool convert::fromString<int8_t >(const char * v, int8_t & dest)
338
356
{
339
357
if (!stringIsNumberWithErrorMessage (v, NumberType::INTEGER))
340
358
{
341
359
return false ;
342
360
}
343
361
344
- auto call = makeSmartC (strtoll , ReturnMode::PRE_DEFINED_ERROR_CODE, {LLONG_MAX, LLONG_MIN }, {}, v, nullptr , 10 );
362
+ auto call = makeSmartC (strtol , ReturnMode::PRE_DEFINED_ERROR_CODE, {LONG_MAX, LONG_MIN }, {}, v, nullptr , 10 );
345
363
if (call.hasErrors ())
346
364
{
347
365
return false ;
348
366
}
349
367
350
- dest = call.getReturnValue ();
368
+ if (call.getReturnValue () > std::numeric_limits<int8_t >::max ()
369
+ || call.getReturnValue () < std::numeric_limits<int8_t >::min ())
370
+ {
371
+ std::cerr << call.getReturnValue () << " is out of range, int8_t overflow" << std::endl;
372
+ return false ;
373
+ }
374
+
375
+ dest = static_cast <int8_t >(call.getReturnValue ());
351
376
return true ;
352
377
}
353
378
0 commit comments