From 603b36a2ccfb6f825dff7259c4d5397c896cc08e Mon Sep 17 00:00:00 2001 From: Mathias Gumz <akira at fluxbox dot org> Date: Fri, 17 Sep 2010 14:46:01 +0200 Subject: use FbTk::API to reduce code duplication --- src/FbTk/StringUtil.cc | 22 ++++++++++++++++++++-- src/FbTk/StringUtil.hh | 6 +++++- src/Resources.cc | 28 +++++++++------------------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc index 182233b..0a7ebd3 100644 --- a/src/FbTk/StringUtil.cc +++ b/src/FbTk/StringUtil.cc @@ -123,9 +123,27 @@ int extractNumber(const std::string& in, unsigned int& out) { return ::extractUnsignedNumber<unsigned int>(in, out); } -std::string number2String(int num) { +int extractNumber(const std::string& in, long& out) { + return ::extractSignedNumber<long>(in, out); +} + +int extractNumber(const std::string& in, unsigned long& out) { + return ::extractUnsignedNumber<unsigned long>(in, out); +} + +int extractNumber(const std::string& in, long long& out) { + return ::extractSignedNumber<long long>(in, out); +} + +int extractNumber(const std::string& in, unsigned long long& out) { + return ::extractUnsignedNumber<unsigned long long>(in, out); +} + + + +std::string number2String(long long num) { char s[128]; - sprintf(s, "%d", num); + sprintf(s, "%lld", num); return std::string(s); } diff --git a/src/FbTk/StringUtil.hh b/src/FbTk/StringUtil.hh index a16e709..9e45460 100644 --- a/src/FbTk/StringUtil.hh +++ b/src/FbTk/StringUtil.hh @@ -36,10 +36,14 @@ namespace StringUtil { /// @return 1 - ok, result stored in 'out' int extractNumber(const std::string& in, unsigned int& out); int extractNumber(const std::string& in, int& out); +int extractNumber(const std::string& in, unsigned long& out); +int extractNumber(const std::string& in, long& out); +int extractNumber(const std::string& in, long long& out); +int extractNumber(const std::string& in, unsigned long& out); /// \@} /// creates a number to a string -std::string number2String(int num); +std::string number2String(long long num); /// Similar to `strstr' but this function ignores the case of both strings const char *strcasestr(const char *str, const char *ptn); diff --git a/src/Resources.cc b/src/Resources.cc index 734cd75..0364477 100644 --- a/src/Resources.cc +++ b/src/Resources.cc @@ -51,17 +51,13 @@ namespace FbTk { template<> string FbTk::Resource<int>:: getString() const { - char strval[256]; - sprintf(strval, "%d", **this); - return string(strval); + return FbTk::StringUtil::number2String(**this); } template<> void FbTk::Resource<int>:: setFromString(const char* strval) { - int val; - if (sscanf(strval, "%d", &val)==1) - *this = val; + FbTk::StringUtil::extractNumber(strval, get()); } template<> @@ -142,7 +138,7 @@ setFromString(char const *strval) { m_value.push_back(WinButton::SHADE); else if (v == "stick") m_value.push_back(WinButton::STICK); - else if (v == "menuIcon") + else if (v == "menuicon") m_value.push_back(WinButton::MENUICON); else if (v == "close") m_value.push_back(WinButton::CLOSE); @@ -170,15 +166,13 @@ setFromString(char const *strval) { template<> string FbTk::Resource<unsigned int>:: getString() const { - char tmpstr[128]; - sprintf(tmpstr, "%ul", m_value); - return string(tmpstr); + return FbTk::StringUtil::number2String(m_value); } template<> void FbTk::Resource<unsigned int>:: setFromString(const char *strval) { - if (sscanf(strval, "%ul", &m_value) != 1) + if (!FbTk::StringUtil::extractNumber(strval, m_value)) setDefaultValue(); } @@ -186,15 +180,13 @@ setFromString(const char *strval) { template<> string FbTk::Resource<long long>:: getString() const { - char tmpstr[128]; - sprintf(tmpstr, "%llu", (unsigned long long) m_value); - return string(tmpstr); + return FbTk::StringUtil::number2String(m_value); } template<> void FbTk::Resource<long long>:: setFromString(const char *strval) { - if (sscanf(strval, "%lld", &m_value) != 1) + if (!FbTk::StringUtil::extractNumber(strval, m_value)) setDefaultValue(); } @@ -219,15 +211,13 @@ setFromString(const char *strval) { template<> string FbTk::Resource<long>:: getString() const { - char tmpstr[128]; - sprintf(tmpstr, "%ld", m_value); - return string(tmpstr); + return FbTk::StringUtil::number2String(m_value); } template<> void FbTk::Resource<long>:: setFromString(const char *strval) { - if (sscanf(strval, "%ld", &m_value) != 1) + if (!FbTk::StringUtil::extractNumber(strval, m_value)) setDefaultValue(); } -- cgit v0.11.2