diff options
author | Mathias Gumz <akira at fluxbox dot org> | 2010-09-08 19:42:48 (GMT) |
---|---|---|
committer | Mathias Gumz <akira at fluxbox dot org> | 2010-09-08 19:42:48 (GMT) |
commit | b76be98227bd6e62bc7e5a5fc1c3af0a3dd01b9c (patch) | |
tree | 11dfb540cdd28037d08c1b53157561a8a882125e /src/FbTk | |
parent | 6ecfa0ef3e20089e7fa3f76303847ae288e9d112 (diff) | |
download | fluxbox_paul-b76be98227bd6e62bc7e5a5fc1c3af0a3dd01b9c.zip fluxbox_paul-b76be98227bd6e62bc7e5a5fc1c3af0a3dd01b9c.tar.bz2 |
improved extract*Number functions from FbTk/StringUtil.cc
Diffstat (limited to 'src/FbTk')
-rw-r--r-- | src/FbTk/StringUtil.cc | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc index 4678f03..691b600 100644 --- a/src/FbTk/StringUtil.cc +++ b/src/FbTk/StringUtil.cc | |||
@@ -63,13 +63,14 @@ using std::transform; | |||
63 | 63 | ||
64 | namespace { | 64 | namespace { |
65 | 65 | ||
66 | int extractLongNumber(const char* in, long long int& out) { | 66 | template <typename T> |
67 | int extractBigNumber(const char* in, T (*extractFunc)(const char*, char**, int), T& out) { | ||
67 | 68 | ||
68 | errno = 0; | 69 | errno = 0; |
69 | 70 | ||
70 | int ret = 0; | 71 | int ret = 0; |
71 | char* end = 0; | 72 | char* end = 0; |
72 | long long int result = strtoll(in, &end, 0); | 73 | T result = extractFunc(in, &end, 0); |
73 | 74 | ||
74 | if (errno == 0 && end != in) { | 75 | if (errno == 0 && end != in) { |
75 | out = result; | 76 | out = result; |
@@ -80,11 +81,11 @@ int extractLongNumber(const char* in, long long int& out) { | |||
80 | } | 81 | } |
81 | 82 | ||
82 | template<typename T> | 83 | template<typename T> |
83 | int extractNumber(const std::string& in, T& out) { | 84 | int extractSignedNumber(const std::string& in, T& out) { |
84 | 85 | ||
85 | long long int result = 0; | 86 | long long int result = 0; |
86 | 87 | ||
87 | if (::extractLongNumber(in.c_str(), result) && out >= 0) { | 88 | if (::extractBigNumber(in.c_str(), strtoll, result)) { |
88 | out = static_cast<T>(result); | 89 | out = static_cast<T>(result); |
89 | return 1; | 90 | return 1; |
90 | } | 91 | } |
@@ -92,6 +93,21 @@ int extractNumber(const std::string& in, T& out) { | |||
92 | return 0; | 93 | return 0; |
93 | } | 94 | } |
94 | 95 | ||
96 | template<typename T> | ||
97 | int extractUnsignedNumber(const std::string& in, T& out) { | ||
98 | |||
99 | unsigned long long int result = 0; | ||
100 | |||
101 | if (::extractBigNumber(in.c_str(), strtoull, result) && result >= 0) { | ||
102 | out = static_cast<T>(result); | ||
103 | return 1; | ||
104 | } | ||
105 | |||
106 | return 0; | ||
107 | } | ||
108 | |||
109 | |||
110 | |||
95 | } | 111 | } |
96 | 112 | ||
97 | 113 | ||
@@ -100,11 +116,11 @@ namespace FbTk { | |||
100 | namespace StringUtil { | 116 | namespace StringUtil { |
101 | 117 | ||
102 | int extractNumber(const std::string& in, int& out) { | 118 | int extractNumber(const std::string& in, int& out) { |
103 | return ::extractNumber<int>(in, out); | 119 | return ::extractSignedNumber<int>(in, out); |
104 | } | 120 | } |
105 | 121 | ||
106 | int extractNumber(const std::string& in, unsigned int& out) { | 122 | int extractNumber(const std::string& in, unsigned int& out) { |
107 | return ::extractNumber<unsigned int>(in, out); | 123 | return ::extractUnsignedNumber<unsigned int>(in, out); |
108 | } | 124 | } |
109 | 125 | ||
110 | std::string number2String(int num) { | 126 | std::string number2String(int num) { |