rjk's `make install' patch.
[sgt/halibut] / ustring.c
CommitLineData
d7482997 1/*
2 * ustring.c: Unicode string routines
3 */
4
5#include <wchar.h>
7e976207 6#include <stdlib.h>
7#include <assert.h>
d7482997 8#include <time.h>
9#include "halibut.h"
10
e4ea58f8 11wchar_t *ustrdup(wchar_t const *s) {
d7482997 12 wchar_t *r;
13 if (s) {
14 r = mknewa(wchar_t, 1+ustrlen(s));
15 ustrcpy(r, s);
16 } else {
17 r = mknew(wchar_t);
18 *r = 0;
19 }
20 return r;
21}
22
e4ea58f8 23static char *ustrtoa_internal(wchar_t const *s, char *outbuf, int size,
24 int charset, int careful) {
25 int len, ret, err;
26 charset_state state = CHARSET_INIT_STATE;
27
d7482997 28 if (!s) {
29 *outbuf = '\0';
30 return outbuf;
31 }
e4ea58f8 32
33 len = ustrlen(s);
34 size--; /* leave room for terminating NUL */
35 *outbuf = '\0';
36 while (len > 0) {
37 err = 0;
38 ret = charset_from_unicode(&s, &len, outbuf, size, charset, &state,
39 (careful ? &err : NULL));
40 if (err)
41 return NULL;
42 if (!ret)
43 return outbuf;
44 size -= ret;
45 outbuf += ret;
46 *outbuf = '\0';
47 }
48 /*
49 * Clean up
50 */
51 ret = charset_from_unicode(NULL, 0, outbuf, size, charset, &state, NULL);
52 size -= ret;
53 outbuf += ret;
54 *outbuf = '\0';
d7482997 55 return outbuf;
56}
57
e4ea58f8 58char *ustrtoa(wchar_t const *s, char *outbuf, int size, int charset) {
59 return ustrtoa_internal(s, outbuf, size, charset, FALSE);
60}
61
62char *ustrtoa_careful(wchar_t const *s, char *outbuf, int size, int charset) {
63 return ustrtoa_internal(s, outbuf, size, charset, TRUE);
64}
65
66wchar_t *ustrfroma(char const *s, wchar_t *outbuf, int size, int charset) {
67 int len, ret;
68 charset_state state = CHARSET_INIT_STATE;
69
ba9c1487 70 if (!s) {
71 *outbuf = L'\0';
72 return outbuf;
73 }
e4ea58f8 74
75 len = strlen(s);
76 size--; /* allow for terminating NUL */
77 *outbuf = L'\0';
78 while (len > 0) {
79 ret = charset_to_unicode(&s, &len, outbuf, size,
80 charset, &state, NULL, 0);
81 if (!ret)
82 return outbuf;
83 outbuf += ret;
84 size -= ret;
85 *outbuf = L'\0';
86 }
ba9c1487 87 return outbuf;
88}
89
e4ea58f8 90char *utoa_internal_dup(wchar_t const *s, int charset, int *lenp, int careful)
91{
92 char *outbuf;
93 int outpos, outlen, len, ret, err;
94 charset_state state = CHARSET_INIT_STATE;
50d6b4bd 95
e4ea58f8 96 if (!s) {
97 return dupstr("");
98 }
50d6b4bd 99
e4ea58f8 100 len = ustrlen(s);
101
102 outlen = len + 10;
103 outbuf = mknewa(char, outlen);
104
105 outpos = 0;
106 outbuf[outpos] = '\0';
107
108 while (len > 0) {
109 err = 0;
110 ret = charset_from_unicode(&s, &len,
111 outbuf + outpos, outlen - outpos - 1,
112 charset, &state, (careful ? &err : NULL));
113 if (err) {
114 sfree(outbuf);
115 return NULL;
116 }
117 if (!ret) {
118 outlen = outlen * 3 / 2;
119 outbuf = resize(outbuf, outlen);
120 }
121 outpos += ret;
122 outbuf[outpos] = '\0';
123 }
124 /*
125 * Clean up
126 */
127 outlen = outpos + 32;
128 outbuf = resize(outbuf, outlen);
129 ret = charset_from_unicode(NULL, 0,
130 outbuf + outpos, outlen - outpos + 1,
131 charset, &state, NULL);
132 outpos += ret;
133 outbuf[outpos] = '\0';
134 if (lenp)
135 *lenp = outpos;
136 return outbuf;
50d6b4bd 137}
138
e4ea58f8 139char *utoa_dup(wchar_t const *s, int charset)
140{
141 return utoa_internal_dup(s, charset, NULL, FALSE);
142}
143
144char *utoa_dup_len(wchar_t const *s, int charset, int *len)
145{
146 return utoa_internal_dup(s, charset, len, FALSE);
147}
148
149char *utoa_careful_dup(wchar_t const *s, int charset)
150{
151 return utoa_internal_dup(s, charset, NULL, TRUE);
152}
153
154wchar_t *ufroma_dup(char const *s, int charset) {
ba9c1487 155 int len;
156 wchar_t *buf = NULL;
157
158 len = strlen(s) + 1;
159 do {
160 buf = resize(buf, len);
e4ea58f8 161 ustrfroma(s, buf, len, charset);
ba9c1487 162 len = (3 * len) / 2 + 1; /* this guarantees a strict increase */
163 } while (ustrlen(buf) >= len-1);
164
165 buf = resize(buf, ustrlen(buf)+1);
166 return buf;
167}
168
7e976207 169char *utoa_locale_dup(wchar_t const *s)
170{
171 /*
172 * This variant uses the C library locale.
173 */
174 char *ret;
175 int len;
176 size_t siz;
177
178 len = ustrlen(s);
179
180 ret = mknewa(char, 1 + MB_CUR_MAX * len);
181
182 siz = wcstombs(ret, s, len);
183
184 if (siz) {
185 assert(siz <= MB_CUR_MAX * len);
186 ret[siz] = '\0';
187 ret = resize(ret, siz+1);
188 return ret;
189 }
190
191 /*
192 * If that failed, try a different strategy (which we will also
193 * attempt in the total absence of wcstombs). Retrieve the
194 * locale's charset from nl_langinfo or equivalent, and use
195 * normal utoa_dup.
196 */
197 return utoa_dup(s, charset_from_locale());
198}
199
200wchar_t *ufroma_locale_dup(char const *s)
201{
202 /*
203 * This variant uses the C library locale.
204 */
205 wchar_t *ret;
206 int len;
207 size_t siz;
208
209 len = strlen(s);
210
211 ret = mknewa(wchar_t, 1 + 2*len); /* be conservative */
212
213 siz = mbstowcs(ret, s, len);
214
215 if (siz) {
216 assert(siz <= (size_t)(2 * len));
217 ret[siz] = L'\0';
218 ret = resize(ret, siz+1);
219 return ret;
220 }
221
222 /*
223 * If that failed, try a different strategy (which we will also
224 * attempt in the total absence of wcstombs). Retrieve the
225 * locale's charset from nl_langinfo or equivalent, and use
226 * normal ufroma_dup.
227 */
228 return ufroma_dup(s, charset_from_locale());
229}
230
5dd44dce 231int ustrlen(wchar_t const *s) {
d7482997 232 int len = 0;
233 while (*s++) len++;
234 return len;
235}
236
237wchar_t *uadv(wchar_t *s) {
238 return s + 1 + ustrlen(s);
239}
240
5dd44dce 241wchar_t *ustrcpy(wchar_t *dest, wchar_t const *source) {
d7482997 242 wchar_t *ret = dest;
243 do {
244 *dest++ = *source;
245 } while (*source++);
246 return ret;
247}
248
08e78486 249wchar_t *ustrncpy(wchar_t *dest, wchar_t const *source, int n) {
250 wchar_t *ret = dest;
251 do {
252 *dest++ = *source;
253 if (*source) source++;
254 } while (n-- > 0);
255 return ret;
256}
257
d7482997 258int ustrcmp(wchar_t *lhs, wchar_t *rhs) {
259 if (!lhs && !rhs) return 0;
260 if (!lhs) return -1;
261 if (!rhs) return +1;
262 while (*lhs && *rhs && *lhs==*rhs)
263 lhs++, rhs++;
264 if (*lhs < *rhs)
265 return -1;
266 else if (*lhs > *rhs)
267 return 1;
268 return 0;
269}
270
271wchar_t utolower(wchar_t c) {
272 if (c == L'\0')
273 return c; /* this property needed by ustricmp */
9badd775 274#ifdef HAS_TOWLOWER
275 return towlower(c);
276#else
d7482997 277 if (c >= 'A' && c <= 'Z')
278 c += 'a'-'A';
279 return c;
9badd775 280#endif
d7482997 281}
282
831da32e 283int uisalpha(wchar_t c) {
9badd775 284#ifdef HAS_ISWALPHA
285 return iswalpha(c);
286#else
831da32e 287 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
9badd775 288#endif
831da32e 289}
290
d7482997 291int ustricmp(wchar_t *lhs, wchar_t *rhs) {
292 wchar_t lc, rc;
293 while ((lc = utolower(*lhs)) == (rc = utolower(*rhs)) && lc && rc)
294 lhs++, rhs++;
295 if (!lc && !rc)
296 return 0;
297 if (lc < rc)
298 return -1;
299 else
300 return 1;
301}
302
303wchar_t *ustrlow(wchar_t *s) {
304 wchar_t *p = s;
305 while (*p) {
306 *p = utolower(*p);
307 p++;
308 }
309 return s;
310}
311
dd567011 312int utoi(wchar_t const *s) {
d7482997 313 int sign = +1;
314 int n;
315
316 if (*s == L'-') {
317 s++;
318 sign = -1;
319 }
320
321 n = 0;
322 while (*s && *s >= L'0' && *s <= L'9') {
323 n *= 10;
324 n += (*s - '0');
325 s++;
326 }
327
328 return n;
329}
330
dd567011 331double utof(wchar_t const *s)
332{
333 char *cs = utoa_dup(s, CS_ASCII);
334 double ret = atof(cs);
335 sfree(cs);
336 return ret;
337}
338
339int utob(wchar_t const *s) {
d7482997 340 if (!ustricmp(s, L"yes") || !ustricmp(s, L"y") ||
341 !ustricmp(s, L"true") || !ustricmp(s, L"t"))
342 return TRUE;
343 return FALSE;
344}
345
346int uisdigit(wchar_t c) {
347 return c >= L'0' && c <= L'9';
348}
349
350#define USTRFTIME_DELTA 128
c8422236 351static void ustrftime_internal(rdstring *rs, char formatchr,
352 const struct tm *timespec)
353{
e4ea58f8 354 /*
d7482997 355 * strftime has the entertaining property that it returns 0
356 * _either_ on out-of-space _or_ on successful generation of
357 * the empty string. Hence we must ensure our format can never
358 * generate the empty string. Somebody throw a custard pie at
359 * whoever was responsible for that. Please?
360 */
c8422236 361
362#ifdef HAS_WCSFTIME
363 wchar_t *buf = NULL;
364 wchar_t fmt[4];
365 int size, ret;
366
367 fmt[0] = L' ';
368 fmt[1] = L'%';
369 /* Format chars are all ASCII, so conversion to Unicode is no problem */
370 fmt[2] = formatchr;
371 fmt[3] = L'\0';
372
373 size = 0;
374 do {
d7482997 375 size += USTRFTIME_DELTA;
c8422236 376 buf = resize(buf, size);
377 ret = (int) wcsftime(buf, size, fmt, timespec);
378 } while (ret == 0);
379
380 rdadds(rs, buf+1);
381 sfree(buf);
382#else
383 char *buf = NULL;
384 wchar_t *cvtbuf;
385 char fmt[4];
386 int size, ret;
387
388 fmt[0] = ' ';
389 fmt[1] = '%';
390 fmt[2] = formatchr;
391 fmt[3] = '\0';
392
393 size = 0;
394 do {
395 size += USTRFTIME_DELTA;
396 buf = resize(buf, size);
397 ret = (int) strftime(buf, size, fmt, timespec);
398 } while (ret == 0);
399
400 cvtbuf = ufroma_locale_dup(buf+1);
401 rdadds(rs, cvtbuf);
402 sfree(cvtbuf);
403 sfree(buf);
404#endif
405}
406
407wchar_t *ustrftime(const wchar_t *wfmt, const struct tm *timespec)
408{
409 rdstring rs = { 0, 0, NULL };
410
411 if (!wfmt)
412 wfmt = L"%c";
413
414 while (*wfmt) {
415 if (wfmt[0] == L'%' && wfmt[1] == L'%') {
416 rdadd(&rs, L'%');
417 wfmt += 2;
418 } else if (wfmt[0] == L'%' && wfmt[1]) {
419 ustrftime_internal(&rs, wfmt[1], timespec);
420 wfmt += 2;
421 } else {
422 rdadd(&rs, wfmt[0]);
423 wfmt++;
424 }
d7482997 425 }
426
c8422236 427 return rdtrim(&rs);
d7482997 428}
91f93b94 429
430/*
431 * Determine whether a Unicode string can be translated into a
432 * given charset without any missing characters.
433 */
434int cvt_ok(int charset, const wchar_t *s)
435{
436 char buf[256];
437 charset_state state = CHARSET_INIT_STATE;
438 int err, len = ustrlen(s);
439
440 err = 0;
441 while (len > 0) {
442 (void)charset_from_unicode(&s, &len, buf, lenof(buf),
443 charset, &state, &err);
444 if (err)
445 return FALSE;
446 }
447 return TRUE;
448}