]> git.sur5r.net Git - i3/i3/blob - i3-input/ucs2_to_utf8.c
Merge branch 'fix-var-tabs'
[i3/i3] / i3-input / ucs2_to_utf8.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * ucs2_to_utf8.c: Converts between UCS-2 and UTF-8, both of which are used in
8  *                 different contexts in X11.
9  *
10  */
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <err.h>
15 #include <iconv.h>
16
17 #include "libi3.h"
18
19 static iconv_t conversion_descriptor = 0;
20 static iconv_t conversion_descriptor2 = 0;
21
22 /*
23  * Returns the input string, but converted from UCS-2 to UTF-8. Memory will be
24  * allocated, thus the caller has to free the output.
25  *
26  */
27 char *convert_ucs_to_utf8(char *input) {
28     size_t input_size = 2;
29     /* UTF-8 may consume up to 4 byte */
30     int buffer_size = 8;
31
32     char *buffer = scalloc(buffer_size);
33     size_t output_size = buffer_size;
34     /* We need to use an additional pointer, because iconv() modifies it */
35     char *output = buffer;
36
37     /* We convert the input into UCS-2 big endian */
38     if (conversion_descriptor == 0) {
39         conversion_descriptor = iconv_open("UTF-8", "UCS-2BE");
40         if (conversion_descriptor == 0)
41             errx(EXIT_FAILURE, "Error opening the conversion context");
42     }
43
44     /* Get the conversion descriptor back to original state */
45     iconv(conversion_descriptor, NULL, NULL, NULL, NULL);
46
47     /* Convert our text */
48     int rc = iconv(conversion_descriptor, (void*)&input, &input_size, &output, &output_size);
49     if (rc == (size_t)-1) {
50         free(buffer);
51         perror("Converting to UCS-2 failed");
52         return NULL;
53     }
54
55     return buffer;
56 }
57
58 /*
59  * Converts the given string to UCS-2 big endian for use with
60  * xcb_image_text_16(). The amount of real glyphs is stored in real_strlen,
61  * a buffer containing the UCS-2 encoded string (16 bit per glyph) is
62  * returned. It has to be freed when done.
63  *
64  */
65 char *convert_utf8_to_ucs2(char *input, int *real_strlen) {
66     size_t input_size = strlen(input) + 1;
67     /* UCS-2 consumes exactly two bytes for each glyph */
68     int buffer_size = input_size * 2;
69
70     char *buffer = smalloc(buffer_size);
71     size_t output_size = buffer_size;
72     /* We need to use an additional pointer, because iconv() modifies it */
73     char *output = buffer;
74
75     /* We convert the input into UCS-2 big endian */
76     if (conversion_descriptor2 == 0) {
77         conversion_descriptor2 = iconv_open("UCS-2BE", "UTF-8");
78         if (conversion_descriptor2 == 0)
79             errx(EXIT_FAILURE, "Error opening the conversion context");
80     }
81
82     /* Get the conversion descriptor back to original state */
83     iconv(conversion_descriptor2, NULL, NULL, NULL, NULL);
84
85     /* Convert our text */
86     int rc = iconv(conversion_descriptor2, (void*)&input, &input_size, &output, &output_size);
87     if (rc == (size_t)-1) {
88         perror("Converting to UCS-2 failed");
89         free(buffer);
90         if (real_strlen != NULL)
91             *real_strlen = 0;
92         return NULL;
93     }
94
95     if (real_strlen != NULL)
96         *real_strlen = ((buffer_size - output_size) / 2) - 1;
97
98     return buffer;
99 }
100