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
);
102 dst
= mpdm_pokews(dst
, dsize
, ptr
);
109 wchar_t *mpdm_mbstowcs(const char *str
, int *s
, int l
)
110 /* converts an mbs to a wcs, but filling invalid chars
111 with question marks instead of just failing */
114 char tmp
[64]; /* really MB_CUR_MAX + 1 */
119 /* allow NULL values for s */
123 /* if there is a limit, duplicate and break the string */
131 /* try first a direct conversion with mbstowcs */
132 if ((*s
= mbstowcs(NULL
, cstr
, 0)) != -1) {
133 /* direct conversion is possible; do it */
134 if ((ptr
= malloc((*s
+ 1) * sizeof(wchar_t))) != NULL
) {
135 mbstowcs(ptr
, cstr
, *s
);
140 /* zero everything */
144 /* no more characters to process? */
145 if ((c
= cstr
[n
+ i
]) == '\0' && i
== 0)
152 if (mbstowcs(&wc
, tmp
, 1) == (size_t) -1) {
153 /* can still be an incomplete multibyte char? */
154 if (c
!= '\0' && i
<= (int) MB_CUR_MAX
)
157 /* too many failing bytes; skip 1 byte */
163 /* skip used bytes and back again */
168 if ((ptr
= mpdm_poke(ptr
, s
, &wc
, 1, sizeof(wchar_t))) == NULL
)
172 /* null terminate and count one less */
174 ptr
= mpdm_poke(ptr
, s
, L
"", 1, sizeof(wchar_t));
179 /* free the duplicate */
187 char *mpdm_wcstombs(const wchar_t * str
, int *s
)
188 /* converts a wcs to an mbs, but filling invalid chars
189 with question marks instead of just failing */
192 char tmp
[64]; /* really MB_CUR_MAX + 1 */
195 /* allow NULL values for s */
199 /* try first a direct conversion with wcstombs */
200 if ((*s
= wcstombs(NULL
, str
, 0)) != -1) {
201 /* direct conversion is possible; do it and return */
202 if ((ptr
= malloc(*s
+ 1)) != NULL
) {
203 wcstombs(ptr
, str
, *s
);
210 /* invalid encoding? convert characters one by one */
214 if ((l
= wctomb(tmp
, *str
)) <= 0) {
215 /* if char couldn't be converted,
216 write a question mark instead */
217 l
= wctomb(tmp
, L
'?');
221 if ((ptr
= mpdm_poke(ptr
, s
, tmp
, l
, 1)) == NULL
)
227 /* null terminate and count one less */
229 ptr
= mpdm_poke(ptr
, s
, "", 1, 1);
237 mpdm_t
mpdm_new_wcs(int flags
, const wchar_t * str
, int size
, int cpy
)
238 /* creates a new string value from a wcs */
242 /* a size of -1 means 'calculate it' */
243 if (size
== -1 && str
!= NULL
)
248 /* free() on destruction */
252 if ((ptr
= malloc((size
+ 1) * sizeof(wchar_t))) == NULL
)
255 /* if no source, reset to zeroes; otherwise, copy */
257 memset(ptr
, '\0', size
* sizeof(wchar_t));
259 wcsncpy(ptr
, str
, size
);
264 ptr
= (wchar_t *)str
;
267 flags
|= MPDM_STRING
;
269 return mpdm_new(flags
, ptr
, size
);
273 mpdm_t
mpdm_new_mbstowcs(int flags
, const char *str
, int l
)
274 /* creates a new string value from an mbs */
279 if ((ptr
= mpdm_mbstowcs(str
, &size
, l
)) == NULL
)
283 flags
|= (MPDM_STRING
| MPDM_FREE
);
285 return mpdm_new(flags
, ptr
, size
);
289 mpdm_t
mpdm_new_wcstombs(int flags
, const wchar_t * str
)
290 /* creates a new mbs value from a wbs */
295 ptr
= mpdm_wcstombs(str
, &size
);
299 /* unset the string flag; mbs,s are not 'strings' */
300 flags
&= ~MPDM_STRING
;
302 return mpdm_new(flags
, ptr
, size
);
306 mpdm_t
mpdm_new_i(int ival
)
307 /* creates a new string value from an integer */
312 /* creates the visual representation */
313 snprintf(tmp
, sizeof(tmp
), "%d", ival
);
317 return mpdm_set_ival(v
, ival
);
321 mpdm_t
mpdm_new_r(double rval
)
322 /* creates a new string value from a real number */
327 /* creates the visual representation */
328 snprintf(tmp
, sizeof(tmp
), "%lf", rval
);
330 /* manually strip useless zeroes */
331 if (strchr(tmp
, '.') != NULL
) {
334 for (ptr
= tmp
+ strlen(tmp
) - 1; *ptr
== '0'; ptr
--);
336 /* if it's over the ., strip it also */
345 return mpdm_set_rval(v
, rval
);
352 * mpdm_string - Returns a printable representation of a value.
355 * Returns a printable representation of a value. For strings, it's
356 * the value data itself; for any other type, a conversion to string
357 * is returned instead. This value should be used immediately, as it
358 * can be a pointer to a static buffer.
361 wchar_t *mpdm_string(const mpdm_t v
)
363 static wchar_t wtmp
[32];
366 /* if it's NULL, return a constant */
370 /* if it's a string, return it */
371 if (v
->flags
& MPDM_STRING
)
372 return (wchar_t *) v
->data
;
374 /* otherwise, return a visual representation */
375 snprintf(tmp
, sizeof(tmp
), "%p", v
);
376 mbstowcs(wtmp
, tmp
, sizeof(wtmp
));
377 wtmp
[(sizeof(wtmp
) / sizeof(wchar_t)) - 1] = L
'\0';
384 * mpdm_cmp - Compares two values.
385 * @v1: the first value
386 * @v2: the second value
388 * Compares two values. If both has the MPDM_STRING flag set,
389 * a comparison using wcscoll() is returned; if both are arrays,
390 * the size is compared first and, if they have the same number
391 * elements, each one is compared; otherwise, a simple pointer
392 * comparison is done.
395 int mpdm_cmp(const mpdm_t v1
, const mpdm_t v2
)
403 /* is any value NULL? */
409 /* different values, but same content? (unlikely) */
410 if (v1
->data
== v2
->data
)
413 if (MPDM_IS_STRING(v1
) && MPDM_IS_STRING(v2
))
414 r
= wcscoll((wchar_t *) v1
->data
, (wchar_t *) v2
->data
);
416 if (MPDM_IS_ARRAY(v1
) && MPDM_IS_ARRAY(v2
)) {
417 /* compare first the sizes */
418 if ((r
= mpdm_size(v1
) - mpdm_size(v2
)) == 0) {
421 /* they have the same size;
422 compare each pair of elements */
423 for (n
= 0; n
< mpdm_size(v1
); n
++) {
424 if ((r
= mpdm_cmp(mpdm_aget(v1
, n
),
425 mpdm_aget(v2
, n
))) != 0)
431 /* in any other case, compare just pointers */
432 r
= (int) ((char *)v1
->data
- (char *)v2
->data
);
439 * mpdm_cmp_s - Compares two values (string version).
440 * @v1: the first value
441 * @v2: the second value
443 * Compares two values. Compares both values using wcscoll()
444 * if the first one is a string, or returns 1 otherwise.
446 int mpdm_cmp_s(const mpdm_t v1
, const wchar_t *v2
)
450 if (MPDM_IS_STRING(v1
))
451 r
= wcscoll((wchar_t *) v1
->data
, v2
);
458 * mpdm_splice - Creates a new string value from another.
459 * @v: the original value
460 * @i: the value to be inserted
461 * @offset: offset where the substring is to be inserted
462 * @del: number of characters to delete
464 * Creates a new string value from @v, deleting @del chars at @offset
465 * and substituting them by @i. If @del is 0, no deletion is done.
466 * both @offset and @del can be negative; if this is the case, it's
467 * assumed as counting from the end of @v. If @v is NULL, @i will become
468 * the new string, and both @offset and @del will be ignored. If @v is
469 * not NULL and @i is, no insertion process is done (only deletion, if
472 * Returns a two element array, with the new string in the first
473 * element and the deleted string in the second (with a NULL value
477 mpdm_t
mpdm_splice(const mpdm_t v
, const mpdm_t i
, int offset
, int del
)
489 /* negative offsets start from the end */
491 offset
= os
+ 1 - offset
;
493 /* never add further the end */
497 /* negative del counts as 'characters left' */
499 del
= os
+ 1 - offset
+ del
;
501 /* something to delete? */
503 /* never delete further the end */
504 if (offset
+ del
> os
)
508 d
= MPDM_NS(((wchar_t *) v
->data
) + offset
, del
);
513 /* something to insert? */
516 /* new size and remainder */
520 if ((n
= MPDM_NS(NULL
, ns
)) == NULL
)
523 ptr
= (wchar_t *)n
->data
;
525 /* copy the beginning */
527 wcsncpy(ptr
, v
->data
, offset
);
531 /* copy the text to be inserted */
533 wcsncpy(ptr
, i
->data
, ins
);
537 /* copy the remaining */
540 wcsncpy(ptr
, ((wchar_t *) v
->data
) + r
, os
);
550 /* creates the output array */
561 * mpdm_strcat_sn - Concatenates two strings (string with size version).
562 * @s1: the first string
563 * @s2: the second string
564 * @size: the size of the second string
566 * Returns a new string formed by the concatenation of @s1 and @s2.
569 mpdm_t
mpdm_strcat_sn(const mpdm_t s1
, const wchar_t *s2
, int size
)
574 if (s1
== NULL
&& s2
== NULL
)
577 ptr
= mpdm_pokev(ptr
, &s
, s1
);
578 ptr
= mpdm_pokewsn(ptr
, &s
, s2
, size
);
580 /* if no characters were added, returns an empty string */
584 ptr
= mpdm_poke(ptr
, &s
, L
"", 1, sizeof(wchar_t));
585 return MPDM_ENS(ptr
, s
- 1);
590 * mpdm_strcat_s - Concatenates two strings (string version).
591 * @s1: the first string
592 * @s2: the second string
594 * Returns a new string formed by the concatenation of @s1 and @s2.
597 mpdm_t
mpdm_strcat_s(const mpdm_t s1
, const wchar_t *s2
)
599 return mpdm_strcat_sn(s1
, s2
, s2
? wcslen(s2
) : 0);
604 * mpdm_strcat - Concatenates two strings.
605 * @s1: the first string
606 * @s2: the second string
608 * Returns a new string formed by the concatenation of @s1 and @s2.
611 mpdm_t
mpdm_strcat(const mpdm_t s1
, const mpdm_t s2
)
613 return mpdm_strcat_s(s1
, s2
? mpdm_string(s2
) : NULL
);
618 * mpdm_ival - Returns a value's data as an integer.
621 * Returns a value's data as an integer. If the value is a string,
622 * it's converted via sscanf and returned; non-string values have all
623 * an ival of 0. The converted integer is cached, so costly string
624 * conversions are only done once. Values created with the MPDM_IVAL
625 * flag set have its ival cached from the beginning.
629 int mpdm_ival(mpdm_t v
)
634 /* if there is no cached integer, calculate it */
635 if (!(v
->flags
& MPDM_IVAL
)) {
638 /* if it's a string, calculate it; other
639 values will have an ival of 0 */
640 if (v
->flags
& MPDM_STRING
) {
644 wcstombs(tmp
, (wchar_t *) v
->data
, sizeof(tmp
));
645 tmp
[sizeof(tmp
) - 1] = '\0';
647 /* workaround for mingw32: as it doesn't
648 correctly parse octal and hexadecimal
649 numbers, they are tried as special cases */
651 if (tmp
[1] == 'b' || tmp
[1] == 'B') {
656 while (*ptr
== '0' || *ptr
== '1') {
666 if (tmp
[1] == 'x' || tmp
[1] == 'X')
673 sscanf(tmp
, fmt
, &i
);
684 * mpdm_rval - Returns a value's data as a real number (double).
687 * Returns a value's data as a real number (double float). If the value
688 * is a string, it's converted via sscanf and returned; non-string values
689 * have all an rval of 0. The converted double is cached, so costly string
690 * conversions are only done once. Values created with the MPDM_RVAL
691 * flag set have its rval cached from the beginning.
695 double mpdm_rval(mpdm_t v
)
700 /* if there is no cached double, calculate it */
701 if (!(v
->flags
& MPDM_RVAL
)) {
704 /* if it's a string, calculate it; other
705 values will have an rval of 0.0 */
706 if (v
->flags
& MPDM_STRING
) {
710 wcstombs(tmp
, (wchar_t *) v
->data
, sizeof(tmp
));
711 tmp
[sizeof(tmp
) - 1] = '\0';
713 /* if the number starts with 0, it's
714 an octal or hexadecimal number; just
715 take the integer value and cast it */
716 if (tmp
[0] == '0' && tmp
[1] != '.')
717 r
= (double) mpdm_ival(v
);
719 /* set locale to C for non locale-dependent
720 floating point conversion */
721 prev_locale
= setlocale(LC_NUMERIC
, "C");
724 sscanf(tmp
, "%lf", &r
);
726 /* set previous locale */
727 setlocale(LC_NUMERIC
, prev_locale
);
739 * mpdm_gettext - Translates a string to the current language.
742 * Translates the @str string to the current language.
744 * This function can still be used even if there is no real gettext
745 * support() by manually filling the __I18N__ hash.
747 * If the string is found in the current table, the translation is
748 * returned; otherwise, the same @str value is returned.
752 mpdm_t
mpdm_gettext(const mpdm_t str
)
757 /* gets the cache, if any */
758 if ((i18n
= mpdm_hget_s(mpdm_root(), L
"__I18N__")) == NULL
)
761 /* try first the cache */
762 if ((v
= mpdm_hget(i18n
, str
)) == NULL
) {
763 #ifdef CONFOPT_GETTEXT
768 t
= mpdm_ref(MPDM_2MBS(str
->data
));
770 /* ask gettext for it */
771 s
= gettext((char *) t
->data
);
773 /* create new value only if it's different */
777 /* store in the cache */
778 mpdm_hset(i18n
, str
, v
);
785 #else /* CONFOPT_GETTEXT */
789 #endif /* CONFOPT_GETTEXT */
797 * mpdm_gettext_domain - Sets domain and data directory for translations.
798 * @dom: the domain (application name)
799 * @data: directory contaning the .mo files
801 * Sets the domain (application name) and translation data for translating
802 * strings that will be returned by mpdm_gettext().@data must point to a
803 * directory containing the .mo (compiled .po) files.
805 * If there is no gettext support, returns 0, or 1 otherwise.
809 int mpdm_gettext_domain(const mpdm_t dom
, const mpdm_t data
)
813 #ifdef CONFOPT_GETTEXT
818 /* convert both to mbs,s */
819 dm
= mpdm_ref(MPDM_2MBS(dom
->data
));
820 dt
= mpdm_ref(MPDM_2MBS(data
->data
));
822 /* bind and set domain */
823 bindtextdomain((char *) dm
->data
, (char *) dt
->data
);
824 textdomain((char *) dm
->data
);
826 mpdm_hset_s(mpdm_root(), L
"__I18N__", MPDM_H(0));
833 #endif /* CONFOPT_GETTEXT */
839 if ((v
= mpdm_hget_s(mpdm_root(), L
"ENV")) != NULL
&&
840 mpdm_hget_s(v
, L
"LANG") == NULL
) {
841 wchar_t *wptr
= L
"en";
843 /* MS Windows crappy language constants... */
845 switch((GetSystemDefaultLangID() & 0x00ff)) {
846 case 0x01: wptr
= L
"ar"; break; /* arabic */
847 case 0x02: wptr
= L
"bg"; break; /* bulgarian */
848 case 0x03: wptr
= L
"ca"; break; /* catalan */
849 case 0x04: wptr
= L
"zh"; break; /* chinese */
850 case 0x05: wptr
= L
"cz"; break; /* czech */
851 case 0x06: wptr
= L
"da"; break; /* danish */
852 case 0x07: wptr
= L
"de"; break; /* german */
853 case 0x08: wptr
= L
"el"; break; /* greek */
854 case 0x09: wptr
= L
"en"; break; /* english */
855 case 0x0a: wptr
= L
"es"; break; /* spanish */
856 case 0x0b: wptr
= L
"fi"; break; /* finnish */
857 case 0x0c: wptr
= L
"fr"; break; /* french */
858 case 0x0d: wptr
= L
"he"; break; /* hebrew */
859 case 0x0e: wptr
= L
"hu"; break; /* hungarian */
860 case 0x0f: wptr
= L
"is"; break; /* icelandic */
861 case 0x10: wptr
= L
"it"; break; /* italian */
862 case 0x11: wptr
= L
"jp"; break; /* japanese */
863 case 0x12: wptr
= L
"ko"; break; /* korean */
864 case 0x13: wptr
= L
"nl"; break; /* dutch */
865 case 0x14: wptr
= L
"no"; break; /* norwegian */
866 case 0x15: wptr
= L
"po"; break; /* polish */
867 case 0x16: wptr
= L
"pt"; break; /* portuguese */
868 case 0x17: wptr
= L
"rm"; break; /* romansh (switzerland) */
869 case 0x18: wptr
= L
"ro"; break; /* romanian */
870 case 0x19: wptr
= L
"ru"; break; /* russian */
871 case 0x1a: wptr
= L
"sr"; break; /* serbian */
872 case 0x1b: wptr
= L
"sk"; break; /* slovak */
873 case 0x1c: wptr
= L
"sq"; break; /* albanian */
874 case 0x1d: wptr
= L
"sv"; break; /* swedish */
877 mpdm_hset_s(v
, L
"LANG", MPDM_S(wptr
));
880 #endif /* CONFOPT_WIN32 */
886 #ifdef CONFOPT_WCWIDTH
888 int wcwidth(wchar_t);
890 int mpdm_wcwidth(wchar_t c
)
895 #else /* CONFOPT_WCWIDTH */
899 int mpdm_wcwidth(wchar_t c
)
901 return mk_wcwidth(c
);
904 #endif /* CONFOPT_WCWIDTH */
908 * mpdm_sprintf - Formats a sprintf()-like string.
909 * @fmt: the string format
910 * @args: an array of values
912 * Formats a string using the sprintf() format taking the values from @args.
915 mpdm_t
mpdm_sprintf(const mpdm_t fmt
, const mpdm_t args
)
917 const wchar_t *i
= fmt
->data
;
922 /* loop all characters */
923 while ((c
= *i
++) != L
'\0') {
925 wchar_t *tptr
= NULL
;
926 wchar_t *wptr
= NULL
;
929 /* format directive */
938 /* transform the format to mbs */
939 while (*i
!= L
'\0' &&
940 m
< (int)(sizeof(t_fmt
) - MB_CUR_MAX
- 1) &&
941 wcschr(L
"-.0123456789", *i
) != NULL
)
942 m
+= wctomb(&t_fmt
[m
], *i
++);
944 /* transfer the directive */
945 m
+= wctomb(&t_fmt
[m
], *i
++);
949 /* by default, copies the format */
952 /* pick next value */
953 v
= mpdm_aget(args
, n
++);
955 switch (t_fmt
[m
- 1]) {
964 snprintf(tmp
, sizeof(tmp
) - 1,
965 t_fmt
, mpdm_ival(v
));
970 /* float (real) value */
971 snprintf(tmp
, sizeof(tmp
) - 1,
972 t_fmt
, mpdm_rval(v
));
978 ptr
= mpdm_wcstombs(mpdm_string(v
), NULL
);
979 snprintf(tmp
, sizeof(tmp
) - 1, t_fmt
, ptr
);
998 mask
= 1 << ((sizeof(int) * 8) - 1);
1000 if (mask
& (unsigned int) mpdm_ival(v
)) {
1027 wptr
= tptr
= mpdm_mbstowcs(tmp
, &m
, -1);
1036 o
= mpdm_poke(o
, &l
, wptr
, m
, sizeof(wchar_t));
1038 /* free the temporary buffer, if any */
1046 /* null-terminate */
1047 o
= mpdm_poke(o
, &l
, L
"", 1, sizeof(wchar_t));
1049 return MPDM_ENS(o
, l
- 1);
1054 * mpdm_ulc - Converts a string to uppercase or lowecase.
1056 * @u: convert to uppercase (1) or to lowercase (0).
1058 * Converts @s to uppercase (for @u == 1) or to lowercase (@u == 0).
1061 mpdm_t
mpdm_ulc(const mpdm_t s
, int u
)
1065 int i
= mpdm_size(s
);
1067 if ((optr
= malloc((i
+ 1) * sizeof(wchar_t))) != NULL
) {
1068 wchar_t *iptr
= mpdm_string(s
);
1071 for (n
= 0; n
< i
; n
++)
1072 optr
[n
] = u
? towupper(iptr
[n
]) : towlower(iptr
[n
]);
1075 r
= MPDM_ENS(optr
, i
);
1082 /* scanf working buffers */
1083 #define SCANF_BUF_SIZE 1024
1084 static wchar_t scanf_yset
[SCANF_BUF_SIZE
];
1085 static wchar_t scanf_nset
[SCANF_BUF_SIZE
];
1086 static wchar_t scanf_mark
[SCANF_BUF_SIZE
];
1093 { L
's', L
"", L
" \t" },
1094 { L
'u', L
"0123456789", L
"" },
1095 { L
'd', L
"-0123456789", L
"" },
1096 { L
'i', L
"-0123456789", L
"" },
1097 { L
'f', L
"-0123456789.", L
"" },
1098 { L
'x', L
"-0123456789xabcdefABCDEF", L
"" },
1099 { L
'\0', NULL
, NULL
},
1103 * mpdm_sscanf - Extracts data like sscanf().
1104 * @fmt: the string format
1105 * @str: the string to be parsed
1106 * @offset: the character offset to start scanning
1108 * Extracts data from a string using a special format pattern, very
1109 * much like the scanf() series of functions in the C library. Apart
1110 * from the standard percent-sign-commands (s, u, d, i, f, x,
1111 * n, [, with optional size and * to ignore), it implements S,
1112 * to match a string of characters upto what follows in the format
1113 * string. Also, the [ set of characters can include other % formats.
1115 * Returns an array with the extracted values. If %n is used, the
1116 * position in the scanned string is returned as the value.
1119 mpdm_t
mpdm_sscanf(const mpdm_t fmt
, const mpdm_t str
, int offset
)
1121 wchar_t *i
= (wchar_t *)str
->data
;
1122 wchar_t *f
= (wchar_t *)fmt
->data
;
1130 wchar_t *ptr
= NULL
;
1137 /* empty all buffers */
1138 scanf_yset
[0] = scanf_nset
[0] = scanf_mark
[0] = L
'\0';
1142 /* an asterisk? don't return next value */
1148 /* does it have a size? */
1149 while (wcschr(L
"0123456789", *f
)) {
1155 /* if no size, set it to an arbitrary big limit */
1159 /* now *f should contain a command */
1163 /* is it a verbatim percent sign? */
1167 wcscpy(scanf_yset
, L
"%");
1174 mpdm_push(r
, MPDM_I(i
- (wchar_t *)str
->data
));
1177 /* string upto a mark */
1181 /* fill the mark upto another command */
1186 /* is it an 'n'? ignore and go on */
1193 scanf_mark
[msize
++] = *tmp
;
1198 scanf_mark
[msize
++] = *tmp
;
1203 scanf_mark
[msize
] = L
'\0';
1209 wchar_t *set
= scanf_yset
;
1211 /* is it an inverse set? */
1217 /* first one is a ]? add it */
1223 /* now build the set */
1224 for (; n
< SCANF_BUF_SIZE
- 1 && *f
&& *f
!= L
']'; f
++) {
1225 /* is it a range? */
1229 /* start or end? hyphen itself */
1230 if (n
== 0 || *f
== L
']')
1233 /* pick previous char */
1234 wchar_t c
= set
[n
- 1];
1237 while (n
< SCANF_BUF_SIZE
- 1 && c
< *f
)
1242 /* is it another command? */
1247 for (i
= 0; scanf_sets
[i
].cmd
; i
++) {
1248 if (*f
== scanf_sets
[i
].cmd
) {
1250 wcscat(set
, scanf_sets
[i
].yset
);
1251 n
+= wcslen(scanf_sets
[i
].yset
);
1266 /* a standard set? */
1270 for (n
= 0; scanf_sets
[n
].cmd
!= L
'\0'; n
++) {
1271 if (cmd
== scanf_sets
[n
].cmd
) {
1272 wcscpy(scanf_yset
, scanf_sets
[n
].yset
);
1273 wcscpy(scanf_nset
, scanf_sets
[n
].nset
);
1279 /* now fill the dynamic string */
1281 !wcschr(scanf_nset
, *i
) &&
1282 (scanf_yset
[0] == L
'\0' || wcschr(scanf_yset
, *i
)) &&
1283 (msize
== 0 || wcsncmp(i
, scanf_mark
, msize
) != 0)) {
1285 /* only add if not being ignored */
1287 ptr
= mpdm_poke(ptr
, &size
, i
, 1, sizeof(wchar_t));
1293 if (!ignore
&& size
) {
1294 /* null terminate and push */
1295 ptr
= mpdm_poke(ptr
, &size
, L
"", 1, sizeof(wchar_t));
1296 mpdm_push(r
, MPDM_ENS(ptr
, size
));
1300 if (*f
== L
' ' || *f
== L
'\t') {
1301 /* if it's a blank, sync to next non-blank */
1304 while (*i
== L
' ' || *i
== L
'\t')
1308 /* test for literals in the format string */