]> git.sur5r.net Git - i3/i3/blob - libi3/ucs2_conversion.c
convert_utf8_to_ucs2: Allow partial conversion
[i3/i3] / libi3 / ucs2_conversion.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #include "libi3.h"
9
10 #include <err.h>
11 #include <errno.h>
12 #include <iconv.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 static iconv_t utf8_conversion_descriptor = (iconv_t)-1;
17 static iconv_t ucs2_conversion_descriptor = (iconv_t)-1;
18
19 /*
20  * Converts the given string to UTF-8 from UCS-2 big endian. The return value
21  * must be freed after use.
22  *
23  */
24 char *convert_ucs2_to_utf8(xcb_char2b_t *text, size_t num_glyphs) {
25     /* Allocate the output buffer (UTF-8 is at most 4 bytes per glyph) */
26     size_t buffer_size = num_glyphs * 4 + 1;
27     char *buffer = scalloc(buffer_size, 1);
28
29     /* We need to use an additional pointer, because iconv() modifies it */
30     char *output = buffer;
31     size_t output_size = buffer_size - 1;
32
33     if (utf8_conversion_descriptor == (iconv_t)-1) {
34         /* Get a new conversion descriptor */
35         utf8_conversion_descriptor = iconv_open("UTF-8", "UCS-2BE");
36         if (utf8_conversion_descriptor == (iconv_t)-1)
37             err(EXIT_FAILURE, "Error opening the conversion context");
38     } else {
39         /* Reset the existing conversion descriptor */
40         iconv(utf8_conversion_descriptor, NULL, NULL, NULL, NULL);
41     }
42
43     /* Do the conversion */
44     size_t input_len = num_glyphs * sizeof(xcb_char2b_t);
45     size_t rc = iconv(utf8_conversion_descriptor, (char **)&text,
46                       &input_len, &output, &output_size);
47     if (rc == (size_t)-1) {
48         perror("Converting to UTF-8 failed");
49         free(buffer);
50         return NULL;
51     }
52
53     return buffer;
54 }
55
56 /*
57  * Converts the given string to UCS-2 big endian for use with
58  * xcb_image_text_16(). The amount of real glyphs is stored in real_strlen,
59  * a buffer containing the UCS-2 encoded string (16 bit per glyph) is
60  * returned. It has to be freed when done.
61  *
62  */
63 xcb_char2b_t *convert_utf8_to_ucs2(char *input, size_t *real_strlen) {
64     /* Calculate the input buffer size (UTF-8 is strlen-safe) */
65     size_t input_size = strlen(input);
66
67     /* Calculate the output buffer size and allocate the buffer */
68     size_t buffer_size = input_size * sizeof(xcb_char2b_t);
69     xcb_char2b_t *buffer = smalloc(buffer_size);
70
71     /* We need to use an additional pointer, because iconv() modifies it */
72     size_t output_bytes_left = buffer_size;
73     xcb_char2b_t *output = buffer;
74
75     if (ucs2_conversion_descriptor == (iconv_t)-1) {
76         /* Get a new conversion descriptor. //IGNORE is a GNU suffix that makes
77          * iconv to silently discard characters that cannot be represented in
78          * the target character set. */
79         ucs2_conversion_descriptor = iconv_open("UCS-2BE//IGNORE", "UTF-8");
80         if (ucs2_conversion_descriptor == (iconv_t)-1) {
81             ucs2_conversion_descriptor = iconv_open("UCS-2BE", "UTF-8");
82         }
83         if (ucs2_conversion_descriptor == (iconv_t)-1) {
84             err(EXIT_FAILURE, "Error opening the conversion context");
85         }
86     } else {
87         /* Reset the existing conversion descriptor */
88         iconv(ucs2_conversion_descriptor, NULL, NULL, NULL, NULL);
89     }
90
91     /* Do the conversion */
92     size_t rc = iconv(ucs2_conversion_descriptor, &input, &input_size,
93                       (char **)&output, &output_bytes_left);
94     if (rc == (size_t)-1) {
95         /* Conversion will only be partial. */
96         perror("Converting to UCS-2 failed");
97     }
98
99     /* If no bytes where converted, this is equivalent to freeing buffer. */
100     buffer_size -= output_bytes_left;
101     buffer = srealloc(buffer, buffer_size);
102
103     /* Return the resulting string's length */
104     if (real_strlen != NULL) {
105         *real_strlen = buffer_size / sizeof(xcb_char2b_t);
106     }
107
108     return buffer;
109 }