annotate th_string.c @ 378:afbc3bfd3e03

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