diff options
author | Mathias Gumz <akira at fluxbox dot org> | 2009-10-01 19:16:46 (GMT) |
---|---|---|
committer | Mathias Gumz <akira at fluxbox dot org> | 2009-10-01 19:16:46 (GMT) |
commit | 261ba26d27c3a64a627b6f407e057da0d17b621c (patch) | |
tree | c01a497e688192e0a2049f7181b4cbb3f93fa0da /src | |
parent | 57b6e5a778d597aa279ac72aac38a3b178221931 (diff) | |
download | fluxbox-261ba26d27c3a64a627b6f407e057da0d17b621c.zip fluxbox-261ba26d27c3a64a627b6f407e057da0d17b621c.tar.bz2 |
another little helper for FbTk::StringUtil: extractNumber()
Diffstat (limited to 'src')
-rw-r--r-- | src/FbTk/StringUtil.cc | 46 | ||||
-rw-r--r-- | src/FbTk/StringUtil.hh | 8 | ||||
-rw-r--r-- | src/Layer.hh | 2 |
3 files changed, 55 insertions, 1 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 @@ | |||
56 | using std::string; | 61 | using std::string; |
57 | using std::transform; | 62 | using std::transform; |
58 | 63 | ||
64 | namespace { | ||
65 | |||
66 | int 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 | |||
82 | template<typename T> | ||
83 | int 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 | |||
59 | namespace FbTk { | 98 | namespace FbTk { |
60 | 99 | ||
61 | namespace StringUtil { | 100 | namespace StringUtil { |
62 | 101 | ||
102 | int extractNumber(const std::string& in, int& out) { | ||
103 | return ::extractNumber<int>(in, out); | ||
104 | } | ||
105 | |||
106 | int extractNumber(const std::string& in, unsigned int& out) { | ||
107 | return ::extractNumber<unsigned int>(in, out); | ||
108 | } | ||
63 | 109 | ||
64 | std::string number2String(int num) { | 110 | std::string number2String(int num) { |
65 | char s[128]; | 111 | char s[128]; |
diff --git a/src/FbTk/StringUtil.hh b/src/FbTk/StringUtil.hh index 6493f74..7bc8f80 100644 --- a/src/FbTk/StringUtil.hh +++ b/src/FbTk/StringUtil.hh | |||
@@ -30,6 +30,14 @@ namespace FbTk { | |||
30 | 30 | ||
31 | namespace StringUtil { | 31 | namespace StringUtil { |
32 | 32 | ||
33 | /// \@{ | ||
34 | /// @param in - input string, might be 0xab or 0123 | ||
35 | /// @param out - result if extraction was ok | ||
36 | /// @return 1 - ok, result stored in 'out' | ||
37 | int extractNumber(const std::string& in, unsigned int& out); | ||
38 | int extractNumber(const std::string& in, int& out); | ||
39 | /// \@} | ||
40 | |||
33 | /// creates a number to a string | 41 | /// creates a number to a string |
34 | std::string number2String(int num); | 42 | std::string number2String(int num); |
35 | 43 | ||
diff --git a/src/Layer.hh b/src/Layer.hh index 14054b0..9aaca0a 100644 --- a/src/Layer.hh +++ b/src/Layer.hh | |||
@@ -48,7 +48,7 @@ public: | |||
48 | static int getNumFromString(const std::string &str) { | 48 | static int getNumFromString(const std::string &str) { |
49 | int tempnum = 0; | 49 | int tempnum = 0; |
50 | std::string v = FbTk::StringUtil::toLower(str); | 50 | std::string v = FbTk::StringUtil::toLower(str); |
51 | if (sscanf(str.c_str(), "%d", &tempnum) == 1) | 51 | if (FbTk::StringUtil::extractNumber(str, tempnum)) |
52 | return tempnum; | 52 | return tempnum; |
53 | if (v == "menu") | 53 | if (v == "menu") |
54 | return ::Layer::MENU; | 54 | return ::Layer::MENU; |