Use mpdm_string() instead of v->data in regex1().
[mpdm.git] / mpdm_s.c
blob9dd3e8c06a801abf842d77ec349c5f4860c8a40b
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 <malloc.h>
33 #include <locale.h>
34 #include <wctype.h>
36 #ifdef CONFOPT_GETTEXT
37 #include <libintl.h>
38 #endif
40 #ifdef CONFOPT_WIN32
41 #include <windows.h>
42 #endif
44 #include "mpdm.h"
47 /** code **/
49 void *mpdm_poke_o(void *dst, int *dsize, int *offset, const void *org,
50 int osize, int esize)
52 if (org != NULL && osize) {
53 /* enough room? */
54 if (*offset + osize > *dsize) {
55 /* no; enlarge */
56 *dsize += osize;
58 dst = realloc(dst, *dsize * esize);
61 memcpy((char *) dst + (*offset * esize), org, osize * esize);
62 *offset += osize;
65 return dst;
69 void *mpdm_poke(void *dst, int *dsize, const void *org, int osize,
70 int esize)
71 /* pokes (adds) org into dst, which is a dynamic string, making it grow */
73 int offset = *dsize;
75 return mpdm_poke_o(dst, dsize, &offset, org, osize, esize);
79 wchar_t *mpdm_pokewsn(wchar_t * dst, int *dsize, const wchar_t * str,
80 int slen)
81 /* adds a wide string to dst using mpdm_poke() with size */
83 if (str)
84 dst = mpdm_poke(dst, dsize, str, slen, sizeof(wchar_t));
86 return dst;
90 wchar_t *mpdm_pokews(wchar_t * dst, int *dsize, const wchar_t * str)
91 /* adds a wide string to dst using mpdm_poke() */
93 if (str)
94 dst = mpdm_pokewsn(dst, dsize, str, wcslen(str));
96 return dst;
100 wchar_t *mpdm_pokev(wchar_t * dst, int *dsize, const mpdm_t v)
101 /* adds the string in v to dst using mpdm_poke() */
103 if (v != NULL) {
104 const wchar_t *ptr = mpdm_string(v);
106 mpdm_ref(v);
107 dst = mpdm_pokews(dst, dsize, ptr);
108 mpdm_unref(v);
111 return dst;
115 wchar_t *mpdm_mbstowcs(const char *str, int *s, int l)
116 /* converts an mbs to a wcs, but filling invalid chars
117 with question marks instead of just failing */
119 wchar_t *ptr = NULL;
120 char tmp[64]; /* really MB_CUR_MAX + 1 */
121 wchar_t wc;
122 int n, i, c, t = 0;
123 char *cstr;
125 /* allow NULL values for s */
126 if (s == NULL)
127 s = &t;
129 /* if there is a limit, duplicate and break the string */
130 if (l >= 0) {
131 cstr = strdup(str);
132 cstr[l] = '\0';
134 else
135 cstr = (char *) str;
137 /* try first a direct conversion with mbstowcs */
138 if ((*s = mbstowcs(NULL, cstr, 0)) != -1) {
139 /* direct conversion is possible; do it */
140 if ((ptr = malloc((*s + 1) * sizeof(wchar_t))) != NULL) {
141 mbstowcs(ptr, cstr, *s);
142 ptr[*s] = L'\0';
145 else {
146 /* zero everything */
147 *s = n = i = 0;
149 for (;;) {
150 /* no more characters to process? */
151 if ((c = cstr[n + i]) == '\0' && i == 0)
152 break;
154 tmp[i++] = c;
155 tmp[i] = '\0';
157 /* try to convert */
158 if (mbstowcs(&wc, tmp, 1) == (size_t) - 1) {
159 /* can still be an incomplete multibyte char? */
160 if (c != '\0' && i <= (int) MB_CUR_MAX)
161 continue;
162 else {
163 /* too many failing bytes; skip 1 byte */
164 wc = L'?';
165 i = 1;
169 /* skip used bytes and back again */
170 n += i;
171 i = 0;
173 /* store new char */
174 if ((ptr = mpdm_poke(ptr, s, &wc, 1, sizeof(wchar_t))) == NULL)
175 break;
178 /* null terminate and count one less */
179 if (ptr != NULL) {
180 ptr = mpdm_poke(ptr, s, L"", 1, sizeof(wchar_t));
181 (*s)--;
185 /* free the duplicate */
186 if (cstr != str)
187 free(cstr);
189 return ptr;
193 char *mpdm_wcstombs(const wchar_t * str, int *s)
194 /* converts a wcs to an mbs, but filling invalid chars
195 with question marks instead of just failing */
197 char *ptr = NULL;
198 char tmp[64]; /* really MB_CUR_MAX + 1 */
199 int l, t = 0;
201 /* allow NULL values for s */
202 if (s == NULL)
203 s = &t;
205 /* try first a direct conversion with wcstombs */
206 if ((*s = wcstombs(NULL, str, 0)) != -1) {
207 /* direct conversion is possible; do it and return */
208 if ((ptr = malloc(*s + 1)) != NULL) {
209 wcstombs(ptr, str, *s);
210 ptr[*s] = '\0';
213 return ptr;
216 /* invalid encoding? convert characters one by one */
217 *s = 0;
219 while (*str) {
220 if ((l = wctomb(tmp, *str)) <= 0) {
221 /* if char couldn't be converted,
222 write a question mark instead */
223 l = wctomb(tmp, L'?');
226 tmp[l] = '\0';
227 if ((ptr = mpdm_poke(ptr, s, tmp, l, 1)) == NULL)
228 break;
230 str++;
233 /* null terminate and count one less */
234 if (ptr != NULL) {
235 ptr = mpdm_poke(ptr, s, "", 1, 1);
236 (*s)--;
239 return ptr;
243 mpdm_t mpdm_new_wcs(int flags, const wchar_t * str, int size, int cpy)
244 /* creates a new string value from a wcs */
246 wchar_t *ptr;
248 /* a size of -1 means 'calculate it' */
249 if (size == -1 && str != NULL)
250 size = wcslen(str);
252 /* create a copy? */
253 if (cpy) {
254 /* free() on destruction */
255 flags |= MPDM_FREE;
257 /* allocs */
258 if ((ptr = malloc((size + 1) * sizeof(wchar_t))) == NULL)
259 return NULL;
261 /* if no source, reset to zeroes; otherwise, copy */
262 if (str == NULL)
263 memset(ptr, '\0', size * sizeof(wchar_t));
264 else {
265 wcsncpy(ptr, str, size);
266 ptr[size] = L'\0';
269 else
270 ptr = (wchar_t *) str;
272 /* it's a string */
273 flags |= MPDM_STRING;
275 return mpdm_new(flags, ptr, size);
279 mpdm_t mpdm_new_mbstowcs(int flags, const char *str, int l)
280 /* creates a new string value from an mbs */
282 wchar_t *ptr;
283 int size;
285 if ((ptr = mpdm_mbstowcs(str, &size, l)) == NULL)
286 return NULL;
288 /* it's a string */
289 flags |= (MPDM_STRING | MPDM_FREE);
291 return mpdm_new(flags, ptr, size);
295 mpdm_t mpdm_new_wcstombs(int flags, const wchar_t * str)
296 /* creates a new mbs value from a wbs */
298 char *ptr;
299 int size;
301 ptr = mpdm_wcstombs(str, &size);
303 flags |= MPDM_FREE;
305 /* unset the string flag; mbs,s are not 'strings' */
306 flags &= ~MPDM_STRING;
308 return mpdm_new(flags, ptr, size);
312 mpdm_t mpdm_new_i(int ival)
313 /* creates a new string value from an integer */
315 mpdm_t v;
316 char tmp[32];
318 /* creates the visual representation */
319 snprintf(tmp, sizeof(tmp), "%d", ival);
321 v = MPDM_MBS(tmp);
323 return mpdm_set_ival(v, ival);
327 mpdm_t mpdm_new_r(double rval)
328 /* creates a new string value from a real number */
330 mpdm_t v;
331 char tmp[128];
333 /* creates the visual representation */
334 snprintf(tmp, sizeof(tmp), "%lf", rval);
336 /* manually strip useless zeroes */
337 if (strchr(tmp, '.') != NULL) {
338 char *ptr;
340 for (ptr = tmp + strlen(tmp) - 1; *ptr == '0'; ptr--);
342 /* if it's over the ., strip it also */
343 if (*ptr != '.')
344 ptr++;
346 *ptr = '\0';
349 v = MPDM_MBS(tmp);
351 return mpdm_set_rval(v, rval);
355 /* interface */
358 * mpdm_string2 - Returns a printable representation of a value (with buffer).
359 * @v: the value
360 * @wtmp: the external buffer
362 * Returns a printable representation of a value. For strings, it's
363 * the value data itself; for any other type, a conversion to string
364 * is returned instead. If @v is not a string, the @wtmp buffer
365 * can be used as a placeholder for the string representation.
367 * The reference count value in @v is not touched.
368 * [Strings]
370 wchar_t *mpdm_string2(const mpdm_t v, wchar_t *wtmp)
372 char tmp[32];
373 wchar_t *ret;
375 /* if it's NULL, return a constant */
376 if (v == NULL)
377 ret = L"[NULL]";
378 else
379 /* if it's a string, return it */
380 if (v->flags & MPDM_STRING)
381 ret = (wchar_t *) v->data;
382 else {
383 /* otherwise, return a visual representation */
384 snprintf(tmp, sizeof(tmp), "%p", v);
385 mbstowcs(wtmp, tmp, sizeof(tmp) * sizeof(wchar_t));
387 ret = wtmp;
390 return ret;
395 * mpdm_string - Returns a printable representation of a value.
396 * @v: the value
398 * Returns a printable representation of a value. For strings, it's
399 * the value data itself; for any other type, a conversion to string
400 * is returned instead. This value should be used immediately, as it
401 * can be a pointer to a static buffer.
403 * The reference count value in @v is not touched.
404 * [Strings]
406 wchar_t *mpdm_string(const mpdm_t v)
408 static wchar_t tmp[32];
410 return mpdm_string2(v, tmp);
415 * mpdm_cmp - Compares two values.
416 * @v1: the first value
417 * @v2: the second value
419 * Compares two values. If both has the MPDM_STRING flag set,
420 * a comparison using wcscoll() is returned; if both are arrays,
421 * the size is compared first and, if they have the same number
422 * elements, each one is compared; otherwise, a simple visual
423 * representation comparison is done.
424 * [Strings]
426 int mpdm_cmp(const mpdm_t v1, const mpdm_t v2)
428 int r;
430 mpdm_ref(v1);
431 mpdm_ref(v2);
433 /* same values? */
434 if (v1 == v2)
435 r = 0;
436 else
437 /* is any value NULL? */
438 if (v1 == NULL)
439 r = -1;
440 else
441 if (v2 == NULL)
442 r = 1;
443 else
444 /* different values, but same content? (unlikely) */
445 if (v1->data == v2->data)
446 r = 0;
447 else
448 if (MPDM_IS_STRING(v1) && MPDM_IS_STRING(v2))
449 r = wcscoll((wchar_t *) v1->data, (wchar_t *) v2->data);
450 else
451 if (MPDM_IS_ARRAY(v1) && MPDM_IS_ARRAY(v2)) {
452 /* compare first the sizes */
453 if ((r = mpdm_size(v1) - mpdm_size(v2)) == 0) {
454 int n;
456 /* they have the same size;
457 compare each pair of elements */
458 for (n = 0; n < mpdm_size(v1); n++) {
459 if ((r = mpdm_cmp(mpdm_aget(v1, n),
460 mpdm_aget(v2, n))) != 0)
461 break;
465 else {
466 wchar_t tmp[32];
468 r = wcscoll(mpdm_string(v1), mpdm_string2(v2, tmp));
471 mpdm_unref(v2);
472 mpdm_unref(v1);
474 return r;
479 * mpdm_cmp_s - Compares two values (string version).
480 * @v1: the first value
481 * @v2: the second value
483 * Compares two values. If both has the MPDM_STRING flag set,
484 * a comparison using wcscoll() is returned; if both are arrays,
485 * the size is compared first and, if they have the same number
486 * elements, each one is compared; otherwise, a simple visual
487 * representation comparison is done.
489 int mpdm_cmp_s(const mpdm_t v1, const wchar_t * v2)
491 return mpdm_cmp(v1, MPDM_S(v2));
496 * mpdm_splice - Creates a new string value from another.
497 * @v: the original value
498 * @i: the value to be inserted
499 * @offset: offset where the substring is to be inserted
500 * @del: number of characters to delete
502 * Creates a new string value from @v, deleting @del chars at @offset
503 * and substituting them by @i. If @del is 0, no deletion is done.
504 * both @offset and @del can be negative; if this is the case, it's
505 * assumed as counting from the end of @v. If @v is NULL, @i will become
506 * the new string, and both @offset and @del will be ignored. If @v is
507 * not NULL and @i is, no insertion process is done (only deletion, if
508 * applicable).
510 * Returns a two element array, with the new string in the first
511 * element and the deleted string in the second (with a NULL value
512 * if @del is 0).
513 * [Strings]
515 mpdm_t mpdm_splice(const mpdm_t v, const mpdm_t i, int offset, int del)
517 mpdm_t w;
518 mpdm_t n = NULL;
519 mpdm_t d = NULL;
520 int os, ns, r;
521 int ins = 0;
522 wchar_t *ptr;
524 mpdm_ref(v);
525 mpdm_ref(i);
527 if (v != NULL) {
528 os = mpdm_size(v);
530 /* negative offsets start from the end */
531 if (offset < 0)
532 offset = os + 1 - offset;
534 /* never add further the end */
535 if (offset > os)
536 offset = os;
538 /* negative del counts as 'characters left' */
539 if (del < 0)
540 del = os + 1 - offset + del;
542 /* something to delete? */
543 if (del > 0) {
544 /* never delete further the end */
545 if (offset + del > os)
546 del = os - offset;
548 /* deleted string */
549 d = MPDM_NS(((wchar_t *) v->data) + offset, del);
551 else
552 del = 0;
554 /* something to insert? */
555 ins = mpdm_size(i);
557 /* new size and remainder */
558 ns = os + ins - del;
559 r = offset + del;
561 n = MPDM_NS(NULL, ns);
563 ptr = (wchar_t *) n->data;
565 /* copy the beginning */
566 if (offset > 0) {
567 wcsncpy(ptr, v->data, offset);
568 ptr += offset;
571 /* copy the text to be inserted */
572 if (ins > 0) {
573 wcsncpy(ptr, i->data, ins);
574 ptr += ins;
577 /* copy the remaining */
578 os -= r;
579 if (os > 0) {
580 wcsncpy(ptr, ((wchar_t *) v->data) + r, os);
581 ptr += os;
584 /* null terminate */
585 *ptr = L'\0';
587 else
588 n = i;
590 /* creates the output array */
591 w = MPDM_A(2);
593 mpdm_ref(w);
594 mpdm_aset(w, n, 0);
595 mpdm_aset(w, d, 1);
596 mpdm_unrefnd(w);
598 mpdm_unref(i);
599 mpdm_unref(v);
601 return w;
606 * mpdm_strcat_sn - Concatenates two strings (string with size version).
607 * @s1: the first string
608 * @s2: the second string
609 * @size: the size of the second string
611 * Returns a new string formed by the concatenation of @s1 and @s2.
612 * [Strings]
614 mpdm_t mpdm_strcat_sn(const mpdm_t s1, const wchar_t * s2, int size)
616 wchar_t *ptr = NULL;
617 int s = 0;
618 mpdm_t r;
620 if (s1 == NULL && s2 == NULL)
621 r = NULL;
622 else {
623 ptr = mpdm_pokev(ptr, &s, s1);
624 ptr = mpdm_pokewsn(ptr, &s, s2, size);
626 ptr = mpdm_poke(ptr, &s, L"", 1, sizeof(wchar_t));
627 r = MPDM_ENS(ptr, s - 1);
630 return r;
635 * mpdm_strcat_s - Concatenates two strings (string version).
636 * @s1: the first string
637 * @s2: the second string
639 * Returns a new string formed by the concatenation of @s1 and @s2.
640 * [Strings]
642 mpdm_t mpdm_strcat_s(const mpdm_t s1, const wchar_t * s2)
644 return mpdm_strcat_sn(s1, s2, s2 ? wcslen(s2) : 0);
649 * mpdm_strcat - Concatenates two strings.
650 * @s1: the first string
651 * @s2: the second string
653 * Returns a new string formed by the concatenation of @s1 and @s2.
654 * [Strings]
656 mpdm_t mpdm_strcat(const mpdm_t s1, const mpdm_t s2)
658 mpdm_t r;
660 mpdm_ref(s2);
661 r = mpdm_strcat_s(s1, s2 ? mpdm_string(s2) : NULL);
662 mpdm_unref(s2);
664 return r;
669 * mpdm_ival - Returns a value's data as an integer.
670 * @v: the value
672 * Returns a value's data as an integer. If the value is a string,
673 * it's converted via sscanf and returned; non-string values have all
674 * an ival of 0. The converted integer is cached, so costly string
675 * conversions are only done once. Values created with the MPDM_IVAL
676 * flag set have its ival cached from the beginning.
677 * [Strings]
678 * [Value Management]
680 int mpdm_ival(mpdm_t v)
682 int i = 0;
684 mpdm_ref(v);
686 if (v != NULL) {
687 /* if there is no cached integer, calculate it */
688 if (!(v->flags & MPDM_IVAL)) {
689 /* if it's a string, calculate it; other
690 values will have an ival of 0 */
691 if (v->flags & MPDM_STRING) {
692 char tmp[32];
693 char *fmt = "%i";
695 wcstombs(tmp, (wchar_t *) v->data, sizeof(tmp));
696 tmp[sizeof(tmp) - 1] = '\0';
698 /* workaround for mingw32: as it doesn't
699 correctly parse octal and hexadecimal
700 numbers, they are tried as special cases */
701 if (tmp[0] == '0') {
702 if (tmp[1] == 'b' || tmp[1] == 'B') {
703 /* binary number */
704 fmt = NULL;
705 char *ptr = &tmp[2];
707 while (*ptr == '0' || *ptr == '1') {
708 i <<= 1;
710 if (*ptr == '1')
711 i |= 1;
713 ptr++;
716 else
717 if (tmp[1] == 'x' || tmp[1] == 'X')
718 fmt = "%x";
719 else
720 fmt = "%o";
723 if (fmt != NULL)
724 sscanf(tmp, fmt, &i);
727 mpdm_set_ival(v, i);
730 i = v->ival;
733 mpdm_unref(v);
735 return i;
740 * mpdm_rval - Returns a value's data as a real number (double).
741 * @v: the value
743 * Returns a value's data as a real number (double float). If the value
744 * is a string, it's converted via sscanf and returned; non-string values
745 * have all an rval of 0. The converted double is cached, so costly string
746 * conversions are only done once. Values created with the MPDM_RVAL
747 * flag set have its rval cached from the beginning.
748 * [Strings]
749 * [Value Management]
751 double mpdm_rval(mpdm_t v)
753 double r = 0.0;
755 mpdm_ref(v);
757 if (v != NULL) {
758 /* if there is no cached double, calculate it */
759 if (!(v->flags & MPDM_RVAL)) {
760 /* if it's a string, calculate it; other
761 values will have an rval of 0.0 */
762 if (v->flags & MPDM_STRING) {
763 char tmp[128];
764 char *prev_locale;
766 wcstombs(tmp, (wchar_t *) v->data, sizeof(tmp));
767 tmp[sizeof(tmp) - 1] = '\0';
769 /* if the number starts with 0, it's
770 an octal or hexadecimal number; just
771 take the integer value and cast it */
772 if (tmp[0] == '0' && tmp[1] != '.')
773 r = (double) mpdm_ival(v);
774 else {
775 /* set locale to C for non locale-dependent
776 floating point conversion */
777 prev_locale = setlocale(LC_NUMERIC, "C");
779 /* read */
780 sscanf(tmp, "%lf", &r);
782 /* set previous locale */
783 setlocale(LC_NUMERIC, prev_locale);
787 mpdm_set_rval(v, r);
790 r = v->rval;
793 mpdm_unref(v);
795 return r;
800 * mpdm_gettext - Translates a string to the current language.
801 * @str: the string
803 * Translates the @str string to the current language.
805 * This function can still be used even if there is no real gettext
806 * support() by manually filling the __I18N__ hash.
808 * If the string is found in the current table, the translation is
809 * returned; otherwise, the same @str value is returned.
810 * [Strings]
811 * [Localization]
813 mpdm_t mpdm_gettext(const mpdm_t str)
815 mpdm_t v;
816 mpdm_t i18n = NULL;
818 /* gets the cache */
819 if ((i18n = mpdm_hget_s(mpdm_root(), L"__I18N__")) == NULL)
820 i18n = mpdm_hset_s(mpdm_root(), L"__I18N__", MPDM_H(0));
822 mpdm_ref(str);
824 /* try first the cache */
825 if ((v = mpdm_hget(i18n, str)) == NULL) {
826 #ifdef CONFOPT_GETTEXT
827 char *s;
828 mpdm_t t;
830 /* convert to mbs */
831 t = mpdm_ref(MPDM_2MBS(str->data));
833 /* ask gettext for it */
834 s = gettext((char *) t->data);
836 if (s != t->data)
837 v = MPDM_MBS(s);
838 else
839 v = str;
841 mpdm_unref(t);
843 #else /* CONFOPT_GETTEXT */
845 v = str;
847 #endif /* CONFOPT_GETTEXT */
849 /* store in the cache */
850 mpdm_hset(i18n, str, v);
853 mpdm_unref(str);
855 return v;
860 * mpdm_gettext_domain - Sets domain and data directory for translations.
861 * @dom: the domain (application name)
862 * @data: directory contaning the .mo files
864 * Sets the domain (application name) and translation data for translating
865 * strings that will be returned by mpdm_gettext().@data must point to a
866 * directory containing the .mo (compiled .po) files.
868 * If there is no gettext support, returns 0, or 1 otherwise.
869 * [Strings]
870 * [Localization]
872 int mpdm_gettext_domain(const mpdm_t dom, const mpdm_t data)
874 int ret = 0;
876 mpdm_ref(dom);
877 mpdm_ref(data);
879 #ifdef CONFOPT_GETTEXT
881 mpdm_t dm;
882 mpdm_t dt;
884 /* convert both to mbs,s */
885 dm = mpdm_ref(MPDM_2MBS(dom->data));
886 dt = mpdm_ref(MPDM_2MBS(data->data));
888 /* bind and set domain */
889 bindtextdomain((char *) dm->data, (char *) dt->data);
890 textdomain((char *) dm->data);
892 mpdm_hset_s(mpdm_root(), L"__I18N__", MPDM_H(0));
894 mpdm_unref(dt);
895 mpdm_unref(dm);
897 ret = 1;
899 #endif /* CONFOPT_GETTEXT */
901 #ifdef CONFOPT_WIN32
903 mpdm_t v;
905 if ((v = mpdm_hget_s(mpdm_root(), L"ENV")) != NULL &&
906 mpdm_hget_s(v, L"LANG") == NULL) {
907 wchar_t *wptr = L"en";
909 /* MS Windows crappy language constants... */
911 switch ((GetSystemDefaultLangID() & 0x00ff)) {
912 case 0x01:
913 wptr = L"ar";
914 break; /* arabic */
915 case 0x02:
916 wptr = L"bg";
917 break; /* bulgarian */
918 case 0x03:
919 wptr = L"ca";
920 break; /* catalan */
921 case 0x04:
922 wptr = L"zh";
923 break; /* chinese */
924 case 0x05:
925 wptr = L"cz";
926 break; /* czech */
927 case 0x06:
928 wptr = L"da";
929 break; /* danish */
930 case 0x07:
931 wptr = L"de";
932 break; /* german */
933 case 0x08:
934 wptr = L"el";
935 break; /* greek */
936 case 0x09:
937 wptr = L"en";
938 break; /* english */
939 case 0x0a:
940 wptr = L"es";
941 break; /* spanish */
942 case 0x0b:
943 wptr = L"fi";
944 break; /* finnish */
945 case 0x0c:
946 wptr = L"fr";
947 break; /* french */
948 case 0x0d:
949 wptr = L"he";
950 break; /* hebrew */
951 case 0x0e:
952 wptr = L"hu";
953 break; /* hungarian */
954 case 0x0f:
955 wptr = L"is";
956 break; /* icelandic */
957 case 0x10:
958 wptr = L"it";
959 break; /* italian */
960 case 0x11:
961 wptr = L"jp";
962 break; /* japanese */
963 case 0x12:
964 wptr = L"ko";
965 break; /* korean */
966 case 0x13:
967 wptr = L"nl";
968 break; /* dutch */
969 case 0x14:
970 wptr = L"no";
971 break; /* norwegian */
972 case 0x15:
973 wptr = L"po";
974 break; /* polish */
975 case 0x16:
976 wptr = L"pt";
977 break; /* portuguese */
978 case 0x17:
979 wptr = L"rm";
980 break; /* romansh (switzerland) */
981 case 0x18:
982 wptr = L"ro";
983 break; /* romanian */
984 case 0x19:
985 wptr = L"ru";
986 break; /* russian */
987 case 0x1a:
988 wptr = L"sr";
989 break; /* serbian */
990 case 0x1b:
991 wptr = L"sk";
992 break; /* slovak */
993 case 0x1c:
994 wptr = L"sq";
995 break; /* albanian */
996 case 0x1d:
997 wptr = L"sv";
998 break; /* swedish */
1001 mpdm_hset_s(v, L"LANG", MPDM_S(wptr));
1004 #endif /* CONFOPT_WIN32 */
1006 mpdm_unref(data);
1007 mpdm_unref(dom);
1009 return ret;
1013 #ifdef CONFOPT_WCWIDTH
1015 int wcwidth(wchar_t);
1017 int mpdm_wcwidth(wchar_t c)
1019 return wcwidth(c);
1022 #else /* CONFOPT_WCWIDTH */
1024 #include "wcwidth.c"
1026 int mpdm_wcwidth(wchar_t c)
1028 return mk_wcwidth(c);
1031 #endif /* CONFOPT_WCWIDTH */
1035 * mpdm_sprintf - Formats a sprintf()-like string.
1036 * @fmt: the string format
1037 * @args: an array of values
1039 * Formats a string using the sprintf() format taking the values from @args.
1040 * [Strings]
1042 mpdm_t mpdm_sprintf(const mpdm_t fmt, const mpdm_t args)
1044 const wchar_t *i = fmt->data;
1045 wchar_t *o = NULL;
1046 int l = 0, n = 0;
1047 wchar_t c;
1049 mpdm_ref(fmt);
1050 mpdm_ref(args);
1052 /* loop all characters */
1053 while ((c = *i++) != L'\0') {
1054 int m = 0;
1055 wchar_t *tptr = NULL;
1056 wchar_t *wptr = NULL;
1058 if (c == L'%') {
1059 /* format directive */
1060 char t_fmt[128];
1061 char tmp[1024];
1062 mpdm_t v;
1063 char *ptr = NULL;
1065 /* transfer the % */
1066 t_fmt[m++] = '%';
1068 /* transform the format to mbs */
1069 while (*i != L'\0' &&
1070 m < (int) (sizeof(t_fmt) - MB_CUR_MAX - 1) &&
1071 wcschr(L"-.0123456789", *i) != NULL)
1072 m += wctomb(&t_fmt[m], *i++);
1074 /* transfer the directive */
1075 m += wctomb(&t_fmt[m], *i++);
1077 t_fmt[m] = '\0';
1079 /* by default, copies the format */
1080 strcpy(tmp, t_fmt);
1082 /* pick next value */
1083 v = mpdm_aget(args, n++);
1085 switch (t_fmt[m - 1]) {
1086 case 'd':
1087 case 'i':
1088 case 'u':
1089 case 'x':
1090 case 'X':
1091 case 'o':
1093 /* integer value */
1094 snprintf(tmp, sizeof(tmp) - 1, t_fmt, mpdm_ival(v));
1095 break;
1097 case 'f':
1099 /* float (real) value */
1100 snprintf(tmp, sizeof(tmp) - 1, t_fmt, mpdm_rval(v));
1101 break;
1103 case 's':
1105 /* string value */
1106 ptr = mpdm_wcstombs(mpdm_string(v), NULL);
1107 snprintf(tmp, sizeof(tmp) - 1, t_fmt, ptr);
1108 free(ptr);
1110 break;
1112 case 'c':
1114 /* char */
1115 m = 1;
1116 wptr = &c;
1117 c = mpdm_ival(v);
1118 break;
1120 case 'b':
1122 ptr = tmp;
1123 unsigned int mask;
1124 int p = 0;
1126 mask = 1 << ((sizeof(int) * 8) - 1);
1127 while (mask) {
1128 if (mask & (unsigned int) mpdm_ival(v)) {
1129 *ptr++ = '1';
1130 p = 1;
1132 else
1133 if (p)
1134 *ptr++ = '0';
1136 mask >>= 1;
1139 if (ptr == tmp)
1140 *ptr++ = '0';
1142 *ptr = '\0';
1143 break;
1145 case '%':
1147 /* percent sign */
1148 m = 1;
1149 wptr = &c;
1150 break;
1153 /* transfer */
1154 if (wptr == NULL)
1155 wptr = tptr = mpdm_mbstowcs(tmp, &m, -1);
1157 else {
1158 /* raw character */
1159 m = 1;
1160 wptr = &c;
1163 /* transfer */
1164 o = mpdm_poke(o, &l, wptr, m, sizeof(wchar_t));
1166 /* free the temporary buffer, if any */
1167 if (tptr != NULL)
1168 free(tptr);
1171 if (o == NULL)
1172 return NULL;
1174 /* null-terminate */
1175 o = mpdm_poke(o, &l, L"", 1, sizeof(wchar_t));
1177 mpdm_unref(args);
1178 mpdm_unref(fmt);
1180 return MPDM_ENS(o, l - 1);
1185 * mpdm_ulc - Converts a string to uppercase or lowecase.
1186 * @s: the string
1187 * @u: convert to uppercase (1) or to lowercase (0).
1189 * Converts @s to uppercase (for @u == 1) or to lowercase (@u == 0).
1190 * [Strings]
1192 mpdm_t mpdm_ulc(const mpdm_t s, int u)
1194 mpdm_t r = NULL;
1195 wchar_t *optr;
1196 int i;
1198 mpdm_ref(s);
1200 i = mpdm_size(s);
1202 if ((optr = malloc((i + 1) * sizeof(wchar_t))) != NULL) {
1203 wchar_t *iptr = mpdm_string(s);
1204 int n;
1206 for (n = 0; n < i; n++)
1207 optr[n] = u ? towupper(iptr[n]) : towlower(iptr[n]);
1209 optr[n] = L'\0';
1210 r = MPDM_ENS(optr, i);
1213 mpdm_unref(s);
1215 return r;
1219 /* scanf working buffers */
1220 #define SCANF_BUF_SIZE 1024
1221 static wchar_t scanf_yset[SCANF_BUF_SIZE];
1222 static wchar_t scanf_nset[SCANF_BUF_SIZE];
1223 static wchar_t scanf_mark[SCANF_BUF_SIZE];
1225 struct {
1226 wchar_t cmd;
1227 wchar_t *yset;
1228 wchar_t *nset;
1229 } scanf_sets[] = {
1230 { L's', L"", L" \t"},
1231 { L'u', L"0123456789", L""},
1232 { L'd', L"-0123456789", L""},
1233 { L'i', L"-0123456789", L""},
1234 { L'f', L"-0123456789.", L""},
1235 { L'x', L"-0123456789xabcdefABCDEF", L""},
1236 { L'\0', NULL, NULL},
1240 * mpdm_sscanf - Extracts data like sscanf().
1241 * @str: the string to be parsed
1242 * @fmt: the string format
1243 * @offset: the character offset to start scanning
1245 * Extracts data from a string using a special format pattern, very
1246 * much like the scanf() series of functions in the C library. Apart
1247 * from the standard percent-sign-commands (s, u, d, i, f, x,
1248 * n, [, with optional size and * to ignore), it implements S,
1249 * to match a string of characters upto what follows in the format
1250 * string. Also, the [ set of characters can include other % formats.
1252 * Returns an array with the extracted values. If %n is used, the
1253 * position in the scanned string is returned as the value.
1254 * [Strings]
1256 mpdm_t mpdm_sscanf(const mpdm_t str, const mpdm_t fmt, int offset)
1258 wchar_t *i = (wchar_t *) str->data;
1259 wchar_t *f = (wchar_t *) fmt->data;
1260 mpdm_t r;
1262 mpdm_ref(fmt);
1263 mpdm_ref(str);
1265 i += offset;
1266 r = MPDM_A(0);
1267 mpdm_ref(r);
1269 while (*f) {
1270 if (*f == L'%') {
1271 wchar_t *ptr = NULL;
1272 int size = 0;
1273 wchar_t cmd;
1274 int vsize = 0;
1275 int ignore = 0;
1276 int msize = 0;
1278 /* empty all buffers */
1279 scanf_yset[0] = scanf_nset[0] = scanf_mark[0] = L'\0';
1281 f++;
1283 /* an asterisk? don't return next value */
1284 if (*f == L'*') {
1285 ignore = 1;
1286 f++;
1289 /* does it have a size? */
1290 while (wcschr(L"0123456789", *f)) {
1291 vsize *= 10;
1292 vsize += *f - L'0';
1293 f++;
1296 /* if no size, set it to an arbitrary big limit */
1297 if (!vsize)
1298 vsize = 0xfffffff;
1300 /* now *f should contain a command */
1301 cmd = *f;
1302 f++;
1304 /* is it a verbatim percent sign? */
1305 if (cmd == L'%') {
1306 vsize = 1;
1307 ignore = 1;
1308 wcscpy(scanf_yset, L"%");
1310 else
1311 /* a position? */
1312 if (cmd == L'n') {
1313 vsize = 0;
1314 ignore = 1;
1315 mpdm_push(r, MPDM_I(i - (wchar_t *) str->data));
1317 else
1318 /* string upto a mark */
1319 if (cmd == L'S') {
1320 wchar_t *tmp = f;
1322 /* fill the mark upto another command */
1323 while (*tmp) {
1324 if (*tmp == L'%') {
1325 tmp++;
1327 /* is it an 'n'? ignore and go on */
1328 if (*tmp == L'n') {
1329 tmp++;
1330 continue;
1332 else
1333 if (*tmp == L'%')
1334 scanf_mark[msize++] = *tmp;
1335 else
1336 break;
1338 else
1339 scanf_mark[msize++] = *tmp;
1341 tmp++;
1344 scanf_mark[msize] = L'\0';
1346 else
1347 /* raw set */
1348 if (cmd == L'[') {
1349 int n = 0;
1350 wchar_t *set = scanf_yset;
1352 /* is it an inverse set? */
1353 if (*f == L'^') {
1354 set = scanf_nset;
1355 f++;
1358 /* first one is a ]? add it */
1359 if (*f == L']') {
1360 set[n++] = *f;
1361 f++;
1364 /* now build the set */
1365 for (; n < SCANF_BUF_SIZE - 1 && *f && *f != L']'; f++) {
1366 /* is it a range? */
1367 if (*f == L'-') {
1368 f++;
1370 /* start or end? hyphen itself */
1371 if (n == 0 || *f == L']')
1372 set[n++] = L'-';
1373 else {
1374 /* pick previous char */
1375 wchar_t c = set[n - 1];
1377 /* fill */
1378 while (n < SCANF_BUF_SIZE - 1 && c < *f)
1379 set[n++] = ++c;
1382 else
1383 /* is it another command? */
1384 if (*f == L'%') {
1385 int i;
1387 f++;
1388 for (i = 0; scanf_sets[i].cmd; i++) {
1389 if (*f == scanf_sets[i].cmd) {
1390 set[n] = L'\0';
1391 wcscat(set, scanf_sets[i].yset);
1392 n += wcslen(scanf_sets[i].yset);
1393 break;
1397 else
1398 set[n++] = *f;
1401 /* skip the ] */
1402 f++;
1404 set[n] = L'\0';
1406 else
1407 /* a standard set? */
1409 int n;
1411 for (n = 0; scanf_sets[n].cmd != L'\0'; n++) {
1412 if (cmd == scanf_sets[n].cmd) {
1413 wcscpy(scanf_yset, scanf_sets[n].yset);
1414 wcscpy(scanf_nset, scanf_sets[n].nset);
1415 break;
1420 /* now fill the dynamic string */
1421 while (vsize &&
1422 !wcschr(scanf_nset, *i) &&
1423 (scanf_yset[0] == L'\0' || wcschr(scanf_yset, *i)) &&
1424 (msize == 0 || wcsncmp(i, scanf_mark, msize) != 0)) {
1426 /* only add if not being ignored */
1427 if (!ignore)
1428 ptr = mpdm_poke(ptr, &size, i, 1, sizeof(wchar_t));
1430 i++;
1431 vsize--;
1434 if (!ignore && size) {
1435 /* null terminate and push */
1436 ptr = mpdm_poke(ptr, &size, L"", 1, sizeof(wchar_t));
1437 mpdm_push(r, MPDM_ENS(ptr, size - 1));
1440 else
1441 if (*f == L' ' || *f == L'\t') {
1442 /* if it's a blank, sync to next non-blank */
1443 f++;
1445 while (*i == L' ' || *i == L'\t')
1446 i++;
1448 else
1449 /* test for literals in the format string */
1450 if (*i == *f) {
1451 i++;
1452 f++;
1454 else
1455 break;
1458 mpdm_unref(str);
1459 mpdm_unref(fmt);
1461 mpdm_unrefnd(r);
1463 return r;