aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/StringUtil.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/StringUtil.cc')
-rw-r--r--src/FbTk/StringUtil.cc46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc
index a0e402d..639041b 100644
--- a/src/FbTk/StringUtil.cc
+++ b/src/FbTk/StringUtil.cc
@@ -48,6 +48,11 @@
48 #include <string.h> 48 #include <string.h>
49#endif 49#endif
50 50
51#ifdef HAVE_CERRNO
52 #include <cerrno>
53#else
54 #include <errno.h>
55#endif
51 56
52#include <memory> 57#include <memory>
53#include <algorithm> 58#include <algorithm>
@@ -56,10 +61,51 @@
56using std::string; 61using std::string;
57using std::transform; 62using std::transform;
58 63
64namespace {
65
66int extractLongNumber(const char* in, long long int& out) {
67
68 errno = 0;
69
70 int ret = 0;
71 char* end = 0;
72 long long int result = strtoll(in, &end, 0);
73
74 if (errno == 0 && end != in) {
75 out = result;
76 ret = 1;
77 }
78
79 return ret;
80}
81
82template<typename T>
83int extractNumber(const std::string& in, T& out) {
84
85 long long int result = 0;
86
87 if (::extractLongNumber(in.c_str(), result) && out >= 0) {
88 out = static_cast<T>(result);
89 return 1;
90 }
91
92 return 0;
93}
94
95}
96
97
59namespace FbTk { 98namespace FbTk {
60 99
61namespace StringUtil { 100namespace StringUtil {
62 101
102int extractNumber(const std::string& in, int& out) {
103 return ::extractNumber<int>(in, out);
104}
105
106int extractNumber(const std::string& in, unsigned int& out) {
107 return ::extractNumber<unsigned int>(in, out);
108}
63 109
64std::string number2String(int num) { 110std::string number2String(int num) {
65 char s[128]; 111 char s[128];