-
Notifications
You must be signed in to change notification settings - Fork 17
/
TypeConversion.h
87 lines (73 loc) · 1.81 KB
/
TypeConversion.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef _TYPECONVERSION_H_
#define _TYPECONVERSION_H_
#include <errno.h>
#include <limits.h>
#include <math.h> // for HUGE_VALH, HUGE_VALL
#include <sstream>
// convert double/int/byte to string type
template<class T>
std::string toString(T i){
std::stringstream ss;
ss << i;
return ss.str();
}
// convert std::string to integer
// @return true if conversion succeed
bool str2int(const char* input, int* output) {
char* endptr;
long val;
errno = 0;
val = strtol(input, &endptr, 10);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
|| (errno != 0 && val == 0)) {
perror("strtol");
return false;
}
if (endptr == input) {
// no digits found
return false;
}
*output = val;
return true;
}
bool str2int(const std::string& input, int* output) {
return str2int(input.c_str(), output);
}
int toInt(const char* input) {
return (atoi(input));
};
int toInt(const std::string& input) {
return (atoi(input.c_str()));
};
float toFloat(const char* input) {
return (atof(input));
};
float toFloat(const std::string& input) {
return (atof(input.c_str()));
};
double toDouble(const char* input) {
return (atof(input));
};
double toDouble(const std::string& input) {
return (atof(input.c_str()));
};
// convert std::string to double
// @return true if conversion succeed
bool str2double(const char* input, double* output) {
char* endptr;
double val;
errno = 0;
val = strtod(input, &endptr);
if ((errno == ERANGE && (val == HUGE_VALF || val == HUGE_VALL))
|| (errno != 0 && val == 0.)) {
perror("strtod");
return false;
}
if (endptr == input) {
// no digits found
return false;
}
*output = val;
return true;
}
#endif /* _TYPECONVERSION_H_ */