From dc18666ef2fbd1bbbec5b422d87a7cea54f22305 Mon Sep 17 00:00:00 2001 From: Mathias Gumz Date: Sun, 23 Oct 2011 09:01:19 +0200 Subject: small code simplification --- src/FbTk/FbString.cc | 141 +++++++++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 73 deletions(-) diff --git a/src/FbTk/FbString.cc b/src/FbTk/FbString.cc index 5295a66..d62f5fa 100644 --- a/src/FbTk/FbString.cc +++ b/src/FbTk/FbString.cc @@ -50,13 +50,14 @@ #include #include +#ifndef HAVE_ICONV +typedef int iconv_t; +#endif // HAVE_ICONV + #ifdef HAVE_FRIBIDI #include #endif - -using std::string; - #ifdef DEBUG using std::cerr; using std::endl; @@ -64,6 +65,8 @@ using std::endl; namespace { +const iconv_t ICONV_NULL = (iconv_t)(-1); + #ifdef HAVE_FRIBIDI FbTk::FbString makeVisualFromLogical(const FbTk::FbString& src) { @@ -106,7 +109,6 @@ FbTk::FbString makeVisualFromLogical(const FbTk::FbString& src) { namespace FbTk { - BiDiString::BiDiString(const FbString& logical) #ifdef HAVE_FRIBIDI : m_visual_dirty(false) @@ -145,63 +147,60 @@ const FbString& BiDiString::visual() const { namespace FbStringUtil { -enum ConvType { FB2X = 0, X2FB, LOCALE2FB, FB2LOCALE, CONVSIZE }; +enum ConvType { + FB2X = 0, + X2FB, + LOCALE2FB, + FB2LOCALE, + CONVSIZE +}; -#ifdef HAVE_ICONV -static iconv_t *iconv_convs = 0; -#else -typedef int iconv_t; -static int iconv_convs[CONVSIZE]; -#endif // HAVE_ICONV - -static string locale_codeset; +static bool s_inited = false; +static iconv_t s_iconv_convs[CONVSIZE]; +static std::string s_locale_codeset; /// Initialise all of the iconv conversion descriptors void init() { - setlocale(LC_CTYPE, ""); -#ifdef HAVE_ICONV - if (iconv_convs != 0) + if (s_inited) return; - iconv_convs = new iconv_t[CONVSIZE]; + s_inited = true; + setlocale(LC_CTYPE, ""); +#ifdef HAVE_ICONV #ifdef CODESET - locale_codeset = nl_langinfo(CODESET); + s_locale_codeset = nl_langinfo(CODESET); #else // openbsd doesnt have this (yet?) - locale_codeset = ""; - string locale = setlocale(LC_CTYPE, NULL); + std::string locale = setlocale(LC_CTYPE, NULL); size_t pos = locale.find('.'); if (pos != string::npos) - locale_codeset = locale.substr(pos+1); + s_locale_codeset = locale.substr(pos+1); #endif // CODESET #ifdef DEBUG - cerr<<"FbTk::FbString: setup converts for local codeset = "< out(outsize); + char* out_ptr = &out[0]; size_t inbytesleft = insize; size_t outbytesleft = outsize; @@ -245,7 +244,7 @@ string recode(iconv_t cd, const string &in) { #ifdef HAVE_CONST_ICONV const char* in_ptr = in.data(); #else - char * in_ptr = const_cast(in.data()); + char* in_ptr = const_cast(in.data()); #endif size_t result = (size_t)(-1); bool again = true; @@ -267,11 +266,11 @@ string recode(iconv_t cd, const string &in) { case E2BIG: // need more space! outsize += insize; - out = (char *) realloc(out, outsize*sizeof(char)); - if (out != NULL) + out.resize(outsize); + if (out.capacity() != outsize) again = true; outbytesleft += insize; - out_ptr = out + outsize - outbytesleft; + out_ptr = (&out[0] + outsize) - outbytesleft; break; default: // something else broke @@ -282,42 +281,39 @@ string recode(iconv_t cd, const string &in) { } // copy to our return string - string ret; - ret.append(out, outsize - outbytesleft); + std::string ret; + ret.append(&out[0], outsize - outbytesleft); // reset the conversion descriptor iconv(cd, NULL, NULL, NULL, NULL); - if (out) - free(out); - return ret; #else return in; #endif // HAVE_ICONV } -FbString XStrToFb(const string &src) { - return recode(iconv_convs[X2FB], src); +FbString XStrToFb(const std::string &src) { + return recode(s_iconv_convs[X2FB], src); } -string FbStrToX(const FbString &src) { - return recode(iconv_convs[FB2X], src); +std::string FbStrToX(const FbString &src) { + return recode(s_iconv_convs[FB2X], src); } /// Handle thislocale string encodings (strings coming from userspace) -FbString LocaleStrToFb(const string &src) { - return recode(iconv_convs[LOCALE2FB], src); +FbString LocaleStrToFb(const std::string &src) { + return recode(s_iconv_convs[LOCALE2FB], src); } -string FbStrToLocale(const FbString &src) { - return recode(iconv_convs[FB2LOCALE], src); +std::string FbStrToLocale(const FbString &src) { + return recode(s_iconv_convs[FB2LOCALE], src); } bool haveUTF8() { #ifdef HAVE_ICONV - if (iconv_convs[LOCALE2FB] != ((iconv_t)(-1))) + if (s_iconv_convs[LOCALE2FB] != ICONV_NULL) return true; #endif // HAVE_ICONV @@ -328,9 +324,9 @@ bool haveUTF8() { } // end namespace StringUtil #ifdef HAVE_ICONV -StringConvertor::StringConvertor(EncodingTarget target) : m_iconv((iconv_t)(-1)) { +StringConvertor::StringConvertor(EncodingTarget target) : m_iconv(ICONV_NULL) { if (target == ToLocaleStr) - m_destencoding = FbStringUtil::locale_codeset; + m_destencoding = FbStringUtil::s_locale_codeset; else m_destencoding = "UTF-8"; } @@ -339,23 +335,22 @@ StringConvertor::StringConvertor(EncodingTarget target) { } #endif StringConvertor::~StringConvertor() { -#ifdef HAVE_ICONV - if (m_iconv != ((iconv_t)-1)) - iconv_close(m_iconv); -#endif + reset(); } -bool StringConvertor::setSource(const string &encoding) { +bool StringConvertor::setSource(const std::string &encoding) { #ifdef HAVE_ICONV - string tempenc = encoding; - if (encoding == "") - tempenc = FbStringUtil::locale_codeset; + std::string tempenc = encoding.empty() ? FbStringUtil::s_locale_codeset : encoding; + + if ((tempenc == m_destencoding) && (m_iconv == ICONV_NULL)) { + return true; + } iconv_t newiconv = iconv_open(m_destencoding.c_str(), tempenc.c_str()); - if (newiconv == ((iconv_t)(-1))) + if (newiconv == ICONV_NULL) return false; else { - if (m_iconv != ((iconv_t)-1)) + if (m_iconv != ICONV_NULL) iconv_close(m_iconv); m_iconv = newiconv; return true; @@ -365,7 +360,7 @@ bool StringConvertor::setSource(const string &encoding) { #endif } -FbString StringConvertor::recode(const string &src) { +FbString StringConvertor::recode(const std::string &src) { #ifdef HAVE_ICONV return FbStringUtil::recode(m_iconv, src); #else @@ -375,9 +370,9 @@ FbString StringConvertor::recode(const string &src) { void StringConvertor::reset() { #ifdef HAVE_ICONV - if (m_iconv != ((iconv_t)-1)) + if (m_iconv != ICONV_NULL) iconv_close(m_iconv); - m_iconv = ((iconv_t)(-1)); + m_iconv = ICONV_NULL; #endif } -- cgit v0.11.2