Mercurial > hg > th-libs
annotate th_string.c @ 49:598609fb49b0
Change how "config.h" is included, etc.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 03 Oct 2011 15:40:54 +0300 |
parents | 669d2bc97c57 |
children | b10f07101e67 |
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 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 * (C) Copyright 2002-2008 Tecnic Software productions (TNSP) |
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 |
8 | 11 /* strdup with a NULL check |
12 */ | |
13 char *th_strdup(const char *s) | |
14 { | |
10 | 15 char *res; |
16 if (s == NULL) | |
17 return NULL; | |
44 | 18 |
10 | 19 if ((res = th_malloc(strlen(s) + 1)) == NULL) |
20 return NULL; | |
44 | 21 |
10 | 22 strcpy(res, s); |
23 return res; | |
8 | 24 } |
25 | |
26 | |
43 | 27 char *th_strncpy(char *dst, const char *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
|
28 { |
10 | 29 const char *s = src; |
30 char *d = dst; | |
31 size_t i; | |
32 assert(src != NULL); | |
33 assert(dst != NULL); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 |
10 | 35 /* Copy to the destination */ |
36 i = n; | |
39 | 37 while (*s && i > 0) |
38 { | |
10 | 39 *(d++) = *(s++); |
40 i--; | |
41 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 |
10 | 43 /* Fill rest of space with zeros */ |
39 | 44 while (i > 0) |
45 { | |
10 | 46 *(d++) = 0; |
47 i--; | |
48 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 |
10 | 50 /* Ensure that last is always zero */ |
51 dst[n - 1] = 0; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 |
10 | 53 return dst; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
57 /* Simulate a sprintf() that allocates memory |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
58 */ |
43 | 59 char *th_strdup_vprintf(const char *fmt, va_list args) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 { |
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
|
61 int size = 64; |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
62 char *buf, *nbuf = NULL; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
64 if ((buf = th_malloc(size)) == NULL) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
65 return NULL; |
44 | 66 |
39 | 67 while (1) |
68 { | |
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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
75 if (n > -1 && n < size) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
76 return buf; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
77 if (n > -1) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
78 size = n + 1; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
79 else |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
80 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
|
81 |
39 | 82 if ((nbuf = th_realloc(nbuf, size)) == NULL) |
83 { | |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
84 th_free(buf); |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
85 return NULL; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
86 } |
44 | 87 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
88 buf = nbuf; |
10 | 89 } |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
90 } |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
92 |
43 | 93 char *th_strdup_printf(const char *fmt, ...) |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
94 { |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
95 char *res; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
96 va_list ap; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
97 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
98 va_start(ap, fmt); |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
99 res = th_strdup_vprintf(fmt, ap); |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
100 va_end(ap); |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
101 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
102 return res; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 |
30
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
106 void th_pstr_vprintf(char **buf, const char *fmt, va_list ap) |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
107 { |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
108 char *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
|
109 th_free(*buf); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
110 *buf = tmp; |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
111 } |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
112 |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
113 |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
114 void th_pstr_printf(char **buf, const char *fmt, ...) |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
115 { |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
116 char *tmp; |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
117 va_list ap; |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
118 |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
119 va_start(ap, fmt); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
120 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
|
121 va_end(ap); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
122 |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
123 th_free(*buf); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
124 *buf = tmp; |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
125 } |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
126 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 /* 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
|
128 */ |
43 | 129 int th_strcasecmp(const char *str1, const char *str2) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
130 { |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
131 const char *s1 = str1, *s2 = str2; |
10 | 132 assert(str1 != NULL); |
133 assert(str2 != NULL); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 |
10 | 135 if (str1 == str2) |
136 return 0; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 |
39 | 138 while (*s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) |
139 { | |
10 | 140 s1++; |
141 s2++; | |
142 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
143 |
10 | 144 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
|
145 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 |
43 | 148 int th_strncasecmp(const char *str1, const char *str2, size_t n) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 { |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
150 const char *s1 = str1, *s2 = str2; |
10 | 151 assert(str1 != NULL); |
152 assert(str2 != NULL); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 |
10 | 154 if (str1 == str2) |
155 return 0; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 |
39 | 157 while (n > 0 && *s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) |
158 { | |
10 | 159 s1++; |
160 s2++; | |
161 n--; | |
162 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
164 return n > 0 ? (th_tolower(*s1) - th_tolower(*s2)) : 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 /* Remove all occurences of control characters, in-place. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 * Resulting string is always shorter or same length than original. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 */ |
43 | 171 void th_strip_ctrlchars(char *str) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 { |
10 | 173 char *i, *j; |
174 assert(str != NULL); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 |
10 | 176 i = str; |
177 j = str; | |
39 | 178 while (*i) |
179 { | |
10 | 180 if (!th_iscntrl(*i)) |
181 *(j++) = *i; | |
182 i++; | |
183 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
184 |
10 | 185 *j = 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
186 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
187 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
188 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
189 /* Copy a given string over in *result. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
190 */ |
43 | 191 int th_pstrcpy(char **result, const char *str) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 { |
10 | 193 assert(result != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 |
10 | 195 if (str == NULL) |
196 return -1; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
197 |
10 | 198 th_free(*result); |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
199 if ((*result = th_malloc(strlen(str) + 1)) == NULL) |
10 | 200 return -2; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
201 |
10 | 202 strcpy(*result, str); |
203 return 0; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
206 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 /* Concatenates a given string into string pointed by *result. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 */ |
43 | 209 int th_pstrcat(char **result, const char *str) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
210 { |
10 | 211 assert(result != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
212 |
10 | 213 if (str == NULL) |
214 return -1; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
215 |
39 | 216 if (*result != NULL) |
217 { | |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
218 *result = th_realloc(*result, strlen(*result) + strlen(str) + 1); |
10 | 219 if (*result == NULL) |
220 return -1; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
221 |
10 | 222 strcat(*result, str); |
39 | 223 } |
224 else | |
225 { | |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
226 *result = th_malloc(strlen(str) + 1); |
10 | 227 if (*result == NULL) |
228 return -1; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
229 |
10 | 230 strcpy(*result, str); |
231 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 |
10 | 233 return 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
236 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
237 /* 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
|
238 * 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
|
239 * returns pointer to the string. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 */ |
43 | 241 const char *th_findnext(const char *str, size_t *pos) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 { |
10 | 243 assert(str != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 |
10 | 245 /* Terminating NULL-character is not whitespace! */ |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
246 while (th_isspace(str[*pos])) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
247 (*pos)++; |
39 | 248 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
249 return &str[*pos]; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
250 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
253 /* 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
|
254 */ |
43 | 255 const char *th_findsep(const char *str, size_t *pos, char sep) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
256 { |
10 | 257 assert(str != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
258 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
259 while (str[*pos] && str[*pos] != sep) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
260 (*pos)++; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
261 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
262 return &str[*pos]; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
263 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
264 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
266 /* 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
|
267 */ |
43 | 268 const char *th_findseporspace(const char *str, size_t *pos, char sep) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
269 { |
10 | 270 assert(str != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
271 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
272 while (!th_isspace(str[*pos]) && str[*pos] != sep) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
273 (*pos)++; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
274 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
275 return &str[*pos]; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
276 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
277 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
278 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
279 /* 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
|
280 * 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
|
281 * 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
|
282 * any number of characters. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
283 */ |
43 | 284 BOOL th_strmatch(const char *str, const char *pattern) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
285 { |
10 | 286 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
287 const char *tmpPattern = NULL; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
288 |
10 | 289 /* Check given pattern and string */ |
290 if (str == NULL || pattern == NULL) | |
291 return FALSE; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
292 |
10 | 293 /* Start comparision */ |
294 do { | |
295 didMatch = FALSE; | |
39 | 296 switch (*pattern) |
297 { | |
10 | 298 case '?': |
299 /* Any single character matches */ | |
39 | 300 if (*str) |
301 { | |
10 | 302 didMatch = TRUE; |
303 pattern++; | |
304 str++; | |
305 } | |
306 break; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
307 |
10 | 308 case '*': |
309 didMatch = TRUE; | |
310 pattern++; | |
311 if (!*pattern) | |
312 isEnd = TRUE; | |
313 isAnyMode = TRUE; | |
314 tmpPattern = pattern; | |
315 break; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
316 |
10 | 317 case 0: |
39 | 318 if (isAnyMode) |
319 { | |
10 | 320 if (*str) |
321 str++; | |
322 else | |
323 isEnd = TRUE; | |
39 | 324 } |
325 else | |
326 { | |
327 if (*str) | |
328 { | |
329 if (tmpPattern) | |
330 { | |
10 | 331 isAnyMode = TRUE; |
332 pattern = tmpPattern; | |
39 | 333 } |
334 else | |
10 | 335 didMatch = FALSE; |
44 | 336 } |
337 else | |
10 | 338 isEnd = TRUE; |
339 } | |
340 break; | |
341 default: | |
39 | 342 if (isAnyMode) |
343 { | |
344 if (*pattern == *str) | |
345 { | |
10 | 346 isAnyMode = FALSE; |
347 didMatch = TRUE; | |
39 | 348 } |
349 else | |
350 { | |
351 if (*str) | |
352 { | |
10 | 353 didMatch = TRUE; |
354 str++; | |
355 } | |
356 } | |
39 | 357 } |
358 else | |
359 { | |
360 if (*pattern == *str) | |
361 { | |
10 | 362 didMatch = TRUE; |
363 if (*pattern) | |
364 pattern++; | |
365 if (*str) | |
366 str++; | |
39 | 367 } |
368 else | |
369 { | |
370 if (tmpPattern) | |
371 { | |
10 | 372 didMatch = TRUE; |
373 isAnyMode = TRUE; | |
374 pattern = tmpPattern; | |
375 } | |
376 } | |
377 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
378 |
10 | 379 if (!*str && !*pattern) |
380 isEnd = TRUE; | |
381 break; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
382 |
10 | 383 } /* switch */ |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
384 |
10 | 385 } while (didMatch && !isEnd); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
386 |
10 | 387 return didMatch; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
388 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
389 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
390 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
391 /* 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
|
392 */ |
43 | 393 BOOL th_strcasematch(const char *str, const char *pattern) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
394 { |
10 | 395 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
396 const char *tmpPattern = NULL; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
397 |
10 | 398 /* Check given pattern and string */ |
399 if (str == NULL || pattern == NULL) | |
400 return FALSE; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
401 |
10 | 402 /* Start comparision */ |
403 do { | |
404 switch (*pattern) { | |
405 case '?': | |
406 /* Any single character matches */ | |
39 | 407 if (*str) |
408 { | |
10 | 409 pattern++; |
410 str++; | |
44 | 411 } |
412 else | |
10 | 413 didMatch = FALSE; |
414 break; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
415 |
10 | 416 case '*': |
417 pattern++; | |
418 if (!*pattern || *pattern == '?') | |
419 isEnd = TRUE; | |
420 isAnyMode = TRUE; | |
421 tmpPattern = pattern; | |
422 break; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
423 |
10 | 424 case 0: |
39 | 425 if (isAnyMode) |
426 { | |
10 | 427 if (*str) |
428 str++; | |
429 else | |
430 isEnd = TRUE; | |
39 | 431 } |
432 else | |
433 { | |
434 if (*str) | |
435 { | |
436 if (tmpPattern) | |
437 { | |
10 | 438 isAnyMode = TRUE; |
439 pattern = tmpPattern; | |
39 | 440 } |
441 else | |
10 | 442 didMatch = FALSE; |
44 | 443 } |
444 else | |
10 | 445 isEnd = TRUE; |
446 } | |
447 break; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
448 |
10 | 449 default: |
39 | 450 if (isAnyMode) |
451 { | |
452 if (th_tolower(*pattern) == th_tolower(*str)) | |
453 { | |
10 | 454 isAnyMode = FALSE; |
39 | 455 } |
456 else | |
457 { | |
10 | 458 if (*str) |
459 str++; | |
460 else | |
461 didMatch = FALSE; | |
462 } | |
39 | 463 } |
464 else | |
465 { | |
466 if (th_tolower(*pattern) == th_tolower(*str)) | |
467 { | |
10 | 468 if (*pattern) |
469 pattern++; | |
470 if (*str) | |
471 str++; | |
39 | 472 } |
473 else | |
474 { | |
475 if (tmpPattern) | |
476 { | |
10 | 477 isAnyMode = TRUE; |
478 pattern = tmpPattern; | |
39 | 479 } |
480 else | |
10 | 481 didMatch = FALSE; |
482 } | |
483 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
484 |
10 | 485 if (!*str && !*pattern) |
486 isEnd = TRUE; | |
39 | 487 |
10 | 488 break; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
489 |
10 | 490 } /* switch */ |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
491 |
10 | 492 } while (didMatch && !isEnd); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
493 |
10 | 494 return didMatch; |
0
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 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
497 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
498 int th_get_hex_triplet(const char *str) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
499 { |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
500 const char *p = str; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
501 int len, val = 0; |
44 | 502 |
39 | 503 for (len = 0; *p && len < 6; p++, len++) |
504 { | |
505 if (*p >= '0' && *p <= '9') | |
506 { | |
507 val *= 16; | |
508 val += (*p - '0'); | |
509 } | |
510 else if (*p >= 'A' && *p <= 'F') | |
511 { | |
512 val *= 16; | |
513 val += (*p - 'A') + 10; | |
514 } | |
515 else if (*p >= 'a' && *p <= 'f') | |
516 { | |
517 val *= 16; | |
518 val += (*p - 'a') + 10; | |
519 } | |
520 else | |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
521 return -1; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
522 } |
44 | 523 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
524 return (len == 6) ? val : -1; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
525 } |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
526 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
527 |
35 | 528 BOOL th_growbuf(char **buf, size_t *bufsize, size_t *len, size_t grow) |
21
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
529 { |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
530 if (*buf == NULL) |
31
a3ad77382c64
Fix an annoying bug in th_growbuf().
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
531 *bufsize = *len = 0; |
44 | 532 |
39 | 533 if (*buf == NULL || *len + grow >= *bufsize) |
534 { | |
21
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
535 *bufsize += grow + TH_BUFGROW; |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
536 *buf = (char *) th_realloc(*buf, *bufsize); |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
537 if (*buf == NULL) |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
538 return FALSE; |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
539 } |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
540 return TRUE; |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
541 } |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
542 |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
543 |
29
04cfdefc3253
th_vputs() and th_vputch() now return a boolean value, TRUE if operation was successful, FALSE if it failed. If result was FALSE, the buffer pointer may now be NULL.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
544 BOOL th_vputch(char **buf, size_t *bufsize, size_t *len, const char ch) |
21
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
545 { |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
546 if (!th_growbuf(buf, bufsize, len, 1)) |
29
04cfdefc3253
th_vputs() and th_vputch() now return a boolean value, TRUE if operation was successful, FALSE if it failed. If result was FALSE, the buffer pointer may now be NULL.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
547 return FALSE; |
44 | 548 |
21
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
549 (*buf)[*len] = ch; |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
550 (*len)++; |
29
04cfdefc3253
th_vputs() and th_vputch() now return a boolean value, TRUE if operation was successful, FALSE if it failed. If result was FALSE, the buffer pointer may now be NULL.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
551 |
04cfdefc3253
th_vputs() and th_vputch() now return a boolean value, TRUE if operation was successful, FALSE if it failed. If result was FALSE, the buffer pointer may now be NULL.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
552 return TRUE; |
21
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
553 } |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
554 |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
555 |
29
04cfdefc3253
th_vputs() and th_vputch() now return a boolean value, TRUE if operation was successful, FALSE if it failed. If result was FALSE, the buffer pointer may now be NULL.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
556 BOOL th_vputs(char **buf, size_t *bufsize, size_t *len, const char *str) |
21
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
557 { |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
558 size_t slen; |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
559 if (str == NULL) |
29
04cfdefc3253
th_vputs() and th_vputch() now return a boolean value, TRUE if operation was successful, FALSE if it failed. If result was FALSE, the buffer pointer may now be NULL.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
560 return FALSE; |
44 | 561 |
21
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
562 slen = strlen(str); |
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
563 if (!th_growbuf(buf, bufsize, len, slen)) |
29
04cfdefc3253
th_vputs() and th_vputch() now return a boolean value, TRUE if operation was successful, FALSE if it failed. If result was FALSE, the buffer pointer may now be NULL.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
564 return FALSE; |
21
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
565 |
32
7e0a2a125830
Use strcpy() instead of strcat() in th_vputs().
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
566 strcpy(*buf + *len, str); |
21
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
567 (*len) += slen; |
44 | 568 |
29
04cfdefc3253
th_vputs() and th_vputch() now return a boolean value, TRUE if operation was successful, FALSE if it failed. If result was FALSE, the buffer pointer may now be NULL.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
569 return TRUE; |
21
1f7d8693fda8
Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
570 } |