annotate th_string.c @ 789:d61d3eb29053 default tip

Bump copyright.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 08 Mar 2024 15:26:24 +0200
parents 79f888e4616f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Miscellaneous string-handling related utility-functions
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
726
29e44a58bc73 Bump copyrights.
Matti Hamalainen <ccr@tnsp.org>
parents: 722
diff changeset
4 * (C) Copyright 2002-2022 Tecnic Software productions (TNSP)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
8 #include "th_util.h"
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include "th_string.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
531
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents: 529
diff changeset
11 // Include printf implementation
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents: 529
diff changeset
12 #include "th_printf.c"
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents: 529
diff changeset
13
768
600a3c08747f Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents: 744
diff changeset
14 // Include strmatch / glob function implementations
600a3c08747f Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents: 744
diff changeset
15 #define TH_STRGLOB_IMPL 1
600a3c08747f Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents: 744
diff changeset
16
600a3c08747f Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents: 744
diff changeset
17 #define TH_STRGLOB_FUNC th_strmatch
600a3c08747f Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents: 744
diff changeset
18 #define TH_STRGLOB_COLLATE(pch) (pch)
600a3c08747f Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents: 744
diff changeset
19 #include "th_strglob.c"
600a3c08747f Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents: 744
diff changeset
20
600a3c08747f Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents: 744
diff changeset
21 #define TH_STRGLOB_FUNC th_strcasematch
600a3c08747f Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents: 744
diff changeset
22 #define TH_STRGLOB_COLLATE(pch) th_tolower(pch)
600a3c08747f Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents: 744
diff changeset
23 #include "th_strglob.c"
600a3c08747f Add handle_escapes parameter to th_str{case}match() functions to
Matti Hamalainen <ccr@tnsp.org>
parents: 744
diff changeset
24
80
12f1af4ffc6d Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
25
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
26 /**
687
5a7254b78614 Add th_strchr().
Matti Hamalainen <ccr@tnsp.org>
parents: 683
diff changeset
27 * Implementation of strchr() for th_char_t.
702
5653e386730b Fix th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 701
diff changeset
28 * @param[in] src string to find @p valid from
687
5a7254b78614 Add th_strchr().
Matti Hamalainen <ccr@tnsp.org>
parents: 683
diff changeset
29 * @returns pointer to the match position, NULL if no match found
5a7254b78614 Add th_strchr().
Matti Hamalainen <ccr@tnsp.org>
parents: 683
diff changeset
30 */
702
5653e386730b Fix th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 701
diff changeset
31 th_char_t *th_strchr(const th_char_t *str, const th_char_t valid)
687
5a7254b78614 Add th_strchr().
Matti Hamalainen <ccr@tnsp.org>
parents: 683
diff changeset
32 {
702
5653e386730b Fix th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 701
diff changeset
33 for (th_char_t *ch = (th_char_t *) str; *ch; ch++)
687
5a7254b78614 Add th_strchr().
Matti Hamalainen <ccr@tnsp.org>
parents: 683
diff changeset
34 {
702
5653e386730b Fix th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 701
diff changeset
35 if (*ch == valid)
5653e386730b Fix th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 701
diff changeset
36 return ch;
687
5a7254b78614 Add th_strchr().
Matti Hamalainen <ccr@tnsp.org>
parents: 683
diff changeset
37 }
5a7254b78614 Add th_strchr().
Matti Hamalainen <ccr@tnsp.org>
parents: 683
diff changeset
38 return NULL;
5a7254b78614 Add th_strchr().
Matti Hamalainen <ccr@tnsp.org>
parents: 683
diff changeset
39 }
5a7254b78614 Add th_strchr().
Matti Hamalainen <ccr@tnsp.org>
parents: 683
diff changeset
40
5a7254b78614 Add th_strchr().
Matti Hamalainen <ccr@tnsp.org>
parents: 683
diff changeset
41
5a7254b78614 Add th_strchr().
Matti Hamalainen <ccr@tnsp.org>
parents: 683
diff changeset
42 /**
701
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
43 * Implementation of strpbrk() for th_char_t.
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
44 * @param[in] src string to find any th_char_t in @p valid from
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
45 * @returns pointer to the match position, NULL if no match found
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
46 */
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
47 th_char_t *th_strpbrk(const th_char_t *str, const th_char_t *valid)
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
48 {
702
5653e386730b Fix th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 701
diff changeset
49 for (th_char_t *ch = (th_char_t *) str; *ch; ch++)
701
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
50 {
702
5653e386730b Fix th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 701
diff changeset
51 for (const th_char_t *cmp = valid; *cmp; cmp++)
701
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
52 {
702
5653e386730b Fix th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 701
diff changeset
53 if (*ch == *cmp)
5653e386730b Fix th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 701
diff changeset
54 return ch;
701
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
55 }
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
56 }
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
57 return NULL;
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
58 }
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
59
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
60
d687bbb54c1a Add th_strpbrk().
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
61 /**
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
62 * Implementation of strdup() with a @c NULL check.
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
63 * @param[in] src string to create a copy of
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
64 * @returns copy of the given string, or @c NULL if @p src was @c NULL or memory could not be allocated.
8
acc2a8035dd5 Re-add th_strdup().
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
65 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
66 th_char_t *th_strdup(const th_char_t *src)
8
acc2a8035dd5 Re-add th_strdup().
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
67 {
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
68 th_char_t *res;
522
b4884e59fdd3 Simplify th_strdup().
Matti Hamalainen <ccr@tnsp.org>
parents: 507
diff changeset
69 size_t len;
b4884e59fdd3 Simplify th_strdup().
Matti Hamalainen <ccr@tnsp.org>
parents: 507
diff changeset
70
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
71 if (src == NULL)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
72 return NULL;
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
73
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
74 len = th_strlen(src);
665
4932188c9101 Oops, missed one case of * sizeof(th_char_t).
Matti Hamalainen <ccr@tnsp.org>
parents: 663
diff changeset
75 if ((res = th_malloc((len + 1) * sizeof(th_char_t))) == NULL)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
76 return NULL;
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
77
665
4932188c9101 Oops, missed one case of * sizeof(th_char_t).
Matti Hamalainen <ccr@tnsp.org>
parents: 663
diff changeset
78 memcpy(res, src, (len + 1) * sizeof(th_char_t));
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
79 return res;
8
acc2a8035dd5 Re-add th_strdup().
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
80 }
acc2a8035dd5 Re-add th_strdup().
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
81
acc2a8035dd5 Re-add th_strdup().
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
82
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
83 /**
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
84 * Implementation of strndup() with @c NULL check. Copies up to @p n
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
85 * characters from the source. A NUL terminator is always added,
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
86 * unless @p src was @c NULL or memory could not be allocated, in which
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
87 * case @c NULL pointer is returned.
659
da9618444c81 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
88 *
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
89 * @param[in] src string pointer to be copied from
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
90 * @param[in] n maximum number of characters to copy
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
91 * @returns the resulting string or @c NULL pointer
81
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
92 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
93 th_char_t *th_strndup(const th_char_t *src, const size_t n)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 {
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
95 th_char_t *res;
81
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
96 size_t len;
79
909ae72c0763 Remove th_strncpy().
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
97
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
98 if (src == NULL)
81
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
99 return NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100
714
ec6a3790a91b Do not use th_strlen() in th_strndup(), as the string might not have a
Matti Hamalainen <ccr@tnsp.org>
parents: 707
diff changeset
101 for (len = 0; len < n && src[len]; len++);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
103 if ((res = th_malloc((len + 1) * sizeof(th_char_t))) == NULL)
81
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
104 return NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
106 memcpy(res, src, len * sizeof(th_char_t));
81
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
107 res[len] = 0;
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
108
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
109 return res;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
113 /**
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
114 * Helper function for th_strdup_trim() and friends. Copies @p len characters from
630
d3aace9903fa Doxygen improvements.
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
115 * given string, and trims whitespace from it according to specified @p flags.
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
116 * See TH_TRIM_* in th_string.h. If @p len or the resulting trimmed string would
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
117 * be empty (length 0), no copy is allocated and a @c NULL pointer is returned.
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
118 * @param[in] src source string (does not need to be NUL terminated, as length must be specified)
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
119 * @param[in] len length of the source string, or desired number of characters to copy at maximum
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
120 * @param[in] flags trimming flags, see TH_TRIM_* in th_string.h
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
121 * @returns the resulting string or @c NULL pointer
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
122 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
123 static th_char_t * th_strdup_trim_do(const th_char_t *src, size_t len, const int flags)
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
124 {
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
125 th_char_t *res;
220
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
126 size_t start, end;
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
127
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
128 if (len == 0)
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
129 return NULL;
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
130
174
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
131 // Trim start: find first non-whitespace character
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
132 if (flags & TH_TRIM_START)
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
133 for (start = 0; start < len && th_isspace(src[start]); start++);
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
134 else
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
135 start = 0;
174
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
136
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
137 // Trim end: find last non-whitespace character
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
138 if (flags & TH_TRIM_END)
224
128c8cb80205 Fix th_str(n)dup_trim() functions. :S
Matti Hamalainen <ccr@tnsp.org>
parents: 223
diff changeset
139 for (end = len - 1; end > start && th_isspace(src[end]); end--);
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
140 else
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
141 end = len;
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
142
174
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
143 // Allocate memory for result
224
128c8cb80205 Fix th_str(n)dup_trim() functions. :S
Matti Hamalainen <ccr@tnsp.org>
parents: 223
diff changeset
144 if (src[end] == 0 || th_isspace(src[end]))
222
723a69185102 Fix th_str{n}dup_trim() functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 221
diff changeset
145 return NULL;
723a69185102 Fix th_str{n}dup_trim() functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 221
diff changeset
146
224
128c8cb80205 Fix th_str(n)dup_trim() functions. :S
Matti Hamalainen <ccr@tnsp.org>
parents: 223
diff changeset
147 len = end - start + 1;
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
148 if ((res = th_malloc((len + 1) * sizeof(th_char_t))) == NULL)
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
149 return NULL;
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
150
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
151 memcpy(res, src + start, len * sizeof(th_char_t));
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
152 res[len] = 0;
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
153 return res;
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
154 }
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
155
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
156
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
157 /**
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
158 * Create copy of the given string, but trim the result according to specified @p flags.
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
159 * See TH_TRIM_* in th_string.h. If length the resulting trimmed string would
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
160 * be empty (0), no copy is allocated and a NULL pointer is returned.
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
161 * @param[in] src source string
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
162 * @param[in] flags trimming flags, see TH_TRIM_* in th_string.h
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
163 * @returns the resulting string or @c NULL pointer
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
164 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
165 th_char_t *th_strdup_trim(const th_char_t *src, const int flags)
220
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
166 {
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
167 if (src == NULL)
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
168 return NULL;
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
169
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
170 return th_strdup_trim_do(src, th_strlen(src), flags);
220
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
171 }
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
172
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
173
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
174 /**
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
175 * Create copy of the given string @p src, up to @p n characters (or less, if the string
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
176 * is shorter.) The result is trimmed according to specified @p flags.
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
177 * See TH_TRIM_* in th_string.h. If @p n or the resulting trimmed string would
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
178 * be empty (length 0), no copy is allocated and a NULL pointer is returned.
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
179 * @param[in] src source string
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
180 * @param[in] n maximum number of characters to copy from the source string
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
181 * @param[in] flags trimming flags, see TH_TRIM_* in th_string.h
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
182 * @returns the resulting string or @c NULL pointer
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
183 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
184 th_char_t *th_strndup_trim(const th_char_t *src, const size_t n, const int flags)
220
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
185 {
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
186 size_t len;
659
da9618444c81 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
187
220
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
188 if (src == NULL || n == 0)
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
189 return NULL;
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
190
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
191 for (len = 0; len < n && src[len]; len++);
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
192
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
193 return th_strdup_trim_do(src, len, flags);
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
194 }
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
195
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
196
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
197 th_char_t *th_strndup_no0(const th_char_t *src, const size_t len)
487
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
198 {
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
199 th_char_t *res;
488
8917c39ef6c5 Oops. Forgot to declare a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 487
diff changeset
200
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
201 if ((res = th_malloc((len + 1) * sizeof(th_char_t))) == NULL)
487
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
202 return NULL;
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
203
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
204 memcpy(res, src, len * sizeof(th_char_t));
487
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
205 res[len] = 0;
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
206
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
207 return res;
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
208 }
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
209
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
210
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
211 th_char_t *th_strndup_no0_trim(const th_char_t *src, const size_t len, const int flags)
487
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
212 {
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
213 return th_strdup_trim_do(src, len, flags);
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
214 }
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
215
702c64a6c570 Add new string helper functions th_strndup_no0() and th_strndup_no0_trim()
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
216
239
10f596441e75 Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents: 238
diff changeset
217 #ifdef TH_USE_INTERNAL_SPRINTF
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
218 static int th_pbuf_vputch(th_vprintf_ctx *ctx, const th_char_t ch)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
219 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
220 if (ctx->pos < ctx->size)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
221 ctx->buf[ctx->pos] = ch;
283
2b76cc316205 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 281
diff changeset
222
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
223 ctx->pos++;
279
15e2ee6c7bfc Add th_printf_ctx.ipos and use it for return values. It's a hack. :S
Matti Hamalainen <ccr@tnsp.org>
parents: 278
diff changeset
224 ctx->ipos++;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
225 return ch;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
226 }
239
10f596441e75 Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents: 238
diff changeset
227 #endif
10f596441e75 Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents: 238
diff changeset
228
10f596441e75 Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents: 238
diff changeset
229
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
230 /**
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
231 * Wrapper for either libc vsnprintf() or th-libs internal
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
232 * vsnprintf() implementation, as determined by a compile-time define.
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
233 * @param[out] buf pointer to buffer to print to
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
234 * @param[in] size available space in the buffer in bytes
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
235 * @param[in] fmt format string
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
236 * @param[in] ap a va_list variable arguments list structure
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
237 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
238 int th_vsnprintf(th_char_t *buf, size_t size, const th_char_t *fmt, va_list ap)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
239 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
240 #ifdef TH_USE_INTERNAL_SPRINTF
252
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
241 int ret;
374
1d8ae82304ec Rename s/th_printf_ctx/th_vprintf_cts/g.
Matti Hamalainen <ccr@tnsp.org>
parents: 373
diff changeset
242 th_vprintf_ctx ctx;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
243 ctx.buf = buf;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
244 ctx.size = size;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
245 ctx.pos = 0;
281
0c70dcfb6796 Remember to initialize th_printf_ctx.ipos to 0!
Matti Hamalainen <ccr@tnsp.org>
parents: 280
diff changeset
246 ctx.ipos = 0;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
247
568
2fbe42d957c4 Actually, partially revert the previous commit and unbreak the API. Leave
Matti Hamalainen <ccr@tnsp.org>
parents: 567
diff changeset
248 ret = th_vprintf_do(&ctx, th_pbuf_vputch, fmt, ap);
252
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
249
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
250 if (ctx.pos < size)
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
251 buf[ctx.pos] = 0;
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
252 else
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
253 if (size > 0)
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
254 buf[size - 1] = 0;
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
255
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
256 return ret;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
257 #else
237
c55ebc438243 Oops, s/vsnpintf/vsnprintf/.
Matti Hamalainen <ccr@tnsp.org>
parents: 236
diff changeset
258 return vsnprintf(buf, size, fmt, ap);
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
259 #endif
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
260 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
261
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
262
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
263 /**
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
264 * Wrapper for either libc snprintf() or th-libs internal
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
265 * snprintf() implementation, as determined by a compile-time define.
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
266 * @param[out] buf pointer to buffer to print to
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
267 * @param[in] size available space in the buffer in bytes
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
268 * @param[in] fmt format string
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
269 * @param[in] ... variable arguments
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
270 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
271 int th_snprintf(th_char_t *buf, size_t size, const th_char_t *fmt, ...)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
272 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
273 int n;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
274 va_list ap;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
275 va_start(ap, fmt);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
276 #ifdef TH_USE_INTERNAL_SPRINTF
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
277 n = th_vsnprintf(buf, size, fmt, ap);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
278 #else
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
279 n = vsnprintf(buf, size, fmt, ap);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
280 #endif
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
281 va_end(ap);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
282 return n;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
283 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
284
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
285
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
286 #ifdef TH_USE_INTERNAL_SPRINTF
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
287 static int th_stdio_vputch(th_vprintf_ctx *ctx, const th_char_t ch)
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
288 {
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
289 ctx->pos++;
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
290 ctx->ipos++;
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
291 return fputc(ch, (FILE *) ctx->data);
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
292 }
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
293 #endif
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
294
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
295
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
296 int th_vfprintf(FILE *fh, const th_char_t *fmt, va_list ap)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
297 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
298 #ifdef TH_USE_INTERNAL_SPRINTF
374
1d8ae82304ec Rename s/th_printf_ctx/th_vprintf_cts/g.
Matti Hamalainen <ccr@tnsp.org>
parents: 373
diff changeset
299 th_vprintf_ctx ctx;
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
300 ctx.data = (void *) fh;
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
301 ctx.pos = 0;
281
0c70dcfb6796 Remember to initialize th_printf_ctx.ipos to 0!
Matti Hamalainen <ccr@tnsp.org>
parents: 280
diff changeset
302 ctx.ipos = 0;
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
303
568
2fbe42d957c4 Actually, partially revert the previous commit and unbreak the API. Leave
Matti Hamalainen <ccr@tnsp.org>
parents: 567
diff changeset
304 return th_vprintf_do(&ctx, th_stdio_vputch, fmt, ap);
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
305 #else
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
306 return vfprintf(fh, fmt, ap);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
307 #endif
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
308 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
309
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
310
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
311 int th_fprintf(FILE *fh, const th_char_t *fmt, ...)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
312 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
313 int ret;
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
314 va_list ap;
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
315 #ifdef TH_USE_INTERNAL_SPRINTF
374
1d8ae82304ec Rename s/th_printf_ctx/th_vprintf_cts/g.
Matti Hamalainen <ccr@tnsp.org>
parents: 373
diff changeset
316 th_vprintf_ctx ctx;
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
317 #endif
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
318
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
319 va_start(ap, fmt);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
320 #ifdef TH_USE_INTERNAL_SPRINTF
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
321 ctx.data = (void *) fh;
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
322 ctx.pos = 0;
281
0c70dcfb6796 Remember to initialize th_printf_ctx.ipos to 0!
Matti Hamalainen <ccr@tnsp.org>
parents: 280
diff changeset
323 ctx.ipos = 0;
0c70dcfb6796 Remember to initialize th_printf_ctx.ipos to 0!
Matti Hamalainen <ccr@tnsp.org>
parents: 280
diff changeset
324
568
2fbe42d957c4 Actually, partially revert the previous commit and unbreak the API. Leave
Matti Hamalainen <ccr@tnsp.org>
parents: 567
diff changeset
325 ret = th_vprintf_do(&ctx, th_stdio_vputch, fmt, ap);
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
326 #else
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
327 ret = fprintf(fh, fmt, ap);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
328 #endif
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
329 va_end(ap);
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
330
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
331 return ret;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
332 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
333
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
334
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
335 /* Simulate a sprintf() that allocates memory
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
336 */
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
337 #ifdef TH_USE_INTERNAL_SPRINTF
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
338 static int th_pbuf_alloc_vputch1(th_vprintf_ctx *ctx, const th_char_t ch)
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
339 {
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
340 ctx->pos++;
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
341 return ch;
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
342 }
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
343
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
344 static int th_pbuf_alloc_vputch2(th_vprintf_ctx *ctx, const th_char_t ch)
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
345 {
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
346 ctx->buf[ctx->pos++] = ch;
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
347 return ch;
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
348 }
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
349 #endif
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
350
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
351
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
352 /**
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
353 * Combination of vsprintf() and strdup. Automatically allocates memory
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
354 * for the resulting string.
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
355 * @param[in] fmt format string
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
356 * @param[in] args variable arguments list structure
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
357 * @returns the resulting string or @c NULL pointer in case of error
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
358 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
359 th_char_t *th_strdup_vprintf(const th_char_t *fmt, va_list args)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 {
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
361 #ifdef TH_USE_INTERNAL_SPRINTF
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
362 // Using internal printf() implementation
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
363 th_vprintf_ctx ctx;
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
364 va_list ap;
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
365
659
da9618444c81 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
366 // Get the size of the output
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
367 va_copy(ap, args);
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
368 ctx.pos = 0;
568
2fbe42d957c4 Actually, partially revert the previous commit and unbreak the API. Leave
Matti Hamalainen <ccr@tnsp.org>
parents: 567
diff changeset
369 th_vprintf_do(&ctx, th_pbuf_alloc_vputch1, fmt, ap);
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
370 va_end(ap);
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
371
659
da9618444c81 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
372 // Allocate memory for it
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
373 ctx.size = ctx.pos + 1;
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
374 if ((ctx.buf = th_malloc(ctx.size * sizeof(th_char_t))) == NULL)
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
375 return NULL;
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
376
659
da9618444c81 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 630
diff changeset
377 // Print the final result into the buffer
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
378 va_copy(ap, args);
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
379 ctx.pos = 0;
568
2fbe42d957c4 Actually, partially revert the previous commit and unbreak the API. Leave
Matti Hamalainen <ccr@tnsp.org>
parents: 567
diff changeset
380 th_vprintf_do(&ctx, th_pbuf_alloc_vputch2, fmt, ap);
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
381 va_end(ap);
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
382 ctx.buf[ctx.pos] = 0;
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
383
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
384 return ctx.buf;
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
385
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
386 #else
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
387 // Using libc vsnprintf()
133
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
388 int size = 64;
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
389 th_char_t *buf, *tmp;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390
260
ab18ebbb5529 Check for NULL fmt.
Matti Hamalainen <ccr@tnsp.org>
parents: 256
diff changeset
391 if (fmt == NULL)
ab18ebbb5529 Check for NULL fmt.
Matti Hamalainen <ccr@tnsp.org>
parents: 256
diff changeset
392 return NULL;
ab18ebbb5529 Check for NULL fmt.
Matti Hamalainen <ccr@tnsp.org>
parents: 256
diff changeset
393
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
394 if ((buf = th_malloc(size * sizeof(th_char_t))) == NULL)
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
395 return NULL;
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
396
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
397 while (1)
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
398 {
37
3986b139bbbd Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
399 int n;
3986b139bbbd Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
400 va_list ap;
3986b139bbbd Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
401 va_copy(ap, args);
3986b139bbbd Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
402 n = vsnprintf(buf, size, fmt, ap);
3986b139bbbd Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
403 va_end(ap);
3986b139bbbd Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
404
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
405 if (n > -1 && n < size)
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
406 return buf;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
407 if (n > -1)
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
408 size = n + 1;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
409 else
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
410 size *= 2;
37
3986b139bbbd Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
411
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
412 if ((tmp = th_realloc(buf, size * sizeof(th_char_t))) == NULL)
133
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
413 {
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
414 th_free(buf);
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
415 return NULL;
133
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
416 }
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
417 else
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
418 buf = tmp;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
419 }
549
dfd6dcc486a6 Implement optional use of internal printf() implementation for use in th_strdup_vprintf().
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
420 #endif
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
421 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
423
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
424 /**
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
425 * Combination of sprintf() and strdup. Automatically allocates memory
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
426 * for the resulting string.
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
427 * @param[in] fmt format string
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
428 * @param[in] ... optional printf arguments
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
429 * @returns the resulting string or @c NULL pointer in case of error
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
430 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
431 th_char_t *th_strdup_printf(const th_char_t *fmt, ...)
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
432 {
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
433 th_char_t *res;
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
434 va_list ap;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
435
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
436 va_start(ap, fmt);
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
437 res = th_strdup_vprintf(fmt, ap);
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
438 va_end(ap);
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
439
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
440 return res;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
444 /**
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
445 * A helper function that is given a pointer to a pointer of string,
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
446 * which will be automatically freed (if necessary) and replaced with
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
447 * a pointer to the newly created string.
770
79f888e4616f Fix Doxygen.
Matti Hamalainen <ccr@tnsp.org>
parents: 768
diff changeset
448 * @param[in,out] buf pointer to a th_char_t pointer/string
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
449 * @param[in] fmt format string
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
450 * @param[in] args variable arguments list structure
744
4181d43f91f9 Change th_pstr_printf()/th_pstr_vprintf() to return pointer to the newly
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
451 * @returns the resulting string or @c NULL pointer in case of error
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
452 */
744
4181d43f91f9 Change th_pstr_printf()/th_pstr_vprintf() to return pointer to the newly
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
453 th_char_t *th_pstr_vprintf(th_char_t **buf, const th_char_t *fmt, va_list ap)
30
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
454 {
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
455 th_char_t *tmp = th_strdup_vprintf(fmt, ap);
30
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
456 th_free(*buf);
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
457 *buf = tmp;
744
4181d43f91f9 Change th_pstr_printf()/th_pstr_vprintf() to return pointer to the newly
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
458 return tmp;
30
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
459 }
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
460
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
461
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
462 /**
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
463 * A helper function that is given a pointer to a pointer of string,
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
464 * which will be automatically freed (if necessary) and replaced with
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
465 * a pointer to the newly created string.
770
79f888e4616f Fix Doxygen.
Matti Hamalainen <ccr@tnsp.org>
parents: 768
diff changeset
466 * @param[in,out] buf pointer to a th_char_t pointer/string
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
467 * @param[in] fmt format string
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
468 * @param[in] ... optional printf arguments
744
4181d43f91f9 Change th_pstr_printf()/th_pstr_vprintf() to return pointer to the newly
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
469 * @returns the resulting string or @c NULL pointer in case of error
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
470 */
744
4181d43f91f9 Change th_pstr_printf()/th_pstr_vprintf() to return pointer to the newly
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
471 th_char_t *th_pstr_printf(th_char_t **buf, const th_char_t *fmt, ...)
30
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
472 {
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
473 th_char_t *tmp;
30
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
474 va_list ap;
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
475
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
476 va_start(ap, fmt);
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
477 tmp = th_strdup_vprintf(fmt, ap);
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
478 va_end(ap);
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
479
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
480 th_free(*buf);
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
481 *buf = tmp;
744
4181d43f91f9 Change th_pstr_printf()/th_pstr_vprintf() to return pointer to the newly
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
482 return tmp;
30
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
483 }
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
484
88
d3d3cd41a95a Slight adjustments.
Matti Hamalainen <ccr@tnsp.org>
parents: 87
diff changeset
485
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486 /* Compare two strings ignoring case [strcasecmp, strncasecmp]
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
487 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
488 int th_strcasecmp(const th_char_t *haystack, const th_char_t *needle)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 {
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
490 const th_char_t *s1 = haystack, *s2 = needle;
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
491 assert(haystack != NULL);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
492 assert(needle != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
494 if (haystack == needle)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
495 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496
262
e459a28ee1be Found some nasty bugs hiding in th_str{n}casecmp() functions via unit tests. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 260
diff changeset
497 while (*s1 && *s2)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
498 {
262
e459a28ee1be Found some nasty bugs hiding in th_str{n}casecmp() functions via unit tests. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 260
diff changeset
499 int k = th_tolower(*s1) - th_tolower(*s2);
e459a28ee1be Found some nasty bugs hiding in th_str{n}casecmp() functions via unit tests. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 260
diff changeset
500 if (k != 0)
283
2b76cc316205 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 281
diff changeset
501 return k;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
502 s1++;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
503 s2++;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
504 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505
690
953e16582f25 Fix th_strcasecmp() and th_strncasecmp() for certain .. cases.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
506 return th_tolower(*s1) - th_tolower(*s2);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
508
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
510 int th_strncasecmp(const th_char_t *haystack, const th_char_t *needle, size_t n)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511 {
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
512 const th_char_t *s1 = haystack, *s2 = needle;
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
513 assert(haystack != NULL);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
514 assert(needle != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
515
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
516 if (haystack == needle)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
517 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518
262
e459a28ee1be Found some nasty bugs hiding in th_str{n}casecmp() functions via unit tests. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 260
diff changeset
519 while (n > 0 && *s1 && *s2)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
520 {
262
e459a28ee1be Found some nasty bugs hiding in th_str{n}casecmp() functions via unit tests. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 260
diff changeset
521 int k = th_tolower(*s1) - th_tolower(*s2);
e459a28ee1be Found some nasty bugs hiding in th_str{n}casecmp() functions via unit tests. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 260
diff changeset
522 if (k != 0)
283
2b76cc316205 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 281
diff changeset
523 return k;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
524 s1++;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
525 s2++;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
526 n--;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
527 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528
715
6f627f4f11db Fix th_strncasecmp() edge case.
Matti Hamalainen <ccr@tnsp.org>
parents: 714
diff changeset
529 return (n == 0) ? 0 : th_tolower(*s1) - th_tolower(*s2);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532
89
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
533 /* Check if end of the given string str matches needle
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
534 * case-insensitively, return pointer to start of the match,
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
535 * if found, NULL otherwise.
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
536 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
537 th_char_t *th_strrcasecmp(th_char_t *str, const th_char_t *needle)
89
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
538 {
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
539 if (str == NULL || needle == NULL)
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
540 return NULL;
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
541 else
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
542 {
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
543 const size_t
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
544 slen = th_strlen(str),
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
545 nlen = th_strlen(needle);
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
546
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
547 if (slen < nlen)
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
548 return NULL;
89
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
549
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
550 if (th_strcasecmp(str + slen - nlen, needle) == 0)
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
551 return str + slen - nlen;
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
552 else
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
553 return NULL;
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
554 }
89
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
555 }
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
556
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
557
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
558 /* Copy a given string over in *pdst.
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
560 int th_pstr_cpy(th_char_t **pdst, const th_char_t *src)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 {
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
562 size_t slen;
696
9bbacd72df3d Allow src and dst to "overlap" for th_pstr_cpy().
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
563 th_char_t *tmp;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
565 if (pdst == NULL || src == NULL)
559
6021ef8fc341 Return proper THERR_ error codes from th_pstr_*()
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
566 return THERR_NULLPTR;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
568 slen = th_strlen(src);
696
9bbacd72df3d Allow src and dst to "overlap" for th_pstr_cpy().
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
569 if ((tmp = th_malloc((slen + 1) * sizeof(th_char_t))) == NULL)
559
6021ef8fc341 Return proper THERR_ error codes from th_pstr_*()
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
570 return THERR_MALLOC;
6021ef8fc341 Return proper THERR_ error codes from th_pstr_*()
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
571
696
9bbacd72df3d Allow src and dst to "overlap" for th_pstr_cpy().
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
572 memcpy(tmp, src, (slen + 1) * sizeof(th_char_t));
9bbacd72df3d Allow src and dst to "overlap" for th_pstr_cpy().
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
573
9bbacd72df3d Allow src and dst to "overlap" for th_pstr_cpy().
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
574 th_free(*pdst);
9bbacd72df3d Allow src and dst to "overlap" for th_pstr_cpy().
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
575 *pdst = tmp;
559
6021ef8fc341 Return proper THERR_ error codes from th_pstr_*()
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
576
6021ef8fc341 Return proper THERR_ error codes from th_pstr_*()
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
577 return THERR_OK;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
580
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
581 /* Concatenates a given string into string pointed by *pdst.
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
582 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
583 int th_pstr_cat(th_char_t **pdst, const th_char_t *src)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
584 {
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
585 if (pdst == NULL || src == NULL)
559
6021ef8fc341 Return proper THERR_ error codes from th_pstr_*()
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
586 return THERR_NULLPTR;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
587
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
588 if (*pdst != NULL)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
589 {
559
6021ef8fc341 Return proper THERR_ error codes from th_pstr_*()
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
590 size_t dlen = strlen(*pdst), slen = strlen(src);
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
591 if ((*pdst = th_realloc(*pdst, (dlen + slen + 1) * sizeof(th_char_t))) == NULL)
559
6021ef8fc341 Return proper THERR_ error codes from th_pstr_*()
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
592 return THERR_MALLOC;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
594 memcpy((*pdst) + dlen, src, (slen + 1) * sizeof(th_char_t));
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
595 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
596 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
597 {
559
6021ef8fc341 Return proper THERR_ error codes from th_pstr_*()
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
598 size_t slen = strlen(src);
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
599 if ((*pdst = th_malloc((slen + 1) * sizeof(th_char_t))) == NULL)
559
6021ef8fc341 Return proper THERR_ error codes from th_pstr_*()
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
600 return THERR_MALLOC;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
601
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
602 memcpy(*pdst, src, (slen + 1) * sizeof(th_char_t));
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
603 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
604
559
6021ef8fc341 Return proper THERR_ error codes from th_pstr_*()
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
605 return THERR_OK;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
606 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
608
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
609 int th_split_string_elems(const th_char_t *str, th_strelems_t *ctx, const th_char_t *sep)
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
610 {
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
611 size_t start = 0, end;
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
612 bool match = false;
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
613
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
614 if (str == NULL || ctx == NULL || sep == NULL)
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
615 return THERR_NULLPTR;
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
616
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
617 ctx->elems = NULL;
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
618 ctx->nelems = 0;
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
619
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
620 do
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
621 {
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
622 // Split foremost str element out
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
623 match = false;
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
624 for (end = start; str[end] != 0; end++)
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
625 {
687
5a7254b78614 Add th_strchr().
Matti Hamalainen <ccr@tnsp.org>
parents: 683
diff changeset
626 if (th_strchr(sep, str[end]))
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
627 {
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
628 match = true;
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
629 break;
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
630 }
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
631 }
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
632
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
633 // If the element is there, create it
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
634 if (str[start] != 0 && end >= start)
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
635 {
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
636 th_char_t *elem = th_strndup(str + start, end - start);
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
637 if (elem == NULL)
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
638 return THERR_MALLOC;
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
639
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
640 if ((ctx->elems = th_realloc(ctx->elems, sizeof(th_char_t **) * (ctx->nelems + 1))) == NULL)
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
641 return THERR_MALLOC;
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
642
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
643 ctx->elems[ctx->nelems] = elem;
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
644 ctx->nelems++;
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
645 }
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
646
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
647 start = end + 1;
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
648 } while (match);
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
649
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
650 return THERR_OK;
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
651 }
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
652
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
653
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
654 int th_split_string(const th_char_t *str, th_char_t ***elems, size_t *nelems, const th_char_t *sep)
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
655 {
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
656 th_strelems_t ctx;
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
657 int res;
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
658
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
659 if (elems == NULL || nelems == NULL)
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
660 return THERR_NULLPTR;
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
661
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
662 if ((res = th_split_string_elems(str, &ctx, sep)) == THERR_OK)
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
663 return res;
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
664
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
665 *elems = ctx.elems;
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
666 *nelems = ctx.nelems;
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
667
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
668 return THERR_OK;
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
669 }
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
670
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
671
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
672 int th_join_string_elems(th_char_t **str, const th_strelems_t *ctx, const th_char_t *sep)
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
673 {
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
674 size_t len, n, offs, seplen, *elemlens;
706
d289fae3c1a8 Oops, forgot to free elemlens in th_join_string_elems() in no-error case. Ugh.
Matti Hamalainen <ccr@tnsp.org>
parents: 702
diff changeset
675 int res = THERR_OK;
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
676
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
677 if (str == NULL || ctx == NULL || sep == NULL)
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
678 return THERR_NULLPTR;
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
679
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
680 if ((elemlens = th_malloc(ctx->nelems * sizeof(size_t))) == NULL)
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
681 return THERR_MALLOC;
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
682
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
683 seplen = th_strlen(sep);
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
684
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
685 for (len = n = 0; n < ctx->nelems; n++)
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
686 {
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
687 len += elemlens[n] = th_strlen(ctx->elems[n]);
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
688 }
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
689
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
690 len += 1 + n * seplen;
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
691
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
692 if ((*str = th_malloc(len)) == NULL)
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
693 {
706
d289fae3c1a8 Oops, forgot to free elemlens in th_join_string_elems() in no-error case. Ugh.
Matti Hamalainen <ccr@tnsp.org>
parents: 702
diff changeset
694 res = THERR_MALLOC;
d289fae3c1a8 Oops, forgot to free elemlens in th_join_string_elems() in no-error case. Ugh.
Matti Hamalainen <ccr@tnsp.org>
parents: 702
diff changeset
695 goto out;
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
696 }
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
697
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
698 for (offs = n = 0; n < ctx->nelems; n++)
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
699 {
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
700 if (n > 0)
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
701 {
679
83a48de05fe9 Add in th_strlen() which is a custom function if TH_CHAR_TYPE is defined.
Matti Hamalainen <ccr@tnsp.org>
parents: 674
diff changeset
702 memcpy((*str) + offs, sep, seplen * sizeof(th_char_t));
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
703 offs += seplen;
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
704 }
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
705
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
706 memcpy((*str) + offs, ctx->elems[n], elemlens[n] * sizeof(th_char_t));
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
707 offs += elemlens[n];
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
708 }
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
709
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
710 (*str)[offs] = 0;
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
711
706
d289fae3c1a8 Oops, forgot to free elemlens in th_join_string_elems() in no-error case. Ugh.
Matti Hamalainen <ccr@tnsp.org>
parents: 702
diff changeset
712 out:
d289fae3c1a8 Oops, forgot to free elemlens in th_join_string_elems() in no-error case. Ugh.
Matti Hamalainen <ccr@tnsp.org>
parents: 702
diff changeset
713 th_free(elemlens);
707
57d30f49fe1e Return the correct return value from th_join_string_elems().
Matti Hamalainen <ccr@tnsp.org>
parents: 706
diff changeset
714 return res;
674
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
715 }
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
716
dfabc7eef3dd Add new functions th_split_string() and th_join_string().
Matti Hamalainen <ccr@tnsp.org>
parents: 665
diff changeset
717
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
718 int th_join_string(th_char_t **str, th_char_t **elems, const size_t nelems, const th_char_t *sep)
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
719 {
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
720 th_strelems_t ctx;
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
721
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
722 ctx.elems = elems;
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
723 ctx.nelems = nelems;
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
724
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
725 return th_join_string_elems(str, &ctx, sep);
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
726 }
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
727
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
728
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
729 void th_strelems_free(th_strelems_t *ctx)
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
730 {
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
731 if (ctx != NULL)
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
732 {
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
733 for (size_t n = 0; n < ctx->nelems; n++)
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
734 {
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
735 th_free(ctx->elems[n]);
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
736 }
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
737
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
738 th_free(ctx->elems);
683
d8738bfad0a8 Nullify the strelems when freed.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
739 ctx->elems = NULL;
d8738bfad0a8 Nullify the strelems when freed.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
740 ctx->nelems = 0;
680
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
741 }
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
742 }
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
743
39c82d877251 Add new th_strelems_t structure for and join/split functions using them to
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
744
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
745 /* Find next non-whitespace character in string.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
746 * Updates iPos into the position of such character and
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
747 * returns pointer to the string.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
748 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
749 const th_char_t *th_findnext(const th_char_t *str, size_t *pos)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
750 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
751 assert(str != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
752
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
753 // Terminating NULL-character is not whitespace!
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
754 while (th_isspace(str[*pos]))
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
755 (*pos)++;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
756
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
757 return str + *pos;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
758 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
759
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
760
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
761 /* Find next sep-character from string
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
762 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
763 const th_char_t *th_findsep(const th_char_t *str, size_t *pos, const th_char_t sep)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
764 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
765 assert(str != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
766
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
767 while (str[*pos] && str[*pos] != sep)
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
768 (*pos)++;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
769
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
770 return str + *pos;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
771 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
772
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
773
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
774 /* Find next sep- or whitespace from string
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
775 */
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
776 const th_char_t *th_findseporspace(const th_char_t *str, size_t *pos, const th_char_t sep)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
777 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
778 assert(str != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
779
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
780 while (!th_isspace(str[*pos]) && str[*pos] != sep)
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
781 (*pos)++;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
782
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
783 return str + *pos;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
784 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
785
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
786
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
787 bool th_get_hex_triplet(const th_char_t *str, unsigned int *value)
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
788 {
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 660
diff changeset
789 const th_char_t *p = str;
497
ff3ebe22f6c5 Change int th_get_hex_triplet(const char *str) API to
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
790 int len;
ff3ebe22f6c5 Change int th_get_hex_triplet(const char *str) API to
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
791 *value = 0;
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
792
507
8bb0817210a0 Fix th_get_hex_triplet() some more ..
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
793 for (len = 0; *p && len < 4 * 2; p++, len++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
794 {
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
795 if (*p >= '0' && *p <= '9')
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
796 {
503
12dbe0102d72 Fix a stupid bug in th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents: 497
diff changeset
797 (*value) <<= 4;
12dbe0102d72 Fix a stupid bug in th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents: 497
diff changeset
798 (*value) |= (*p - '0');
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
799 }
174
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
800 else
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
801 if (*p >= 'A' && *p <= 'F')
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
802 {
503
12dbe0102d72 Fix a stupid bug in th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents: 497
diff changeset
803 (*value) <<= 4;
12dbe0102d72 Fix a stupid bug in th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents: 497
diff changeset
804 (*value) |= (*p - 'A') + 10;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
805 }
174
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
806 else
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
807 if (*p >= 'a' && *p <= 'f')
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
808 {
503
12dbe0102d72 Fix a stupid bug in th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents: 497
diff changeset
809 (*value) <<= 4;
12dbe0102d72 Fix a stupid bug in th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents: 497
diff changeset
810 (*value) |= (*p - 'a') + 10;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
811 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
812 else
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
813 return false;
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
814 }
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
815
507
8bb0817210a0 Fix th_get_hex_triplet() some more ..
Matti Hamalainen <ccr@tnsp.org>
parents: 503
diff changeset
816 return (len >= 3 * 2 && len <= 4 * 2);
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
817 }
162
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
818
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
819
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
820 bool th_get_boolean(const th_char_t *str, bool *value)
167
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
821 {
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
822 if (!th_strcasecmp(str, "yes") ||
169
a06a87b771ce Add on/off to valid boolean values.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
823 !th_strcasecmp(str, "on") ||
167
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
824 !th_strcasecmp(str, "true") ||
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
825 !th_strcasecmp(str, "1"))
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
826 {
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
827 *value = true;
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
828 return true;
167
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
829 }
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
830 else
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
831 if (!th_strcasecmp(str, "no") ||
169
a06a87b771ce Add on/off to valid boolean values.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
832 !th_strcasecmp(str, "off") ||
167
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
833 !th_strcasecmp(str, "false") ||
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
834 !th_strcasecmp(str, "0"))
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
835 {
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
836 *value = false;
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
837 return true;
167
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
838 }
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
839 else
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
840 return false;
167
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
841 }
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
842
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
843
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
844 bool th_get_int(const th_char_t *str, unsigned int *value, bool *neg)
479
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
845 {
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
846 int ch;
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
847 bool hex = false;
479
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
848
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
849 // Is the value negative?
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
850 if (*str == '-')
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
851 {
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
852 if (neg == NULL)
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
853 return false;
479
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
854
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
855 *neg = true;
479
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
856 str++;
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
857 }
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
858 else
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
859 if (neg != NULL)
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
860 *neg = false;
479
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
861
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
862 // Is it hexadecimal?
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
863 if (*str == '$')
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
864 {
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
865 hex = true;
479
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
866 str++;
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
867 }
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
868 else
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
869 if (str[0] == '0' && str[1] == 'x')
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
870 {
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
871 hex = true;
479
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
872 str += 2;
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
873 }
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
874
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
875 // Parse the value
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
876 *value = 0;
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
877 if (hex)
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
878 {
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
879 while ((ch = *str++))
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
880 {
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
881 if (ch >= '0' && ch <= '9')
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
882 {
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
883 *value <<= 4;
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
884 *value |= ch - '0';
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
885 }
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
886 else
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
887 if (ch >= 'A' && ch <= 'F')
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
888 {
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
889 *value <<= 4;
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
890 *value |= ch - 'A' + 10;
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
891 }
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
892 else
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
893 if (ch >= 'a' && ch <= 'f')
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
894 {
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
895 *value <<= 4;
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
896 *value |= ch - 'a' + 10;
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
897 }
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
898 else
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
899 return false;
479
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
900 }
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
901 }
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
902 else
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
903 {
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
904 while ((ch = *str++))
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
905 {
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
906 if (ch >= '0' && ch <= '9')
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
907 {
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
908 *value *= 10;
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
909 *value += ch - '0';
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
910 }
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
911 else
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
912 return false;
479
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
913 }
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
914 }
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
915 return true;
479
77ad030e82c9 Add th_get_int() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
916 }