annotate th_string.c @ 38:cdaa16f99474

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