aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/FbString.cc
diff options
context:
space:
mode:
authorMathias Gumz <akira at fluxbox dot org>2011-10-23 07:01:19 (GMT)
committerMathias Gumz <akira at fluxbox dot org>2011-10-23 07:01:19 (GMT)
commitdc18666ef2fbd1bbbec5b422d87a7cea54f22305 (patch)
tree811e18aaf8fb8ad52f277751f4e38eaa38b5e32f /src/FbTk/FbString.cc
parentee34b376d768ecafc2fa21a79a854df503d1f115 (diff)
downloadfluxbox-dc18666ef2fbd1bbbec5b422d87a7cea54f22305.zip
fluxbox-dc18666ef2fbd1bbbec5b422d87a7cea54f22305.tar.bz2
small code simplification
Diffstat (limited to 'src/FbTk/FbString.cc')
-rw-r--r--src/FbTk/FbString.cc141
1 files 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 @@
50#include <iostream> 50#include <iostream>
51#include <vector> 51#include <vector>
52 52
53#ifndef HAVE_ICONV
54typedef int iconv_t;
55#endif // HAVE_ICONV
56
53#ifdef HAVE_FRIBIDI 57#ifdef HAVE_FRIBIDI
54 #include <fribidi/fribidi.h> 58 #include <fribidi/fribidi.h>
55#endif 59#endif
56 60
57
58using std::string;
59
60#ifdef DEBUG 61#ifdef DEBUG
61using std::cerr; 62using std::cerr;
62using std::endl; 63using std::endl;
@@ -64,6 +65,8 @@ using std::endl;
64 65
65namespace { 66namespace {
66 67
68const iconv_t ICONV_NULL = (iconv_t)(-1);
69
67#ifdef HAVE_FRIBIDI 70#ifdef HAVE_FRIBIDI
68FbTk::FbString makeVisualFromLogical(const FbTk::FbString& src) { 71FbTk::FbString makeVisualFromLogical(const FbTk::FbString& src) {
69 72
@@ -106,7 +109,6 @@ FbTk::FbString makeVisualFromLogical(const FbTk::FbString& src) {
106 109
107namespace FbTk { 110namespace FbTk {
108 111
109
110BiDiString::BiDiString(const FbString& logical) 112BiDiString::BiDiString(const FbString& logical)
111#ifdef HAVE_FRIBIDI 113#ifdef HAVE_FRIBIDI
112 : m_visual_dirty(false) 114 : m_visual_dirty(false)
@@ -145,63 +147,60 @@ const FbString& BiDiString::visual() const {
145 147
146namespace FbStringUtil { 148namespace FbStringUtil {
147 149
148enum ConvType { FB2X = 0, X2FB, LOCALE2FB, FB2LOCALE, CONVSIZE }; 150enum ConvType {
151 FB2X = 0,
152 X2FB,
153 LOCALE2FB,
154 FB2LOCALE,
155 CONVSIZE
156};
149 157
150#ifdef HAVE_ICONV 158static bool s_inited = false;
151static iconv_t *iconv_convs = 0; 159static iconv_t s_iconv_convs[CONVSIZE];
152#else 160static std::string s_locale_codeset;
153typedef int iconv_t;
154static int iconv_convs[CONVSIZE];
155#endif // HAVE_ICONV
156
157static string locale_codeset;
158 161
159/// Initialise all of the iconv conversion descriptors 162/// Initialise all of the iconv conversion descriptors
160void init() { 163void init() {
161 setlocale(LC_CTYPE, "");
162 164
163#ifdef HAVE_ICONV 165 if (s_inited)
164 if (iconv_convs != 0)
165 return; 166 return;
166 167
167 iconv_convs = new iconv_t[CONVSIZE]; 168 s_inited = true;
169 setlocale(LC_CTYPE, "");
168 170
171#ifdef HAVE_ICONV
169#ifdef CODESET 172#ifdef CODESET
170 locale_codeset = nl_langinfo(CODESET); 173 s_locale_codeset = nl_langinfo(CODESET);
171#else // openbsd doesnt have this (yet?) 174#else // openbsd doesnt have this (yet?)
172 locale_codeset = ""; 175 std::string locale = setlocale(LC_CTYPE, NULL);
173 string locale = setlocale(LC_CTYPE, NULL);
174 size_t pos = locale.find('.'); 176 size_t pos = locale.find('.');
175 if (pos != string::npos) 177 if (pos != string::npos)
176 locale_codeset = locale.substr(pos+1); 178 s_locale_codeset = locale.substr(pos+1);
177#endif // CODESET 179#endif // CODESET
178 180
179#ifdef DEBUG 181#ifdef DEBUG
180 cerr<<"FbTk::FbString: setup converts for local codeset = "<<locale_codeset<<endl; 182 cerr << "FbTk::FbString: setup converts for local codeset = " << s_locale_codeset << endl;
181#endif // DEBUG 183#endif // DEBUG
182 184
183 iconv_convs[FB2X] = iconv_open("ISO8859-1", "UTF-8"); 185 s_iconv_convs[FB2X] = iconv_open("ISO8859-1", "UTF-8");
184 iconv_convs[X2FB] = iconv_open("UTF-8", "ISO8859-1"); 186 s_iconv_convs[X2FB] = iconv_open("UTF-8", "ISO8859-1");
185 iconv_convs[FB2LOCALE] = iconv_open(locale_codeset.c_str(), "UTF-8"); 187 s_iconv_convs[FB2LOCALE] = iconv_open(s_locale_codeset.c_str(), "UTF-8");
186 iconv_convs[LOCALE2FB] = iconv_open("UTF-8", locale_codeset.c_str()); 188 s_iconv_convs[LOCALE2FB] = iconv_open("UTF-8", s_locale_codeset.c_str());
187#else 189#else
188 for (int i=0; i < CONVSIZE; ++i) 190 memset(s_iconv_convs, 0, sizeof(s_iconv_convs));
189 iconv_convs[i] = 0;
190#endif // HAVE_ICONV 191#endif // HAVE_ICONV
191 192
192} 193}
193 194
194void shutdown() { 195void shutdown() {
195#ifdef HAVE_ICONV 196#ifdef HAVE_ICONV
196 if (iconv_convs == 0) 197 int i;
197 return; 198 for (i = 0; i < CONVSIZE; ++i)
198 199 if (s_iconv_convs[i] != ICONV_NULL)
199 for (int i=0; i < CONVSIZE; ++i) 200 iconv_close(s_iconv_convs[i]);
200 if (iconv_convs[i] != (iconv_t)(-1))
201 iconv_close(iconv_convs[i]);
202 201
203 delete[] iconv_convs; 202 memset(s_iconv_convs, 0, sizeof(s_iconv_convs));
204 iconv_convs = 0; 203 s_inited = false;
205#endif // HAVE_ICONV 204#endif // HAVE_ICONV
206} 205}
207 206
@@ -216,7 +215,7 @@ void shutdown() {
216 @param size number of BYTES to convert 215 @param size number of BYTES to convert
217 @return the recoded string, or 0 on failure 216 @return the recoded string, or 0 on failure
218*/ 217*/
219string recode(iconv_t cd, const string &in) { 218std::string recode(iconv_t cd, const std::string &in) {
220 219
221#ifdef HAVE_ICONV 220#ifdef HAVE_ICONV
222/** 221/**
@@ -231,13 +230,13 @@ string recode(iconv_t cd, const string &in) {
231 if (in.empty()) 230 if (in.empty())
232 return ""; 231 return "";
233 232
234 if (cd == ((iconv_t)(-1))) 233 if (cd == ICONV_NULL)
235 return in; // can't convert 234 return in; // can't convert
236 235
237 size_t insize = in.size(); 236 size_t insize = in.size();
238 size_t outsize = insize; 237 size_t outsize = insize;
239 char * out = (char *) malloc(outsize * sizeof(char)); // need realloc 238 std::vector<char> out(outsize);
240 char * out_ptr = out; 239 char* out_ptr = &out[0];
241 240
242 size_t inbytesleft = insize; 241 size_t inbytesleft = insize;
243 size_t outbytesleft = outsize; 242 size_t outbytesleft = outsize;
@@ -245,7 +244,7 @@ string recode(iconv_t cd, const string &in) {
245#ifdef HAVE_CONST_ICONV 244#ifdef HAVE_CONST_ICONV
246 const char* in_ptr = in.data(); 245 const char* in_ptr = in.data();
247#else 246#else
248 char * in_ptr = const_cast<char *>(in.data()); 247 char* in_ptr = const_cast<char*>(in.data());
249#endif 248#endif
250 size_t result = (size_t)(-1); 249 size_t result = (size_t)(-1);
251 bool again = true; 250 bool again = true;
@@ -267,11 +266,11 @@ string recode(iconv_t cd, const string &in) {
267 case E2BIG: 266 case E2BIG:
268 // need more space! 267 // need more space!
269 outsize += insize; 268 outsize += insize;
270 out = (char *) realloc(out, outsize*sizeof(char)); 269 out.resize(outsize);
271 if (out != NULL) 270 if (out.capacity() != outsize)
272 again = true; 271 again = true;
273 outbytesleft += insize; 272 outbytesleft += insize;
274 out_ptr = out + outsize - outbytesleft; 273 out_ptr = (&out[0] + outsize) - outbytesleft;
275 break; 274 break;
276 default: 275 default:
277 // something else broke 276 // something else broke
@@ -282,42 +281,39 @@ string recode(iconv_t cd, const string &in) {
282 } 281 }
283 282
284 // copy to our return string 283 // copy to our return string
285 string ret; 284 std::string ret;
286 ret.append(out, outsize - outbytesleft); 285 ret.append(&out[0], outsize - outbytesleft);
287 286
288 // reset the conversion descriptor 287 // reset the conversion descriptor
289 iconv(cd, NULL, NULL, NULL, NULL); 288 iconv(cd, NULL, NULL, NULL, NULL);
290 289
291 if (out)
292 free(out);
293
294 return ret; 290 return ret;
295#else 291#else
296 return in; 292 return in;
297#endif // HAVE_ICONV 293#endif // HAVE_ICONV
298} 294}
299 295
300FbString XStrToFb(const string &src) { 296FbString XStrToFb(const std::string &src) {
301 return recode(iconv_convs[X2FB], src); 297 return recode(s_iconv_convs[X2FB], src);
302} 298}
303 299
304string FbStrToX(const FbString &src) { 300std::string FbStrToX(const FbString &src) {
305 return recode(iconv_convs[FB2X], src); 301 return recode(s_iconv_convs[FB2X], src);
306} 302}
307 303
308 304
309/// Handle thislocale string encodings (strings coming from userspace) 305/// Handle thislocale string encodings (strings coming from userspace)
310FbString LocaleStrToFb(const string &src) { 306FbString LocaleStrToFb(const std::string &src) {
311 return recode(iconv_convs[LOCALE2FB], src); 307 return recode(s_iconv_convs[LOCALE2FB], src);
312} 308}
313 309
314string FbStrToLocale(const FbString &src) { 310std::string FbStrToLocale(const FbString &src) {
315 return recode(iconv_convs[FB2LOCALE], src); 311 return recode(s_iconv_convs[FB2LOCALE], src);
316} 312}
317 313
318bool haveUTF8() { 314bool haveUTF8() {
319#ifdef HAVE_ICONV 315#ifdef HAVE_ICONV
320 if (iconv_convs[LOCALE2FB] != ((iconv_t)(-1))) 316 if (s_iconv_convs[LOCALE2FB] != ICONV_NULL)
321 return true; 317 return true;
322#endif // HAVE_ICONV 318#endif // HAVE_ICONV
323 319
@@ -328,9 +324,9 @@ bool haveUTF8() {
328} // end namespace StringUtil 324} // end namespace StringUtil
329 325
330#ifdef HAVE_ICONV 326#ifdef HAVE_ICONV
331StringConvertor::StringConvertor(EncodingTarget target) : m_iconv((iconv_t)(-1)) { 327StringConvertor::StringConvertor(EncodingTarget target) : m_iconv(ICONV_NULL) {
332 if (target == ToLocaleStr) 328 if (target == ToLocaleStr)
333 m_destencoding = FbStringUtil::locale_codeset; 329 m_destencoding = FbStringUtil::s_locale_codeset;
334 else 330 else
335 m_destencoding = "UTF-8"; 331 m_destencoding = "UTF-8";
336} 332}
@@ -339,23 +335,22 @@ StringConvertor::StringConvertor(EncodingTarget target) { }
339#endif 335#endif
340 336
341StringConvertor::~StringConvertor() { 337StringConvertor::~StringConvertor() {
342#ifdef HAVE_ICONV 338 reset();
343 if (m_iconv != ((iconv_t)-1))
344 iconv_close(m_iconv);
345#endif
346} 339}
347 340
348bool StringConvertor::setSource(const string &encoding) { 341bool StringConvertor::setSource(const std::string &encoding) {
349#ifdef HAVE_ICONV 342#ifdef HAVE_ICONV
350 string tempenc = encoding; 343 std::string tempenc = encoding.empty() ? FbStringUtil::s_locale_codeset : encoding;
351 if (encoding == "") 344
352 tempenc = FbStringUtil::locale_codeset; 345 if ((tempenc == m_destencoding) && (m_iconv == ICONV_NULL)) {
346 return true;
347 }
353 348
354 iconv_t newiconv = iconv_open(m_destencoding.c_str(), tempenc.c_str()); 349 iconv_t newiconv = iconv_open(m_destencoding.c_str(), tempenc.c_str());
355 if (newiconv == ((iconv_t)(-1))) 350 if (newiconv == ICONV_NULL)
356 return false; 351 return false;
357 else { 352 else {
358 if (m_iconv != ((iconv_t)-1)) 353 if (m_iconv != ICONV_NULL)
359 iconv_close(m_iconv); 354 iconv_close(m_iconv);
360 m_iconv = newiconv; 355 m_iconv = newiconv;
361 return true; 356 return true;
@@ -365,7 +360,7 @@ bool StringConvertor::setSource(const string &encoding) {
365#endif 360#endif
366} 361}
367 362
368FbString StringConvertor::recode(const string &src) { 363FbString StringConvertor::recode(const std::string &src) {
369#ifdef HAVE_ICONV 364#ifdef HAVE_ICONV
370 return FbStringUtil::recode(m_iconv, src); 365 return FbStringUtil::recode(m_iconv, src);
371#else 366#else
@@ -375,9 +370,9 @@ FbString StringConvertor::recode(const string &src) {
375 370
376void StringConvertor::reset() { 371void StringConvertor::reset() {
377#ifdef HAVE_ICONV 372#ifdef HAVE_ICONV
378 if (m_iconv != ((iconv_t)-1)) 373 if (m_iconv != ICONV_NULL)
379 iconv_close(m_iconv); 374 iconv_close(m_iconv);
380 m_iconv = ((iconv_t)(-1)); 375 m_iconv = ICONV_NULL;
381#endif 376#endif
382} 377}
383 378