use FbTk::API to reduce code duplication
This commit is contained in:
parent
041d586972
commit
603b36a2cc
3 changed files with 34 additions and 22 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue