aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ecore/src/lib/ecore_x/xcb/ecore_xcb_textlist.c
blob: 2a5c8544947a583eec616590304aaeb7cec6ed76 (plain)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "ecore_xcb_private.h"
//#include "Ecore_X_Atoms.h"
#include <langinfo.h>
#ifdef HAVE_ICONV
# include <iconv.h>
#endif
#ifndef CODESET
# define CODESET "INVALID"
#endif

static int _ecore_xcb_textlist_get_buffer_size(Eina_Bool is_wide,
                                               void     *list,
                                               int       count);
static int   _ecore_xcb_textlist_get_wc_len(wchar_t *wstr);
static void *_ecore_xcb_textlist_alloc_list(Eina_Bool is_wide,
                                            int       count,
                                            int       nitems);
static void _ecore_xcb_textlist_copy_list(Eina_Bool is_wide,
                                          void     *text,
                                          char    **list,
                                          int       count);
static wchar_t *_ecore_xcb_textlist_copy_wchar(wchar_t *str1,
                                               wchar_t *str2);
static int      _ecore_xcb_textlist_len_wchar(wchar_t *str);

#ifdef HAVE_ICONV
Eina_Bool
_ecore_xcb_utf8_textlist_to_textproperty(char                   **list,
                                         int                      count,
                                         Ecore_Xcb_Encoding_Style style,
                                         Ecore_Xcb_Textproperty  *ret)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   return _ecore_xcb_textlist_to_textproperty("utf8string", list, count,
                                              style, ret);
}

#endif

Eina_Bool
_ecore_xcb_mb_textlist_to_textproperty(char                   **list,
                                       int                      count,
                                       Ecore_Xcb_Encoding_Style style,
                                       Ecore_Xcb_Textproperty  *ret)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   return _ecore_xcb_textlist_to_textproperty("multiByte", list, count,
                                              style, ret);
}

/* NB: This Function May Not Be Correct !!!
 * (as I do not know text conversion, locales, etc, etc very well)
 *
 * Portions were ripped from libX11 XTextListToTextProperty
 */
Eina_Bool
_ecore_xcb_textlist_to_textproperty(const char              *type,
                                    char                   **list,
                                    int                      count,
                                    Ecore_Xcb_Encoding_Style style,
                                    Ecore_Xcb_Textproperty  *ret)
{
   Eina_Bool is_wide = EINA_FALSE;
   Ecore_X_Atom encoding;
   int len = 0, nitems = 0, i = 0;
   size_t from_left = 0, to_left = 0;
   int unconv_num = 0, val = 0;
   char *buff, *to, *value, *from;
   const char *to_type, *from_type;
   char **mb = NULL;
   wchar_t **wc = NULL;
#ifdef HAVE_ICONV
   iconv_t conv;
#endif

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!strcmp("wideChar", type)) is_wide = EINA_TRUE;
   len = _ecore_xcb_textlist_get_buffer_size(is_wide, list, count);
   if (!(buff = (char *)malloc(len * sizeof(char)))) return EINA_FALSE;
   from_type = nl_langinfo(CODESET);
   switch (style)
     {
      case XcbStringStyle:
      case XcbStdICCTextStyle:
        encoding = ECORE_X_ATOM_STRING;
        to_type = nl_langinfo(CODESET);
//        to_type = "string";
        break;

      case XcbUTF8StringStyle:
        encoding = ECORE_X_ATOM_UTF8_STRING;
        to_type = "UTF-8";
        break;

      case XcbCompoundTextStyle:
        encoding = ECORE_X_ATOM_COMPOUND_TEXT;
        to_type = nl_langinfo(CODESET);
//        to_type = "compoundText";
        break;

      case XcbTextStyle:
        encoding = ECORE_X_ATOM_TEXT;
        to_type = nl_langinfo(CODESET);
//        to_type = "multiByte";
        if (!is_wide)
          {
             nitems = 0;
             mb = (char **)list;
             to = buff;
             for (i = 0; ((i < count) && (len > 0)); i++)
               {
                  if (*mb) strcpy(to, *mb);
                  else *to = '\0';
                  from_left = (*mb ? strlen(*mb) : 0) + 1;
                  nitems += from_left;
                  to += from_left;
                  mb++;
               }
             unconv_num = 0;
             goto done;
          }
        break;

      default:
        free(buff);
        return EINA_FALSE;
        break;
     }

   if (count < 1)
     {
        nitems = 0;
        goto done;
     }

retry:
#ifdef HAVE_ICONV
   conv = iconv_open(to_type, from_type);
#endif

   if (is_wide)
     wc = (wchar_t **)list;
   else
     mb = (char **)list;

   to = buff;
   to_left = len;
   unconv_num = 0;
   for (i = 1; to_left > 0; i++)
     {
        if (is_wide)
          {
             from = (char *)*wc;
             from_left = _ecore_xcb_textlist_get_wc_len(*wc);
             wc++;
          }
        else
          {
             from = *mb;
             from_left = (*mb ? strlen(*mb) : 0);
             mb++;
          }

#ifdef HAVE_ICONV
        val = iconv(conv, &from, &from_left, &to, &to_left);
#endif
        if (val < 0) continue;
        if ((val > 0) && (style == XcbStdICCTextStyle) &&
            (encoding == ECORE_X_ATOM_STRING))
          {
#ifdef HAVE_ICONV
             iconv_close(conv);
#endif
             encoding = ECORE_X_ATOM_COMPOUND_TEXT;
             goto retry;
          }

        unconv_num += val;
        *to++ = '\0';
        to_left--;
        if (i >= count) break;
     }

#ifdef HAVE_ICONV
   iconv_close(conv);
#endif
   nitems = (to - buff);

done:
   if (nitems <= 0) nitems = 1;
   if (!(value = (char *)malloc(nitems * sizeof(char))))
     {
        free(buff);
        return EINA_FALSE;
     }
   if (nitems == 1)
     *value = 0;
   else
     memcpy(value, buff, nitems);
   nitems--;
   free(buff);

   ret->value = value;
   ret->encoding = encoding;
   ret->format = 8;
   ret->nitems = nitems;

   return EINA_TRUE;
}

#ifdef HAVE_ICONV
Eina_Bool
_ecore_xcb_utf8_textproperty_to_textlist(const Ecore_Xcb_Textproperty *text_prop,
                                         char                       ***list_ret,
                                         int                          *count_ret)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   return _ecore_xcb_textproperty_to_textlist(text_prop, "utf8String",
                                              list_ret, count_ret);
}

#endif

Eina_Bool
_ecore_xcb_mb_textproperty_to_textlist(const Ecore_Xcb_Textproperty *text_prop,
                                       char                       ***list_ret,
                                       int                          *count_ret)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   return _ecore_xcb_textproperty_to_textlist(text_prop, "multiByte",
                                              list_ret, count_ret);
}

Eina_Bool
_ecore_xcb_textproperty_to_textlist(const Ecore_Xcb_Textproperty *text_prop,
                                    const char                   *type,
                                    char                       ***list_ret,
                                    int                          *count_ret)
{
   Eina_Bool is_wide = EINA_FALSE;
   Eina_Bool do_strcpy = EINA_FALSE;
   const char *from_type;
   char *buff, *to, *from;
   char *lptr, *sptr;
   int nitems = 0, len = 0, num = 0, ret = 0;
   size_t from_left = 0, to_left = 0;
#ifdef HAVE_ICONV
   iconv_t conv = 0;
#endif

   *list_ret = NULL;
   *count_ret = 0;
   if (!strcmp("wideChar", type)) is_wide = EINA_TRUE;

   nitems = text_prop->nitems;
   if (nitems <= 0) return EINA_TRUE;

   if (text_prop->format != 8) return EINA_FALSE;

   from_type = nl_langinfo(CODESET);
   if (text_prop->encoding == ECORE_X_ATOM_UTF8_STRING)
     from_type = "UTF-8";

   if (is_wide)
     len = (text_prop->nitems + 1) * sizeof(wchar_t);
   else
     {
        if (!strcmp(type, "utf8String"))
          len = text_prop->nitems * 6 + 1;
        else
          len = text_prop->nitems * MB_CUR_MAX + 1;
     }

   buff = (char *)malloc(len * sizeof(char));
   if (!buff) return EINA_FALSE;

   to = buff;
   to_left = len;

   if (!strcmp(from_type, type))
     do_strcpy = EINA_TRUE;
   else
     {
#ifdef HAVE_ICONV
        conv = iconv_open(type, from_type);
#endif
        if (!conv)
          {
             free(buff);
             return EINA_FALSE;
          }
     }

   lptr = sptr = text_prop->value;
   num = *count_ret = 0;
   while (1)
     {
        if ((nitems == 0) || (*sptr == 0))
          {
             from = lptr;
             from_left = sptr - lptr;
             lptr = sptr;
             if (do_strcpy)
               {
                  int l = 0;

                  l = MIN(from_left, to_left);
                  strncpy(to, from, l);
                  from += len;
                  to += len;
                  from_left -= l;
                  to_left -= l;
                  ret = 0;
               }
             else
               ret = iconv(conv, &from, &from_left, &to, &to_left);

             if (ret < 0) continue;
             num += ret;
             (*count_ret)++;
             if (nitems == 0) break;
             lptr = ++sptr;
             if (is_wide)
               {
                  *((wchar_t *)to) = (wchar_t)0;
                  to += sizeof(wchar_t);
                  to_left -= sizeof(wchar_t);
               }
             else
               {
                  *((char *)to) = '\0';
                  to++;
                  to_left--;
               }
          }
        else
          sptr++;

        nitems--;
     }

#if HAVE_ICONV
   if (!do_strcpy) iconv_close(conv);
#endif

   if (is_wide)
     {
        *((wchar_t *)to) = (wchar_t)0;
        to_left -= sizeof(wchar_t);
     }
   else
     {
        *((char *)to) = '\0';
        to_left--;
     }

   *list_ret =
     _ecore_xcb_textlist_alloc_list(is_wide, *count_ret, (len - to_left));
   if (*list_ret)
     _ecore_xcb_textlist_copy_list(is_wide, buff, *list_ret, *count_ret);

   free(buff);

   return EINA_TRUE;
}

static int
_ecore_xcb_textlist_get_buffer_size(Eina_Bool is_wide,
                                    void     *list,
                                    int       count)
{
   int len = 0;
   char **mb;
   wchar_t **wc;

   if (!list) return 0;
   if (is_wide)
     {
        wc = (wchar_t **)list;
        for (; count-- > 0; wc++)
          if (*wc) len += _ecore_xcb_textlist_get_wc_len(*wc) + 1;
        len *= 5;
     }
   else
     {
        mb = (char **)list;
        for (; count-- > 0; mb++)
          if (*mb) len += strlen(*mb) + 1;
        len *= 3;
     }
   len = (len / 2048 + 1) * 2048;
   return len;
}

static int
_ecore_xcb_textlist_get_wc_len(wchar_t *wstr)
{
   wchar_t *ptr;

   ptr = wstr;
   while (*ptr)
     ptr++;

   return ptr - wstr;
}

static void *
_ecore_xcb_textlist_alloc_list(Eina_Bool is_wide,
                               int       count,
                               int       nitems)
{
   if (is_wide)
     {
        wchar_t **list;

        list = (wchar_t **)malloc(count * sizeof(wchar_t *));
        if (!list) return NULL;
        *list = (wchar_t *)malloc(nitems * sizeof(wchar_t));
        if (!*list)
          {
             free(list);
             return NULL;
          }
        return *list;
     }
   else
     {
        char **list;

        list = (char **)malloc(count * sizeof(char *));
        if (!list) return NULL;
        *list = (char *)malloc(nitems * sizeof(char));
        if (!*list)
          {
             free(list);
             return NULL;
          }
        return *list;
     }
}

static void
_ecore_xcb_textlist_copy_list(Eina_Bool is_wide,
                              void     *text,
                              char    **list,
                              int       count)
{
   int len = 0;

   if (is_wide)
     {
        wchar_t *txt, *str, **wlist;

        txt = (wchar_t *)text;
        wlist = (wchar_t **)list;
        for (str = *wlist; count > 0; count--, wlist++)
          {
             _ecore_xcb_textlist_copy_wchar(str, txt);
             *wlist = str;
             len = (_ecore_xcb_textlist_len_wchar(str) + 1);
             str += len;
             txt += len;
          }
     }
   else
     {
        char *txt, *str, **slist;

        txt = (char *)text;
        slist = (char **)list;
        for (str = *slist; count > 0; count--, slist++)
          {
             strcpy(str, txt);
             *slist = str;
             len = strlen(str) + 1;
             str += len;
             txt += len;
          }
     }
}

static wchar_t *
_ecore_xcb_textlist_copy_wchar(wchar_t *str1,
                               wchar_t *str2)
{
   wchar_t *tmp;

   tmp = str1;
   while ((*str1++ = *str2++))
     ;
   return tmp;
}

static int
_ecore_xcb_textlist_len_wchar(wchar_t *str)
{
   wchar_t *ptr;

   ptr = str;
   while (*ptr)
     ptr++;
   return ptr - str;
}