3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2009 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
54 void *mpdm_poke(void *dst
, int *dsize
, const void *org
, int osize
, int esize
)
55 /* pokes (adds) org into dst, which is a dynamic string, making it grow */
57 if (org
!= NULL
&& osize
) {
58 /* makes room for the new string */
59 if ((dst
= realloc(dst
, (*dsize
+ osize
) * esize
)) != NULL
) {
61 memcpy((char *)dst
+ (*dsize
* esize
), org
, osize
* esize
);
63 /* adds to final size */
72 wchar_t *mpdm_pokews(wchar_t *dst
, int *dsize
, const wchar_t *str
)
73 /* adds a wide string to dst using mpdm_poke() */
75 return mpdm_poke(dst
, dsize
, str
, wcslen(str
), sizeof(wchar_t));
79 wchar_t *mpdm_pokev(wchar_t * dst
, int *dsize
, const mpdm_t v
)
80 /* adds the string in v to dst using mpdm_poke() */
83 const wchar_t *ptr
= mpdm_string(v
);
85 dst
= mpdm_pokews(dst
, dsize
, ptr
);
92 wchar_t *mpdm_mbstowcs(const char *str
, int *s
, int l
)
93 /* converts an mbs to a wcs, but filling invalid chars
94 with question marks instead of just failing */
97 char tmp
[64]; /* really MB_CUR_MAX + 1 */
102 /* allow NULL values for s */
106 /* if there is a limit, duplicate and break the string */
114 /* try first a direct conversion with mbstowcs */
115 if ((*s
= mbstowcs(NULL
, cstr
, 0)) != -1) {
116 /* direct conversion is possible; do it */
117 if ((ptr
= malloc((*s
+ 1) * sizeof(wchar_t))) != NULL
) {
118 mbstowcs(ptr
, cstr
, *s
);
123 /* zero everything */
127 /* no more characters to process? */
128 if ((c
= cstr
[n
+ i
]) == '\0' && i
== 0)
135 if (mbstowcs(&wc
, tmp
, 1) == (size_t) -1) {
136 /* can still be an incomplete multibyte char? */
137 if (c
!= '\0' && i
<= (int) MB_CUR_MAX
)
140 /* too many failing bytes; skip 1 byte */
146 /* skip used bytes and back again */
151 if ((ptr
= mpdm_poke(ptr
, s
, &wc
, 1, sizeof(wchar_t))) == NULL
)
155 /* null terminate and count one less */
157 ptr
= mpdm_poke(ptr
, s
, L
"", 1, sizeof(wchar_t));
162 /* free the duplicate */
170 char *mpdm_wcstombs(const wchar_t * str
, int *s
)
171 /* converts a wcs to an mbs, but filling invalid chars
172 with question marks instead of just failing */
175 char tmp
[64]; /* really MB_CUR_MAX + 1 */
178 /* allow NULL values for s */
182 /* try first a direct conversion with wcstombs */
183 if ((*s
= wcstombs(NULL
, str
, 0)) != -1) {
184 /* direct conversion is possible; do it and return */
185 if ((ptr
= malloc(*s
+ 1)) != NULL
) {
186 wcstombs(ptr
, str
, *s
);
193 /* invalid encoding? convert characters one by one */
197 if ((l
= wctomb(tmp
, *str
)) <= 0) {
198 /* if char couldn't be converted,
199 write a question mark instead */
200 l
= wctomb(tmp
, L
'?');
204 if ((ptr
= mpdm_poke(ptr
, s
, tmp
, l
, 1)) == NULL
)
210 /* null terminate and count one less */
212 ptr
= mpdm_poke(ptr
, s
, "", 1, 1);
220 mpdm_t
mpdm_new_wcs(int flags
, const wchar_t * str
, int size
, int cpy
)
221 /* creates a new string value from a wcs */
225 /* a size of -1 means 'calculate it' */
226 if (size
== -1 && str
!= NULL
)
231 /* free() on destruction */
235 if ((ptr
= malloc((size
+ 1) * sizeof(wchar_t))) == NULL
)
238 /* if no source, reset to zeroes; otherwise, copy */
240 memset(ptr
, '\0', size
* sizeof(wchar_t));
242 wcsncpy(ptr
, str
, size
);
247 ptr
= (wchar_t *)str
;
250 flags
|= MPDM_STRING
;
252 return mpdm_new(flags
, ptr
, size
);
256 mpdm_t
mpdm_new_mbstowcs(int flags
, const char *str
, int l
)
257 /* creates a new string value from an mbs */
262 if ((ptr
= mpdm_mbstowcs(str
, &size
, l
)) == NULL
)
266 flags
|= (MPDM_STRING
| MPDM_FREE
);
268 return mpdm_new(flags
, ptr
, size
);
272 mpdm_t
mpdm_new_wcstombs(int flags
, const wchar_t * str
)
273 /* creates a new mbs value from a wbs */
278 ptr
= mpdm_wcstombs(str
, &size
);
282 /* unset the string flag; mbs,s are not 'strings' */
283 flags
&= ~MPDM_STRING
;
285 return mpdm_new(flags
, ptr
, size
);
289 mpdm_t
mpdm_new_i(int ival
)
290 /* creates a new string value from an integer */
295 /* creates the visual representation */
296 snprintf(tmp
, sizeof(tmp
), "%d", ival
);
300 return mpdm_set_ival(v
, ival
);
304 mpdm_t
mpdm_new_r(double rval
)
305 /* creates a new string value from a real number */
310 /* creates the visual representation */
311 snprintf(tmp
, sizeof(tmp
), "%lf", rval
);
313 /* manually strip useless zeroes */
314 if (strchr(tmp
, '.') != NULL
) {
317 for (ptr
= tmp
+ strlen(tmp
) - 1; *ptr
== '0'; ptr
--);
319 /* if it's over the ., strip it also */
328 return mpdm_set_rval(v
, rval
);
335 * mpdm_string - Returns a printable representation of a value.
338 * Returns a printable representation of a value. For strings, it's
339 * the value data itself; for any other type, a conversion to string
340 * is returned instead. This value should be used immediately, as it
341 * can be a pointer to a static buffer.
344 wchar_t *mpdm_string(const mpdm_t v
)
346 static wchar_t wtmp
[32];
349 /* if it's NULL, return a constant */
353 /* if it's a string, return it */
354 if (v
->flags
& MPDM_STRING
)
355 return (wchar_t *) v
->data
;
357 /* otherwise, return a visual representation */
358 snprintf(tmp
, sizeof(tmp
), "%p", v
);
359 mbstowcs(wtmp
, tmp
, sizeof(wtmp
));
360 wtmp
[(sizeof(wtmp
) / sizeof(wchar_t)) - 1] = L
'\0';
367 * mpdm_cmp - Compares two values.
368 * @v1: the first value
369 * @v2: the second value
371 * Compares two values. If both has the MPDM_STRING flag set,
372 * a comparison using wcscmp() is returned; if both are arrays,
373 * the size is compared first and, if they have the same number
374 * elements, each one is compared; otherwise, a simple pointer
375 * comparison is done.
378 int mpdm_cmp(const mpdm_t v1
, const mpdm_t v2
)
386 /* is any value NULL? */
392 /* different values, but same content? (unlikely) */
393 if (v1
->data
== v2
->data
)
396 if (MPDM_IS_STRING(v1
) && MPDM_IS_STRING(v2
))
397 r
= wcscoll((wchar_t *) v1
->data
, (wchar_t *) v2
->data
);
399 if (MPDM_IS_ARRAY(v1
) && MPDM_IS_ARRAY(v2
)) {
400 /* compare first the sizes */
401 if ((r
= mpdm_size(v1
) - mpdm_size(v2
)) == 0) {
404 /* they have the same size;
405 compare each pair of elements */
406 for (n
= 0; n
< mpdm_size(v1
); n
++) {
407 if ((r
= mpdm_cmp(mpdm_aget(v1
, n
),
408 mpdm_aget(v2
, n
))) != 0)
414 /* in any other case, compare just pointers */
415 r
= (int) ((char *)v1
->data
- (char *)v2
->data
);
422 * mpdm_splice - Creates a new string value from another.
423 * @v: the original value
424 * @i: the value to be inserted
425 * @offset: offset where the substring is to be inserted
426 * @del: number of characters to delete
428 * Creates a new string value from @v, deleting @del chars at @offset
429 * and substituting them by @i. If @del is 0, no deletion is done.
430 * both @offset and @del can be negative; if this is the case, it's
431 * assumed as counting from the end of @v. If @v is NULL, @i will become
432 * the new string, and both @offset and @del will be ignored. If @v is
433 * not NULL and @i is, no insertion process is done (only deletion, if
436 * Returns a two element array, with the new string in the first
437 * element and the deleted string in the second (with a NULL value
441 mpdm_t
mpdm_splice(const mpdm_t v
, const mpdm_t i
, int offset
, int del
)
453 /* negative offsets start from the end */
455 offset
= os
+ 1 - offset
;
457 /* never add further the end */
461 /* negative del counts as 'characters left' */
463 del
= os
+ 1 - offset
+ del
;
465 /* something to delete? */
467 /* never delete further the end */
468 if (offset
+ del
> os
)
472 d
= MPDM_NS(((wchar_t *) v
->data
) + offset
, del
);
477 /* something to insert? */
480 /* new size and remainder */
484 if ((n
= MPDM_NS(NULL
, ns
)) == NULL
)
487 ptr
= (wchar_t *)n
->data
;
489 /* copy the beginning */
491 wcsncpy(ptr
, v
->data
, offset
);
495 /* copy the text to be inserted */
497 wcsncpy(ptr
, i
->data
, ins
);
501 /* copy the remaining */
504 wcsncpy(ptr
, ((wchar_t *) v
->data
) + r
, os
);
514 /* creates the output array */
525 * mpdm_strcat - Concatenates two strings.
526 * @s1: the first string
527 * @s2: the second string
529 * Returns a new string formed by the concatenation of @s1 and @s2.
532 mpdm_t
mpdm_strcat(const mpdm_t s1
, const mpdm_t s2
)
537 if (s1
== NULL
&& s2
== NULL
)
540 ptr
= mpdm_pokev(ptr
, &s
, s1
);
541 ptr
= mpdm_pokev(ptr
, &s
, s2
);
543 /* if no characters were added, returns an empty string */
547 ptr
= mpdm_poke(ptr
, &s
, L
"", 1, sizeof(wchar_t));
548 return MPDM_ENS(ptr
, s
- 1);
553 * mpdm_ival - Returns a value's data as an integer.
556 * Returns a value's data as an integer. If the value is a string,
557 * it's converted via sscanf and returned; non-string values have all
558 * an ival of 0. The converted integer is cached, so costly string
559 * conversions are only done once. Values created with the MPDM_IVAL
560 * flag set have its ival cached from the beginning.
564 int mpdm_ival(mpdm_t v
)
569 /* if there is no cached integer, calculate it */
570 if (!(v
->flags
& MPDM_IVAL
)) {
573 /* if it's a string, calculate it; other
574 values will have an ival of 0 */
575 if (v
->flags
& MPDM_STRING
) {
579 wcstombs(tmp
, (wchar_t *) v
->data
, sizeof(tmp
));
580 tmp
[sizeof(tmp
) - 1] = '\0';
582 /* workaround for mingw32: as it doesn't
583 correctly parse octal and hexadecimal
584 numbers, they are tried as special cases */
586 if (tmp
[1] == 'x' || tmp
[1] == 'X')
592 sscanf(tmp
, fmt
, &i
);
603 * mpdm_rval - Returns a value's data as a real number (double).
606 * Returns a value's data as a real number (double float). If the value
607 * is a string, it's converted via sscanf and returned; non-string values
608 * have all an rval of 0. The converted double is cached, so costly string
609 * conversions are only done once. Values created with the MPDM_RVAL
610 * flag set have its rval cached from the beginning.
614 double mpdm_rval(mpdm_t v
)
619 /* if there is no cached double, calculate it */
620 if (!(v
->flags
& MPDM_RVAL
)) {
623 /* if it's a string, calculate it; other
624 values will have an rval of 0.0 */
625 if (v
->flags
& MPDM_STRING
) {
629 wcstombs(tmp
, (wchar_t *) v
->data
, sizeof(tmp
));
630 tmp
[sizeof(tmp
) - 1] = '\0';
632 /* if the number starts with 0, it's
633 an octal or hexadecimal number; just
634 take the integer value and cast it */
635 if (tmp
[0] == '0' && tmp
[1] != '.')
636 r
= (double) mpdm_ival(v
);
638 /* set locale to C for non locale-dependent
639 floating point conversion */
640 prev_locale
= setlocale(LC_NUMERIC
, "C");
643 sscanf(tmp
, "%lf", &r
);
645 /* set previous locale */
646 setlocale(LC_NUMERIC
, prev_locale
);
658 * mpdm_gettext - Translates a string to the current language.
661 * Translates the @str string to the current language.
663 * This function can still be used even if there is no real gettext
664 * support() by manually filling the __I18N__ hash.
666 * If the string is found in the current table, the translation is
667 * returned; otherwise, the same @str value is returned.
671 mpdm_t
mpdm_gettext(const mpdm_t str
)
676 /* gets the cache, if any */
677 if ((i18n
= mpdm_hget_s(mpdm_root(), L
"__I18N__")) == NULL
)
680 /* try first the cache */
681 if ((v
= mpdm_hget(i18n
, str
)) == NULL
) {
682 #ifdef CONFOPT_GETTEXT
686 v
= MPDM_2MBS(str
->data
);
688 /* ask gettext for it */
689 s
= gettext((char *) v
->data
);
691 /* create new value only if it's different */
695 /* store in the cache */
696 mpdm_hset(i18n
, str
, v
);
699 #endif /* CONFOPT_GETTEXT */
709 * mpdm_gettext_domain - Sets domain and data directory for translations.
710 * @dom: the domain (application name)
711 * @data: directory contaning the .mo files
713 * Sets the domain (application name) and translation data for translating
714 * strings that will be returned by mpdm_gettext().@data must point to a
715 * directory containing the .mo (compiled .po) files.
717 * If there is no gettext support, returns 0, or 1 otherwise.
721 int mpdm_gettext_domain(const mpdm_t dom
, const mpdm_t data
)
725 #ifdef CONFOPT_GETTEXT
730 /* convert both to mbs,s */
731 dm
= MPDM_2MBS(dom
->data
);
732 dt
= MPDM_2MBS(data
->data
);
734 /* bind and set domain */
735 bindtextdomain((char *) dm
->data
, (char *) dt
->data
);
736 textdomain((char *) dm
->data
);
738 mpdm_hset_s(mpdm_root(), L
"__I18N__", MPDM_H(0));
742 #endif /* CONFOPT_GETTEXT */
748 if ((v
= mpdm_hget_s(mpdm_root(), L
"ENV")) != NULL
&&
749 mpdm_hget_s(v
, L
"LANG") == NULL
) {
750 wchar_t *wptr
= L
"en";
752 /* MS Windows crappy language constants... */
754 switch((GetSystemDefaultLangID() & 0x00ff)) {
755 case 0x01: wptr
= L
"ar"; break; /* arabic */
756 case 0x02: wptr
= L
"bg"; break; /* bulgarian */
757 case 0x03: wptr
= L
"ca"; break; /* catalan */
758 case 0x04: wptr
= L
"zh"; break; /* chinese */
759 case 0x05: wptr
= L
"cz"; break; /* czech */
760 case 0x06: wptr
= L
"da"; break; /* danish */
761 case 0x07: wptr
= L
"de"; break; /* german */
762 case 0x08: wptr
= L
"el"; break; /* greek */
763 case 0x09: wptr
= L
"en"; break; /* english */
764 case 0x0a: wptr
= L
"es"; break; /* spanish */
765 case 0x0b: wptr
= L
"fi"; break; /* finnish */
766 case 0x0c: wptr
= L
"fr"; break; /* french */
767 case 0x0d: wptr
= L
"he"; break; /* hebrew */
768 case 0x0e: wptr
= L
"hu"; break; /* hungarian */
769 case 0x0f: wptr
= L
"is"; break; /* icelandic */
770 case 0x10: wptr
= L
"it"; break; /* italian */
771 case 0x11: wptr
= L
"jp"; break; /* japanese */
772 case 0x12: wptr
= L
"ko"; break; /* korean */
773 case 0x13: wptr
= L
"nl"; break; /* dutch */
774 case 0x14: wptr
= L
"no"; break; /* norwegian */
775 case 0x15: wptr
= L
"po"; break; /* polish */
776 case 0x16: wptr
= L
"pt"; break; /* portuguese */
777 case 0x17: wptr
= L
"rm"; break; /* romansh (switzerland) */
778 case 0x18: wptr
= L
"ro"; break; /* romanian */
779 case 0x19: wptr
= L
"ru"; break; /* russian */
780 case 0x1a: wptr
= L
"sr"; break; /* serbian */
781 case 0x1b: wptr
= L
"sk"; break; /* slovak */
782 case 0x1c: wptr
= L
"sq"; break; /* albanian */
783 case 0x1d: wptr
= L
"sv"; break; /* swedish */
786 mpdm_hset_s(v
, L
"LANG", MPDM_S(wptr
));
789 #endif /* CONFOPT_WIN32 */
795 #ifdef CONFOPT_WCWIDTH
797 int wcwidth(wchar_t);
799 int mpdm_wcwidth(wchar_t c
)
804 #else /* CONFOPT_WCWIDTH */
808 int mpdm_wcwidth(wchar_t c
)
810 return mk_wcwidth(c
);
813 #endif /* CONFOPT_WCWIDTH */
817 * mpdm_sprintf - Formats a sprintf()-like string
818 * @fmt: the string format
819 * @args: an array of values
821 * Formats a string using the sprintf() format taking the values from @args.
824 mpdm_t
mpdm_sprintf(const mpdm_t fmt
, const mpdm_t args
)
826 const wchar_t *i
= fmt
->data
;
831 /* loop all characters */
832 while ((c
= *i
++) != L
'\0') {
834 wchar_t *tptr
= NULL
;
835 wchar_t *wptr
= NULL
;
838 /* format directive */
847 /* transform the format to mbs */
848 while (*i
!= L
'\0' &&
849 m
< (int)(sizeof(t_fmt
) - MB_CUR_MAX
- 1) &&
850 wcschr(L
"-.0123456789", *i
) != NULL
)
851 m
+= wctomb(&t_fmt
[m
], *i
++);
853 /* transfer the directive */
854 m
+= wctomb(&t_fmt
[m
], *i
++);
858 /* by default, copies the format */
861 /* pick next value */
862 v
= mpdm_aget(args
, n
++);
864 switch (t_fmt
[m
- 1]) {
872 snprintf(tmp
, sizeof(tmp
) - 1,
873 t_fmt
, mpdm_ival(v
));
878 /* float (real) value */
879 snprintf(tmp
, sizeof(tmp
) - 1,
880 t_fmt
, mpdm_rval(v
));
886 ptr
= mpdm_wcstombs(mpdm_string(v
), NULL
);
887 snprintf(tmp
, sizeof(tmp
) - 1, t_fmt
, ptr
);
910 wptr
= tptr
= mpdm_mbstowcs(tmp
, &m
, -1);
919 o
= mpdm_poke(o
, &l
, wptr
, m
, sizeof(wchar_t));
921 /* free the temporary buffer, if any */
930 o
= mpdm_poke(o
, &l
, L
"", 1, sizeof(wchar_t));
932 return MPDM_ENS(o
, l
- 1);
937 * mpdm_ulc - Converts a string to uppercase or lowecase
939 * @u: convert to uppercase (1) or to lowercase (0).
941 * Converts @s to uppercase (for @u == 1) or to lowercase (@u == 0).
944 mpdm_t
mpdm_ulc(const mpdm_t s
, int u
)
948 int i
= mpdm_size(s
);
950 if ((optr
= malloc((i
+ 1) * sizeof(wchar_t))) != NULL
) {
951 wchar_t *iptr
= mpdm_string(s
);
954 for (n
= 0; n
< i
; n
++)
955 optr
[n
] = u
? towupper(iptr
[n
]) : towlower(iptr
[n
]);
958 r
= MPDM_ENS(optr
, i
);
965 mpdm_t
mpdm_scanf(const mpdm_t string
, const mpdm_t format
)
967 wchar_t *i
= (wchar_t *)string
->data
;
968 wchar_t *f
= (wchar_t *)format
->data
;
977 wchar_t yset
[1024] = L
"";
978 wchar_t nset
[1024] = L
"";
985 /* an asterisk? don't use next value */
991 /* does it have a size? */
992 while (wcschr(L
"0123456789", *f
)) {
998 /* if no size, set it to an arbitrary limit */
1002 /* now *f should contain a command */
1006 /* is it a number? */
1007 if (wcschr(L
"udixf", cmd
)) {
1008 wcscpy(yset
, L
"0123456789");
1013 wcscat(yset
, L
"xabcdefABCDEF");
1018 /* non-space string */
1020 wcscpy(nset
, L
" \t");
1028 /* is it an inverse set? */
1034 /* first one is a ]? add it */
1040 /* now build the set */
1041 for (; n
< sizeof(tmp
) - 1 && *f
&& *f
!= L
']'; f
++) {
1042 /* is it a range? */
1046 /* start or end? hyphen itself */
1047 if (n
== 0 || *f
== L
']')
1050 /* pick previous char */
1051 wchar_t c
= tmp
[n
- 1];
1054 while (n
< sizeof(tmp
) - 1 && c
< *f
)
1064 wcscpy(inv
? nset
: yset
, tmp
);
1067 /* now fill the dynamic string */
1068 while (vsize
&& !wcschr(nset
, *i
) && (yset
[0] == L
'\0' || wcschr(yset
, *i
))) {
1069 ptr
= mpdm_poke(ptr
, &size
, i
, 1, sizeof(wchar_t));
1074 /* null terminate */
1075 ptr
= mpdm_poke(ptr
, &size
, L
"", 1, sizeof(wchar_t));
1078 mpdm_push(r
, MPDM_ENS(ptr
, size
));
1083 /* test for literals in the format string */