123
128
setlocale (LC_ALL, "C.utf8");
131
buffer = malloc (sizeof (char32_t) * 4);
127
133
schar out[MB_CUR_MAX];
128
134
for(size_t n = 0, buflen = 4, bufpos = 0;
129
135
n < s_ustring_len (us);
136
n++, buflen += MB_CUR_MAX) { /* could be: buflen *= 2 ?*/
131
137
sint rc = c32rtomb(out, us[n], &mbstate);
132
if(! (resized = s_realloc (buffer, buflen)))
138
if(! (resized = s_realloc (buffer, buflen))) {
134
142
for (sint i = 0; i < rc; ++i) {
143
buffer[bufpos] = out[i];
149
setlocale (LC_ALL, saved_locale);
140
151
/* shrink buffer to actually required size */
141
152
if (! (resized = s_realloc (buffer, bufpos + 1)))
145
setlocale (LC_ALL, saved_locale);
150
setlocale (LC_ALL, saved_locale);
163
/******************************************************************************/
165
s_string_to_ustring (const schar * str) {
170
memset (&mbstate, 0, sizeof (mbstate));
172
suint slen = s_string_len (str);
175
/* Addaptation of the code in s_wstring_to_string to make it convert
176
* mb strings to char32_t strings.
180
schar * saved_locale;
183
size_t old_locale_len;
184
old_locale = setlocale (LC_ALL, NULL);
185
old_locale_len = strlen (old_locale) + 1;
186
saved_locale = s_malloc (sizeof (char *) * old_locale_len);
187
memcpy (saved_locale, old_locale, old_locale_len);
189
setlocale (LC_ALL, "C.utf8");
192
buffer = malloc (sizeof (char32_t) * 4);
194
suchar * out = s_malloc (sizeof (suchar));
195
for(size_t n = 0, buflen = 4, bufpos = 0;
198
sint rc = mbrtoc32(out, &str[n], slen ,&mbstate);
199
n += rc; // Get next mb in string...?
200
if(! (resized = s_realloc (buffer, buflen * sizeof (suchar)))){
204
buffer[bufpos] = *out;
211
setlocale (LC_ALL, saved_locale);
213
/* shrink buffer to actually required size */
214
if (! (resized = s_realloc (buffer, (bufpos + 1) * sizeof (suchar)))) {
227
/******************************************************************************/
158
229
* This should not be used. If you need to use this, uncomment it.
159
230
* I am conveting to something more sane than wchar_t.
211
282
bufpos += converted;
286
setlocale (LC_ALL, saved_locale);
214
288
/* shrink buffer to actually required size */
215
if (! (resized = s_realloc (buffer, bufpos + 1)))
289
if (! (resized = s_realloc (buffer, bufpos + 1))) {
219
setlocale (LC_ALL, saved_locale);
224
setlocale (LC_ALL, saved_locale);