use FbTk::API to reduce code duplication

This commit is contained in:
Mathias Gumz 2010-09-17 14:46:01 +02:00
parent 041d586972
commit 603b36a2cc
3 changed files with 34 additions and 22 deletions

View file

@ -123,9 +123,27 @@ int extractNumber(const std::string& in, unsigned int& out) {
return ::extractUnsignedNumber<unsigned int>(in, 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]; char s[128];
sprintf(s, "%d", num); sprintf(s, "%lld", num);
return std::string(s); return std::string(s);
} }

View file

@ -36,10 +36,14 @@ namespace StringUtil {
/// @return 1 - ok, result stored in 'out' /// @return 1 - ok, result stored in 'out'
int extractNumber(const std::string& in, unsigned int& out); int extractNumber(const std::string& in, unsigned int& out);
int extractNumber(const std::string& in, 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 /// 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 /// Similar to `strstr' but this function ignores the case of both strings
const char *strcasestr(const char *str, const char *ptn); const char *strcasestr(const char *str, const char *ptn);

View file

@ -51,17 +51,13 @@ namespace FbTk {
template<> template<>
string FbTk::Resource<int>:: string FbTk::Resource<int>::
getString() const { getString() const {
char strval[256]; return FbTk::StringUtil::number2String(**this);
sprintf(strval, "%d", **this);
return string(strval);
} }
template<> template<>
void FbTk::Resource<int>:: void FbTk::Resource<int>::
setFromString(const char* strval) { setFromString(const char* strval) {
int val; FbTk::StringUtil::extractNumber(strval, get());
if (sscanf(strval, "%d", &val)==1)
*this = val;
} }
template<> template<>
@ -142,7 +138,7 @@ setFromString(char const *strval) {
m_value.push_back(WinButton::SHADE); m_value.push_back(WinButton::SHADE);
else if (v == "stick") else if (v == "stick")
m_value.push_back(WinButton::STICK); m_value.push_back(WinButton::STICK);
else if (v == "menuIcon") else if (v == "menuicon")
m_value.push_back(WinButton::MENUICON); m_value.push_back(WinButton::MENUICON);
else if (v == "close") else if (v == "close")
m_value.push_back(WinButton::CLOSE); m_value.push_back(WinButton::CLOSE);
@ -170,15 +166,13 @@ setFromString(char const *strval) {
template<> template<>
string FbTk::Resource<unsigned int>:: string FbTk::Resource<unsigned int>::
getString() const { getString() const {
char tmpstr[128]; return FbTk::StringUtil::number2String(m_value);
sprintf(tmpstr, "%ul", m_value);
return string(tmpstr);
} }
template<> template<>
void FbTk::Resource<unsigned int>:: void FbTk::Resource<unsigned int>::
setFromString(const char *strval) { setFromString(const char *strval) {
if (sscanf(strval, "%ul", &m_value) != 1) if (!FbTk::StringUtil::extractNumber(strval, m_value))
setDefaultValue(); setDefaultValue();
} }
@ -186,15 +180,13 @@ setFromString(const char *strval) {
template<> template<>
string FbTk::Resource<long long>:: string FbTk::Resource<long long>::
getString() const { getString() const {
char tmpstr[128]; return FbTk::StringUtil::number2String(m_value);
sprintf(tmpstr, "%llu", (unsigned long long) m_value);
return string(tmpstr);
} }
template<> template<>
void FbTk::Resource<long long>:: void FbTk::Resource<long long>::
setFromString(const char *strval) { setFromString(const char *strval) {
if (sscanf(strval, "%lld", &m_value) != 1) if (!FbTk::StringUtil::extractNumber(strval, m_value))
setDefaultValue(); setDefaultValue();
} }
@ -219,15 +211,13 @@ setFromString(const char *strval) {
template<> template<>
string FbTk::Resource<long>:: string FbTk::Resource<long>::
getString() const { getString() const {
char tmpstr[128]; return FbTk::StringUtil::number2String(m_value);
sprintf(tmpstr, "%ld", m_value);
return string(tmpstr);
} }
template<> template<>
void FbTk::Resource<long>:: void FbTk::Resource<long>::
setFromString(const char *strval) { setFromString(const char *strval) {
if (sscanf(strval, "%ld", &m_value) != 1) if (!FbTk::StringUtil::extractNumber(strval, m_value))
setDefaultValue(); setDefaultValue();
} }