annotate th_string.c @ 124:fe4d5f3b486c

Sync.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 29 Oct 2010 16:41:08 +0300
parents 69aed051f84d
children 9ae3d87a686f
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
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #define LPREV (pNode->pPrev)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 #define LNEXT (pNode->pNext)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15
79
e36df57c5b0f Use th_strdup() again.
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
16
e36df57c5b0f Use th_strdup() again.
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
17 /* strdup with a NULL check
e36df57c5b0f Use th_strdup() again.
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
18 */
e36df57c5b0f Use th_strdup() again.
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
19 char *th_strdup(const char *s)
e36df57c5b0f Use th_strdup() again.
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
20 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
21 char *res;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
22 if (s == NULL)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
23 return NULL;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
24
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
25 if ((res = th_malloc(strlen(s) + 1)) == NULL)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
26 return NULL;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
27
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
28 strcpy(res, s);
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
29 return res;
79
e36df57c5b0f Use th_strdup() again.
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
30 }
e36df57c5b0f Use th_strdup() again.
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
31
e36df57c5b0f Use th_strdup() again.
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
32
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
33 char *th_strncpy(char * dst, const char * src, const size_t n)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
35 const char *s = src;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
36 char *d = dst;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
37 size_t i;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
38 assert(src != NULL);
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
39 assert(dst != NULL);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
41 /* Copy to the destination */
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
42 i = n;
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
43 while (*s && i > 0) {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
44 *(d++) = *(s++);
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
45 i--;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
46 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
48 /* Fill rest of space with zeros */
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
49 while (i > 0) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
50 *(d++) = 0;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
51 i--;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
52 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
54 /* Ensure that last is always zero */
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
55 dst[n - 1] = 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
57 return dst;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
61 #ifdef STRDUP_PRINTF
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
62 /*
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
63 */
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
64 enum {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
65 TH_PAD_RIGHT = 1,
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
66 TH_PAD_ZERO = 2
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
67 };
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
68
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
69
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
70 static size_t th_printbuf_str(char **buf, const char *str, size_t width, int flags)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
71 {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
72 size_t len = strlen(str);
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
73 char *out = (buf != NULL) ? *buf : NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
74 char pad = ' ';
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
75
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
76 if (width > 0) {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
77 width = (len >= width) ? 0 : width - len;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
78 if (flags & TH_PAD_ZERO)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
79 pad = '0';
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
80 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
81
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
82 if ((flags & TH_PAD_RIGHT) == 0 && out != NULL) {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
83 while (width-- > 0)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
84 *out++ = pad;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
85 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
86
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
87 if (out != NULL) {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
88 while (*str)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
89 *out++ = *str++;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
90 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
91
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
92 if (flags & TH_PAD_RIGHT && out != NULL) {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
93 while (width-- > 0)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
94 *out++ = pad;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
95 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
96
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
97 if (buf != NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
98 *buf = out;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
99
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
100 return len + width;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
101 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
102
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
103 #define TH_INTBUF_LEN (32)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
104
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
105 static size_t th_printbuf_int(char **buf, int i, int b, int sg, int width, int pad, int letbase)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 {
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
107 char tmpbuf[TH_INTBUF_LEN], *s;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
108 int t, neg = 0, pc = 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
109 unsigned int u = i;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
110
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
111 if (i == 0) {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
112 tmpbuf[0] = '0';
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
113 tmpbuf[1] = 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
114 return th_printbuf_str(buf, tmpbuf, width, pad);
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
115 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
116
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
117 if (sg && b == 10 && i < 0) {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
118 neg = 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
119 u = -i;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
120 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
121
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
122 s = tmpbuf + TH_INTBUF_LEN - 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
123 *s = 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
125 while (u) {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
126 t = u % b;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
127 if (t >= 10)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
128 t += letbase - '0' - 10;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
129
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
130 *--s = t + '0';
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
131 u /= b;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
132 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
133
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
134 if (neg) {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
135 if (width && (pad & PAD_ZERO)) {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
136 printchar (out, '-');
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
137 ++pc;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
138 --width;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
139 } else {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
140 *--s = '-';
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
141 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
142 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
143
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
144 return pc + th_printbuf_str(buf, s, width, pad);
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
145 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
146
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
147
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
148 char * th_strdup_vprintf(const char *fmt, va_list args)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
149 {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
150 const char *s = fmt;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
151 char *res;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
152 size_t len = 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
153
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
154 /* 1. Determine required space for final string */
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
155 while (*s) {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
156 if (*s == '%') {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
157 s++;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
158 } else {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
159 s++;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
160 len++;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
161 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
162 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
163
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
164 /* 2. Allocate space */
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
165
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
166 /* 3. Create final string */
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
167
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
168 return res;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
169 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
170
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
171
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
172 char * th_strdup_printf(const char *fmt, ...)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
173 {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
174 char *res;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
175 va_list ap;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
176
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
177 va_start(ap, fmt);
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
178 res = th_strdup_vprintf(fmt, ap);
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
179 va_end(ap);
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
180
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
181 return res;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
182 }
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
183 #endif
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
184
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
185 /* Compare two strings ignoring case [strcasecmp, strncasecmp]
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
186 */
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
187 int th_strcasecmp(const char * str1, const char * str2)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
188 {
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
189 const char *s1 = str1, *s2 = str2;
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
190 assert(str1 != NULL);
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
191 assert(str2 != NULL);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
193 if (str1 == str2)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
194 return 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
196 while (*s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
197 s1++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
198 s2++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
199 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
201 return (th_tolower(*s1) - th_tolower(*s2));
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
205 int th_strncasecmp(const char * str1, const char * str2, size_t n)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 {
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
207 const char *s1 = str1, *s2 = str2;
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
208 assert(str1 != NULL);
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
209 assert(str2 != NULL);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
211 if (str1 == str2)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
212 return 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
214 while (n > 0 && *s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
215 s1++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
216 s2++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
217 n--;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
218 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
220 return n > 0 ? (th_tolower(*s1) - th_tolower(*s2)) : 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 /* Remove all occurences of control characters, in-place.
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 * Resulting string is always shorter or same length than original.
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 */
11
707e35b03f89 Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
227 void th_strip_ctrlchars(char * str)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
229 char *i, *j;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
230 assert(str != NULL);
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 i = str;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
233 j = str;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
234 while (*i) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
235 if (!th_iscntrl(*i))
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
236 *(j++) = *i;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
237 i++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
238 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
240 *j = 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243
11
707e35b03f89 Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
244 /* Copy a given string over in *result.
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 */
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
246 int th_pstrcpy(char ** result, const char * str)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
248 assert(result != NULL);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
250 if (str == NULL)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
251 return -1;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
253 th_free(*result);
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
254 if ((*result = th_malloc(strlen(str) + 1)) == NULL)
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
255 return -2;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
257 strcpy(*result, str);
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
258 return 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261
11
707e35b03f89 Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
262 /* Concatenates a given string into string pointed by *result.
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 */
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
264 int th_pstrcat(char ** result, const char * str)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
266 assert(result != NULL);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
268 if (str == NULL)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
269 return -1;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
271 if (*result != NULL) {
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
272 *result = th_realloc(*result, strlen(*result) + strlen(str) + 1);
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
273 if (*result == NULL)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
274 return -1;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
276 strcat(*result, str);
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
277 } else {
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
278 *result = th_malloc(strlen(str) + 1);
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
279 if (*result == NULL)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
280 return -1;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
282 strcpy(*result, str);
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
283 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
285 return 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 /* Find next non-whitespace character in string.
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 * Updates iPos into the position of such character and
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291 * returns pointer to the string.
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 */
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
293 const char *th_findnext(const char * str, size_t * pos)
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 assert(str != NULL);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
297 /* Terminating NULL-character is not whitespace! */
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
298 while (th_isspace(str[*pos]))
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
299 (*pos)++;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
300 return &str[*pos];
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
304 /* Find next sep-character from string
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 */
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
306 const char *th_findsep(const char * str, size_t * pos, char sep)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
308 assert(str != NULL);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
310 while (str[*pos] && str[*pos] != sep)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
311 (*pos)++;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
312
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
313 return &str[*pos];
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
317 /* Find next sep- or whitespace from string
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 */
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
319 const char *th_findseporspace(const char * str, size_t * pos, char sep)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
321 assert(str != NULL);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
323 while (!th_isspace(str[*pos]) && str[*pos] != sep)
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
324 (*pos)++;
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
325
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
326 return &str[*pos];
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 /* Compare a string to a pattern. Case-SENSITIVE version.
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 * The matching pattern can consist of any normal characters plus
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 * wildcards ? and *. "?" matches any character and "*" matches
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 * any number of characters.
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 */
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
335 BOOL th_strmatch(const char * str, const char * pattern)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
337 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE;
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
338 const char *tmpPattern = NULL;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
340 /* Check given pattern and string */
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
341 if (str == NULL || pattern == NULL)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
342 return FALSE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
344 /* Start comparision */
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
345 do {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
346 didMatch = FALSE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
347 switch (*pattern) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
348 case '?':
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
349 /* Any single character matches */
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
350 if (*str) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
351 didMatch = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
352 pattern++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
353 str++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
354 }
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
355 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
357 case '*':
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
358 didMatch = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
359 pattern++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
360 if (!*pattern)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
361 isEnd = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
362 isAnyMode = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
363 tmpPattern = pattern;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
364 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
366 case 0:
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
367 if (isAnyMode) {
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++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
370 else
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
371 isEnd = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
372 } else {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
373 if (*str) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
374 if (tmpPattern) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
375 isAnyMode = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
376 pattern = tmpPattern;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
377 } else
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
378 didMatch = FALSE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
379 } else
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
380 isEnd = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
381 }
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
382 break;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
383 default:
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
384 if (isAnyMode) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
385 if (*pattern == *str) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
386 isAnyMode = FALSE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
387 didMatch = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
388 } else {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
389 if (*str) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
390 didMatch = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
391 str++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
392 }
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
393 }
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
394 } else {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
395 if (*pattern == *str) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
396 didMatch = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
397 if (*pattern)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
398 pattern++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
399 if (*str)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
400 str++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
401 } else {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
402 if (tmpPattern) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
403 didMatch = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
404 isAnyMode = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
405 pattern = tmpPattern;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
406 }
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
407 }
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
408 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
410 if (!*str && !*pattern)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
411 isEnd = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
412 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
414 } /* switch */
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
416 } while (didMatch && !isEnd);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
418 return didMatch;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 /* Compare a string to a pattern. Case-INSENSITIVE version.
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 */
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
424 BOOL th_strcasematch(const char * str, const char * pattern)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
426 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE;
124
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
427 const char *tmpPattern = NULL;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
429 /* Check given pattern and string */
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
430 if (str == NULL || pattern == NULL)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
431 return FALSE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
433 /* Start comparision */
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
434 do {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
435 switch (*pattern) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
436 case '?':
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
437 /* Any single character matches */
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
438 if (*str) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
439 pattern++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
440 str++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
441 } else
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
442 didMatch = FALSE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
443 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
445 case '*':
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
446 pattern++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
447 if (!*pattern || *pattern == '?')
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
448 isEnd = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
449 isAnyMode = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
450 tmpPattern = pattern;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
451 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
453 case 0:
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
454 if (isAnyMode) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
455 if (*str)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
456 str++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
457 else
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
458 isEnd = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
459 } else {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
460 if (*str) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
461 if (tmpPattern) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
462 isAnyMode = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
463 pattern = tmpPattern;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
464 } else
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
465 didMatch = FALSE;
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 isEnd = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
468 }
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
469 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
471 default:
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
472 if (isAnyMode) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
473 if (th_tolower(*pattern) == th_tolower(*str)) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
474 isAnyMode = FALSE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
475 } else {
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++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
478 else
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
479 didMatch = FALSE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
480 }
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
481 } else {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
482 if (th_tolower(*pattern) == th_tolower(*str)) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
483 if (*pattern)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
484 pattern++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
485 if (*str)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
486 str++;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
487 } else {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
488 if (tmpPattern) {
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
489 isAnyMode = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
490 pattern = tmpPattern;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
491 } else
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
492 didMatch = FALSE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
493 }
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
494 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
495
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
496 if (!*str && !*pattern)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
497 isEnd = TRUE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
498 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
500 } /* switch */
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
502 } while (didMatch && !isEnd);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
504 return didMatch;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506