-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompat.h
89 lines (79 loc) · 1.83 KB
/
compat.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
88
89
#ifndef COMPAT_H
#define COMPAT_H
#include <string>
#include <sstream>
#include <stdexcept>
#include <cstdlib>
#include <climits>
#include <cerrno>
#ifndef HAVE_CXX_TO_STRING
namespace std {
template <typename T> inline std::string to_string(const T& n) {
std::ostringstream stm;
stm << n;
return stm.str();
}
}
#endif
#ifndef HAVE_CXX_STOI
namespace std {
inline int stoi(const std::string& str, size_t *idx = 0, int base = 10) {
char *endp;
int val = static_cast<int>(strtol(str.c_str(), &endp, base));
if(endp == str.c_str()) {
throw std::invalid_argument("stoi");
}
if(val == LONG_MAX && errno == ERANGE) {
throw std::out_of_range("stoi");
}
if(idx) {
*idx = endp - str.c_str();
}
return val;
}
}
#endif
#ifndef HAVE_CXX_STOUL
namespace std {
inline unsigned long stoul(const std::string& str, size_t *idx = 0, int base = 10) {
char *endp;
unsigned long val = strtoul(str.c_str(), &endp, base);
if(endp == str.c_str()) {
throw std::invalid_argument("strtoul");
}
if(val == ULONG_MAX && errno == ERANGE) {
throw std::out_of_range("strtoul");
}
if(idx) {
*idx = endp - str.c_str();
}
return val;
}
}
#endif
#ifndef HAVE_CXX_STOULL
namespace std {
inline unsigned long long stoull(const std::string& str, size_t *idx = 0, int base = 10) {
char *endp;
unsigned long long val = strtoull(str.c_str(), &endp, base);
if(endp == str.c_str()) {
throw std::invalid_argument("strtoull");
}
if(val == ULLONG_MAX && errno == ERANGE) {
throw std::out_of_range("strtoull");
}
if(idx) {
*idx = endp - str.c_str();
}
return val;
}
}
#endif
#ifndef HAVE_CXX_STOF
namespace std {
inline float stof(const std::string& str) {
return atof(str.c_str());
}
}
#endif
#endif /* COMPAT_H */