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