1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
// FbString.cc for fluxbox
// Copyright (c) 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
// Copyright (c) 2006 Simon Bowden (rathnor at fluxbox dot org)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#include "FbString.hh"
#ifdef HAVE_CERRNO
#include <cerrno>
#else
#include <errno.h>
#endif
#ifdef HAVE_CSTRING
#include <cstring>
#else
#include <string.h>
#endif
#ifdef HAVE_CSTDLIB
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#ifdef HAVE_CSTDIO
#include <cstdio>
#else
#include <stdio.h>
#endif
#include <langinfo.h>
#include <locale.h>
#include <iostream>
#include <vector>
#ifndef HAVE_ICONV
typedef int iconv_t;
#endif // HAVE_ICONV
#ifdef HAVE_FRIBIDI
#include <fribidi/fribidi.h>
#endif
#ifdef DEBUG
using std::cerr;
using std::endl;
#endif // DEBUG
namespace {
const iconv_t ICONV_NULL = (iconv_t)(-1);
#ifdef HAVE_FRIBIDI
FbTk::FbString makeVisualFromLogical(const FbTk::FbString& src) {
FriBidiCharType base = FRIBIDI_TYPE_N;
// reuse allocated memory for reencoding / reordering
static std::vector<FriBidiChar> us;
static std::vector<FriBidiChar> out_us;
static FbTk::FbString result;
const size_t S = src.size() + 1;
const size_t S4 = S * 4;
if (us.capacity() < S)
us.reserve(S);
if (out_us.capacity() < S)
out_us.reserve(S);
if (result.capacity() < S4)
result.reserve(S4);
us.resize(S);
FriBidiStrIndex len = fribidi_charset_to_unicode(FRIBIDI_CHAR_SET_UTF8,
const_cast<char*>(src.c_str()), S - 1,
&us[0]);
out_us.resize(S);
fribidi_log2vis(&us[0], len, &base, &out_us[0], NULL, NULL, NULL);
result.resize(S4);
len = fribidi_unicode_to_charset(FRIBIDI_CHAR_SET_UTF8, &out_us[0], len, &result[0]);
result.resize(len); // trim to currently used chars
return result;
}
#endif
} // end of anonymous namespace
namespace FbTk {
BiDiString::BiDiString(const FbString& logical)
#ifdef HAVE_FRIBIDI
: m_visual_dirty(false)
#endif
{
if (!logical.empty())
setLogical(logical);
}
const FbString& BiDiString::setLogical(const FbString& logical) {
m_logical = logical;
#if HAVE_FRIBIDI
if (m_logical.empty()) {
m_visual_dirty = false;
m_visual.clear();
} else {
m_visual_dirty = true;
}
#endif
return m_logical;
}
const FbString& BiDiString::visual() const {
#if HAVE_FRIBIDI
if (m_visual_dirty) {
m_visual = ::makeVisualFromLogical(logical());
}
m_visual_dirty = false;
return m_visual;
#else
return m_logical;
#endif
}
namespace FbStringUtil {
enum ConvType {
FB2X = 0,
X2FB,
LOCALE2FB,
FB2LOCALE,
CONVSIZE
};
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() {
if (s_inited)
return;
s_inited = true;
setlocale(LC_CTYPE, "");
#ifdef HAVE_ICONV
#if defined(CODESET) && !defined(_WIN32)
s_locale_codeset = nl_langinfo(CODESET);
#else // openbsd doesnt have this (yet?)
std::string locale = setlocale(LC_CTYPE, NULL);
size_t pos = locale.find('.');
if (pos != std::string::npos)
s_locale_codeset = locale.substr(pos+1);
#endif // CODESET
#ifdef DEBUG
cerr << "FbTk::FbString: setup converts for local codeset = " << s_locale_codeset << endl;
#endif // DEBUG
s_iconv_convs[FB2X] = iconv_open("ISO8859-1", "UTF-8");
s_iconv_convs[X2FB] = iconv_open("UTF-8", "ISO8859-1");
s_iconv_convs[FB2LOCALE] = iconv_open(s_locale_codeset.c_str(), "UTF-8");
s_iconv_convs[LOCALE2FB] = iconv_open("UTF-8", s_locale_codeset.c_str());
#else
memset(s_iconv_convs, 0, sizeof(s_iconv_convs));
#endif // HAVE_ICONV
}
void shutdown() {
#ifdef HAVE_ICONV
int i;
for (i = 0; i < CONVSIZE; ++i)
if (s_iconv_convs[i] != ICONV_NULL)
iconv_close(s_iconv_convs[i]);
memset(s_iconv_convs, 0, sizeof(s_iconv_convs));
s_inited = false;
#endif // HAVE_ICONV
}
/**
Recodes the text from one encoding to another
assuming cd is correct
@param cd the iconv type
@param msg text to be converted, **NOT** necessarily NULL terminated
@param size number of BYTES to convert
@return the recoded string, or 0 on failure
*/
std::string recode(iconv_t cd, const std::string &in) {
#ifdef HAVE_ICONV
/**
--NOTE--
In the "C" locale, this will strip any high-bit characters
because C means 7-bit ASCII charset. If you don't want this
then you need to set your locale to something UTF-8, OR something
ISO8859-1.
*/
// If empty message, yes this can happen, return
if (in.empty())
return "";
if (cd == ICONV_NULL)
return in; // can't convert
size_t insize = in.size();
size_t outsize = insize;
std::vector<char> out(outsize);
char* out_ptr = &out[0];
size_t inbytesleft = insize;
size_t outbytesleft = outsize;
#ifdef HAVE_CONST_ICONV
const char* in_ptr = in.data();
#else
char* in_ptr = const_cast<char*>(in.data());
#endif
size_t result = (size_t)(-1);
bool again = true;
while (again) {
again = false;
result = iconv(cd, &in_ptr, &inbytesleft, &out_ptr, &outbytesleft);
if (result == (size_t)(-1)) {
switch(errno) {
case EILSEQ:
// Try skipping a byte
in_ptr++;
inbytesleft--;
again = true;
case EINVAL:
break;
case E2BIG:
// need more space!
outsize += insize;
out.resize(outsize);
if (out.capacity() != outsize)
again = true;
outbytesleft += insize;
out_ptr = (&out[0] + outsize) - outbytesleft;
break;
default:
// something else broke
perror("iconv");
break;
}
}
}
// copy to our return string
std::string ret;
ret.append(&out[0], outsize - outbytesleft);
// reset the conversion descriptor
iconv(cd, NULL, NULL, NULL, NULL);
return ret;
#else
return in;
#endif // HAVE_ICONV
}
FbString XStrToFb(const std::string &src) {
return recode(s_iconv_convs[X2FB], 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 std::string &src) {
return recode(s_iconv_convs[LOCALE2FB], src);
}
std::string FbStrToLocale(const FbString &src) {
return recode(s_iconv_convs[FB2LOCALE], src);
}
bool haveUTF8() {
#ifdef HAVE_ICONV
if (s_iconv_convs[LOCALE2FB] != ICONV_NULL)
return true;
#endif // HAVE_ICONV
return false;
}
} // end namespace StringUtil
#ifdef HAVE_ICONV
StringConvertor::StringConvertor(EncodingTarget target) : m_iconv(ICONV_NULL) {
if (target == ToLocaleStr)
m_destencoding = FbStringUtil::s_locale_codeset;
else
m_destencoding = "UTF-8";
}
#else
StringConvertor::StringConvertor(EncodingTarget target) { }
#endif
StringConvertor::~StringConvertor() {
reset();
}
bool StringConvertor::setSource(const std::string &encoding) {
#ifdef HAVE_ICONV
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_NULL)
return false;
else {
if (m_iconv != ICONV_NULL)
iconv_close(m_iconv);
m_iconv = newiconv;
return true;
}
#else
return false;
#endif
}
FbString StringConvertor::recode(const std::string &src) {
#ifdef HAVE_ICONV
return FbStringUtil::recode(m_iconv, src);
#else
return src;
#endif
}
void StringConvertor::reset() {
#ifdef HAVE_ICONV
if (m_iconv != ICONV_NULL)
iconv_close(m_iconv);
m_iconv = ICONV_NULL;
#endif
}
} // end namespace FbTk
|