Ref/unref all args in mpdm_seek().
[mpdm.git] / mpdm_s.c
blob5a12dbeb6251225e53f85d62ea62ca40cd2be2e4
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, int osize, int esize)
50 if (org != NULL && osize) {
51 /* enough room? */
52 if (*offset + osize > *dsize) {
53 /* no; enlarge */
54 *dsize += osize;
56 dst = realloc(dst, *dsize * esize);
59 memcpy((char *)dst + (*offset * esize), org, osize * esize);
60 *offset += osize;
63 return dst;
67 void *mpdm_poke(void *dst, int *dsize, const void *org, int osize, int esize)
68 /* pokes (adds) org into dst, which is a dynamic string, making it grow */
70 int offset = *dsize;
72 return mpdm_poke_o(dst, dsize, &offset, org, osize, esize);
76 wchar_t *mpdm_pokewsn(wchar_t *dst, int *dsize, const wchar_t *str, int slen)
77 /* adds a wide string to dst using mpdm_poke() with size */
79 if (str)
80 dst = mpdm_poke(dst, dsize, str, slen, sizeof(wchar_t));
82 return dst;
86 wchar_t *mpdm_pokews(wchar_t *dst, int *dsize, const wchar_t *str)
87 /* adds a wide string to dst using mpdm_poke() */
89 if (str)
90 dst = mpdm_pokewsn(dst, dsize, str, wcslen(str));
92 return dst;
96 wchar_t *mpdm_pokev(wchar_t * dst, int *dsize, const mpdm_t v)
97 /* adds the string in v to dst using mpdm_poke() */
99 if (v != NULL) {
100 const wchar_t *ptr = mpdm_string(v);
102 mpdm_ref(v);
103 dst = mpdm_pokews(dst, dsize, ptr);
104 mpdm_unref(v);
107 return dst;
111 wchar_t *mpdm_mbstowcs(const char *str, int *s, int l)
112 /* converts an mbs to a wcs, but filling invalid chars
113 with question marks instead of just failing */
115 wchar_t *ptr = NULL;
116 char tmp[64]; /* really MB_CUR_MAX + 1 */
117 wchar_t wc;
118 int n, i, c, t = 0;
119 char *cstr;
121 /* allow NULL values for s */
122 if (s == NULL)
123 s = &t;
125 /* if there is a limit, duplicate and break the string */
126 if (l >= 0) {
127 cstr = strdup(str);
128 cstr[l] = '\0';
130 else
131 cstr = (char *) str;
133 /* try first a direct conversion with mbstowcs */
134 if ((*s = mbstowcs(NULL, cstr, 0)) != -1) {
135 /* direct conversion is possible; do it */
136 if ((ptr = malloc((*s + 1) * sizeof(wchar_t))) != NULL) {
137 mbstowcs(ptr, cstr, *s);
138 ptr[*s] = L'\0';
141 else {
142 /* zero everything */
143 *s = n = i = 0;
145 for (;;) {
146 /* no more characters to process? */
147 if ((c = cstr[n + i]) == '\0' && i == 0)
148 break;
150 tmp[i++] = c;
151 tmp[i] = '\0';
153 /* try to convert */
154 if (mbstowcs(&wc, tmp, 1) == (size_t) -1) {
155 /* can still be an incomplete multibyte char? */
156 if (c != '\0' && i <= (int) MB_CUR_MAX)
157 continue;
158 else {
159 /* too many failing bytes; skip 1 byte */
160 wc = L'?';
161 i = 1;
165 /* skip used bytes and back again */
166 n += i;
167 i = 0;
169 /* store new char */
170 if ((ptr = mpdm_poke(ptr, s, &wc, 1, sizeof(wchar_t))) == NULL)
171 break;
174 /* null terminate and count one less */
175 if (ptr != NULL) {
176 ptr = mpdm_poke(ptr, s, L"", 1, sizeof(wchar_t));
177 (*s)--;
181 /* free the duplicate */
182 if (cstr != str)
183 free(cstr);
185 return ptr;
189 char *mpdm_wcstombs(const wchar_t * str, int *s)
190 /* converts a wcs to an mbs, but filling invalid chars
191 with question marks instead of just failing */
193 char *ptr = NULL;
194 char tmp[64]; /* really MB_CUR_MAX + 1 */
195 int l, t = 0;
197 /* allow NULL values for s */
198 if (s == NULL)
199 s = &t;
201 /* try first a direct conversion with wcstombs */
202 if ((*s = wcstombs(NULL, str, 0)) != -1) {
203 /* direct conversion is possible; do it and return */
204 if ((ptr = malloc(*s + 1)) != NULL) {
205 wcstombs(ptr, str, *s);
206 ptr[*s] = '\0';
209 return ptr;
212 /* invalid encoding? convert characters one by one */
213 *s = 0;
215 while (*str) {
216 if ((l = wctomb(tmp, *str)) <= 0) {
217 /* if char couldn't be converted,
218 write a question mark instead */
219 l = wctomb(tmp, L'?');
222 tmp[l] = '\0';
223 if ((ptr = mpdm_poke(ptr, s, tmp, l, 1)) == NULL)
224 break;
226 str++;
229 /* null terminate and count one less */
230 if (ptr != NULL) {
231 ptr = mpdm_poke(ptr, s, "", 1, 1);
232 (*s)--;
235 return ptr;
239 mpdm_t mpdm_new_wcs(int flags, const wchar_t * str, int size, int cpy)
240 /* creates a new string value from a wcs */
242 wchar_t *ptr;
244 /* a size of -1 means 'calculate it' */
245 if (size == -1 && str != NULL)
246 size = wcslen(str);
248 /* create a copy? */
249 if (cpy) {
250 /* free() on destruction */
251 flags |= MPDM_FREE;
253 /* allocs */
254 if ((ptr = malloc((size + 1) * sizeof(wchar_t))) == NULL)
255 return NULL;
257 /* if no source, reset to zeroes; otherwise, copy */
258 if (str == NULL)
259 memset(ptr, '\0', size * sizeof(wchar_t));
260 else {
261 wcsncpy(ptr, str, size);
262 ptr[size] = L'\0';
265 else
266 ptr = (wchar_t *)str;
268 /* it's a string */
269 flags |= MPDM_STRING;
271 return mpdm_new(flags, ptr, size);
275 mpdm_t mpdm_new_mbstowcs(int flags, const char *str, int l)
276 /* creates a new string value from an mbs */
278 wchar_t *ptr;
279 int size;
281 if ((ptr = mpdm_mbstowcs(str, &size, l)) == NULL)
282 return NULL;
284 /* it's a string */
285 flags |= (MPDM_STRING | MPDM_FREE);
287 return mpdm_new(flags, ptr, size);
291 mpdm_t mpdm_new_wcstombs(int flags, const wchar_t * str)
292 /* creates a new mbs value from a wbs */
294 char *ptr;
295 int size;
297 ptr = mpdm_wcstombs(str, &size);
299 flags |= MPDM_FREE;
301 /* unset the string flag; mbs,s are not 'strings' */
302 flags &= ~MPDM_STRING;
304 return mpdm_new(flags, ptr, size);
308 mpdm_t mpdm_new_i(int ival)
309 /* creates a new string value from an integer */
311 mpdm_t v;
312 char tmp[32];
314 /* creates the visual representation */
315 snprintf(tmp, sizeof(tmp), "%d", ival);
317 v = MPDM_MBS(tmp);
319 return mpdm_set_ival(v, ival);
323 mpdm_t mpdm_new_r(double rval)
324 /* creates a new string value from a real number */
326 mpdm_t v;
327 char tmp[128];
329 /* creates the visual representation */
330 snprintf(tmp, sizeof(tmp), "%lf", rval);
332 /* manually strip useless zeroes */
333 if (strchr(tmp, '.') != NULL) {
334 char *ptr;
336 for (ptr = tmp + strlen(tmp) - 1; *ptr == '0'; ptr--);
338 /* if it's over the ., strip it also */
339 if (*ptr != '.')
340 ptr++;
342 *ptr = '\0';
345 v = MPDM_MBS(tmp);
347 return mpdm_set_rval(v, rval);
351 /* interface */
354 * mpdm_string - Returns a printable representation of a value.
355 * @v: the value
357 * Returns a printable representation of a value. For strings, it's
358 * the value data itself; for any other type, a conversion to string
359 * is returned instead. This value should be used immediately, as it
360 * can be a pointer to a static buffer.
361 * [Strings]
363 wchar_t *mpdm_string(const mpdm_t v)
365 static wchar_t wtmp[32];
366 char tmp[32];
367 static wchar_t *ret;
369 /* if it's NULL, return a constant */
370 if (v == NULL)
371 ret = L"[NULL]";
372 else
373 /* if it's a string, return it */
374 if (v->flags & MPDM_STRING)
375 ret = (wchar_t *) v->data;
376 else {
377 /* otherwise, return a visual representation */
378 snprintf(tmp, sizeof(tmp), "%p", v);
379 mbstowcs(wtmp, tmp, sizeof(wtmp));
380 wtmp[(sizeof(wtmp) / sizeof(wchar_t)) - 1] = L'\0';
382 ret = wtmp;
385 return ret;
390 * mpdm_cmp - Compares two values.
391 * @v1: the first value
392 * @v2: the second value
394 * Compares two values. If both has the MPDM_STRING flag set,
395 * a comparison using wcscoll() is returned; if both are arrays,
396 * the size is compared first and, if they have the same number
397 * elements, each one is compared; otherwise, a simple pointer
398 * comparison is done.
399 * [Strings]
401 int mpdm_cmp(const mpdm_t v1, const mpdm_t v2)
403 int r;
405 /* same values? */
406 if (v1 == v2)
407 r = 0;
408 else
409 /* is any value NULL? */
410 if (v1 == NULL)
411 r = -1;
412 else
413 if (v2 == NULL)
414 r = 1;
415 else
416 /* different values, but same content? (unlikely) */
417 if (v1->data == v2->data)
418 r = 0;
419 else
420 if (MPDM_IS_STRING(v1) && MPDM_IS_STRING(v2))
421 r = wcscoll((wchar_t *) v1->data, (wchar_t *) v2->data);
422 else
423 if (MPDM_IS_ARRAY(v1) && MPDM_IS_ARRAY(v2)) {
424 /* compare first the sizes */
425 if ((r = mpdm_size(v1) - mpdm_size(v2)) == 0) {
426 int n;
428 /* they have the same size;
429 compare each pair of elements */
430 for (n = 0; n < mpdm_size(v1); n++) {
431 if ((r = mpdm_cmp(mpdm_aget(v1, n),
432 mpdm_aget(v2, n))) != 0)
433 break;
437 else
438 /* in any other case, compare just pointers */
439 r = (int) ((char *)v1->data - (char *)v2->data);
441 return r;
446 * mpdm_cmp_s - Compares two values (string version).
447 * @v1: the first value
448 * @v2: the second value
450 * Compares two values. Compares both values using wcscoll()
451 * if the first one is a string, or returns 1 otherwise.
453 int mpdm_cmp_s(const mpdm_t v1, const wchar_t *v2)
455 int r;
457 mpdm_ref(v1);
459 if (MPDM_IS_STRING(v1))
460 r = wcscoll((wchar_t *) v1->data, v2);
461 else
462 r = (int) ((wchar_t *)v1->data - v2);
464 mpdm_unref(v1);
466 return r;
471 * mpdm_splice - Creates a new string value from another.
472 * @v: the original value
473 * @i: the value to be inserted
474 * @offset: offset where the substring is to be inserted
475 * @del: number of characters to delete
477 * Creates a new string value from @v, deleting @del chars at @offset
478 * and substituting them by @i. If @del is 0, no deletion is done.
479 * both @offset and @del can be negative; if this is the case, it's
480 * assumed as counting from the end of @v. If @v is NULL, @i will become
481 * the new string, and both @offset and @del will be ignored. If @v is
482 * not NULL and @i is, no insertion process is done (only deletion, if
483 * applicable).
485 * Returns a two element array, with the new string in the first
486 * element and the deleted string in the second (with a NULL value
487 * if @del is 0).
488 * [Strings]
490 mpdm_t mpdm_splice(const mpdm_t v, const mpdm_t i, int offset, int del)
492 mpdm_t w;
493 mpdm_t n = NULL;
494 mpdm_t d = NULL;
495 int os, ns, r;
496 int ins = 0;
497 wchar_t *ptr;
499 mpdm_ref(v);
500 mpdm_ref(i);
502 if (v != NULL) {
503 os = mpdm_size(v);
505 /* negative offsets start from the end */
506 if (offset < 0)
507 offset = os + 1 - offset;
509 /* never add further the end */
510 if (offset > os)
511 offset = os;
513 /* negative del counts as 'characters left' */
514 if (del < 0)
515 del = os + 1 - offset + del;
517 /* something to delete? */
518 if (del > 0) {
519 /* never delete further the end */
520 if (offset + del > os)
521 del = os - offset;
523 /* deleted string */
524 d = MPDM_NS(((wchar_t *) v->data) + offset, del);
526 else
527 del = 0;
529 /* something to insert? */
530 ins = mpdm_size(i);
532 /* new size and remainder */
533 ns = os + ins - del;
534 r = offset + del;
536 n = MPDM_NS(NULL, ns);
538 ptr = (wchar_t *)n->data;
540 /* copy the beginning */
541 if (offset > 0) {
542 wcsncpy(ptr, v->data, offset);
543 ptr += offset;
546 /* copy the text to be inserted */
547 if (ins > 0) {
548 wcsncpy(ptr, i->data, ins);
549 ptr += ins;
552 /* copy the remaining */
553 os -= r;
554 if (os > 0) {
555 wcsncpy(ptr, ((wchar_t *) v->data) + r, os);
556 ptr += os;
559 /* null terminate */
560 *ptr = L'\0';
562 else
563 n = i;
565 /* creates the output array */
566 w = MPDM_A(2);
568 mpdm_aset(w, n, 0);
569 mpdm_aset(w, d, 1);
571 mpdm_unref(i);
572 mpdm_unref(v);
574 return w;
579 * mpdm_strcat_sn - Concatenates two strings (string with size version).
580 * @s1: the first string
581 * @s2: the second string
582 * @size: the size of the second string
584 * Returns a new string formed by the concatenation of @s1 and @s2.
585 * [Strings]
587 mpdm_t mpdm_strcat_sn(const mpdm_t s1, const wchar_t *s2, int size)
589 wchar_t *ptr = NULL;
590 int s = 0;
591 mpdm_t r;
593 if (s1 == NULL && s2 == NULL)
594 r = NULL;
595 else {
596 ptr = mpdm_pokev(ptr, &s, s1);
597 ptr = mpdm_pokewsn(ptr, &s, s2, size);
599 ptr = mpdm_poke(ptr, &s, L"", 1, sizeof(wchar_t));
600 r = MPDM_ENS(ptr, s - 1);
603 return r;
608 * mpdm_strcat_s - Concatenates two strings (string version).
609 * @s1: the first string
610 * @s2: the second string
612 * Returns a new string formed by the concatenation of @s1 and @s2.
613 * [Strings]
615 mpdm_t mpdm_strcat_s(const mpdm_t s1, const wchar_t *s2)
617 return mpdm_strcat_sn(s1, s2, s2 ? wcslen(s2) : 0);
622 * mpdm_strcat - Concatenates two strings.
623 * @s1: the first string
624 * @s2: the second string
626 * Returns a new string formed by the concatenation of @s1 and @s2.
627 * [Strings]
629 mpdm_t mpdm_strcat(const mpdm_t s1, const mpdm_t s2)
631 mpdm_t r;
633 mpdm_ref(s2);
634 r = mpdm_strcat_s(s1, s2 ? mpdm_string(s2) : NULL);
635 mpdm_unref(s2);
637 return r;
642 * mpdm_ival - Returns a value's data as an integer.
643 * @v: the value
645 * Returns a value's data as an integer. If the value is a string,
646 * it's converted via sscanf and returned; non-string values have all
647 * an ival of 0. The converted integer is cached, so costly string
648 * conversions are only done once. Values created with the MPDM_IVAL
649 * flag set have its ival cached from the beginning.
650 * [Strings]
651 * [Value Management]
653 int mpdm_ival(mpdm_t v)
655 int i = 0;
657 mpdm_ref(v);
659 if (v != NULL) {
660 /* if there is no cached integer, calculate it */
661 if (!(v->flags & MPDM_IVAL)) {
662 /* if it's a string, calculate it; other
663 values will have an ival of 0 */
664 if (v->flags & MPDM_STRING) {
665 char tmp[32];
666 char *fmt = "%i";
668 wcstombs(tmp, (wchar_t *) v->data, sizeof(tmp));
669 tmp[sizeof(tmp) - 1] = '\0';
671 /* workaround for mingw32: as it doesn't
672 correctly parse octal and hexadecimal
673 numbers, they are tried as special cases */
674 if (tmp[0] == '0') {
675 if (tmp[1] == 'b' || tmp[1] == 'B') {
676 /* binary number */
677 fmt = NULL;
678 char *ptr = &tmp[2];
680 while (*ptr == '0' || *ptr == '1') {
681 i <<= 1;
683 if (*ptr == '1')
684 i |= 1;
686 ptr++;
689 else
690 if (tmp[1] == 'x' || tmp[1] == 'X')
691 fmt = "%x";
692 else
693 fmt = "%o";
696 if (fmt != NULL)
697 sscanf(tmp, fmt, &i);
700 mpdm_set_ival(v, i);
703 i = v->ival;
706 mpdm_unref(v);
708 return i;
713 * mpdm_rval - Returns a value's data as a real number (double).
714 * @v: the value
716 * Returns a value's data as a real number (double float). If the value
717 * is a string, it's converted via sscanf and returned; non-string values
718 * have all an rval of 0. The converted double is cached, so costly string
719 * conversions are only done once. Values created with the MPDM_RVAL
720 * flag set have its rval cached from the beginning.
721 * [Strings]
722 * [Value Management]
724 double mpdm_rval(mpdm_t v)
726 double r = 0.0;
728 mpdm_ref(v);
730 if (v != NULL) {
731 /* if there is no cached double, calculate it */
732 if (!(v->flags & MPDM_RVAL)) {
733 /* if it's a string, calculate it; other
734 values will have an rval of 0.0 */
735 if (v->flags & MPDM_STRING) {
736 char tmp[128];
737 char *prev_locale;
739 wcstombs(tmp, (wchar_t *) v->data, sizeof(tmp));
740 tmp[sizeof(tmp) - 1] = '\0';
742 /* if the number starts with 0, it's
743 an octal or hexadecimal number; just
744 take the integer value and cast it */
745 if (tmp[0] == '0' && tmp[1] != '.')
746 r = (double) mpdm_ival(v);
747 else {
748 /* set locale to C for non locale-dependent
749 floating point conversion */
750 prev_locale = setlocale(LC_NUMERIC, "C");
752 /* read */
753 sscanf(tmp, "%lf", &r);
755 /* set previous locale */
756 setlocale(LC_NUMERIC, prev_locale);
760 mpdm_set_rval(v, r);
763 r = v->rval;
766 mpdm_unref(v);
768 return r;
773 * mpdm_gettext - Translates a string to the current language.
774 * @str: the string
776 * Translates the @str string to the current language.
778 * This function can still be used even if there is no real gettext
779 * support() by manually filling the __I18N__ hash.
781 * If the string is found in the current table, the translation is
782 * returned; otherwise, the same @str value is returned.
783 * [Strings]
784 * [Localization]
786 mpdm_t mpdm_gettext(const mpdm_t str)
788 mpdm_t v;
789 mpdm_t i18n = NULL;
791 /* gets the cache */
792 if ((i18n = mpdm_hget_s(mpdm_root(), L"__I18N__")) == NULL)
793 i18n = mpdm_hset_s(mpdm_root(), L"__I18N__", MPDM_H(0));
795 mpdm_ref(str);
797 /* try first the cache */
798 if ((v = mpdm_hget(i18n, str)) == NULL) {
799 #ifdef CONFOPT_GETTEXT
800 char *s;
801 mpdm_t t;
803 /* convert to mbs */
804 t = mpdm_ref(MPDM_2MBS(str->data));
806 /* ask gettext for it */
807 s = gettext((char *) t->data);
809 if (s != t->data)
810 v = MPDM_MBS(s);
811 else
812 v = str;
814 mpdm_unref(t);
816 #else /* CONFOPT_GETTEXT */
818 v = str;
820 #endif /* CONFOPT_GETTEXT */
822 /* store in the cache */
823 mpdm_hset(i18n, str, v);
826 mpdm_unref(str);
828 return v;
833 * mpdm_gettext_domain - Sets domain and data directory for translations.
834 * @dom: the domain (application name)
835 * @data: directory contaning the .mo files
837 * Sets the domain (application name) and translation data for translating
838 * strings that will be returned by mpdm_gettext().@data must point to a
839 * directory containing the .mo (compiled .po) files.
841 * If there is no gettext support, returns 0, or 1 otherwise.
842 * [Strings]
843 * [Localization]
845 int mpdm_gettext_domain(const mpdm_t dom, const mpdm_t data)
847 int ret = 0;
849 mpdm_ref(dom);
850 mpdm_ref(data);
852 #ifdef CONFOPT_GETTEXT
854 mpdm_t dm;
855 mpdm_t dt;
857 /* convert both to mbs,s */
858 dm = mpdm_ref(MPDM_2MBS(dom->data));
859 dt = mpdm_ref(MPDM_2MBS(data->data));
861 /* bind and set domain */
862 bindtextdomain((char *) dm->data, (char *) dt->data);
863 textdomain((char *) dm->data);
865 mpdm_hset_s(mpdm_root(), L"__I18N__", MPDM_H(0));
867 mpdm_unref(dt);
868 mpdm_unref(dm);
870 ret = 1;
872 #endif /* CONFOPT_GETTEXT */
874 #ifdef CONFOPT_WIN32
876 mpdm_t v;
878 if ((v = mpdm_hget_s(mpdm_root(), L"ENV")) != NULL &&
879 mpdm_hget_s(v, L"LANG") == NULL) {
880 wchar_t *wptr = L"en";
882 /* MS Windows crappy language constants... */
884 switch((GetSystemDefaultLangID() & 0x00ff)) {
885 case 0x01: wptr = L"ar"; break; /* arabic */
886 case 0x02: wptr = L"bg"; break; /* bulgarian */
887 case 0x03: wptr = L"ca"; break; /* catalan */
888 case 0x04: wptr = L"zh"; break; /* chinese */
889 case 0x05: wptr = L"cz"; break; /* czech */
890 case 0x06: wptr = L"da"; break; /* danish */
891 case 0x07: wptr = L"de"; break; /* german */
892 case 0x08: wptr = L"el"; break; /* greek */
893 case 0x09: wptr = L"en"; break; /* english */
894 case 0x0a: wptr = L"es"; break; /* spanish */
895 case 0x0b: wptr = L"fi"; break; /* finnish */
896 case 0x0c: wptr = L"fr"; break; /* french */
897 case 0x0d: wptr = L"he"; break; /* hebrew */
898 case 0x0e: wptr = L"hu"; break; /* hungarian */
899 case 0x0f: wptr = L"is"; break; /* icelandic */
900 case 0x10: wptr = L"it"; break; /* italian */
901 case 0x11: wptr = L"jp"; break; /* japanese */
902 case 0x12: wptr = L"ko"; break; /* korean */
903 case 0x13: wptr = L"nl"; break; /* dutch */
904 case 0x14: wptr = L"no"; break; /* norwegian */
905 case 0x15: wptr = L"po"; break; /* polish */
906 case 0x16: wptr = L"pt"; break; /* portuguese */
907 case 0x17: wptr = L"rm"; break; /* romansh (switzerland) */
908 case 0x18: wptr = L"ro"; break; /* romanian */
909 case 0x19: wptr = L"ru"; break; /* russian */
910 case 0x1a: wptr = L"sr"; break; /* serbian */
911 case 0x1b: wptr = L"sk"; break; /* slovak */
912 case 0x1c: wptr = L"sq"; break; /* albanian */
913 case 0x1d: wptr = L"sv"; break; /* swedish */
916 mpdm_hset_s(v, L"LANG", MPDM_S(wptr));
919 #endif /* CONFOPT_WIN32 */
921 mpdm_unref(data);
922 mpdm_unref(dom);
924 return ret;
928 #ifdef CONFOPT_WCWIDTH
930 int wcwidth(wchar_t);
932 int mpdm_wcwidth(wchar_t c)
934 return wcwidth(c);
937 #else /* CONFOPT_WCWIDTH */
939 #include "wcwidth.c"
941 int mpdm_wcwidth(wchar_t c)
943 return mk_wcwidth(c);
946 #endif /* CONFOPT_WCWIDTH */
950 * mpdm_sprintf - Formats a sprintf()-like string.
951 * @fmt: the string format
952 * @args: an array of values
954 * Formats a string using the sprintf() format taking the values from @args.
955 * [Strings]
957 mpdm_t mpdm_sprintf(const mpdm_t fmt, const mpdm_t args)
959 const wchar_t *i = fmt->data;
960 wchar_t *o = NULL;
961 int l = 0, n = 0;
962 wchar_t c;
964 mpdm_ref(fmt);
965 mpdm_ref(args);
967 /* loop all characters */
968 while ((c = *i++) != L'\0') {
969 int m = 0;
970 wchar_t *tptr = NULL;
971 wchar_t *wptr = NULL;
973 if (c == L'%') {
974 /* format directive */
975 char t_fmt[128];
976 char tmp[1024];
977 mpdm_t v;
978 char *ptr = NULL;
980 /* transfer the % */
981 t_fmt[m++] = '%';
983 /* transform the format to mbs */
984 while (*i != L'\0' &&
985 m < (int)(sizeof(t_fmt) - MB_CUR_MAX - 1) &&
986 wcschr(L"-.0123456789", *i) != NULL)
987 m += wctomb(&t_fmt[m], *i++);
989 /* transfer the directive */
990 m += wctomb(&t_fmt[m], *i++);
992 t_fmt[m] = '\0';
994 /* by default, copies the format */
995 strcpy(tmp, t_fmt);
997 /* pick next value */
998 v = mpdm_aget(args, n++);
1000 switch (t_fmt[m - 1]) {
1001 case 'd':
1002 case 'i':
1003 case 'u':
1004 case 'x':
1005 case 'X':
1006 case 'o':
1008 /* integer value */
1009 snprintf(tmp, sizeof(tmp) - 1,
1010 t_fmt, mpdm_ival(v));
1011 break;
1013 case 'f':
1015 /* float (real) value */
1016 snprintf(tmp, sizeof(tmp) - 1,
1017 t_fmt, mpdm_rval(v));
1018 break;
1020 case 's':
1022 /* string value */
1023 ptr = mpdm_wcstombs(mpdm_string(v), NULL);
1024 snprintf(tmp, sizeof(tmp) - 1, t_fmt, ptr);
1025 free(ptr);
1027 break;
1029 case 'c':
1031 /* char */
1032 m = 1;
1033 wptr = &c;
1034 c = mpdm_ival(v);
1035 break;
1037 case 'b':
1039 ptr = tmp;
1040 unsigned int mask;
1041 int p = 0;
1043 mask = 1 << ((sizeof(int) * 8) - 1);
1044 while (mask) {
1045 if (mask & (unsigned int) mpdm_ival(v)) {
1046 *ptr++ = '1';
1047 p = 1;
1049 else
1050 if (p)
1051 *ptr++ = '0';
1053 mask >>= 1;
1056 if (ptr == tmp)
1057 *ptr++ = '0';
1059 *ptr = '\0';
1060 break;
1062 case '%':
1064 /* percent sign */
1065 m = 1;
1066 wptr = &c;
1067 break;
1070 /* transfer */
1071 if (wptr == NULL)
1072 wptr = tptr = mpdm_mbstowcs(tmp, &m, -1);
1074 else {
1075 /* raw character */
1076 m = 1;
1077 wptr = &c;
1080 /* transfer */
1081 o = mpdm_poke(o, &l, wptr, m, sizeof(wchar_t));
1083 /* free the temporary buffer, if any */
1084 if (tptr != NULL)
1085 free(tptr);
1088 if (o == NULL)
1089 return NULL;
1091 /* null-terminate */
1092 o = mpdm_poke(o, &l, L"", 1, sizeof(wchar_t));
1094 mpdm_unref(args);
1095 mpdm_unref(fmt);
1097 return MPDM_ENS(o, l - 1);
1102 * mpdm_ulc - Converts a string to uppercase or lowecase.
1103 * @s: the string
1104 * @u: convert to uppercase (1) or to lowercase (0).
1106 * Converts @s to uppercase (for @u == 1) or to lowercase (@u == 0).
1107 * [Strings]
1109 mpdm_t mpdm_ulc(const mpdm_t s, int u)
1111 mpdm_t r = NULL;
1112 wchar_t *optr;
1113 int i;
1115 mpdm_ref(s);
1117 i = mpdm_size(s);
1119 if ((optr = malloc((i + 1) * sizeof(wchar_t))) != NULL) {
1120 wchar_t *iptr = mpdm_string(s);
1121 int n;
1123 for (n = 0; n < i; n++)
1124 optr[n] = u ? towupper(iptr[n]) : towlower(iptr[n]);
1126 optr[n] = L'\0';
1127 r = MPDM_ENS(optr, i);
1130 mpdm_unref(s);
1132 return r;
1136 /* scanf working buffers */
1137 #define SCANF_BUF_SIZE 1024
1138 static wchar_t scanf_yset[SCANF_BUF_SIZE];
1139 static wchar_t scanf_nset[SCANF_BUF_SIZE];
1140 static wchar_t scanf_mark[SCANF_BUF_SIZE];
1142 struct {
1143 wchar_t cmd;
1144 wchar_t *yset;
1145 wchar_t *nset;
1146 } scanf_sets[] = {
1147 { L's', L"", L" \t" },
1148 { L'u', L"0123456789", L"" },
1149 { L'd', L"-0123456789", L"" },
1150 { L'i', L"-0123456789", L"" },
1151 { L'f', L"-0123456789.", L"" },
1152 { L'x', L"-0123456789xabcdefABCDEF", L"" },
1153 { L'\0', NULL, NULL },
1157 * mpdm_sscanf - Extracts data like sscanf().
1158 * @fmt: the string format
1159 * @str: the string to be parsed
1160 * @offset: the character offset to start scanning
1162 * Extracts data from a string using a special format pattern, very
1163 * much like the scanf() series of functions in the C library. Apart
1164 * from the standard percent-sign-commands (s, u, d, i, f, x,
1165 * n, [, with optional size and * to ignore), it implements S,
1166 * to match a string of characters upto what follows in the format
1167 * string. Also, the [ set of characters can include other % formats.
1169 * Returns an array with the extracted values. If %n is used, the
1170 * position in the scanned string is returned as the value.
1171 * [Strings]
1173 mpdm_t mpdm_sscanf(const mpdm_t fmt, const mpdm_t str, int offset)
1175 wchar_t *i = (wchar_t *)str->data;
1176 wchar_t *f = (wchar_t *)fmt->data;
1177 mpdm_t r;
1179 mpdm_ref(fmt);
1180 mpdm_ref(str);
1182 i += offset;
1183 r = MPDM_A(0);
1185 while (*f) {
1186 if (*f == L'%') {
1187 wchar_t *ptr = NULL;
1188 int size = 0;
1189 wchar_t cmd;
1190 int vsize = 0;
1191 int ignore = 0;
1192 int msize = 0;
1194 /* empty all buffers */
1195 scanf_yset[0] = scanf_nset[0] = scanf_mark[0] = L'\0';
1197 f++;
1199 /* an asterisk? don't return next value */
1200 if (*f == L'*') {
1201 ignore = 1;
1202 f++;
1205 /* does it have a size? */
1206 while (wcschr(L"0123456789", *f)) {
1207 vsize *= 10;
1208 vsize += *f - L'0';
1209 f++;
1212 /* if no size, set it to an arbitrary big limit */
1213 if (!vsize)
1214 vsize = 0xfffffff;
1216 /* now *f should contain a command */
1217 cmd = *f;
1218 f++;
1220 /* is it a verbatim percent sign? */
1221 if (cmd == L'%') {
1222 vsize = 1;
1223 ignore = 1;
1224 wcscpy(scanf_yset, L"%");
1226 else
1227 /* a position? */
1228 if (cmd == L'n') {
1229 vsize = 0;
1230 ignore = 1;
1231 mpdm_push(r, MPDM_I(i - (wchar_t *)str->data));
1233 else
1234 /* string upto a mark */
1235 if (cmd == L'S') {
1236 wchar_t *tmp = f;
1238 /* fill the mark upto another command */
1239 while (*tmp) {
1240 if (*tmp == L'%') {
1241 tmp++;
1243 /* is it an 'n'? ignore and go on */
1244 if (*tmp == L'n') {
1245 tmp++;
1246 continue;
1248 else
1249 if (*tmp == L'%')
1250 scanf_mark[msize++] = *tmp;
1251 else
1252 break;
1254 else
1255 scanf_mark[msize++] = *tmp;
1257 tmp++;
1260 scanf_mark[msize] = L'\0';
1262 else
1263 /* raw set */
1264 if (cmd == L'[') {
1265 int n = 0;
1266 wchar_t *set = scanf_yset;
1268 /* is it an inverse set? */
1269 if (*f == L'^') {
1270 set = scanf_nset;
1271 f++;
1274 /* first one is a ]? add it */
1275 if (*f == L']') {
1276 set[n++] = *f;
1277 f++;
1280 /* now build the set */
1281 for (; n < SCANF_BUF_SIZE - 1 && *f && *f != L']'; f++) {
1282 /* is it a range? */
1283 if (*f == L'-') {
1284 f++;
1286 /* start or end? hyphen itself */
1287 if (n == 0 || *f == L']')
1288 set[n++] = L'-';
1289 else {
1290 /* pick previous char */
1291 wchar_t c = set[n - 1];
1293 /* fill */
1294 while (n < SCANF_BUF_SIZE - 1 && c < *f)
1295 set[n++] = ++c;
1298 else
1299 /* is it another command? */
1300 if (*f == L'%') {
1301 int i;
1303 f++;
1304 for (i = 0; scanf_sets[i].cmd; i++) {
1305 if (*f == scanf_sets[i].cmd) {
1306 set[n] = L'\0';
1307 wcscat(set, scanf_sets[i].yset);
1308 n += wcslen(scanf_sets[i].yset);
1309 break;
1313 else
1314 set[n++] = *f;
1317 /* skip the ] */
1318 f++;
1320 set[n] = L'\0';
1322 else
1323 /* a standard set? */
1325 int n;
1327 for (n = 0; scanf_sets[n].cmd != L'\0'; n++) {
1328 if (cmd == scanf_sets[n].cmd) {
1329 wcscpy(scanf_yset, scanf_sets[n].yset);
1330 wcscpy(scanf_nset, scanf_sets[n].nset);
1331 break;
1336 /* now fill the dynamic string */
1337 while (vsize &&
1338 !wcschr(scanf_nset, *i) &&
1339 (scanf_yset[0] == L'\0' || wcschr(scanf_yset, *i)) &&
1340 (msize == 0 || wcsncmp(i, scanf_mark, msize) != 0)) {
1342 /* only add if not being ignored */
1343 if (!ignore)
1344 ptr = mpdm_poke(ptr, &size, i, 1, sizeof(wchar_t));
1346 i++;
1347 vsize--;
1350 if (!ignore && size) {
1351 /* null terminate and push */
1352 ptr = mpdm_poke(ptr, &size, L"", 1, sizeof(wchar_t));
1353 mpdm_push(r, MPDM_ENS(ptr, size - 1));
1356 else
1357 if (*f == L' ' || *f == L'\t') {
1358 /* if it's a blank, sync to next non-blank */
1359 f++;
1361 while (*i == L' ' || *i == L'\t')
1362 i++;
1364 else
1365 /* test for literals in the format string */
1366 if (*i == *f) {
1367 i++;
1368 f++;
1370 else
1371 break;
1374 mpdm_unref(str);
1375 mpdm_unref(fmt);
1377 return r;