Mercurial > hg > th-libs
annotate th_string.c @ 179:167118a6609a
Remove defines that were meant to cause errors when ancient versions of
certain macro definitions were used.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 02 Mar 2015 22:43:15 +0200 |
parents | 3d0a1f87e393 |
children | 1ee46a95aa8c |
rev | line source |
---|---|
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Miscellaneous string-handling related utility-functions |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
144 | 4 * (C) Copyright 2002-2015 Tecnic Software productions (TNSP) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
8 #include "th_util.h" |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 #include "th_string.h" |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 |
80 | 11 |
12 /* Implementation of strdup() with a NULL check | |
8 | 13 */ |
14 char *th_strdup(const char *s) | |
15 { | |
10 | 16 char *res; |
17 if (s == NULL) | |
18 return NULL; | |
44 | 19 |
10 | 20 if ((res = th_malloc(strlen(s) + 1)) == NULL) |
21 return NULL; | |
44 | 22 |
10 | 23 strcpy(res, s); |
24 return res; | |
8 | 25 } |
26 | |
27 | |
81 | 28 /* Implementation of strndup() with NULL check |
29 */ | |
30 char *th_strndup(const char *s, const size_t n) | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 { |
81 | 32 char *res; |
33 size_t len; | |
79 | 34 |
81 | 35 if (s == NULL) |
36 return NULL; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 |
81 | 38 len = strlen(s); |
39 if (len > n) | |
40 len = n; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 |
82
3e5c0615125c
Oops, th_alloc() -> th_malloc().
Matti Hamalainen <ccr@tnsp.org>
parents:
81
diff
changeset
|
42 if ((res = th_malloc(len + 1)) == NULL) |
81 | 43 return NULL; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 |
81 | 45 memcpy(res, s, len); |
46 res[len] = 0; | |
47 | |
48 return res; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 |
174 | 52 /* Like strdup, but trims whitespace from the string |
53 * according to specified flags. See TH_TRIM_* in | |
54 * th_string.h | |
90
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
55 */ |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
56 char *th_strdup_trim(const char *s, const int flags) |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
57 { |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
58 char *res; |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
59 size_t start, end, len; |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
60 if (s == NULL) |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
61 return NULL; |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
62 |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
63 len = strlen(s); |
174 | 64 |
65 // Trim start: find first non-whitespace character | |
90
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
66 if (flags & TH_TRIM_START) |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
67 for (start = 0; start < len && th_isspace(s[start]); start++); |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
68 else |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
69 start = 0; |
174 | 70 |
71 // Trim end: find last non-whitespace character | |
90
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
72 if (flags & TH_TRIM_END) |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
73 for (end = len; end > start && th_isspace(s[end]); end--); |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
74 else |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
75 end = len; |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
76 |
174 | 77 // Allocate memory for result |
90
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
78 len = end - start + 1; |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
79 if ((res = th_malloc(len + 1)) == NULL) |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
80 return NULL; |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
81 |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
82 memcpy(res, s, len); |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
83 res[len] = 0; |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
84 return res; |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
85 } |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
86 |
2b1f7f1ca8e4
Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
87 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
88 /* Simulate a sprintf() that allocates memory |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
89 */ |
43 | 90 char *th_strdup_vprintf(const char *fmt, va_list args) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 { |
133 | 92 int size = 64; |
93 char *buf, *tmp; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
95 if ((buf = th_malloc(size)) == NULL) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
96 return NULL; |
44 | 97 |
39 | 98 while (1) |
99 { | |
37
3986b139bbbd
Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
100 int n; |
3986b139bbbd
Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
101 va_list ap; |
3986b139bbbd
Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
102 va_copy(ap, args); |
3986b139bbbd
Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
103 n = vsnprintf(buf, size, fmt, ap); |
3986b139bbbd
Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
104 va_end(ap); |
3986b139bbbd
Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
105 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
106 if (n > -1 && n < size) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
107 return buf; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
108 if (n > -1) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
109 size = n + 1; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
110 else |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
111 size *= 2; |
37
3986b139bbbd
Fix th_strdup_vprintf(), another misuse of va_list without making a copy of it... :(
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
112 |
133 | 113 if ((tmp = th_realloc(buf, size)) == NULL) |
114 { | |
115 th_free(buf); | |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
116 return NULL; |
133 | 117 } |
118 else | |
119 buf = tmp; | |
10 | 120 } |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
121 } |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
123 |
43 | 124 char *th_strdup_printf(const char *fmt, ...) |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
125 { |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
126 char *res; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
127 va_list ap; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
128 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
129 va_start(ap, fmt); |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
130 res = th_strdup_vprintf(fmt, ap); |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
131 va_end(ap); |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
132 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
133 return res; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
135 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 |
30
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
137 void th_pstr_vprintf(char **buf, const char *fmt, va_list ap) |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
138 { |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
139 char *tmp = th_strdup_vprintf(fmt, ap); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
140 th_free(*buf); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
141 *buf = tmp; |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
142 } |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
143 |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
144 |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
145 void th_pstr_printf(char **buf, const char *fmt, ...) |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
146 { |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
147 char *tmp; |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
148 va_list ap; |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
149 |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
150 va_start(ap, fmt); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
151 tmp = th_strdup_vprintf(fmt, ap); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
152 va_end(ap); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
153 |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
154 th_free(*buf); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
155 *buf = tmp; |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
156 } |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
157 |
88 | 158 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 /* Compare two strings ignoring case [strcasecmp, strncasecmp] |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 */ |
43 | 161 int th_strcasecmp(const char *str1, const char *str2) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 { |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
163 const char *s1 = str1, *s2 = str2; |
10 | 164 assert(str1 != NULL); |
165 assert(str2 != NULL); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 |
10 | 167 if (str1 == str2) |
168 return 0; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 |
39 | 170 while (*s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) |
171 { | |
10 | 172 s1++; |
173 s2++; | |
174 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 |
88 | 176 return th_tolower(*s1) - th_tolower(*s2); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 |
43 | 180 int th_strncasecmp(const char *str1, const char *str2, size_t n) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 { |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
182 const char *s1 = str1, *s2 = str2; |
10 | 183 assert(str1 != NULL); |
184 assert(str2 != NULL); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
185 |
10 | 186 if (str1 == str2) |
187 return 0; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
188 |
39 | 189 while (n > 0 && *s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) |
190 { | |
10 | 191 s1++; |
192 s2++; | |
193 n--; | |
194 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
196 return n > 0 ? (th_tolower(*s1) - th_tolower(*s2)) : 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
197 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 |
89
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
200 /* Check if end of the given string str matches needle |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
201 * case-insensitively, return pointer to start of the match, |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
202 * if found, NULL otherwise. |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
203 */ |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
204 char *th_strrcasecmp(char *str, const char *needle) |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
205 { |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
206 if (str == NULL || needle == NULL) |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
207 return NULL; |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
208 |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
209 const size_t |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
210 slen = strlen(str), |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
211 nlen = strlen(needle); |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
212 |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
213 if (slen < nlen) |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
214 return NULL; |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
215 |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
216 if (th_strcasecmp(str - nlen - 1, needle) == 0) |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
217 return str - nlen - 1; |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
218 else |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
219 return NULL; |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
220 } |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
221 |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
222 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
223 /* Remove all occurences of control characters, in-place. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
224 * Resulting string is always shorter or same length than original. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
225 */ |
43 | 226 void th_strip_ctrlchars(char *str) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
227 { |
10 | 228 char *i, *j; |
229 assert(str != NULL); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 |
10 | 231 i = str; |
232 j = str; | |
39 | 233 while (*i) |
234 { | |
10 | 235 if (!th_iscntrl(*i)) |
236 *(j++) = *i; | |
237 i++; | |
238 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
239 |
10 | 240 *j = 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
241 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
243 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 /* Copy a given string over in *result. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
245 */ |
43 | 246 int th_pstrcpy(char **result, const char *str) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
247 { |
10 | 248 assert(result != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
249 |
10 | 250 if (str == NULL) |
251 return -1; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 |
10 | 253 th_free(*result); |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
254 if ((*result = th_malloc(strlen(str) + 1)) == NULL) |
10 | 255 return -2; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
256 |
10 | 257 strcpy(*result, str); |
258 return 0; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
259 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
260 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 /* Concatenates a given string into string pointed by *result. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
263 */ |
43 | 264 int th_pstrcat(char **result, const char *str) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 { |
10 | 266 assert(result != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
267 |
10 | 268 if (str == NULL) |
269 return -1; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
270 |
39 | 271 if (*result != NULL) |
272 { | |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
273 *result = th_realloc(*result, strlen(*result) + strlen(str) + 1); |
10 | 274 if (*result == NULL) |
275 return -1; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
276 |
10 | 277 strcat(*result, str); |
39 | 278 } |
279 else | |
280 { | |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
281 *result = th_malloc(strlen(str) + 1); |
10 | 282 if (*result == NULL) |
283 return -1; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
284 |
10 | 285 strcpy(*result, str); |
286 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
287 |
10 | 288 return 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
289 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
290 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
291 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
292 /* Find next non-whitespace character in string. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
293 * Updates iPos into the position of such character and |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
294 * returns pointer to the string. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
295 */ |
43 | 296 const char *th_findnext(const char *str, size_t *pos) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
297 { |
10 | 298 assert(str != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
299 |
129 | 300 // Terminating NULL-character is not whitespace! |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
301 while (th_isspace(str[*pos])) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
302 (*pos)++; |
39 | 303 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
304 return &str[*pos]; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
305 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
306 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
307 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
308 /* Find next sep-character from string |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
309 */ |
43 | 310 const char *th_findsep(const char *str, size_t *pos, char sep) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
311 { |
10 | 312 assert(str != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
313 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
314 while (str[*pos] && str[*pos] != sep) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
315 (*pos)++; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
316 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
317 return &str[*pos]; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
318 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
319 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
320 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
321 /* Find next sep- or whitespace from string |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
322 */ |
43 | 323 const char *th_findseporspace(const char *str, size_t *pos, char sep) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
324 { |
10 | 325 assert(str != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
326 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
327 while (!th_isspace(str[*pos]) && str[*pos] != sep) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
328 (*pos)++; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
329 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
330 return &str[*pos]; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
331 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
332 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
333 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
334 /* Compare a string to a pattern. Case-SENSITIVE version. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
335 * The matching pattern can consist of any normal characters plus |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
336 * wildcards ? and *. "?" matches any character and "*" matches |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
337 * any number of characters. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
338 */ |
43 | 339 BOOL th_strmatch(const char *str, const char *pattern) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
340 { |
175 | 341 BOOL matched = TRUE, any = FALSE, end = FALSE; |
342 const char *tmp = NULL; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
343 |
129 | 344 // Check given pattern and string |
10 | 345 if (str == NULL || pattern == NULL) |
346 return FALSE; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
347 |
129 | 348 // Start comparision |
10 | 349 do { |
175 | 350 matched = FALSE; |
39 | 351 switch (*pattern) |
352 { | |
10 | 353 case '?': |
129 | 354 // Any single character matches |
39 | 355 if (*str) |
356 { | |
175 | 357 matched = TRUE; |
10 | 358 pattern++; |
359 str++; | |
360 } | |
361 break; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
362 |
10 | 363 case '*': |
175 | 364 matched = TRUE; |
10 | 365 pattern++; |
366 if (!*pattern) | |
175 | 367 end = TRUE; |
368 any = TRUE; | |
369 tmp = pattern; | |
10 | 370 break; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
371 |
10 | 372 case 0: |
175 | 373 if (any) |
39 | 374 { |
10 | 375 if (*str) |
376 str++; | |
377 else | |
175 | 378 end = TRUE; |
39 | 379 } |
380 else | |
381 { | |
382 if (*str) | |
383 { | |
175 | 384 if (tmp) |
39 | 385 { |
175 | 386 any = TRUE; |
387 pattern = tmp; | |
39 | 388 } |
389 else | |
175 | 390 matched = FALSE; |
44 | 391 } |
392 else | |
175 | 393 end = TRUE; |
10 | 394 } |
395 break; | |
175 | 396 |
10 | 397 default: |
175 | 398 if (any) |
39 | 399 { |
400 if (*pattern == *str) | |
401 { | |
175 | 402 any = FALSE; |
403 matched = TRUE; | |
39 | 404 } |
405 else | |
406 { | |
407 if (*str) | |
408 { | |
175 | 409 matched = TRUE; |
10 | 410 str++; |
411 } | |
412 } | |
39 | 413 } |
414 else | |
415 { | |
416 if (*pattern == *str) | |
417 { | |
175 | 418 matched = TRUE; |
10 | 419 if (*pattern) |
420 pattern++; | |
421 if (*str) | |
422 str++; | |
39 | 423 } |
424 else | |
425 { | |
175 | 426 if (tmp) |
39 | 427 { |
175 | 428 matched = TRUE; |
429 any = TRUE; | |
430 pattern = tmp; | |
10 | 431 } |
432 } | |
433 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
434 |
10 | 435 if (!*str && !*pattern) |
175 | 436 end = TRUE; |
10 | 437 break; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
438 |
129 | 439 } // switch |
175 | 440 } while (matched && !end); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
441 |
175 | 442 return matched; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
443 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
444 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
445 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
446 /* Compare a string to a pattern. Case-INSENSITIVE version. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
447 */ |
43 | 448 BOOL th_strcasematch(const char *str, const char *pattern) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
449 { |
175 | 450 BOOL matched = TRUE, any = FALSE, end = FALSE; |
451 const char *tmp = NULL; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
452 |
129 | 453 // Check given pattern and string |
10 | 454 if (str == NULL || pattern == NULL) |
455 return FALSE; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
456 |
129 | 457 // Start comparision |
10 | 458 do { |
459 switch (*pattern) { | |
460 case '?': | |
129 | 461 // Any single character matches |
39 | 462 if (*str) |
463 { | |
10 | 464 pattern++; |
465 str++; | |
44 | 466 } |
467 else | |
175 | 468 matched = FALSE; |
10 | 469 break; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
470 |
10 | 471 case '*': |
472 pattern++; | |
473 if (!*pattern || *pattern == '?') | |
175 | 474 end = TRUE; |
475 any = TRUE; | |
476 tmp = pattern; | |
10 | 477 break; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
478 |
10 | 479 case 0: |
175 | 480 if (any) |
39 | 481 { |
10 | 482 if (*str) |
483 str++; | |
484 else | |
175 | 485 end = TRUE; |
39 | 486 } |
487 else | |
488 { | |
489 if (*str) | |
490 { | |
175 | 491 if (tmp) |
39 | 492 { |
175 | 493 any = TRUE; |
494 pattern = tmp; | |
39 | 495 } |
496 else | |
175 | 497 matched = FALSE; |
44 | 498 } |
499 else | |
175 | 500 end = TRUE; |
10 | 501 } |
502 break; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
503 |
10 | 504 default: |
175 | 505 if (any) |
39 | 506 { |
507 if (th_tolower(*pattern) == th_tolower(*str)) | |
508 { | |
175 | 509 any = FALSE; |
39 | 510 } |
511 else | |
512 { | |
10 | 513 if (*str) |
514 str++; | |
515 else | |
175 | 516 matched = FALSE; |
10 | 517 } |
39 | 518 } |
519 else | |
520 { | |
521 if (th_tolower(*pattern) == th_tolower(*str)) | |
522 { | |
10 | 523 if (*pattern) |
524 pattern++; | |
525 if (*str) | |
526 str++; | |
39 | 527 } |
528 else | |
529 { | |
175 | 530 if (tmp) |
39 | 531 { |
175 | 532 any = TRUE; |
533 pattern = tmp; | |
39 | 534 } |
535 else | |
175 | 536 matched = FALSE; |
10 | 537 } |
538 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
539 |
10 | 540 if (!*str && !*pattern) |
175 | 541 end = TRUE; |
39 | 542 |
10 | 543 break; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
544 |
129 | 545 } // switch |
175 | 546 } while (matched && !end); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
547 |
175 | 548 return matched; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
549 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
550 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
551 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
552 int th_get_hex_triplet(const char *str) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
553 { |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
554 const char *p = str; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
555 int len, val = 0; |
44 | 556 |
39 | 557 for (len = 0; *p && len < 6; p++, len++) |
558 { | |
559 if (*p >= '0' && *p <= '9') | |
560 { | |
561 val *= 16; | |
562 val += (*p - '0'); | |
563 } | |
174 | 564 else |
565 if (*p >= 'A' && *p <= 'F') | |
39 | 566 { |
567 val *= 16; | |
568 val += (*p - 'A') + 10; | |
569 } | |
174 | 570 else |
571 if (*p >= 'a' && *p <= 'f') | |
39 | 572 { |
573 val *= 16; | |
574 val += (*p - 'a') + 10; | |
575 } | |
576 else | |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
577 return -1; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
578 } |
44 | 579 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
580 return (len == 6) ? val : -1; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
581 } |
162
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
582 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
583 |
167
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
584 BOOL th_get_boolean(const char *str, BOOL *value) |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
585 { |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
586 if (!th_strcasecmp(str, "yes") || |
169
a06a87b771ce
Add on/off to valid boolean values.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
587 !th_strcasecmp(str, "on") || |
167
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
588 !th_strcasecmp(str, "true") || |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
589 !th_strcasecmp(str, "1")) |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
590 { |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
591 *value = TRUE; |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
592 return TRUE; |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
593 } |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
594 else |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
595 if (!th_strcasecmp(str, "no") || |
169
a06a87b771ce
Add on/off to valid boolean values.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
596 !th_strcasecmp(str, "off") || |
167
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
597 !th_strcasecmp(str, "false") || |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
598 !th_strcasecmp(str, "0")) |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
599 { |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
600 *value = FALSE; |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
601 return TRUE; |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
602 } |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
603 else |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
604 return FALSE; |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
605 } |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
606 |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
607 |
162
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
608 static void th_pad(FILE *outFile, int count) |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
609 { |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
610 while (count--) |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
611 fputc(' ', outFile); |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
612 } |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
613 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
614 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
615 void th_print_wrap(FILE *fh, const char *str, int spad, int rpad, int width) |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
616 { |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
617 size_t pos = 0; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
618 BOOL first = TRUE; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
619 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
620 while (str[pos]) |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
621 { |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
622 // Pre-pad line |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
623 int linelen = first ? spad : rpad; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
624 th_pad(fh, first ? 0 : rpad); |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
625 first = FALSE; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
626 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
627 // Skip whitespace at line start |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
628 while (th_isspace(str[pos]) || str[pos] == '\n') pos++; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
629 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
630 // Handle each word |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
631 while (str[pos] && str[pos] != '\n') |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
632 { |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
633 size_t next; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
634 int wlen; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
635 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
636 // Find word length and next break |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
637 for (wlen = 0, next = pos; str[next] && !th_isspace(str[next]) && str[next] != '\n'; next++, wlen++); |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
638 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
639 // Check if we have too much of text? |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
640 if (linelen + wlen >= width) |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
641 break; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
642 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
643 // Print what we have |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
644 for (;pos < next; pos++, linelen++) |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
645 fputc(str[pos], fh); |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
646 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
647 // Check if we are at end of input or hard linefeed |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
648 if (str[next] == '\n' || str[next] == 0) |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
649 break; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
650 else |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
651 { |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
652 fputc(str[pos], fh); |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
653 pos++; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
654 linelen++; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
655 } |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
656 } |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
657 fprintf(fh, "\n"); |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
658 } |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
659 } |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
660 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
661 |