annotate th_string.c @ 79:909ae72c0763

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