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
35 #ifdef CONFOPT_GETTEXT
48 void *mpdm_poke_o(void *dst
, int *dsize
, int *offset
, const void *org
, int osize
, int esize
)
50 if (org
!= NULL
&& osize
) {
52 if (*offset
+ osize
> *dsize
) {
56 dst
= realloc(dst
, *dsize
* esize
);
59 memcpy((char *)dst
+ (*offset
* esize
), org
, osize
* esize
);
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 */
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 */
80 dst
= mpdm_poke(dst
, dsize
, str
, slen
, sizeof(wchar_t));
86 wchar_t *mpdm_pokews(wchar_t *dst
, int *dsize
, const wchar_t *str
)
87 /* adds a wide string to dst using mpdm_poke() */
90 dst
= mpdm_pokewsn(dst
, dsize
, str
, wcslen(str
));
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() */
100 const wchar_t *ptr
= mpdm_string(v
);
103 dst
= mpdm_pokews(dst
, dsize
, ptr
);
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 */
116 char tmp
[64]; /* really MB_CUR_MAX + 1 */
121 /* allow NULL values for s */
125 /* if there is a limit, duplicate and break the string */
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
);
142 /* zero everything */
146 /* no more characters to process? */
147 if ((c
= cstr
[n
+ i
]) == '\0' && i
== 0)
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
)
159 /* too many failing bytes; skip 1 byte */
165 /* skip used bytes and back again */
170 if ((ptr
= mpdm_poke(ptr
, s
, &wc
, 1, sizeof(wchar_t))) == NULL
)
174 /* null terminate and count one less */
176 ptr
= mpdm_poke(ptr
, s
, L
"", 1, sizeof(wchar_t));
181 /* free the duplicate */
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 */
194 char tmp
[64]; /* really MB_CUR_MAX + 1 */
197 /* allow NULL values for s */
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
);
212 /* invalid encoding? convert characters one by one */
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
'?');
223 if ((ptr
= mpdm_poke(ptr
, s
, tmp
, l
, 1)) == NULL
)
229 /* null terminate and count one less */
231 ptr
= mpdm_poke(ptr
, s
, "", 1, 1);
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 */
244 /* a size of -1 means 'calculate it' */
245 if (size
== -1 && str
!= NULL
)
250 /* free() on destruction */
254 if ((ptr
= malloc((size
+ 1) * sizeof(wchar_t))) == NULL
)
257 /* if no source, reset to zeroes; otherwise, copy */
259 memset(ptr
, '\0', size
* sizeof(wchar_t));
261 wcsncpy(ptr
, str
, size
);
266 ptr
= (wchar_t *)str
;
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 */
281 if ((ptr
= mpdm_mbstowcs(str
, &size
, l
)) == NULL
)
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 */
297 ptr
= mpdm_wcstombs(str
, &size
);
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 */
314 /* creates the visual representation */
315 snprintf(tmp
, sizeof(tmp
), "%d", ival
);
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 */
329 /* creates the visual representation */
330 snprintf(tmp
, sizeof(tmp
), "%lf", rval
);
332 /* manually strip useless zeroes */
333 if (strchr(tmp
, '.') != NULL
) {
336 for (ptr
= tmp
+ strlen(tmp
) - 1; *ptr
== '0'; ptr
--);
338 /* if it's over the ., strip it also */
347 return mpdm_set_rval(v
, rval
);
354 * mpdm_string - Returns a printable representation of a 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.
363 wchar_t *mpdm_string(const mpdm_t v
)
365 static wchar_t wtmp
[32];
369 /* if it's NULL, return a constant */
373 /* if it's a string, return it */
374 if (v
->flags
& MPDM_STRING
)
375 ret
= (wchar_t *) v
->data
;
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';
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.
401 int mpdm_cmp(const mpdm_t v1
, const mpdm_t v2
)
412 /* is any value NULL? */
419 /* different values, but same content? (unlikely) */
420 if (v1
->data
== v2
->data
)
423 if (MPDM_IS_STRING(v1
) && MPDM_IS_STRING(v2
))
424 r
= wcscoll((wchar_t *) v1
->data
, (wchar_t *) v2
->data
);
426 if (MPDM_IS_ARRAY(v1
) && MPDM_IS_ARRAY(v2
)) {
427 /* compare first the sizes */
428 if ((r
= mpdm_size(v1
) - mpdm_size(v2
)) == 0) {
431 /* they have the same size;
432 compare each pair of elements */
433 for (n
= 0; n
< mpdm_size(v1
); n
++) {
434 if ((r
= mpdm_cmp(mpdm_aget(v1
, n
),
435 mpdm_aget(v2
, n
))) != 0)
441 /* in any other case, compare just pointers */
442 r
= (int) ((char *)v1
->data
- (char *)v2
->data
);
452 * mpdm_cmp_s - Compares two values (string version).
453 * @v1: the first value
454 * @v2: the second value
456 * Compares two values. Compares both values using wcscoll()
457 * if the first one is a string, or returns 1 otherwise.
459 int mpdm_cmp_s(const mpdm_t v1
, const wchar_t *v2
)
465 if (MPDM_IS_STRING(v1
))
466 r
= wcscoll((wchar_t *) v1
->data
, v2
);
468 r
= (int) ((wchar_t *)v1
->data
- v2
);
477 * mpdm_splice - Creates a new string value from another.
478 * @v: the original value
479 * @i: the value to be inserted
480 * @offset: offset where the substring is to be inserted
481 * @del: number of characters to delete
483 * Creates a new string value from @v, deleting @del chars at @offset
484 * and substituting them by @i. If @del is 0, no deletion is done.
485 * both @offset and @del can be negative; if this is the case, it's
486 * assumed as counting from the end of @v. If @v is NULL, @i will become
487 * the new string, and both @offset and @del will be ignored. If @v is
488 * not NULL and @i is, no insertion process is done (only deletion, if
491 * Returns a two element array, with the new string in the first
492 * element and the deleted string in the second (with a NULL value
496 mpdm_t
mpdm_splice(const mpdm_t v
, const mpdm_t i
, int offset
, int del
)
511 /* negative offsets start from the end */
513 offset
= os
+ 1 - offset
;
515 /* never add further the end */
519 /* negative del counts as 'characters left' */
521 del
= os
+ 1 - offset
+ del
;
523 /* something to delete? */
525 /* never delete further the end */
526 if (offset
+ del
> os
)
530 d
= MPDM_NS(((wchar_t *) v
->data
) + offset
, del
);
535 /* something to insert? */
538 /* new size and remainder */
542 n
= MPDM_NS(NULL
, ns
);
544 ptr
= (wchar_t *)n
->data
;
546 /* copy the beginning */
548 wcsncpy(ptr
, v
->data
, offset
);
552 /* copy the text to be inserted */
554 wcsncpy(ptr
, i
->data
, ins
);
558 /* copy the remaining */
561 wcsncpy(ptr
, ((wchar_t *) v
->data
) + r
, os
);
571 /* creates the output array */
587 * mpdm_strcat_sn - Concatenates two strings (string with size version).
588 * @s1: the first string
589 * @s2: the second string
590 * @size: the size of the second string
592 * Returns a new string formed by the concatenation of @s1 and @s2.
595 mpdm_t
mpdm_strcat_sn(const mpdm_t s1
, const wchar_t *s2
, int size
)
601 if (s1
== NULL
&& s2
== NULL
)
604 ptr
= mpdm_pokev(ptr
, &s
, s1
);
605 ptr
= mpdm_pokewsn(ptr
, &s
, s2
, size
);
607 ptr
= mpdm_poke(ptr
, &s
, L
"", 1, sizeof(wchar_t));
608 r
= MPDM_ENS(ptr
, s
- 1);
616 * mpdm_strcat_s - Concatenates two strings (string version).
617 * @s1: the first string
618 * @s2: the second string
620 * Returns a new string formed by the concatenation of @s1 and @s2.
623 mpdm_t
mpdm_strcat_s(const mpdm_t s1
, const wchar_t *s2
)
625 return mpdm_strcat_sn(s1
, s2
, s2
? wcslen(s2
) : 0);
630 * mpdm_strcat - Concatenates two strings.
631 * @s1: the first string
632 * @s2: the second string
634 * Returns a new string formed by the concatenation of @s1 and @s2.
637 mpdm_t
mpdm_strcat(const mpdm_t s1
, const mpdm_t s2
)
642 r
= mpdm_strcat_s(s1
, s2
? mpdm_string(s2
) : NULL
);
650 * mpdm_ival - Returns a value's data as an integer.
653 * Returns a value's data as an integer. If the value is a string,
654 * it's converted via sscanf and returned; non-string values have all
655 * an ival of 0. The converted integer is cached, so costly string
656 * conversions are only done once. Values created with the MPDM_IVAL
657 * flag set have its ival cached from the beginning.
661 int mpdm_ival(mpdm_t v
)
668 /* if there is no cached integer, calculate it */
669 if (!(v
->flags
& MPDM_IVAL
)) {
670 /* if it's a string, calculate it; other
671 values will have an ival of 0 */
672 if (v
->flags
& MPDM_STRING
) {
676 wcstombs(tmp
, (wchar_t *) v
->data
, sizeof(tmp
));
677 tmp
[sizeof(tmp
) - 1] = '\0';
679 /* workaround for mingw32: as it doesn't
680 correctly parse octal and hexadecimal
681 numbers, they are tried as special cases */
683 if (tmp
[1] == 'b' || tmp
[1] == 'B') {
688 while (*ptr
== '0' || *ptr
== '1') {
698 if (tmp
[1] == 'x' || tmp
[1] == 'X')
705 sscanf(tmp
, fmt
, &i
);
721 * mpdm_rval - Returns a value's data as a real number (double).
724 * Returns a value's data as a real number (double float). If the value
725 * is a string, it's converted via sscanf and returned; non-string values
726 * have all an rval of 0. The converted double is cached, so costly string
727 * conversions are only done once. Values created with the MPDM_RVAL
728 * flag set have its rval cached from the beginning.
732 double mpdm_rval(mpdm_t v
)
739 /* if there is no cached double, calculate it */
740 if (!(v
->flags
& MPDM_RVAL
)) {
741 /* if it's a string, calculate it; other
742 values will have an rval of 0.0 */
743 if (v
->flags
& MPDM_STRING
) {
747 wcstombs(tmp
, (wchar_t *) v
->data
, sizeof(tmp
));
748 tmp
[sizeof(tmp
) - 1] = '\0';
750 /* if the number starts with 0, it's
751 an octal or hexadecimal number; just
752 take the integer value and cast it */
753 if (tmp
[0] == '0' && tmp
[1] != '.')
754 r
= (double) mpdm_ival(v
);
756 /* set locale to C for non locale-dependent
757 floating point conversion */
758 prev_locale
= setlocale(LC_NUMERIC
, "C");
761 sscanf(tmp
, "%lf", &r
);
763 /* set previous locale */
764 setlocale(LC_NUMERIC
, prev_locale
);
781 * mpdm_gettext - Translates a string to the current language.
784 * Translates the @str string to the current language.
786 * This function can still be used even if there is no real gettext
787 * support() by manually filling the __I18N__ hash.
789 * If the string is found in the current table, the translation is
790 * returned; otherwise, the same @str value is returned.
794 mpdm_t
mpdm_gettext(const mpdm_t str
)
800 if ((i18n
= mpdm_hget_s(mpdm_root(), L
"__I18N__")) == NULL
)
801 i18n
= mpdm_hset_s(mpdm_root(), L
"__I18N__", MPDM_H(0));
805 /* try first the cache */
806 if ((v
= mpdm_hget(i18n
, str
)) == NULL
) {
807 #ifdef CONFOPT_GETTEXT
812 t
= mpdm_ref(MPDM_2MBS(str
->data
));
814 /* ask gettext for it */
815 s
= gettext((char *) t
->data
);
824 #else /* CONFOPT_GETTEXT */
828 #endif /* CONFOPT_GETTEXT */
830 /* store in the cache */
831 mpdm_hset(i18n
, str
, v
);
841 * mpdm_gettext_domain - Sets domain and data directory for translations.
842 * @dom: the domain (application name)
843 * @data: directory contaning the .mo files
845 * Sets the domain (application name) and translation data for translating
846 * strings that will be returned by mpdm_gettext().@data must point to a
847 * directory containing the .mo (compiled .po) files.
849 * If there is no gettext support, returns 0, or 1 otherwise.
853 int mpdm_gettext_domain(const mpdm_t dom
, const mpdm_t data
)
860 #ifdef CONFOPT_GETTEXT
865 /* convert both to mbs,s */
866 dm
= mpdm_ref(MPDM_2MBS(dom
->data
));
867 dt
= mpdm_ref(MPDM_2MBS(data
->data
));
869 /* bind and set domain */
870 bindtextdomain((char *) dm
->data
, (char *) dt
->data
);
871 textdomain((char *) dm
->data
);
873 mpdm_hset_s(mpdm_root(), L
"__I18N__", MPDM_H(0));
880 #endif /* CONFOPT_GETTEXT */
886 if ((v
= mpdm_hget_s(mpdm_root(), L
"ENV")) != NULL
&&
887 mpdm_hget_s(v
, L
"LANG") == NULL
) {
888 wchar_t *wptr
= L
"en";
890 /* MS Windows crappy language constants... */
892 switch((GetSystemDefaultLangID() & 0x00ff)) {
893 case 0x01: wptr
= L
"ar"; break; /* arabic */
894 case 0x02: wptr
= L
"bg"; break; /* bulgarian */
895 case 0x03: wptr
= L
"ca"; break; /* catalan */
896 case 0x04: wptr
= L
"zh"; break; /* chinese */
897 case 0x05: wptr
= L
"cz"; break; /* czech */
898 case 0x06: wptr
= L
"da"; break; /* danish */
899 case 0x07: wptr
= L
"de"; break; /* german */
900 case 0x08: wptr
= L
"el"; break; /* greek */
901 case 0x09: wptr
= L
"en"; break; /* english */
902 case 0x0a: wptr
= L
"es"; break; /* spanish */
903 case 0x0b: wptr
= L
"fi"; break; /* finnish */
904 case 0x0c: wptr
= L
"fr"; break; /* french */
905 case 0x0d: wptr
= L
"he"; break; /* hebrew */
906 case 0x0e: wptr
= L
"hu"; break; /* hungarian */
907 case 0x0f: wptr
= L
"is"; break; /* icelandic */
908 case 0x10: wptr
= L
"it"; break; /* italian */
909 case 0x11: wptr
= L
"jp"; break; /* japanese */
910 case 0x12: wptr
= L
"ko"; break; /* korean */
911 case 0x13: wptr
= L
"nl"; break; /* dutch */
912 case 0x14: wptr
= L
"no"; break; /* norwegian */
913 case 0x15: wptr
= L
"po"; break; /* polish */
914 case 0x16: wptr
= L
"pt"; break; /* portuguese */
915 case 0x17: wptr
= L
"rm"; break; /* romansh (switzerland) */
916 case 0x18: wptr
= L
"ro"; break; /* romanian */
917 case 0x19: wptr
= L
"ru"; break; /* russian */
918 case 0x1a: wptr
= L
"sr"; break; /* serbian */
919 case 0x1b: wptr
= L
"sk"; break; /* slovak */
920 case 0x1c: wptr
= L
"sq"; break; /* albanian */
921 case 0x1d: wptr
= L
"sv"; break; /* swedish */
924 mpdm_hset_s(v
, L
"LANG", MPDM_S(wptr
));
927 #endif /* CONFOPT_WIN32 */
936 #ifdef CONFOPT_WCWIDTH
938 int wcwidth(wchar_t);
940 int mpdm_wcwidth(wchar_t c
)
945 #else /* CONFOPT_WCWIDTH */
949 int mpdm_wcwidth(wchar_t c
)
951 return mk_wcwidth(c
);
954 #endif /* CONFOPT_WCWIDTH */
958 * mpdm_sprintf - Formats a sprintf()-like string.
959 * @fmt: the string format
960 * @args: an array of values
962 * Formats a string using the sprintf() format taking the values from @args.
965 mpdm_t
mpdm_sprintf(const mpdm_t fmt
, const mpdm_t args
)
967 const wchar_t *i
= fmt
->data
;
975 /* loop all characters */
976 while ((c
= *i
++) != L
'\0') {
978 wchar_t *tptr
= NULL
;
979 wchar_t *wptr
= NULL
;
982 /* format directive */
991 /* transform the format to mbs */
992 while (*i
!= L
'\0' &&
993 m
< (int)(sizeof(t_fmt
) - MB_CUR_MAX
- 1) &&
994 wcschr(L
"-.0123456789", *i
) != NULL
)
995 m
+= wctomb(&t_fmt
[m
], *i
++);
997 /* transfer the directive */
998 m
+= wctomb(&t_fmt
[m
], *i
++);
1002 /* by default, copies the format */
1005 /* pick next value */
1006 v
= mpdm_aget(args
, n
++);
1008 switch (t_fmt
[m
- 1]) {
1017 snprintf(tmp
, sizeof(tmp
) - 1,
1018 t_fmt
, mpdm_ival(v
));
1023 /* float (real) value */
1024 snprintf(tmp
, sizeof(tmp
) - 1,
1025 t_fmt
, mpdm_rval(v
));
1031 ptr
= mpdm_wcstombs(mpdm_string(v
), NULL
);
1032 snprintf(tmp
, sizeof(tmp
) - 1, t_fmt
, ptr
);
1051 mask
= 1 << ((sizeof(int) * 8) - 1);
1053 if (mask
& (unsigned int) mpdm_ival(v
)) {
1080 wptr
= tptr
= mpdm_mbstowcs(tmp
, &m
, -1);
1089 o
= mpdm_poke(o
, &l
, wptr
, m
, sizeof(wchar_t));
1091 /* free the temporary buffer, if any */
1099 /* null-terminate */
1100 o
= mpdm_poke(o
, &l
, L
"", 1, sizeof(wchar_t));
1105 return MPDM_ENS(o
, l
- 1);
1110 * mpdm_ulc - Converts a string to uppercase or lowecase.
1112 * @u: convert to uppercase (1) or to lowercase (0).
1114 * Converts @s to uppercase (for @u == 1) or to lowercase (@u == 0).
1117 mpdm_t
mpdm_ulc(const mpdm_t s
, int u
)
1127 if ((optr
= malloc((i
+ 1) * sizeof(wchar_t))) != NULL
) {
1128 wchar_t *iptr
= mpdm_string(s
);
1131 for (n
= 0; n
< i
; n
++)
1132 optr
[n
] = u
? towupper(iptr
[n
]) : towlower(iptr
[n
]);
1135 r
= MPDM_ENS(optr
, i
);
1144 /* scanf working buffers */
1145 #define SCANF_BUF_SIZE 1024
1146 static wchar_t scanf_yset
[SCANF_BUF_SIZE
];
1147 static wchar_t scanf_nset
[SCANF_BUF_SIZE
];
1148 static wchar_t scanf_mark
[SCANF_BUF_SIZE
];
1155 { L
's', L
"", L
" \t" },
1156 { L
'u', L
"0123456789", L
"" },
1157 { L
'd', L
"-0123456789", L
"" },
1158 { L
'i', L
"-0123456789", L
"" },
1159 { L
'f', L
"-0123456789.", L
"" },
1160 { L
'x', L
"-0123456789xabcdefABCDEF", L
"" },
1161 { L
'\0', NULL
, NULL
},
1165 * mpdm_sscanf - Extracts data like sscanf().
1166 * @fmt: the string format
1167 * @str: the string to be parsed
1168 * @offset: the character offset to start scanning
1170 * Extracts data from a string using a special format pattern, very
1171 * much like the scanf() series of functions in the C library. Apart
1172 * from the standard percent-sign-commands (s, u, d, i, f, x,
1173 * n, [, with optional size and * to ignore), it implements S,
1174 * to match a string of characters upto what follows in the format
1175 * string. Also, the [ set of characters can include other % formats.
1177 * Returns an array with the extracted values. If %n is used, the
1178 * position in the scanned string is returned as the value.
1181 mpdm_t
mpdm_sscanf(const mpdm_t fmt
, const mpdm_t str
, int offset
)
1183 wchar_t *i
= (wchar_t *)str
->data
;
1184 wchar_t *f
= (wchar_t *)fmt
->data
;
1196 wchar_t *ptr
= NULL
;
1203 /* empty all buffers */
1204 scanf_yset
[0] = scanf_nset
[0] = scanf_mark
[0] = L
'\0';
1208 /* an asterisk? don't return next value */
1214 /* does it have a size? */
1215 while (wcschr(L
"0123456789", *f
)) {
1221 /* if no size, set it to an arbitrary big limit */
1225 /* now *f should contain a command */
1229 /* is it a verbatim percent sign? */
1233 wcscpy(scanf_yset
, L
"%");
1240 mpdm_push(r
, MPDM_I(i
- (wchar_t *)str
->data
));
1243 /* string upto a mark */
1247 /* fill the mark upto another command */
1252 /* is it an 'n'? ignore and go on */
1259 scanf_mark
[msize
++] = *tmp
;
1264 scanf_mark
[msize
++] = *tmp
;
1269 scanf_mark
[msize
] = L
'\0';
1275 wchar_t *set
= scanf_yset
;
1277 /* is it an inverse set? */
1283 /* first one is a ]? add it */
1289 /* now build the set */
1290 for (; n
< SCANF_BUF_SIZE
- 1 && *f
&& *f
!= L
']'; f
++) {
1291 /* is it a range? */
1295 /* start or end? hyphen itself */
1296 if (n
== 0 || *f
== L
']')
1299 /* pick previous char */
1300 wchar_t c
= set
[n
- 1];
1303 while (n
< SCANF_BUF_SIZE
- 1 && c
< *f
)
1308 /* is it another command? */
1313 for (i
= 0; scanf_sets
[i
].cmd
; i
++) {
1314 if (*f
== scanf_sets
[i
].cmd
) {
1316 wcscat(set
, scanf_sets
[i
].yset
);
1317 n
+= wcslen(scanf_sets
[i
].yset
);
1332 /* a standard set? */
1336 for (n
= 0; scanf_sets
[n
].cmd
!= L
'\0'; n
++) {
1337 if (cmd
== scanf_sets
[n
].cmd
) {
1338 wcscpy(scanf_yset
, scanf_sets
[n
].yset
);
1339 wcscpy(scanf_nset
, scanf_sets
[n
].nset
);
1345 /* now fill the dynamic string */
1347 !wcschr(scanf_nset
, *i
) &&
1348 (scanf_yset
[0] == L
'\0' || wcschr(scanf_yset
, *i
)) &&
1349 (msize
== 0 || wcsncmp(i
, scanf_mark
, msize
) != 0)) {
1351 /* only add if not being ignored */
1353 ptr
= mpdm_poke(ptr
, &size
, i
, 1, sizeof(wchar_t));
1359 if (!ignore
&& size
) {
1360 /* null terminate and push */
1361 ptr
= mpdm_poke(ptr
, &size
, L
"", 1, sizeof(wchar_t));
1362 mpdm_push(r
, MPDM_ENS(ptr
, size
- 1));
1366 if (*f
== L
' ' || *f
== L
'\t') {
1367 /* if it's a blank, sync to next non-blank */
1370 while (*i
== L
' ' || *i
== L
'\t')
1374 /* test for literals in the format string */