small code simplification

This commit is contained in:
Mathias Gumz 2011-10-23 09:01:19 +02:00
parent ee34b376d7
commit dc18666ef2

View file

@ -50,13 +50,14 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#ifndef HAVE_ICONV
typedef int iconv_t;
#endif // HAVE_ICONV
#ifdef HAVE_FRIBIDI #ifdef HAVE_FRIBIDI
#include <fribidi/fribidi.h> #include <fribidi/fribidi.h>
#endif #endif
using std::string;
#ifdef DEBUG #ifdef DEBUG
using std::cerr; using std::cerr;
using std::endl; using std::endl;
@ -64,6 +65,8 @@ using std::endl;
namespace { namespace {
const iconv_t ICONV_NULL = (iconv_t)(-1);
#ifdef HAVE_FRIBIDI #ifdef HAVE_FRIBIDI
FbTk::FbString makeVisualFromLogical(const FbTk::FbString& src) { FbTk::FbString makeVisualFromLogical(const FbTk::FbString& src) {
@ -106,7 +109,6 @@ FbTk::FbString makeVisualFromLogical(const FbTk::FbString& src) {
namespace FbTk { namespace FbTk {
BiDiString::BiDiString(const FbString& logical) BiDiString::BiDiString(const FbString& logical)
#ifdef HAVE_FRIBIDI #ifdef HAVE_FRIBIDI
: m_visual_dirty(false) : m_visual_dirty(false)
@ -145,63 +147,60 @@ const FbString& BiDiString::visual() const {
namespace FbStringUtil { namespace FbStringUtil {
enum ConvType { FB2X = 0, X2FB, LOCALE2FB, FB2LOCALE, CONVSIZE }; enum ConvType {
FB2X = 0,
X2FB,
LOCALE2FB,
FB2LOCALE,
CONVSIZE
};
#ifdef HAVE_ICONV static bool s_inited = false;
static iconv_t *iconv_convs = 0; static iconv_t s_iconv_convs[CONVSIZE];
#else static std::string s_locale_codeset;
typedef int iconv_t;
static int iconv_convs[CONVSIZE];
#endif // HAVE_ICONV
static string locale_codeset;
/// Initialise all of the iconv conversion descriptors /// Initialise all of the iconv conversion descriptors
void init() { void init() {
if (s_inited)
return;
s_inited = true;
setlocale(LC_CTYPE, ""); setlocale(LC_CTYPE, "");
#ifdef HAVE_ICONV #ifdef HAVE_ICONV
if (iconv_convs != 0)
return;
iconv_convs = new iconv_t[CONVSIZE];
#ifdef CODESET #ifdef CODESET
locale_codeset = nl_langinfo(CODESET); s_locale_codeset = nl_langinfo(CODESET);
#else // openbsd doesnt have this (yet?) #else // openbsd doesnt have this (yet?)
locale_codeset = ""; std::string locale = setlocale(LC_CTYPE, NULL);
string locale = setlocale(LC_CTYPE, NULL);
size_t pos = locale.find('.'); size_t pos = locale.find('.');
if (pos != string::npos) if (pos != string::npos)
locale_codeset = locale.substr(pos+1); s_locale_codeset = locale.substr(pos+1);
#endif // CODESET #endif // CODESET
#ifdef DEBUG #ifdef DEBUG
cerr<<"FbTk::FbString: setup converts for local codeset = "<<locale_codeset<<endl; cerr << "FbTk::FbString: setup converts for local codeset = " << s_locale_codeset << endl;
#endif // DEBUG #endif // DEBUG
iconv_convs[FB2X] = iconv_open("ISO8859-1", "UTF-8"); s_iconv_convs[FB2X] = iconv_open("ISO8859-1", "UTF-8");
iconv_convs[X2FB] = iconv_open("UTF-8", "ISO8859-1"); s_iconv_convs[X2FB] = iconv_open("UTF-8", "ISO8859-1");
iconv_convs[FB2LOCALE] = iconv_open(locale_codeset.c_str(), "UTF-8"); s_iconv_convs[FB2LOCALE] = iconv_open(s_locale_codeset.c_str(), "UTF-8");
iconv_convs[LOCALE2FB] = iconv_open("UTF-8", locale_codeset.c_str()); s_iconv_convs[LOCALE2FB] = iconv_open("UTF-8", s_locale_codeset.c_str());
#else #else
for (int i=0; i < CONVSIZE; ++i) memset(s_iconv_convs, 0, sizeof(s_iconv_convs));
iconv_convs[i] = 0;
#endif // HAVE_ICONV #endif // HAVE_ICONV
} }
void shutdown() { void shutdown() {
#ifdef HAVE_ICONV #ifdef HAVE_ICONV
if (iconv_convs == 0) int i;
return; for (i = 0; i < CONVSIZE; ++i)
if (s_iconv_convs[i] != ICONV_NULL)
iconv_close(s_iconv_convs[i]);
for (int i=0; i < CONVSIZE; ++i) memset(s_iconv_convs, 0, sizeof(s_iconv_convs));
if (iconv_convs[i] != (iconv_t)(-1)) s_inited = false;
iconv_close(iconv_convs[i]);
delete[] iconv_convs;
iconv_convs = 0;
#endif // HAVE_ICONV #endif // HAVE_ICONV
} }
@ -216,7 +215,7 @@ void shutdown() {
@param size number of BYTES to convert @param size number of BYTES to convert
@return the recoded string, or 0 on failure @return the recoded string, or 0 on failure
*/ */
string recode(iconv_t cd, const string &in) { std::string recode(iconv_t cd, const std::string &in) {
#ifdef HAVE_ICONV #ifdef HAVE_ICONV
/** /**
@ -231,13 +230,13 @@ string recode(iconv_t cd, const string &in) {
if (in.empty()) if (in.empty())
return ""; return "";
if (cd == ((iconv_t)(-1))) if (cd == ICONV_NULL)
return in; // can't convert return in; // can't convert
size_t insize = in.size(); size_t insize = in.size();
size_t outsize = insize; size_t outsize = insize;
char * out = (char *) malloc(outsize * sizeof(char)); // need realloc std::vector<char> out(outsize);
char * out_ptr = out; char* out_ptr = &out[0];
size_t inbytesleft = insize; size_t inbytesleft = insize;
size_t outbytesleft = outsize; size_t outbytesleft = outsize;
@ -267,11 +266,11 @@ string recode(iconv_t cd, const string &in) {
case E2BIG: case E2BIG:
// need more space! // need more space!
outsize += insize; outsize += insize;
out = (char *) realloc(out, outsize*sizeof(char)); out.resize(outsize);
if (out != NULL) if (out.capacity() != outsize)
again = true; again = true;
outbytesleft += insize; outbytesleft += insize;
out_ptr = out + outsize - outbytesleft; out_ptr = (&out[0] + outsize) - outbytesleft;
break; break;
default: default:
// something else broke // something else broke
@ -282,42 +281,39 @@ string recode(iconv_t cd, const string &in) {
} }
// copy to our return string // copy to our return string
string ret; std::string ret;
ret.append(out, outsize - outbytesleft); ret.append(&out[0], outsize - outbytesleft);
// reset the conversion descriptor // reset the conversion descriptor
iconv(cd, NULL, NULL, NULL, NULL); iconv(cd, NULL, NULL, NULL, NULL);
if (out)
free(out);
return ret; return ret;
#else #else
return in; return in;
#endif // HAVE_ICONV #endif // HAVE_ICONV
} }
FbString XStrToFb(const string &src) { FbString XStrToFb(const std::string &src) {
return recode(iconv_convs[X2FB], src); return recode(s_iconv_convs[X2FB], src);
} }
string FbStrToX(const FbString &src) { std::string FbStrToX(const FbString &src) {
return recode(iconv_convs[FB2X], src); return recode(s_iconv_convs[FB2X], src);
} }
/// Handle thislocale string encodings (strings coming from userspace) /// Handle thislocale string encodings (strings coming from userspace)
FbString LocaleStrToFb(const string &src) { FbString LocaleStrToFb(const std::string &src) {
return recode(iconv_convs[LOCALE2FB], src); return recode(s_iconv_convs[LOCALE2FB], src);
} }
string FbStrToLocale(const FbString &src) { std::string FbStrToLocale(const FbString &src) {
return recode(iconv_convs[FB2LOCALE], src); return recode(s_iconv_convs[FB2LOCALE], src);
} }
bool haveUTF8() { bool haveUTF8() {
#ifdef HAVE_ICONV #ifdef HAVE_ICONV
if (iconv_convs[LOCALE2FB] != ((iconv_t)(-1))) if (s_iconv_convs[LOCALE2FB] != ICONV_NULL)
return true; return true;
#endif // HAVE_ICONV #endif // HAVE_ICONV
@ -328,9 +324,9 @@ bool haveUTF8() {
} // end namespace StringUtil } // end namespace StringUtil
#ifdef HAVE_ICONV #ifdef HAVE_ICONV
StringConvertor::StringConvertor(EncodingTarget target) : m_iconv((iconv_t)(-1)) { StringConvertor::StringConvertor(EncodingTarget target) : m_iconv(ICONV_NULL) {
if (target == ToLocaleStr) if (target == ToLocaleStr)
m_destencoding = FbStringUtil::locale_codeset; m_destencoding = FbStringUtil::s_locale_codeset;
else else
m_destencoding = "UTF-8"; m_destencoding = "UTF-8";
} }
@ -339,23 +335,22 @@ StringConvertor::StringConvertor(EncodingTarget target) { }
#endif #endif
StringConvertor::~StringConvertor() { StringConvertor::~StringConvertor() {
#ifdef HAVE_ICONV reset();
if (m_iconv != ((iconv_t)-1))
iconv_close(m_iconv);
#endif
} }
bool StringConvertor::setSource(const string &encoding) { bool StringConvertor::setSource(const std::string &encoding) {
#ifdef HAVE_ICONV #ifdef HAVE_ICONV
string tempenc = encoding; std::string tempenc = encoding.empty() ? FbStringUtil::s_locale_codeset : encoding;
if (encoding == "")
tempenc = FbStringUtil::locale_codeset; if ((tempenc == m_destencoding) && (m_iconv == ICONV_NULL)) {
return true;
}
iconv_t newiconv = iconv_open(m_destencoding.c_str(), tempenc.c_str()); iconv_t newiconv = iconv_open(m_destencoding.c_str(), tempenc.c_str());
if (newiconv == ((iconv_t)(-1))) if (newiconv == ICONV_NULL)
return false; return false;
else { else {
if (m_iconv != ((iconv_t)-1)) if (m_iconv != ICONV_NULL)
iconv_close(m_iconv); iconv_close(m_iconv);
m_iconv = newiconv; m_iconv = newiconv;
return true; return true;
@ -365,7 +360,7 @@ bool StringConvertor::setSource(const string &encoding) {
#endif #endif
} }
FbString StringConvertor::recode(const string &src) { FbString StringConvertor::recode(const std::string &src) {
#ifdef HAVE_ICONV #ifdef HAVE_ICONV
return FbStringUtil::recode(m_iconv, src); return FbStringUtil::recode(m_iconv, src);
#else #else
@ -375,9 +370,9 @@ FbString StringConvertor::recode(const string &src) {
void StringConvertor::reset() { void StringConvertor::reset() {
#ifdef HAVE_ICONV #ifdef HAVE_ICONV
if (m_iconv != ((iconv_t)-1)) if (m_iconv != ICONV_NULL)
iconv_close(m_iconv); iconv_close(m_iconv);
m_iconv = ((iconv_t)(-1)); m_iconv = ICONV_NULL;
#endif #endif
} }