Return a non-zero exit code if any stress test fails.
[mpdm.git] / mpdm_s.c
blobc599fcdaa33a89213fed09bb3d478d5f0d3d7c73
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2010 Angel Ortega <angel@triptico.com>
6 mpdm_s.c - String management
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <wchar.h>
32 #include <locale.h>
33 #include <wctype.h>
35 #ifdef CONFOPT_GETTEXT
36 #include <libintl.h>
37 #endif
39 #ifdef CONFOPT_WIN32
40 #include <windows.h>
41 #endif
43 #include "mpdm.h"
46 /** code **/
48 void *mpdm_poke_o(void *dst, int *dsize, int *offset, const void *org,
49 int osize, int esize)
51 if (org != NULL && osize) {
52 /* enough room? */
53 if (*offset + osize > *dsize) {
54 /* no; enlarge */
55 *dsize += osize;
57 dst = realloc(dst, *dsize * esize);
60 memcpy((char *) dst + (*offset * esize), org, osize * esize);
61 *offset += osize;
64 return dst;
68 void *mpdm_poke(void *dst, int *dsize, const void *org, int osize,
69 int esize)
70 /* pokes (adds) org into dst, which is a dynamic string, making it grow */
72 int offset = *dsize;
74 return mpdm_poke_o(dst, dsize, &offset, org, osize, esize);
78 wchar_t *mpdm_pokewsn(wchar_t * dst, int *dsize, const wchar_t * str,
79 int slen)
80 /* adds a wide string to dst using mpdm_poke() with size */
82 if (str)
83 dst = mpdm_poke(dst, dsize, str, slen, sizeof(wchar_t));
85 return dst;
89 wchar_t *mpdm_pokews(wchar_t * dst, int *dsize, const wchar_t * str)
90 /* adds a wide string to dst using mpdm_poke() */
92 if (str)
93 dst = mpdm_pokewsn(dst, dsize, str, wcslen(str));
95 return dst;
99 wchar_t *mpdm_pokev(wchar_t * dst, int *dsize, const mpdm_t v)
100 /* adds the string in v to dst using mpdm_poke() */
102 if (v != NULL) {
103 const wchar_t *ptr = mpdm_string(v);
105 mpdm_ref(v);
106 dst = mpdm_pokews(dst, dsize, ptr);
107 mpdm_unref(v);
110 return dst;
114 wchar_t *mpdm_mbstowcs(const char *str, int *s, int l)
115 /* converts an mbs to a wcs, but filling invalid chars
116 with question marks instead of just failing */
118 wchar_t *ptr = NULL;
119 char tmp[64]; /* really MB_CUR_MAX + 1 */
120 wchar_t wc;
121 int n, i, c, t = 0;
122 char *cstr;
124 /* allow NULL values for s */
125 if (s == NULL)
126 s = &t;
128 /* if there is a limit, duplicate and break the string */
129 if (l >= 0) {
130 cstr = strdup(str);
131 cstr[l] = '\0';
133 else
134 cstr = (char *) str;
136 /* try first a direct conversion with mbstowcs */
137 if ((*s = mbstowcs(NULL, cstr, 0)) != -1) {
138 /* direct conversion is possible; do it */
139 if ((ptr = malloc((*s + 1) * sizeof(wchar_t))) != NULL) {
140 mbstowcs(ptr, cstr, *s);
141 ptr[*s] = L'\0';
144 else {
145 /* zero everything */
146 *s = n = i = 0;
148 for (;;) {
149 /* no more characters to process? */
150 if ((c = cstr[n + i]) == '\0' && i == 0)
151 break;
153 tmp[i++] = c;
154 tmp[i] = '\0';
156 /* try to convert */
157 if (mbstowcs(&wc, tmp, 1) == (size_t) - 1) {
158 /* can still be an incomplete multibyte char? */
159 if (c != '\0' && i <= (int) MB_CUR_MAX)
160 continue;
161 else {
162 /* too many failing bytes; skip 1 byte */
163 wc = L'?';
164 i = 1;
168 /* skip used bytes and back again */
169 n += i;
170 i = 0;
172 /* store new char */
173 if ((ptr = mpdm_poke(ptr, s, &wc, 1, sizeof(wchar_t))) == NULL)
174 break;
177 /* null terminate and count one less */
178 if (ptr != NULL) {
179 ptr = mpdm_poke(ptr, s, L"", 1, sizeof(wchar_t));
180 (*s)--;
184 /* free the duplicate */
185 if (cstr != str)
186 free(cstr);
188 return ptr;
192 char *mpdm_wcstombs(const wchar_t * str, int *s)
193 /* converts a wcs to an mbs, but filling invalid chars
194 with question marks instead of just failing */
196 char *ptr = NULL;
197 char tmp[64]; /* really MB_CUR_MAX + 1 */
198 int l, t = 0;
200 /* allow NULL values for s */
201 if (s == NULL)
202 s = &t;
204 /* try first a direct conversion with wcstombs */
205 if ((*s = wcstombs(NULL, str, 0)) != -1) {
206 /* direct conversion is possible; do it and return */
207 if ((ptr = malloc(*s + 1)) != NULL) {
208 wcstombs(ptr, str, *s);
209 ptr[*s] = '\0';
212 return ptr;
215 /* invalid encoding? convert characters one by one */
216 *s = 0;
218 while (*str) {
219 if ((l = wctomb(tmp, *str)) <= 0) {
220 /* if char couldn't be converted,
221 write a question mark instead */
222 l = wctomb(tmp, L'?');
225 tmp[l] = '\0';
226 if ((ptr = mpdm_poke(ptr, s, tmp, l, 1)) == NULL)
227 break;
229 str++;
232 /* null terminate and count one less */
233 if (ptr != NULL) {
234 ptr = mpdm_poke(ptr, s, "", 1, 1);
235 (*s)--;
238 return ptr;
242 mpdm_t mpdm_new_wcs(int flags, const wchar_t * str, int size, int cpy)
243 /* creates a new string value from a wcs */
245 wchar_t *ptr;
247 /* a size of -1 means 'calculate it' */
248 if (size == -1 && str != NULL)
249 size = wcslen(str);
251 /* create a copy? */
252 if (cpy) {
253 /* free() on destruction */
254 flags |= MPDM_FREE;
256 /* allocs */
257 if ((ptr = malloc((size + 1) * sizeof(wchar_t))) == NULL)
258 return NULL;
260 /* if no source, reset to zeroes; otherwise, copy */
261 if (str == NULL)
262 memset(ptr, '\0', size * sizeof(wchar_t));
263 else {
264 wcsncpy(ptr, str, size);
265 ptr[size] = L'\0';
268 else
269 ptr = (wchar_t *) str;
271 /* it's a string */
272 flags |= MPDM_STRING;
274 return mpdm_new(flags, ptr, size);
278 mpdm_t mpdm_new_mbstowcs(int flags, const char *str, int l)
279 /* creates a new string value from an mbs */
281 wchar_t *ptr;
282 int size;
284 if ((ptr = mpdm_mbstowcs(str, &size, l)) == NULL)
285 return NULL;
287 /* it's a string */
288 flags |= (MPDM_STRING | MPDM_FREE);
290 return mpdm_new(flags, ptr, size);
294 mpdm_t mpdm_new_wcstombs(int flags, const wchar_t * str)
295 /* creates a new mbs value from a wbs */
297 char *ptr;
298 int size;
300 ptr = mpdm_wcstombs(str, &size);
302 flags |= MPDM_FREE;
304 /* unset the string flag; mbs,s are not 'strings' */
305 flags &= ~MPDM_STRING;
307 return mpdm_new(flags, ptr, size);
311 mpdm_t mpdm_new_i(int ival)
312 /* creates a new string value from an integer */
314 mpdm_t v;
315 char tmp[32];
317 /* creates the visual representation */
318 snprintf(tmp, sizeof(tmp), "%d", ival);
320 v = MPDM_MBS(tmp);
322 return mpdm_set_ival(v, ival);
326 mpdm_t mpdm_new_r(double rval)
327 /* creates a new string value from a real number */
329 mpdm_t v;
330 char tmp[128];
332 /* creates the visual representation */
333 snprintf(tmp, sizeof(tmp), "%lf", rval);
335 /* manually strip useless zeroes */
336 if (strchr(tmp, '.') != NULL) {
337 char *ptr;
339 for (ptr = tmp + strlen(tmp) - 1; *ptr == '0'; ptr--);
341 /* if it's over the ., strip it also */
342 if (*ptr != '.')
343 ptr++;
345 *ptr = '\0';
348 v = MPDM_MBS(tmp);
350 return mpdm_set_rval(v, rval);
354 /* interface */
357 * mpdm_string - Returns a printable representation of a value.
358 * @v: the value
360 * Returns a printable representation of a value. For strings, it's
361 * the value data itself; for any other type, a conversion to string
362 * is returned instead. This value should be used immediately, as it
363 * can be a pointer to a static buffer.
364 * [Strings]
366 wchar_t *mpdm_string(const mpdm_t v)
368 static wchar_t wtmp[32];
369 char tmp[32];
370 static wchar_t *ret;
372 /* if it's NULL, return a constant */
373 if (v == NULL)
374 ret = L"[NULL]";
375 else
376 /* if it's a string, return it */
377 if (v->flags & MPDM_STRING)
378 ret = (wchar_t *) v->data;
379 else {
380 /* otherwise, return a visual representation */
381 snprintf(tmp, sizeof(tmp), "%p", v);
382 mbstowcs(wtmp, tmp, sizeof(wtmp));
383 wtmp[(sizeof(wtmp) / sizeof(wchar_t)) - 1] = L'\0';
385 ret = wtmp;
388 return ret;
393 * mpdm_cmp - Compares two values.
394 * @v1: the first value
395 * @v2: the second value
397 * Compares two values. If both has the MPDM_STRING flag set,
398 * a comparison using wcscoll() is returned; if both are arrays,
399 * the size is compared first and, if they have the same number
400 * elements, each one is compared; otherwise, a simple pointer
401 * comparison is done.
402 * [Strings]
404 int mpdm_cmp(const mpdm_t v1, const mpdm_t v2)
406 int r;
408 mpdm_ref(v1);
409 mpdm_ref(v2);
411 /* same values? */
412 if (v1 == v2)
413 r = 0;
414 else
415 /* is any value NULL? */
416 if (v1 == NULL)
417 r = -1;
418 else
419 if (v2 == NULL)
420 r = 1;
421 else
422 /* different values, but same content? (unlikely) */
423 if (v1->data == v2->data)
424 r = 0;
425 else
426 if (MPDM_IS_STRING(v1) && MPDM_IS_STRING(v2))
427 r = wcscoll((wchar_t *) v1->data, (wchar_t *) v2->data);
428 else
429 if (MPDM_IS_ARRAY(v1) && MPDM_IS_ARRAY(v2)) {
430 /* compare first the sizes */
431 if ((r = mpdm_size(v1) - mpdm_size(v2)) == 0) {
432 int n;
434 /* they have the same size;
435 compare each pair of elements */
436 for (n = 0; n < mpdm_size(v1); n++) {
437 if ((r = mpdm_cmp(mpdm_aget(v1, n),
438 mpdm_aget(v2, n))) != 0)
439 break;
443 else
444 /* in any other case, compare just pointers */
445 r = (int) ((char *) v1->data - (char *) v2->data);
447 mpdm_unref(v2);
448 mpdm_unref(v1);
450 return r;
455 * mpdm_cmp_s - Compares two values (string version).
456 * @v1: the first value
457 * @v2: the second value
459 * Compares two values. Compares both values using wcscoll()
460 * if the first one is a string, or returns 1 otherwise.
462 int mpdm_cmp_s(const mpdm_t v1, const wchar_t * v2)
464 int r;
466 mpdm_ref(v1);
468 if (MPDM_IS_STRING(v1))
469 r = wcscoll((wchar_t *) v1->data, v2);
470 else
471 r = (int) ((wchar_t *) v1->data - v2);
473 mpdm_unref(v1);
475 return r;
480 * mpdm_splice - Creates a new string value from another.
481 * @v: the original value
482 * @i: the value to be inserted
483 * @offset: offset where the substring is to be inserted
484 * @del: number of characters to delete
486 * Creates a new string value from @v, deleting @del chars at @offset
487 * and substituting them by @i. If @del is 0, no deletion is done.
488 * both @offset and @del can be negative; if this is the case, it's
489 * assumed as counting from the end of @v. If @v is NULL, @i will become
490 * the new string, and both @offset and @del will be ignored. If @v is
491 * not NULL and @i is, no insertion process is done (only deletion, if
492 * applicable).
494 * Returns a two element array, with the new string in the first
495 * element and the deleted string in the second (with a NULL value
496 * if @del is 0).
497 * [Strings]
499 mpdm_t mpdm_splice(const mpdm_t v, const mpdm_t i, int offset, int del)
501 mpdm_t w;
502 mpdm_t n = NULL;
503 mpdm_t d = NULL;
504 int os, ns, r;
505 int ins = 0;
506 wchar_t *ptr;
508 mpdm_ref(v);
509 mpdm_ref(i);
511 if (v != NULL) {
512 os = mpdm_size(v);
514 /* negative offsets start from the end */
515 if (offset < 0)
516 offset = os + 1 - offset;
518 /* never add further the end */
519 if (offset > os)
520 offset = os;
522 /* negative del counts as 'characters left' */
523 if (del < 0)
524 del = os + 1 - offset + del;
526 /* something to delete? */
527 if (del > 0) {
528 /* never delete further the end */
529 if (offset + del > os)
530 del = os - offset;
532 /* deleted string */
533 d = MPDM_NS(((wchar_t *) v->data) + offset, del);
535 else
536 del = 0;
538 /* something to insert? */
539 ins = mpdm_size(i);
541 /* new size and remainder */
542 ns = os + ins - del;
543 r = offset + del;
545 n = MPDM_NS(NULL, ns);
547 ptr = (wchar_t *) n->data;
549 /* copy the beginning */
550 if (offset > 0) {
551 wcsncpy(ptr, v->data, offset);
552 ptr += offset;
555 /* copy the text to be inserted */
556 if (ins > 0) {
557 wcsncpy(ptr, i->data, ins);
558 ptr += ins;
561 /* copy the remaining */
562 os -= r;
563 if (os > 0) {
564 wcsncpy(ptr, ((wchar_t *) v->data) + r, os);
565 ptr += os;
568 /* null terminate */
569 *ptr = L'\0';
571 else
572 n = i;
574 /* creates the output array */
575 w = MPDM_A(2);
577 mpdm_ref(w);
578 mpdm_aset(w, n, 0);
579 mpdm_aset(w, d, 1);
580 mpdm_unrefnd(w);
582 mpdm_unref(i);
583 mpdm_unref(v);
585 return w;
590 * mpdm_strcat_sn - Concatenates two strings (string with size version).
591 * @s1: the first string
592 * @s2: the second string
593 * @size: the size of the second string
595 * Returns a new string formed by the concatenation of @s1 and @s2.
596 * [Strings]
598 mpdm_t mpdm_strcat_sn(const mpdm_t s1, const wchar_t * s2, int size)
600 wchar_t *ptr = NULL;
601 int s = 0;
602 mpdm_t r;
604 if (s1 == NULL && s2 == NULL)
605 r = NULL;
606 else {
607 ptr = mpdm_pokev(ptr, &s, s1);
608 ptr = mpdm_pokewsn(ptr, &s, s2, size);
610 ptr = mpdm_poke(ptr, &s, L"", 1, sizeof(wchar_t));
611 r = MPDM_ENS(ptr, s - 1);
614 return r;
619 * mpdm_strcat_s - Concatenates two strings (string version).
620 * @s1: the first string
621 * @s2: the second string
623 * Returns a new string formed by the concatenation of @s1 and @s2.
624 * [Strings]
626 mpdm_t mpdm_strcat_s(const mpdm_t s1, const wchar_t * s2)
628 return mpdm_strcat_sn(s1, s2, s2 ? wcslen(s2) : 0);
633 * mpdm_strcat - Concatenates two strings.
634 * @s1: the first string
635 * @s2: the second string
637 * Returns a new string formed by the concatenation of @s1 and @s2.
638 * [Strings]
640 mpdm_t mpdm_strcat(const mpdm_t s1, const mpdm_t s2)
642 mpdm_t r;
644 mpdm_ref(s2);
645 r = mpdm_strcat_s(s1, s2 ? mpdm_string(s2) : NULL);
646 mpdm_unref(s2);
648 return r;
653 * mpdm_ival - Returns a value's data as an integer.
654 * @v: the value
656 * Returns a value's data as an integer. If the value is a string,
657 * it's converted via sscanf and returned; non-string values have all
658 * an ival of 0. The converted integer is cached, so costly string
659 * conversions are only done once. Values created with the MPDM_IVAL
660 * flag set have its ival cached from the beginning.
661 * [Strings]
662 * [Value Management]
664 int mpdm_ival(mpdm_t v)
666 int i = 0;
668 mpdm_ref(v);
670 if (v != NULL) {
671 /* if there is no cached integer, calculate it */
672 if (!(v->flags & MPDM_IVAL)) {
673 /* if it's a string, calculate it; other
674 values will have an ival of 0 */
675 if (v->flags & MPDM_STRING) {
676 char tmp[32];
677 char *fmt = "%i";
679 wcstombs(tmp, (wchar_t *) v->data, sizeof(tmp));
680 tmp[sizeof(tmp) - 1] = '\0';
682 /* workaround for mingw32: as it doesn't
683 correctly parse octal and hexadecimal
684 numbers, they are tried as special cases */
685 if (tmp[0] == '0') {
686 if (tmp[1] == 'b' || tmp[1] == 'B') {
687 /* binary number */
688 fmt = NULL;
689 char *ptr = &tmp[2];
691 while (*ptr == '0' || *ptr == '1') {
692 i <<= 1;
694 if (*ptr == '1')
695 i |= 1;
697 ptr++;
700 else
701 if (tmp[1] == 'x' || tmp[1] == 'X')
702 fmt = "%x";
703 else
704 fmt = "%o";
707 if (fmt != NULL)
708 sscanf(tmp, fmt, &i);
711 mpdm_set_ival(v, i);
714 i = v->ival;
717 mpdm_unref(v);
719 return i;
724 * mpdm_rval - Returns a value's data as a real number (double).
725 * @v: the value
727 * Returns a value's data as a real number (double float). If the value
728 * is a string, it's converted via sscanf and returned; non-string values
729 * have all an rval of 0. The converted double is cached, so costly string
730 * conversions are only done once. Values created with the MPDM_RVAL
731 * flag set have its rval cached from the beginning.
732 * [Strings]
733 * [Value Management]
735 double mpdm_rval(mpdm_t v)
737 double r = 0.0;
739 mpdm_ref(v);
741 if (v != NULL) {
742 /* if there is no cached double, calculate it */
743 if (!(v->flags & MPDM_RVAL)) {
744 /* if it's a string, calculate it; other
745 values will have an rval of 0.0 */
746 if (v->flags & MPDM_STRING) {
747 char tmp[128];
748 char *prev_locale;
750 wcstombs(tmp, (wchar_t *) v->data, sizeof(tmp));
751 tmp[sizeof(tmp) - 1] = '\0';
753 /* if the number starts with 0, it's
754 an octal or hexadecimal number; just
755 take the integer value and cast it */
756 if (tmp[0] == '0' && tmp[1] != '.')
757 r = (double) mpdm_ival(v);
758 else {
759 /* set locale to C for non locale-dependent
760 floating point conversion */
761 prev_locale = setlocale(LC_NUMERIC, "C");
763 /* read */
764 sscanf(tmp, "%lf", &r);
766 /* set previous locale */
767 setlocale(LC_NUMERIC, prev_locale);
771 mpdm_set_rval(v, r);
774 r = v->rval;
777 mpdm_unref(v);
779 return r;
784 * mpdm_gettext - Translates a string to the current language.
785 * @str: the string
787 * Translates the @str string to the current language.
789 * This function can still be used even if there is no real gettext
790 * support() by manually filling the __I18N__ hash.
792 * If the string is found in the current table, the translation is
793 * returned; otherwise, the same @str value is returned.
794 * [Strings]
795 * [Localization]
797 mpdm_t mpdm_gettext(const mpdm_t str)
799 mpdm_t v;
800 mpdm_t i18n = NULL;
802 /* gets the cache */
803 if ((i18n = mpdm_hget_s(mpdm_root(), L"__I18N__")) == NULL)
804 i18n = mpdm_hset_s(mpdm_root(), L"__I18N__", MPDM_H(0));
806 mpdm_ref(str);
808 /* try first the cache */
809 if ((v = mpdm_hget(i18n, str)) == NULL) {
810 #ifdef CONFOPT_GETTEXT
811 char *s;
812 mpdm_t t;
814 /* convert to mbs */
815 t = mpdm_ref(MPDM_2MBS(str->data));
817 /* ask gettext for it */
818 s = gettext((char *) t->data);
820 if (s != t->data)
821 v = MPDM_MBS(s);
822 else
823 v = str;
825 mpdm_unref(t);
827 #else /* CONFOPT_GETTEXT */
829 v = str;
831 #endif /* CONFOPT_GETTEXT */
833 /* store in the cache */
834 mpdm_hset(i18n, str, v);
837 mpdm_unref(str);
839 return v;
844 * mpdm_gettext_domain - Sets domain and data directory for translations.
845 * @dom: the domain (application name)
846 * @data: directory contaning the .mo files
848 * Sets the domain (application name) and translation data for translating
849 * strings that will be returned by mpdm_gettext().@data must point to a
850 * directory containing the .mo (compiled .po) files.
852 * If there is no gettext support, returns 0, or 1 otherwise.
853 * [Strings]
854 * [Localization]
856 int mpdm_gettext_domain(const mpdm_t dom, const mpdm_t data)
858 int ret = 0;
860 mpdm_ref(dom);
861 mpdm_ref(data);
863 #ifdef CONFOPT_GETTEXT
865 mpdm_t dm;
866 mpdm_t dt;
868 /* convert both to mbs,s */
869 dm = mpdm_ref(MPDM_2MBS(dom->data));
870 dt = mpdm_ref(MPDM_2MBS(data->data));
872 /* bind and set domain */
873 bindtextdomain((char *) dm->data, (char *) dt->data);
874 textdomain((char *) dm->data);
876 mpdm_hset_s(mpdm_root(), L"__I18N__", MPDM_H(0));
878 mpdm_unref(dt);
879 mpdm_unref(dm);
881 ret = 1;
883 #endif /* CONFOPT_GETTEXT */
885 #ifdef CONFOPT_WIN32
887 mpdm_t v;
889 if ((v = mpdm_hget_s(mpdm_root(), L"ENV")) != NULL &&
890 mpdm_hget_s(v, L"LANG") == NULL) {
891 wchar_t *wptr = L"en";
893 /* MS Windows crappy language constants... */
895 switch ((GetSystemDefaultLangID() & 0x00ff)) {
896 case 0x01:
897 wptr = L"ar";
898 break; /* arabic */
899 case 0x02:
900 wptr = L"bg";
901 break; /* bulgarian */
902 case 0x03:
903 wptr = L"ca";
904 break; /* catalan */
905 case 0x04:
906 wptr = L"zh";
907 break; /* chinese */
908 case 0x05:
909 wptr = L"cz";
910 break; /* czech */
911 case 0x06:
912 wptr = L"da";
913 break; /* danish */
914 case 0x07:
915 wptr = L"de";
916 break; /* german */
917 case 0x08:
918 wptr = L"el";
919 break; /* greek */
920 case 0x09:
921 wptr = L"en";
922 break; /* english */
923 case 0x0a:
924 wptr = L"es";
925 break; /* spanish */
926 case 0x0b:
927 wptr = L"fi";
928 break; /* finnish */
929 case 0x0c:
930 wptr = L"fr";
931 break; /* french */
932 case 0x0d:
933 wptr = L"he";
934 break; /* hebrew */
935 case 0x0e:
936 wptr = L"hu";
937 break; /* hungarian */
938 case 0x0f:
939 wptr = L"is";
940 break; /* icelandic */
941 case 0x10:
942 wptr = L"it";
943 break; /* italian */
944 case 0x11:
945 wptr = L"jp";
946 break; /* japanese */
947 case 0x12:
948 wptr = L"ko";
949 break; /* korean */
950 case 0x13:
951 wptr = L"nl";
952 break; /* dutch */
953 case 0x14:
954 wptr = L"no";
955 break; /* norwegian */
956 case 0x15:
957 wptr = L"po";
958 break; /* polish */
959 case 0x16:
960 wptr = L"pt";
961 break; /* portuguese */
962 case 0x17:
963 wptr = L"rm";
964 break; /* romansh (switzerland) */
965 case 0x18:
966 wptr = L"ro";
967 break; /* romanian */
968 case 0x19:
969 wptr = L"ru";
970 break; /* russian */
971 case 0x1a:
972 wptr = L"sr";
973 break; /* serbian */
974 case 0x1b:
975 wptr = L"sk";
976 break; /* slovak */
977 case 0x1c:
978 wptr = L"sq";
979 break; /* albanian */
980 case 0x1d:
981 wptr = L"sv";
982 break; /* swedish */
985 mpdm_hset_s(v, L"LANG", MPDM_S(wptr));
988 #endif /* CONFOPT_WIN32 */
990 mpdm_unref(data);
991 mpdm_unref(dom);
993 return ret;
997 #ifdef CONFOPT_WCWIDTH
999 int wcwidth(wchar_t);
1001 int mpdm_wcwidth(wchar_t c)
1003 return wcwidth(c);
1006 #else /* CONFOPT_WCWIDTH */
1008 #include "wcwidth.c"
1010 int mpdm_wcwidth(wchar_t c)
1012 return mk_wcwidth(c);
1015 #endif /* CONFOPT_WCWIDTH */
1019 * mpdm_sprintf - Formats a sprintf()-like string.
1020 * @fmt: the string format
1021 * @args: an array of values
1023 * Formats a string using the sprintf() format taking the values from @args.
1024 * [Strings]
1026 mpdm_t mpdm_sprintf(const mpdm_t fmt, const mpdm_t args)
1028 const wchar_t *i = fmt->data;
1029 wchar_t *o = NULL;
1030 int l = 0, n = 0;
1031 wchar_t c;
1033 mpdm_ref(fmt);
1034 mpdm_ref(args);
1036 /* loop all characters */
1037 while ((c = *i++) != L'\0') {
1038 int m = 0;
1039 wchar_t *tptr = NULL;
1040 wchar_t *wptr = NULL;
1042 if (c == L'%') {
1043 /* format directive */
1044 char t_fmt[128];
1045 char tmp[1024];
1046 mpdm_t v;
1047 char *ptr = NULL;
1049 /* transfer the % */
1050 t_fmt[m++] = '%';
1052 /* transform the format to mbs */
1053 while (*i != L'\0' &&
1054 m < (int) (sizeof(t_fmt) - MB_CUR_MAX - 1) &&
1055 wcschr(L"-.0123456789", *i) != NULL)
1056 m += wctomb(&t_fmt[m], *i++);
1058 /* transfer the directive */
1059 m += wctomb(&t_fmt[m], *i++);
1061 t_fmt[m] = '\0';
1063 /* by default, copies the format */
1064 strcpy(tmp, t_fmt);
1066 /* pick next value */
1067 v = mpdm_aget(args, n++);
1069 switch (t_fmt[m - 1]) {
1070 case 'd':
1071 case 'i':
1072 case 'u':
1073 case 'x':
1074 case 'X':
1075 case 'o':
1077 /* integer value */
1078 snprintf(tmp, sizeof(tmp) - 1, t_fmt, mpdm_ival(v));
1079 break;
1081 case 'f':
1083 /* float (real) value */
1084 snprintf(tmp, sizeof(tmp) - 1, t_fmt, mpdm_rval(v));
1085 break;
1087 case 's':
1089 /* string value */
1090 ptr = mpdm_wcstombs(mpdm_string(v), NULL);
1091 snprintf(tmp, sizeof(tmp) - 1, t_fmt, ptr);
1092 free(ptr);
1094 break;
1096 case 'c':
1098 /* char */
1099 m = 1;
1100 wptr = &c;
1101 c = mpdm_ival(v);
1102 break;
1104 case 'b':
1106 ptr = tmp;
1107 unsigned int mask;
1108 int p = 0;
1110 mask = 1 << ((sizeof(int) * 8) - 1);
1111 while (mask) {
1112 if (mask & (unsigned int) mpdm_ival(v)) {
1113 *ptr++ = '1';
1114 p = 1;
1116 else
1117 if (p)
1118 *ptr++ = '0';
1120 mask >>= 1;
1123 if (ptr == tmp)
1124 *ptr++ = '0';
1126 *ptr = '\0';
1127 break;
1129 case '%':
1131 /* percent sign */
1132 m = 1;
1133 wptr = &c;
1134 break;
1137 /* transfer */
1138 if (wptr == NULL)
1139 wptr = tptr = mpdm_mbstowcs(tmp, &m, -1);
1141 else {
1142 /* raw character */
1143 m = 1;
1144 wptr = &c;
1147 /* transfer */
1148 o = mpdm_poke(o, &l, wptr, m, sizeof(wchar_t));
1150 /* free the temporary buffer, if any */
1151 if (tptr != NULL)
1152 free(tptr);
1155 if (o == NULL)
1156 return NULL;
1158 /* null-terminate */
1159 o = mpdm_poke(o, &l, L"", 1, sizeof(wchar_t));
1161 mpdm_unref(args);
1162 mpdm_unref(fmt);
1164 return MPDM_ENS(o, l - 1);
1169 * mpdm_ulc - Converts a string to uppercase or lowecase.
1170 * @s: the string
1171 * @u: convert to uppercase (1) or to lowercase (0).
1173 * Converts @s to uppercase (for @u == 1) or to lowercase (@u == 0).
1174 * [Strings]
1176 mpdm_t mpdm_ulc(const mpdm_t s, int u)
1178 mpdm_t r = NULL;
1179 wchar_t *optr;
1180 int i;
1182 mpdm_ref(s);
1184 i = mpdm_size(s);
1186 if ((optr = malloc((i + 1) * sizeof(wchar_t))) != NULL) {
1187 wchar_t *iptr = mpdm_string(s);
1188 int n;
1190 for (n = 0; n < i; n++)
1191 optr[n] = u ? towupper(iptr[n]) : towlower(iptr[n]);
1193 optr[n] = L'\0';
1194 r = MPDM_ENS(optr, i);
1197 mpdm_unref(s);
1199 return r;
1203 /* scanf working buffers */
1204 #define SCANF_BUF_SIZE 1024
1205 static wchar_t scanf_yset[SCANF_BUF_SIZE];
1206 static wchar_t scanf_nset[SCANF_BUF_SIZE];
1207 static wchar_t scanf_mark[SCANF_BUF_SIZE];
1209 struct {
1210 wchar_t cmd;
1211 wchar_t *yset;
1212 wchar_t *nset;
1213 } scanf_sets[] = {
1214 { L's', L"", L" \t"},
1215 { L'u', L"0123456789", L""},
1216 { L'd', L"-0123456789", L""},
1217 { L'i', L"-0123456789", L""},
1218 { L'f', L"-0123456789.", L""},
1219 { L'x', L"-0123456789xabcdefABCDEF", L""},
1220 { L'\0', NULL, NULL},
1224 * mpdm_sscanf - Extracts data like sscanf().
1225 * @fmt: the string format
1226 * @str: the string to be parsed
1227 * @offset: the character offset to start scanning
1229 * Extracts data from a string using a special format pattern, very
1230 * much like the scanf() series of functions in the C library. Apart
1231 * from the standard percent-sign-commands (s, u, d, i, f, x,
1232 * n, [, with optional size and * to ignore), it implements S,
1233 * to match a string of characters upto what follows in the format
1234 * string. Also, the [ set of characters can include other % formats.
1236 * Returns an array with the extracted values. If %n is used, the
1237 * position in the scanned string is returned as the value.
1238 * [Strings]
1240 mpdm_t mpdm_sscanf(const mpdm_t fmt, const mpdm_t str, int offset)
1242 wchar_t *i = (wchar_t *) str->data;
1243 wchar_t *f = (wchar_t *) fmt->data;
1244 mpdm_t r;
1246 mpdm_ref(fmt);
1247 mpdm_ref(str);
1249 i += offset;
1250 r = MPDM_A(0);
1251 mpdm_ref(r);
1253 while (*f) {
1254 if (*f == L'%') {
1255 wchar_t *ptr = NULL;
1256 int size = 0;
1257 wchar_t cmd;
1258 int vsize = 0;
1259 int ignore = 0;
1260 int msize = 0;
1262 /* empty all buffers */
1263 scanf_yset[0] = scanf_nset[0] = scanf_mark[0] = L'\0';
1265 f++;
1267 /* an asterisk? don't return next value */
1268 if (*f == L'*') {
1269 ignore = 1;
1270 f++;
1273 /* does it have a size? */
1274 while (wcschr(L"0123456789", *f)) {
1275 vsize *= 10;
1276 vsize += *f - L'0';
1277 f++;
1280 /* if no size, set it to an arbitrary big limit */
1281 if (!vsize)
1282 vsize = 0xfffffff;
1284 /* now *f should contain a command */
1285 cmd = *f;
1286 f++;
1288 /* is it a verbatim percent sign? */
1289 if (cmd == L'%') {
1290 vsize = 1;
1291 ignore = 1;
1292 wcscpy(scanf_yset, L"%");
1294 else
1295 /* a position? */
1296 if (cmd == L'n') {
1297 vsize = 0;
1298 ignore = 1;
1299 mpdm_push(r, MPDM_I(i - (wchar_t *) str->data));
1301 else
1302 /* string upto a mark */
1303 if (cmd == L'S') {
1304 wchar_t *tmp = f;
1306 /* fill the mark upto another command */
1307 while (*tmp) {
1308 if (*tmp == L'%') {
1309 tmp++;
1311 /* is it an 'n'? ignore and go on */
1312 if (*tmp == L'n') {
1313 tmp++;
1314 continue;
1316 else
1317 if (*tmp == L'%')
1318 scanf_mark[msize++] = *tmp;
1319 else
1320 break;
1322 else
1323 scanf_mark[msize++] = *tmp;
1325 tmp++;
1328 scanf_mark[msize] = L'\0';
1330 else
1331 /* raw set */
1332 if (cmd == L'[') {
1333 int n = 0;
1334 wchar_t *set = scanf_yset;
1336 /* is it an inverse set? */
1337 if (*f == L'^') {
1338 set = scanf_nset;
1339 f++;
1342 /* first one is a ]? add it */
1343 if (*f == L']') {
1344 set[n++] = *f;
1345 f++;
1348 /* now build the set */
1349 for (; n < SCANF_BUF_SIZE - 1 && *f && *f != L']'; f++) {
1350 /* is it a range? */
1351 if (*f == L'-') {
1352 f++;
1354 /* start or end? hyphen itself */
1355 if (n == 0 || *f == L']')
1356 set[n++] = L'-';
1357 else {
1358 /* pick previous char */
1359 wchar_t c = set[n - 1];
1361 /* fill */
1362 while (n < SCANF_BUF_SIZE - 1 && c < *f)
1363 set[n++] = ++c;
1366 else
1367 /* is it another command? */
1368 if (*f == L'%') {
1369 int i;
1371 f++;
1372 for (i = 0; scanf_sets[i].cmd; i++) {
1373 if (*f == scanf_sets[i].cmd) {
1374 set[n] = L'\0';
1375 wcscat(set, scanf_sets[i].yset);
1376 n += wcslen(scanf_sets[i].yset);
1377 break;
1381 else
1382 set[n++] = *f;
1385 /* skip the ] */
1386 f++;
1388 set[n] = L'\0';
1390 else
1391 /* a standard set? */
1393 int n;
1395 for (n = 0; scanf_sets[n].cmd != L'\0'; n++) {
1396 if (cmd == scanf_sets[n].cmd) {
1397 wcscpy(scanf_yset, scanf_sets[n].yset);
1398 wcscpy(scanf_nset, scanf_sets[n].nset);
1399 break;
1404 /* now fill the dynamic string */
1405 while (vsize &&
1406 !wcschr(scanf_nset, *i) &&
1407 (scanf_yset[0] == L'\0' || wcschr(scanf_yset, *i)) &&
1408 (msize == 0 || wcsncmp(i, scanf_mark, msize) != 0)) {
1410 /* only add if not being ignored */
1411 if (!ignore)
1412 ptr = mpdm_poke(ptr, &size, i, 1, sizeof(wchar_t));
1414 i++;
1415 vsize--;
1418 if (!ignore && size) {
1419 /* null terminate and push */
1420 ptr = mpdm_poke(ptr, &size, L"", 1, sizeof(wchar_t));
1421 mpdm_push(r, MPDM_ENS(ptr, size - 1));
1424 else
1425 if (*f == L' ' || *f == L'\t') {
1426 /* if it's a blank, sync to next non-blank */
1427 f++;
1429 while (*i == L' ' || *i == L'\t')
1430 i++;
1432 else
1433 /* test for literals in the format string */
1434 if (*i == *f) {
1435 i++;
1436 f++;
1438 else
1439 break;
1442 mpdm_unref(str);
1443 mpdm_unref(fmt);
1445 mpdm_unrefnd(r);
1447 return r;