Deleted special case in mpdm_cmp().
[mpdm.git] / mpdm_s.c
blobca7b1ba8b55b7287c229c07501242976bb5bc67e
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_ARRAY(v1) && MPDM_IS_ARRAY(v2)) {
449 /* compare first the sizes */
450 if ((r = mpdm_size(v1) - mpdm_size(v2)) == 0) {
451 int n;
453 /* they have the same size;
454 compare each pair of elements */
455 for (n = 0; n < mpdm_size(v1); n++) {
456 if ((r = mpdm_cmp(mpdm_aget(v1, n),
457 mpdm_aget(v2, n))) != 0)
458 break;
462 else {
463 wchar_t tmp[32];
465 r = wcscoll(mpdm_string(v1), mpdm_string2(v2, tmp));
468 mpdm_unref(v2);
469 mpdm_unref(v1);
471 return r;
476 * mpdm_cmp_s - Compares two values (string version).
477 * @v1: the first value
478 * @v2: the second value
480 * Compares two values. If both has the MPDM_STRING flag set,
481 * a comparison using wcscoll() is returned; if both are arrays,
482 * the size is compared first and, if they have the same number
483 * elements, each one is compared; otherwise, a simple visual
484 * representation comparison is done.
486 int mpdm_cmp_s(const mpdm_t v1, const wchar_t * v2)
488 return mpdm_cmp(v1, MPDM_S(v2));
493 * mpdm_splice - Creates a new string value from another.
494 * @v: the original value
495 * @i: the value to be inserted
496 * @offset: offset where the substring is to be inserted
497 * @del: number of characters to delete
499 * Creates a new string value from @v, deleting @del chars at @offset
500 * and substituting them by @i. If @del is 0, no deletion is done.
501 * both @offset and @del can be negative; if this is the case, it's
502 * assumed as counting from the end of @v. If @v is NULL, @i will become
503 * the new string, and both @offset and @del will be ignored. If @v is
504 * not NULL and @i is, no insertion process is done (only deletion, if
505 * applicable).
507 * Returns a two element array, with the new string in the first
508 * element and the deleted string in the second (with a NULL value
509 * if @del is 0).
510 * [Strings]
512 mpdm_t mpdm_splice(const mpdm_t v, const mpdm_t i, int offset, int del)
514 mpdm_t w;
515 mpdm_t n = NULL;
516 mpdm_t d = NULL;
517 int os, ns, r;
518 int ins = 0;
519 wchar_t *ptr;
521 mpdm_ref(v);
522 mpdm_ref(i);
524 if (v != NULL) {
525 os = mpdm_size(v);
527 /* negative offsets start from the end */
528 if (offset < 0)
529 offset = os + 1 - offset;
531 /* never add further the end */
532 if (offset > os)
533 offset = os;
535 /* negative del counts as 'characters left' */
536 if (del < 0)
537 del = os + 1 - offset + del;
539 /* something to delete? */
540 if (del > 0) {
541 /* never delete further the end */
542 if (offset + del > os)
543 del = os - offset;
545 /* deleted string */
546 d = MPDM_NS(((wchar_t *) v->data) + offset, del);
548 else
549 del = 0;
551 /* something to insert? */
552 ins = mpdm_size(i);
554 /* new size and remainder */
555 ns = os + ins - del;
556 r = offset + del;
558 n = MPDM_NS(NULL, ns);
560 ptr = (wchar_t *) n->data;
562 /* copy the beginning */
563 if (offset > 0) {
564 wcsncpy(ptr, v->data, offset);
565 ptr += offset;
568 /* copy the text to be inserted */
569 if (ins > 0) {
570 wcsncpy(ptr, i->data, ins);
571 ptr += ins;
574 /* copy the remaining */
575 os -= r;
576 if (os > 0) {
577 wcsncpy(ptr, ((wchar_t *) v->data) + r, os);
578 ptr += os;
581 /* null terminate */
582 *ptr = L'\0';
584 else
585 n = i;
587 /* creates the output array */
588 w = MPDM_A(2);
590 mpdm_ref(w);
591 mpdm_aset(w, n, 0);
592 mpdm_aset(w, d, 1);
593 mpdm_unrefnd(w);
595 mpdm_unref(i);
596 mpdm_unref(v);
598 return w;
603 * mpdm_strcat_sn - Concatenates two strings (string with size version).
604 * @s1: the first string
605 * @s2: the second string
606 * @size: the size of the second string
608 * Returns a new string formed by the concatenation of @s1 and @s2.
609 * [Strings]
611 mpdm_t mpdm_strcat_sn(const mpdm_t s1, const wchar_t * s2, int size)
613 wchar_t *ptr = NULL;
614 int s = 0;
615 mpdm_t r;
617 if (s1 == NULL && s2 == NULL)
618 r = NULL;
619 else {
620 ptr = mpdm_pokev(ptr, &s, s1);
621 ptr = mpdm_pokewsn(ptr, &s, s2, size);
623 ptr = mpdm_poke(ptr, &s, L"", 1, sizeof(wchar_t));
624 r = MPDM_ENS(ptr, s - 1);
627 return r;
632 * mpdm_strcat_s - Concatenates two strings (string version).
633 * @s1: the first string
634 * @s2: the second string
636 * Returns a new string formed by the concatenation of @s1 and @s2.
637 * [Strings]
639 mpdm_t mpdm_strcat_s(const mpdm_t s1, const wchar_t * s2)
641 return mpdm_strcat_sn(s1, s2, s2 ? wcslen(s2) : 0);
646 * mpdm_strcat - Concatenates two strings.
647 * @s1: the first string
648 * @s2: the second string
650 * Returns a new string formed by the concatenation of @s1 and @s2.
651 * [Strings]
653 mpdm_t mpdm_strcat(const mpdm_t s1, const mpdm_t s2)
655 mpdm_t r;
657 mpdm_ref(s2);
658 r = mpdm_strcat_s(s1, s2 ? mpdm_string(s2) : NULL);
659 mpdm_unref(s2);
661 return r;
666 * mpdm_ival - Returns a value's data as an integer.
667 * @v: the value
669 * Returns a value's data as an integer. If the value is a string,
670 * it's converted via sscanf and returned; non-string values have all
671 * an ival of 0. The converted integer is cached, so costly string
672 * conversions are only done once. Values created with the MPDM_IVAL
673 * flag set have its ival cached from the beginning.
674 * [Strings]
675 * [Value Management]
677 int mpdm_ival(mpdm_t v)
679 int i = 0;
681 mpdm_ref(v);
683 if (v != NULL) {
684 /* if there is no cached integer, calculate it */
685 if (!(v->flags & MPDM_IVAL)) {
686 /* if it's a string, calculate it; other
687 values will have an ival of 0 */
688 if (v->flags & MPDM_STRING) {
689 char tmp[32];
690 char *fmt = "%i";
692 wcstombs(tmp, (wchar_t *) v->data, sizeof(tmp));
693 tmp[sizeof(tmp) - 1] = '\0';
695 /* workaround for mingw32: as it doesn't
696 correctly parse octal and hexadecimal
697 numbers, they are tried as special cases */
698 if (tmp[0] == '0') {
699 if (tmp[1] == 'b' || tmp[1] == 'B') {
700 /* binary number */
701 fmt = NULL;
702 char *ptr = &tmp[2];
704 while (*ptr == '0' || *ptr == '1') {
705 i <<= 1;
707 if (*ptr == '1')
708 i |= 1;
710 ptr++;
713 else
714 if (tmp[1] == 'x' || tmp[1] == 'X')
715 fmt = "%x";
716 else
717 fmt = "%o";
720 if (fmt != NULL)
721 sscanf(tmp, fmt, &i);
724 mpdm_set_ival(v, i);
727 i = v->ival;
730 mpdm_unref(v);
732 return i;
737 * mpdm_rval - Returns a value's data as a real number (double).
738 * @v: the value
740 * Returns a value's data as a real number (double float). If the value
741 * is a string, it's converted via sscanf and returned; non-string values
742 * have all an rval of 0. The converted double is cached, so costly string
743 * conversions are only done once. Values created with the MPDM_RVAL
744 * flag set have its rval cached from the beginning.
745 * [Strings]
746 * [Value Management]
748 double mpdm_rval(mpdm_t v)
750 double r = 0.0;
752 mpdm_ref(v);
754 if (v != NULL) {
755 /* if there is no cached double, calculate it */
756 if (!(v->flags & MPDM_RVAL)) {
757 /* if it's a string, calculate it; other
758 values will have an rval of 0.0 */
759 if (v->flags & MPDM_STRING) {
760 char tmp[128];
761 char *prev_locale;
763 wcstombs(tmp, (wchar_t *) v->data, sizeof(tmp));
764 tmp[sizeof(tmp) - 1] = '\0';
766 /* if the number starts with 0, it's
767 an octal or hexadecimal number; just
768 take the integer value and cast it */
769 if (tmp[0] == '0' && tmp[1] != '.')
770 r = (double) mpdm_ival(v);
771 else {
772 /* set locale to C for non locale-dependent
773 floating point conversion */
774 prev_locale = setlocale(LC_NUMERIC, "C");
776 /* read */
777 sscanf(tmp, "%lf", &r);
779 /* set previous locale */
780 setlocale(LC_NUMERIC, prev_locale);
784 mpdm_set_rval(v, r);
787 r = v->rval;
790 mpdm_unref(v);
792 return r;
797 * mpdm_gettext - Translates a string to the current language.
798 * @str: the string
800 * Translates the @str string to the current language.
802 * This function can still be used even if there is no real gettext
803 * support() by manually filling the __I18N__ hash.
805 * If the string is found in the current table, the translation is
806 * returned; otherwise, the same @str value is returned.
807 * [Strings]
808 * [Localization]
810 mpdm_t mpdm_gettext(const mpdm_t str)
812 mpdm_t v;
813 mpdm_t i18n = NULL;
815 /* gets the cache */
816 if ((i18n = mpdm_hget_s(mpdm_root(), L"__I18N__")) == NULL)
817 i18n = mpdm_hset_s(mpdm_root(), L"__I18N__", MPDM_H(0));
819 mpdm_ref(str);
821 /* try first the cache */
822 if ((v = mpdm_hget(i18n, str)) == NULL) {
823 #ifdef CONFOPT_GETTEXT
824 char *s;
825 mpdm_t t;
827 /* convert to mbs */
828 t = mpdm_ref(MPDM_2MBS(str->data));
830 /* ask gettext for it */
831 s = gettext((char *) t->data);
833 if (s != t->data)
834 v = MPDM_MBS(s);
835 else
836 v = str;
838 mpdm_unref(t);
840 #else /* CONFOPT_GETTEXT */
842 v = str;
844 #endif /* CONFOPT_GETTEXT */
846 /* store in the cache */
847 mpdm_hset(i18n, str, v);
850 mpdm_unref(str);
852 return v;
857 * mpdm_gettext_domain - Sets domain and data directory for translations.
858 * @dom: the domain (application name)
859 * @data: directory contaning the .mo files
861 * Sets the domain (application name) and translation data for translating
862 * strings that will be returned by mpdm_gettext().@data must point to a
863 * directory containing the .mo (compiled .po) files.
865 * If there is no gettext support, returns 0, or 1 otherwise.
866 * [Strings]
867 * [Localization]
869 int mpdm_gettext_domain(const mpdm_t dom, const mpdm_t data)
871 int ret = 0;
873 mpdm_ref(dom);
874 mpdm_ref(data);
876 #ifdef CONFOPT_GETTEXT
878 mpdm_t dm;
879 mpdm_t dt;
881 /* convert both to mbs,s */
882 dm = mpdm_ref(MPDM_2MBS(dom->data));
883 dt = mpdm_ref(MPDM_2MBS(data->data));
885 /* bind and set domain */
886 bindtextdomain((char *) dm->data, (char *) dt->data);
887 textdomain((char *) dm->data);
889 mpdm_hset_s(mpdm_root(), L"__I18N__", MPDM_H(0));
891 mpdm_unref(dt);
892 mpdm_unref(dm);
894 ret = 1;
896 #endif /* CONFOPT_GETTEXT */
898 #ifdef CONFOPT_WIN32
900 mpdm_t v;
902 if ((v = mpdm_hget_s(mpdm_root(), L"ENV")) != NULL &&
903 mpdm_hget_s(v, L"LANG") == NULL) {
904 wchar_t *wptr = L"en";
906 /* MS Windows crappy language constants... */
908 switch ((GetSystemDefaultLangID() & 0x00ff)) {
909 case 0x01:
910 wptr = L"ar";
911 break; /* arabic */
912 case 0x02:
913 wptr = L"bg";
914 break; /* bulgarian */
915 case 0x03:
916 wptr = L"ca";
917 break; /* catalan */
918 case 0x04:
919 wptr = L"zh";
920 break; /* chinese */
921 case 0x05:
922 wptr = L"cz";
923 break; /* czech */
924 case 0x06:
925 wptr = L"da";
926 break; /* danish */
927 case 0x07:
928 wptr = L"de";
929 break; /* german */
930 case 0x08:
931 wptr = L"el";
932 break; /* greek */
933 case 0x09:
934 wptr = L"en";
935 break; /* english */
936 case 0x0a:
937 wptr = L"es";
938 break; /* spanish */
939 case 0x0b:
940 wptr = L"fi";
941 break; /* finnish */
942 case 0x0c:
943 wptr = L"fr";
944 break; /* french */
945 case 0x0d:
946 wptr = L"he";
947 break; /* hebrew */
948 case 0x0e:
949 wptr = L"hu";
950 break; /* hungarian */
951 case 0x0f:
952 wptr = L"is";
953 break; /* icelandic */
954 case 0x10:
955 wptr = L"it";
956 break; /* italian */
957 case 0x11:
958 wptr = L"jp";
959 break; /* japanese */
960 case 0x12:
961 wptr = L"ko";
962 break; /* korean */
963 case 0x13:
964 wptr = L"nl";
965 break; /* dutch */
966 case 0x14:
967 wptr = L"no";
968 break; /* norwegian */
969 case 0x15:
970 wptr = L"po";
971 break; /* polish */
972 case 0x16:
973 wptr = L"pt";
974 break; /* portuguese */
975 case 0x17:
976 wptr = L"rm";
977 break; /* romansh (switzerland) */
978 case 0x18:
979 wptr = L"ro";
980 break; /* romanian */
981 case 0x19:
982 wptr = L"ru";
983 break; /* russian */
984 case 0x1a:
985 wptr = L"sr";
986 break; /* serbian */
987 case 0x1b:
988 wptr = L"sk";
989 break; /* slovak */
990 case 0x1c:
991 wptr = L"sq";
992 break; /* albanian */
993 case 0x1d:
994 wptr = L"sv";
995 break; /* swedish */
998 mpdm_hset_s(v, L"LANG", MPDM_S(wptr));
1001 #endif /* CONFOPT_WIN32 */
1003 mpdm_unref(data);
1004 mpdm_unref(dom);
1006 return ret;
1010 #ifdef CONFOPT_WCWIDTH
1012 int wcwidth(wchar_t);
1014 int mpdm_wcwidth(wchar_t c)
1016 return wcwidth(c);
1019 #else /* CONFOPT_WCWIDTH */
1021 #include "wcwidth.c"
1023 int mpdm_wcwidth(wchar_t c)
1025 return mk_wcwidth(c);
1028 #endif /* CONFOPT_WCWIDTH */
1032 * mpdm_sprintf - Formats a sprintf()-like string.
1033 * @fmt: the string format
1034 * @args: an array of values
1036 * Formats a string using the sprintf() format taking the values from @args.
1037 * [Strings]
1039 mpdm_t mpdm_sprintf(const mpdm_t fmt, const mpdm_t args)
1041 const wchar_t *i = fmt->data;
1042 wchar_t *o = NULL;
1043 int l = 0, n = 0;
1044 wchar_t c;
1046 mpdm_ref(fmt);
1047 mpdm_ref(args);
1049 /* loop all characters */
1050 while ((c = *i++) != L'\0') {
1051 int m = 0;
1052 wchar_t *tptr = NULL;
1053 wchar_t *wptr = NULL;
1055 if (c == L'%') {
1056 /* format directive */
1057 char t_fmt[128];
1058 char tmp[1024];
1059 mpdm_t v;
1060 char *ptr = NULL;
1062 /* transfer the % */
1063 t_fmt[m++] = '%';
1065 /* transform the format to mbs */
1066 while (*i != L'\0' &&
1067 m < (int) (sizeof(t_fmt) - MB_CUR_MAX - 1) &&
1068 wcschr(L"-.0123456789", *i) != NULL)
1069 m += wctomb(&t_fmt[m], *i++);
1071 /* transfer the directive */
1072 m += wctomb(&t_fmt[m], *i++);
1074 t_fmt[m] = '\0';
1076 /* by default, copies the format */
1077 strcpy(tmp, t_fmt);
1079 /* pick next value */
1080 v = mpdm_aget(args, n++);
1082 switch (t_fmt[m - 1]) {
1083 case 'd':
1084 case 'i':
1085 case 'u':
1086 case 'x':
1087 case 'X':
1088 case 'o':
1090 /* integer value */
1091 snprintf(tmp, sizeof(tmp) - 1, t_fmt, mpdm_ival(v));
1092 break;
1094 case 'f':
1096 /* float (real) value */
1097 snprintf(tmp, sizeof(tmp) - 1, t_fmt, mpdm_rval(v));
1098 break;
1100 case 's':
1102 /* string value */
1103 ptr = mpdm_wcstombs(mpdm_string(v), NULL);
1104 snprintf(tmp, sizeof(tmp) - 1, t_fmt, ptr);
1105 free(ptr);
1107 break;
1109 case 'c':
1111 /* char */
1112 m = 1;
1113 wptr = &c;
1114 c = mpdm_ival(v);
1115 break;
1117 case 'b':
1119 ptr = tmp;
1120 unsigned int mask;
1121 int p = 0;
1123 mask = 1 << ((sizeof(int) * 8) - 1);
1124 while (mask) {
1125 if (mask & (unsigned int) mpdm_ival(v)) {
1126 *ptr++ = '1';
1127 p = 1;
1129 else
1130 if (p)
1131 *ptr++ = '0';
1133 mask >>= 1;
1136 if (ptr == tmp)
1137 *ptr++ = '0';
1139 *ptr = '\0';
1140 break;
1142 case '%':
1144 /* percent sign */
1145 m = 1;
1146 wptr = &c;
1147 break;
1150 /* transfer */
1151 if (wptr == NULL)
1152 wptr = tptr = mpdm_mbstowcs(tmp, &m, -1);
1154 else {
1155 /* raw character */
1156 m = 1;
1157 wptr = &c;
1160 /* transfer */
1161 o = mpdm_poke(o, &l, wptr, m, sizeof(wchar_t));
1163 /* free the temporary buffer, if any */
1164 if (tptr != NULL)
1165 free(tptr);
1168 if (o == NULL)
1169 return NULL;
1171 /* null-terminate */
1172 o = mpdm_poke(o, &l, L"", 1, sizeof(wchar_t));
1174 mpdm_unref(args);
1175 mpdm_unref(fmt);
1177 return MPDM_ENS(o, l - 1);
1182 * mpdm_ulc - Converts a string to uppercase or lowecase.
1183 * @s: the string
1184 * @u: convert to uppercase (1) or to lowercase (0).
1186 * Converts @s to uppercase (for @u == 1) or to lowercase (@u == 0).
1187 * [Strings]
1189 mpdm_t mpdm_ulc(const mpdm_t s, int u)
1191 mpdm_t r = NULL;
1192 wchar_t *optr;
1193 int i;
1195 mpdm_ref(s);
1197 i = mpdm_size(s);
1199 if ((optr = malloc((i + 1) * sizeof(wchar_t))) != NULL) {
1200 wchar_t *iptr = mpdm_string(s);
1201 int n;
1203 for (n = 0; n < i; n++)
1204 optr[n] = u ? towupper(iptr[n]) : towlower(iptr[n]);
1206 optr[n] = L'\0';
1207 r = MPDM_ENS(optr, i);
1210 mpdm_unref(s);
1212 return r;
1216 /* scanf working buffers */
1217 #define SCANF_BUF_SIZE 1024
1218 static wchar_t scanf_yset[SCANF_BUF_SIZE];
1219 static wchar_t scanf_nset[SCANF_BUF_SIZE];
1220 static wchar_t scanf_mark[SCANF_BUF_SIZE];
1222 struct {
1223 wchar_t cmd;
1224 wchar_t *yset;
1225 wchar_t *nset;
1226 } scanf_sets[] = {
1227 { L's', L"", L" \t"},
1228 { L'u', L"0123456789", L""},
1229 { L'd', L"-0123456789", L""},
1230 { L'i', L"-0123456789", L""},
1231 { L'f', L"-0123456789.", L""},
1232 { L'x', L"-0123456789xabcdefABCDEF", L""},
1233 { L'\0', NULL, NULL},
1237 * mpdm_sscanf - Extracts data like sscanf().
1238 * @str: the string to be parsed
1239 * @fmt: the string format
1240 * @offset: the character offset to start scanning
1242 * Extracts data from a string using a special format pattern, very
1243 * much like the scanf() series of functions in the C library. Apart
1244 * from the standard percent-sign-commands (s, u, d, i, f, x,
1245 * n, [, with optional size and * to ignore), it implements S,
1246 * to match a string of characters upto what follows in the format
1247 * string. Also, the [ set of characters can include other % formats.
1249 * Returns an array with the extracted values. If %n is used, the
1250 * position in the scanned string is returned as the value.
1251 * [Strings]
1253 mpdm_t mpdm_sscanf(const mpdm_t str, const mpdm_t fmt, int offset)
1255 wchar_t *i = (wchar_t *) str->data;
1256 wchar_t *f = (wchar_t *) fmt->data;
1257 mpdm_t r;
1259 mpdm_ref(fmt);
1260 mpdm_ref(str);
1262 i += offset;
1263 r = MPDM_A(0);
1264 mpdm_ref(r);
1266 while (*f) {
1267 if (*f == L'%') {
1268 wchar_t *ptr = NULL;
1269 int size = 0;
1270 wchar_t cmd;
1271 int vsize = 0;
1272 int ignore = 0;
1273 int msize = 0;
1275 /* empty all buffers */
1276 scanf_yset[0] = scanf_nset[0] = scanf_mark[0] = L'\0';
1278 f++;
1280 /* an asterisk? don't return next value */
1281 if (*f == L'*') {
1282 ignore = 1;
1283 f++;
1286 /* does it have a size? */
1287 while (wcschr(L"0123456789", *f)) {
1288 vsize *= 10;
1289 vsize += *f - L'0';
1290 f++;
1293 /* if no size, set it to an arbitrary big limit */
1294 if (!vsize)
1295 vsize = 0xfffffff;
1297 /* now *f should contain a command */
1298 cmd = *f;
1299 f++;
1301 /* is it a verbatim percent sign? */
1302 if (cmd == L'%') {
1303 vsize = 1;
1304 ignore = 1;
1305 wcscpy(scanf_yset, L"%");
1307 else
1308 /* a position? */
1309 if (cmd == L'n') {
1310 vsize = 0;
1311 ignore = 1;
1312 mpdm_push(r, MPDM_I(i - (wchar_t *) str->data));
1314 else
1315 /* string upto a mark */
1316 if (cmd == L'S') {
1317 wchar_t *tmp = f;
1319 /* fill the mark upto another command */
1320 while (*tmp) {
1321 if (*tmp == L'%') {
1322 tmp++;
1324 /* is it an 'n'? ignore and go on */
1325 if (*tmp == L'n') {
1326 tmp++;
1327 continue;
1329 else
1330 if (*tmp == L'%')
1331 scanf_mark[msize++] = *tmp;
1332 else
1333 break;
1335 else
1336 scanf_mark[msize++] = *tmp;
1338 tmp++;
1341 scanf_mark[msize] = L'\0';
1343 else
1344 /* raw set */
1345 if (cmd == L'[') {
1346 int n = 0;
1347 wchar_t *set = scanf_yset;
1349 /* is it an inverse set? */
1350 if (*f == L'^') {
1351 set = scanf_nset;
1352 f++;
1355 /* first one is a ]? add it */
1356 if (*f == L']') {
1357 set[n++] = *f;
1358 f++;
1361 /* now build the set */
1362 for (; n < SCANF_BUF_SIZE - 1 && *f && *f != L']'; f++) {
1363 /* is it a range? */
1364 if (*f == L'-') {
1365 f++;
1367 /* start or end? hyphen itself */
1368 if (n == 0 || *f == L']')
1369 set[n++] = L'-';
1370 else {
1371 /* pick previous char */
1372 wchar_t c = set[n - 1];
1374 /* fill */
1375 while (n < SCANF_BUF_SIZE - 1 && c < *f)
1376 set[n++] = ++c;
1379 else
1380 /* is it another command? */
1381 if (*f == L'%') {
1382 int i;
1384 f++;
1385 for (i = 0; scanf_sets[i].cmd; i++) {
1386 if (*f == scanf_sets[i].cmd) {
1387 set[n] = L'\0';
1388 wcscat(set, scanf_sets[i].yset);
1389 n += wcslen(scanf_sets[i].yset);
1390 break;
1394 else
1395 set[n++] = *f;
1398 /* skip the ] */
1399 f++;
1401 set[n] = L'\0';
1403 else
1404 /* a standard set? */
1406 int n;
1408 for (n = 0; scanf_sets[n].cmd != L'\0'; n++) {
1409 if (cmd == scanf_sets[n].cmd) {
1410 wcscpy(scanf_yset, scanf_sets[n].yset);
1411 wcscpy(scanf_nset, scanf_sets[n].nset);
1412 break;
1417 /* now fill the dynamic string */
1418 while (vsize &&
1419 !wcschr(scanf_nset, *i) &&
1420 (scanf_yset[0] == L'\0' || wcschr(scanf_yset, *i)) &&
1421 (msize == 0 || wcsncmp(i, scanf_mark, msize) != 0)) {
1423 /* only add if not being ignored */
1424 if (!ignore)
1425 ptr = mpdm_poke(ptr, &size, i, 1, sizeof(wchar_t));
1427 i++;
1428 vsize--;
1431 if (!ignore && size) {
1432 /* null terminate and push */
1433 ptr = mpdm_poke(ptr, &size, L"", 1, sizeof(wchar_t));
1434 mpdm_push(r, MPDM_ENS(ptr, size - 1));
1437 else
1438 if (*f == L' ' || *f == L'\t') {
1439 /* if it's a blank, sync to next non-blank */
1440 f++;
1442 while (*i == L' ' || *i == L'\t')
1443 i++;
1445 else
1446 /* test for literals in the format string */
1447 if (*i == *f) {
1448 i++;
1449 f++;
1451 else
1452 break;
1455 mpdm_unref(str);
1456 mpdm_unref(fmt);
1458 mpdm_unrefnd(r);
1460 return r;