2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
25 /*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*/
32 static iconv_t conv_from_locale = 0;
33 static iconv_t conv_to_locale = 0;
39 if ((conv_from_locale = iconv_open("UTF-8", "")) == (iconv_t) -1)
40 fprintf(stderr, "Error: %s\n", strerror(errno));
42 if ((conv_to_locale = iconv_open("", "UTF-8")) == (iconv_t) -1)
43 fprintf(stderr, "Error: %s\n", strerror(errno));
52 if (conv_from_locale != (iconv_t) -1)
53 iconv_close(conv_from_locale);
55 if (conv_to_locale != (iconv_t) -1)
56 iconv_close(conv_to_locale);
62 /*---------------------------------------------------------------------------*/
64 char *text_from_locale(char *str0)
67 static char buffer[MAXSTR * 2];
72 size_t l0 = strlen(str0);
73 size_t l1 = sizeof (buffer);
75 if (conv_from_locale == (iconv_t) -1)
78 if (iconv(conv_from_locale, &str0p, &l0, &str1p, &l1) == (size_t) -1)
79 fprintf(stderr, "Error while converting to UTF-8: %s\n",
90 char *text_to_locale(char *str0)
93 static char buffer[MAXSTR * 2];
98 size_t l0 = strlen(str0);
99 size_t l1 = sizeof (buffer);
101 if (conv_to_locale == (iconv_t) -1)
104 if (iconv(conv_to_locale, &str0p, &l0, &str1p, &l1) == (size_t) -1)
105 fprintf(stderr, "Error while converting from UTF-8: %s\n",
116 /*---------------------------------------------------------------------------*/
118 int text_add_char(Uint32 unicode, char *string, int maxbytes, int maxchars)
120 size_t pos = strlen(string);
123 if (unicode < 0x80) l = 1;
124 else if (unicode < 0x0800) l = 2;
125 else if (unicode < 0x10000) l = 3;
128 if ((pos + l >= maxbytes) || (text_length(string) + 1 >= maxchars))
132 string[pos++] = (char) unicode;
133 else if (unicode < 0x0800)
135 string[pos++] = (char) ((unicode >> 6) | 0xC0);
136 string[pos++] = (char) ((unicode & 0x3F) | 0x80);
138 else if (unicode < 0x10000)
140 string[pos++] = (char) ((unicode >> 12) | 0xE0);
141 string[pos++] = (char) (((unicode >> 6) & 0x3F) | 0x80);
142 string[pos++] = (char) ((unicode & 0x3F) | 0x80);
146 string[pos++] = (char) ((unicode >> 18) | 0xF0);
147 string[pos++] = (char) (((unicode >> 12) & 0x3F) | 0x80);
148 string[pos++] = (char) (((unicode >> 6) & 0x3F) | 0x80);
149 string[pos++] = (char) ((unicode & 0x3F) | 0x80);
157 int text_del_char(char *string)
159 size_t pos = strlen(string) - 1;
161 while (pos >= 0 && ((string[pos] & 0xC0) == 0x80))
173 int text_length(const char *string)
177 while (*string != '\0')
178 if ((*string++ & 0xC0) != 0x80)
184 /*---------------------------------------------------------------------------*/