annotate th_string.c @ 260:ab18ebbb5529

Check for NULL fmt.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 17 Feb 2016 12:59:51 +0200
parents 855f8dc6f123
children e459a28ee1be
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
231
d480cee946de Update copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 230
diff changeset
4 * (C) Copyright 2002-2016 Tecnic Software productions (TNSP)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
8 #include "th_util.h"
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include "th_string.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
80
12f1af4ffc6d Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
11
12f1af4ffc6d Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
12 /* Implementation of strdup() with a NULL check
8
acc2a8035dd5 Re-add th_strdup().
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
13 */
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
14 char *th_strdup(const char *src)
8
acc2a8035dd5 Re-add th_strdup().
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
15 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
16 char *res;
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
17 if (src == NULL)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
18 return NULL;
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
19
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
20 if ((res = th_malloc(strlen(src) + 1)) == NULL)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
21 return NULL;
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
22
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
23 strcpy(res, src);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
24 return res;
8
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
acc2a8035dd5 Re-add th_strdup().
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
27
81
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
28 /* Implementation of strndup() with NULL check
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
29 */
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
30 char *th_strndup(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
31 {
81
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
32 char *res;
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
33 size_t len;
79
909ae72c0763 Remove th_strncpy().
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
34
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
35 if (src == NULL)
81
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
36 return NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
38 len = strlen(src);
81
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
39 if (len > n)
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
40 len = n;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41
82
3e5c0615125c Oops, th_alloc() -> th_malloc().
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
42 if ((res = th_malloc(len + 1)) == NULL)
81
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
43 return NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
45 memcpy(res, src, len);
81
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
46 res[len] = 0;
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
47
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
48 return res;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51
223
d58b92ef5c03 Improve documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 222
diff changeset
52 /* Like strdup, but trims whitespace from the string according to specified flags.
d58b92ef5c03 Improve documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 222
diff changeset
53 * See TH_TRIM_* in th_string.h. If the resulting string would be empty (length 0),
d58b92ef5c03 Improve documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 222
diff changeset
54 * NULL is returned.
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
55 */
221
81b4688f684b Oops, broken function. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents: 220
diff changeset
56 static inline char * th_strdup_trim_do(const char *src, size_t len, const int flags)
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
57 {
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
58 char *res;
220
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
59 size_t start, end;
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
60
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
61 if (len == 0)
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
62 return NULL;
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
63
174
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
64 // Trim start: find first non-whitespace character
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
65 if (flags & TH_TRIM_START)
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
66 for (start = 0; start < len && th_isspace(src[start]); start++);
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
67 else
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
68 start = 0;
174
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
69
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
70 // Trim end: find last non-whitespace character
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
71 if (flags & TH_TRIM_END)
224
128c8cb80205 Fix th_str(n)dup_trim() functions. :S
Matti Hamalainen <ccr@tnsp.org>
parents: 223
diff changeset
72 for (end = len - 1; end > start && th_isspace(src[end]); end--);
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
73 else
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
74 end = len;
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
75
174
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
76 // Allocate memory for result
224
128c8cb80205 Fix th_str(n)dup_trim() functions. :S
Matti Hamalainen <ccr@tnsp.org>
parents: 223
diff changeset
77 if (src[end] == 0 || th_isspace(src[end]))
222
723a69185102 Fix th_str{n}dup_trim() functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 221
diff changeset
78 return NULL;
723a69185102 Fix th_str{n}dup_trim() functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 221
diff changeset
79
224
128c8cb80205 Fix th_str(n)dup_trim() functions. :S
Matti Hamalainen <ccr@tnsp.org>
parents: 223
diff changeset
80 len = end - start + 1;
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
81 if ((res = th_malloc(len + 1)) == NULL)
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
82 return NULL;
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
83
224
128c8cb80205 Fix th_str(n)dup_trim() functions. :S
Matti Hamalainen <ccr@tnsp.org>
parents: 223
diff changeset
84 memcpy(res, src + start, len);
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
85 res[len] = 0;
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
86 return res;
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
87 }
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
88
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
89
220
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
90 char *th_strdup_trim(const char *src, const int flags)
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
91 {
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
92 if (src == NULL)
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
93 return NULL;
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
94
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
95 return th_strdup_trim_do(src, strlen(src), flags);
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
96 }
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
97
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
98
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
99 char *th_strndup_trim(const char *src, const size_t n, const int flags)
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
100 {
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
101 size_t len;
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
102 if (src == NULL || n == 0)
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
103 return NULL;
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
104
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
105 for (len = 0; len < n && src[len]; len++);
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
106
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
107 return th_strdup_trim_do(src, len, flags);
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
108 }
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
109
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
110
236
334adfa3f646 Function argument list split & indentation cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 235
diff changeset
111 //
334adfa3f646 Function argument list split & indentation cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 235
diff changeset
112 // Simple implementations of printf() type functions
334adfa3f646 Function argument list split & indentation cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 235
diff changeset
113 //
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
114 static int th_vput_int(th_printf_ctx *ctx, int (*vputch)(th_printf_ctx *ctx, const char ch),
253
d1b4f4ea4715 Fix handling of negative values.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
115 int pval, const int radix, const char padMode, const char padChar,
245
c8595941852b Some more preparation work for precision support.
Matti Hamalainen <ccr@tnsp.org>
parents: 244
diff changeset
116 const int width, const BOOL unsig, const BOOL upcase, const BOOL sign)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
117 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
118 char buf[64];
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
119 size_t pos = 0;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
120 BOOL neg = FALSE;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
121 int ret = 0;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
122
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
123 if (radix > 16)
242
11f8cd7d73d3 Fix return values of some helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 241
diff changeset
124 return EOF;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
125
229
38a0719359ef Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 228
diff changeset
126 // Check for negative value
253
d1b4f4ea4715 Fix handling of negative values.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
127 if (pval < 0 && !unsig)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
128 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
129 neg = TRUE;
253
d1b4f4ea4715 Fix handling of negative values.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
130 pval = -pval;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
131 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
132
229
38a0719359ef Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 228
diff changeset
133 // Render the value to a string in buf (reversed)
253
d1b4f4ea4715 Fix handling of negative values.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
134 unsigned int val = pval;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
135 do
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
136 {
253
d1b4f4ea4715 Fix handling of negative values.
Matti Hamalainen <ccr@tnsp.org>
parents: 252
diff changeset
137 unsigned int digit = val % radix;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
138 if (digit < 10)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
139 buf[pos] = '0' + digit;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
140 else
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
141 buf[pos] = (upcase ? 'A' : 'a') + digit - 10;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
142 val /= radix;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
143 pos++;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
144 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
145 while (val > 0 && pos < sizeof(buf) - 1);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
146 buf[pos] = 0;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
147
233
1be1a9bab1d3 Add some error checking to th_vprintf_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 231
diff changeset
148 // Oops, the value did not fit in the buffer!
1be1a9bab1d3 Add some error checking to th_vprintf_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 231
diff changeset
149 if (val > 0)
242
11f8cd7d73d3 Fix return values of some helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 241
diff changeset
150 return EOF;
233
1be1a9bab1d3 Add some error checking to th_vprintf_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 231
diff changeset
151
229
38a0719359ef Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 228
diff changeset
152 // Do we want a sign prefix? Not for unsigned values
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
153 if (!unsig)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
154 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
155 char ch = sign ? (neg ? '-' : '+') : (neg ? '-' : 0);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
156 if (ch && (ret = vputch(ctx, ch)) == EOF)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
157 goto out;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
158 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
159
229
38a0719359ef Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 228
diff changeset
160 // Calculate necessary padding, if any
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
161 int nwidth = width - pos;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
162
241
d0bf513f2118 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 240
diff changeset
163 // Prefix padding?
235
507defb0cb61 Reverse order of certain checks (micro-optimize).
Matti Hamalainen <ccr@tnsp.org>
parents: 234
diff changeset
164 if (padMode != '-' && nwidth > 0)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
165 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
166 while (nwidth--)
234
82acddddb6b1 Add blocks to clarify some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 233
diff changeset
167 {
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
168 if ((ret = vputch(ctx, padMode)) == EOF)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
169 goto out;
234
82acddddb6b1 Add blocks to clarify some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 233
diff changeset
170 }
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
171 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
172
229
38a0719359ef Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 228
diff changeset
173 // Output the value
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
174 while (pos--)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
175 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
176 if ((ret = vputch(ctx, buf[pos])) == EOF)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
177 goto out;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
178 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
179
229
38a0719359ef Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 228
diff changeset
180 // Postfix padding?
235
507defb0cb61 Reverse order of certain checks (micro-optimize).
Matti Hamalainen <ccr@tnsp.org>
parents: 234
diff changeset
181 if (padMode == '-' && nwidth > 0)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
182 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
183 while (nwidth--)
234
82acddddb6b1 Add blocks to clarify some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 233
diff changeset
184 {
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
185 if ((ret = vputch(ctx, ' ')) == EOF)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
186 goto out;
234
82acddddb6b1 Add blocks to clarify some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 233
diff changeset
187 }
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
188 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
189
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
190 out:
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
191 return ret;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
192 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
193
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
194
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
195 static int th_vput_str(th_printf_ctx *ctx, int (*vputch)(th_printf_ctx *ctx, const char ch),
245
c8595941852b Some more preparation work for precision support.
Matti Hamalainen <ccr@tnsp.org>
parents: 244
diff changeset
196 const char *str, const char padMode, const char padChar,
c8595941852b Some more preparation work for precision support.
Matti Hamalainen <ccr@tnsp.org>
parents: 244
diff changeset
197 const int width, const int prec)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
198 {
248
3a2a29f801ed Add check for NULL strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
199 int nwidth, ret = 0;
3a2a29f801ed Add check for NULL strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
200
3a2a29f801ed Add check for NULL strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
201 // Check for null strings
3a2a29f801ed Add check for NULL strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
202 if (str == NULL)
3a2a29f801ed Add check for NULL strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
203 str = "(null)";
3a2a29f801ed Add check for NULL strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
204
3a2a29f801ed Add check for NULL strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
205 nwidth = width - strlen(str);
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
206
241
d0bf513f2118 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 240
diff changeset
207 // Prefix padding?
235
507defb0cb61 Reverse order of certain checks (micro-optimize).
Matti Hamalainen <ccr@tnsp.org>
parents: 234
diff changeset
208 if (padMode != '-' && nwidth > 0)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
209 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
210 while (nwidth--)
234
82acddddb6b1 Add blocks to clarify some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 233
diff changeset
211 {
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
212 if ((ret = vputch(ctx, padMode)) == EOF)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
213 goto out;
234
82acddddb6b1 Add blocks to clarify some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 233
diff changeset
214 }
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
215 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
216
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
217 while (*str)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
218 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
219 if ((ret = vputch(ctx, *str++)) == EOF)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
220 goto out;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
221 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
222
241
d0bf513f2118 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 240
diff changeset
223 // Postfix padding?
235
507defb0cb61 Reverse order of certain checks (micro-optimize).
Matti Hamalainen <ccr@tnsp.org>
parents: 234
diff changeset
224 if (padMode == '-' && nwidth > 0)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
225 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
226 while (nwidth--)
234
82acddddb6b1 Add blocks to clarify some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 233
diff changeset
227 {
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
228 if ((ret = vputch(ctx, ' ')) == EOF)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
229 goto out;
234
82acddddb6b1 Add blocks to clarify some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 233
diff changeset
230 }
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
231 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
232
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
233 out:
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
234 return ret;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
235 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
236
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
237
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
238 int th_vprintf_do(th_printf_ctx *ctx,
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
239 int (*vputch)(th_printf_ctx *ctx, const char ch),
252
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
240 const char *fmt, va_list ap)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
241 {
238
d3ab9d263409 Initialize return value variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 237
diff changeset
242 int ret = 0;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
243
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
244 while (*fmt)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
245 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
246 if (*fmt != '%')
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
247 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
248 if ((ret = vputch(ctx, *fmt)) == EOF)
247
2fc282c365a8 Use special error codes for syntax errors in format string.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
249 goto out;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
250 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
251 else
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
252 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
253 char padMode = ' ', padChar = 0;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
254 BOOL sign = FALSE;
244
59c45e295524 Initialize and check precision.
Matti Hamalainen <ccr@tnsp.org>
parents: 243
diff changeset
255 int width = 0, prec = -1;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
256
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
257 fmt++;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
258
241
d0bf513f2118 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 240
diff changeset
259 // Check sign
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
260 if (*fmt == '+')
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
261 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
262 sign = TRUE;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
263 fmt++;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
264 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
265
241
d0bf513f2118 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 240
diff changeset
266 // Check for padding
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
267 if (*fmt == '0' || *fmt == '-' || *fmt == '\'')
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
268 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
269 padMode = *fmt++;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
270 if (padMode == '\'')
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
271 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
272 padChar = *fmt++;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
273 if (*fmt != '\'')
247
2fc282c365a8 Use special error codes for syntax errors in format string.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
274 return -101;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
275 fmt++;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
276 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
277
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
278 if (*fmt == 0)
247
2fc282c365a8 Use special error codes for syntax errors in format string.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
279 return -102;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
280 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
281
241
d0bf513f2118 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 240
diff changeset
282 // Get width
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
283 while (th_isdigit(*fmt))
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
284 width = width * 10 + (*fmt++ - '0');
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
285
241
d0bf513f2118 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 240
diff changeset
286 // Check for precision
240
5e781dba6136 Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents: 239
diff changeset
287 if (*fmt == '.')
5e781dba6136 Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents: 239
diff changeset
288 {
5e781dba6136 Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents: 239
diff changeset
289 fmt++;
5e781dba6136 Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents: 239
diff changeset
290 if (!th_isdigit(*fmt))
247
2fc282c365a8 Use special error codes for syntax errors in format string.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
291 return -103;
240
5e781dba6136 Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents: 239
diff changeset
292
244
59c45e295524 Initialize and check precision.
Matti Hamalainen <ccr@tnsp.org>
parents: 243
diff changeset
293 prec = 0;
240
5e781dba6136 Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents: 239
diff changeset
294 while (th_isdigit(*fmt))
5e781dba6136 Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents: 239
diff changeset
295 prec = prec * 10 + (*fmt++ - '0');
5e781dba6136 Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents: 239
diff changeset
296 }
5e781dba6136 Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents: 239
diff changeset
297
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
298 switch (*fmt)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
299 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
300 case 0:
247
2fc282c365a8 Use special error codes for syntax errors in format string.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
301 return -104;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
302
249
35cfda75606b Implement %c.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
303 case 'c':
256
855f8dc6f123 Fix '%c' support.
Matti Hamalainen <ccr@tnsp.org>
parents: 253
diff changeset
304 if (padMode != ' ' || width > 0 || prec >= 0 || sign)
249
35cfda75606b Implement %c.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
305 return -105;
250
f4b541e0433e Hmm .. fix %c handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 249
diff changeset
306
f4b541e0433e Hmm .. fix %c handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 249
diff changeset
307 if ((ret = vputch(ctx, va_arg(ap, int))) == EOF)
249
35cfda75606b Implement %c.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
308 goto out;
35cfda75606b Implement %c.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
309 break;
35cfda75606b Implement %c.
Matti Hamalainen <ccr@tnsp.org>
parents: 248
diff changeset
310
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
311 case 'u':
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
312 case 'd':
244
59c45e295524 Initialize and check precision.
Matti Hamalainen <ccr@tnsp.org>
parents: 243
diff changeset
313 if ((padMode != '0' && padMode != '-' && padMode != ' ') || prec >= 0)
247
2fc282c365a8 Use special error codes for syntax errors in format string.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
314 return -105;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
315
246
e00b547af1a7 Rename internal function th_vput_itoa() to th_vput_int()
Matti Hamalainen <ccr@tnsp.org>
parents: 245
diff changeset
316 if ((ret = th_vput_int(ctx, vputch, va_arg(ap, unsigned int),
245
c8595941852b Some more preparation work for precision support.
Matti Hamalainen <ccr@tnsp.org>
parents: 244
diff changeset
317 10, padMode, padChar, width, *fmt == 'u', FALSE, sign)) == EOF)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
318 goto out;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
319 break;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
320
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
321 case 'x':
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
322 case 'X':
244
59c45e295524 Initialize and check precision.
Matti Hamalainen <ccr@tnsp.org>
parents: 243
diff changeset
323 if ((padMode != '0' && padMode != '-' && padMode != ' ') || prec >= 0)
247
2fc282c365a8 Use special error codes for syntax errors in format string.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
324 return -106;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
325
246
e00b547af1a7 Rename internal function th_vput_itoa() to th_vput_int()
Matti Hamalainen <ccr@tnsp.org>
parents: 245
diff changeset
326 if ((ret = th_vput_int(ctx, vputch, va_arg(ap, unsigned int),
245
c8595941852b Some more preparation work for precision support.
Matti Hamalainen <ccr@tnsp.org>
parents: 244
diff changeset
327 16, padMode, padChar, width, TRUE, *fmt == 'X', FALSE)) == EOF)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
328 goto out;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
329 break;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
330
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
331 case 'f':
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
332 goto out;
240
5e781dba6136 Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents: 239
diff changeset
333 break;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
334
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
335 case 's':
230
0b243bc5ffd6 Stray nbsp.
Matti Hamalainen <ccr@tnsp.org>
parents: 229
diff changeset
336 if ((padMode != '-' && padMode != ' ') || sign)
247
2fc282c365a8 Use special error codes for syntax errors in format string.
Matti Hamalainen <ccr@tnsp.org>
parents: 246
diff changeset
337 return -108;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
338
245
c8595941852b Some more preparation work for precision support.
Matti Hamalainen <ccr@tnsp.org>
parents: 244
diff changeset
339 if ((ret = th_vput_str(ctx, vputch, va_arg(ap, char *), padMode, padChar, width, prec)) == EOF)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
340 goto out;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
341 break;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
342
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
343 case '%':
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
344 default:
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
345 if ((ret = vputch(ctx, *fmt)) == EOF)
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
346 goto out;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
347 break;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
348 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
349 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
350 fmt++;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
351 }
243
5049c220d2ae Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 242
diff changeset
352
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
353 out:
252
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
354 return ret == EOF ? ret : ctx->pos;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
355 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
356
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
357
239
10f596441e75 Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents: 238
diff changeset
358 #ifdef TH_USE_INTERNAL_SPRINTF
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
359 static int th_pbuf_vputch(th_printf_ctx *ctx, const char ch)
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
360 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
361 if (ctx->pos < ctx->size)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
362 ctx->buf[ctx->pos] = ch;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
363 ctx->pos++;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
364 return ch;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
365 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
366
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
367
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
368 static int th_stdio_vputch(th_printf_ctx *ctx, const char ch)
239
10f596441e75 Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents: 238
diff changeset
369 {
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
370 ctx->pos++;
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
371 return fputc(ch, (FILE *) ctx->data);
239
10f596441e75 Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents: 238
diff changeset
372 }
10f596441e75 Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents: 238
diff changeset
373 #endif
10f596441e75 Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents: 238
diff changeset
374
10f596441e75 Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents: 238
diff changeset
375
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
376 int th_vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
377 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
378 #ifdef TH_USE_INTERNAL_SPRINTF
252
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
379 int ret;
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
380 th_printf_ctx ctx;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
381 ctx.buf = buf;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
382 ctx.size = size;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
383 ctx.pos = 0;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
384
252
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
385 ret = th_vprintf_do(&ctx, th_pbuf_vputch, fmt, ap);
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
386
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
387 if (ctx.pos < size)
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
388 buf[ctx.pos] = 0;
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
389 else
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
390 if (size > 0)
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
391 buf[size - 1] = 0;
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
392
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
393 return ret;
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
394 #else
237
c55ebc438243 Oops, s/vsnpintf/vsnprintf/.
Matti Hamalainen <ccr@tnsp.org>
parents: 236
diff changeset
395 return vsnprintf(buf, size, fmt, ap);
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
396 #endif
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
397 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
398
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
399
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
400 int th_snprintf(char *buf, size_t size, const char *fmt, ...)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
401 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
402 int n;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
403 va_list ap;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
404 va_start(ap, fmt);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
405 #ifdef TH_USE_INTERNAL_SPRINTF
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
406 n = th_vsnprintf(buf, size, fmt, ap);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
407 #else
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
408 n = vsnprintf(buf, size, fmt, ap);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
409 #endif
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
410 va_end(ap);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
411 return n;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
412 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
413
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
414
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
415 int th_vfprintf(FILE *fh, const char *fmt, va_list ap)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
416 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
417 #ifdef TH_USE_INTERNAL_SPRINTF
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
418 th_printf_ctx ctx;
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
419 ctx.data = (void *) fh;
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
420 ctx.pos = 0;
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
421
252
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
422 return th_vprintf_do(&ctx, th_stdio_vputch, fmt, ap);
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
423 #else
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
424 return vfprintf(fh, fmt, ap);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
425 #endif
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
426 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
427
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
428
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
429 int th_fprintf(FILE *fh, const char *fmt, ...)
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
430 {
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
431 int ret;
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
432 #ifdef TH_USE_INTERNAL_SPRINTF
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
433 th_printf_ctx ctx;
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
434 #endif
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
435 va_list ap;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
436 va_start(ap, fmt);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
437 #ifdef TH_USE_INTERNAL_SPRINTF
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
438 ctx.data = (void *) fh;
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
439 ctx.pos = 0;
252
80ffeb316596 More work on printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 251
diff changeset
440 ret = th_vprintf_do(&ctx, th_stdio_vputch, fmt, ap);
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
441 #else
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
442 ret = fprintf(fh, fmt, ap);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
443 #endif
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
444 va_end(ap);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
445 return ret;
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
446 }
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
447
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
448
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
449 /* Simulate a sprintf() that allocates memory
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
450 */
43
8c015ac0c556 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
451 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
452 {
133
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
453 int size = 64;
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
454 char *buf, *tmp;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455
260
ab18ebbb5529 Check for NULL fmt.
Matti Hamalainen <ccr@tnsp.org>
parents: 256
diff changeset
456 if (fmt == NULL)
ab18ebbb5529 Check for NULL fmt.
Matti Hamalainen <ccr@tnsp.org>
parents: 256
diff changeset
457 return NULL;
ab18ebbb5529 Check for NULL fmt.
Matti Hamalainen <ccr@tnsp.org>
parents: 256
diff changeset
458
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
459 if ((buf = th_malloc(size)) == NULL)
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
460 return NULL;
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
461
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
462 while (1)
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
463 {
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
464 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
465 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
466 va_copy(ap, args);
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
467 #ifdef TH_USE_INTERNAL_SPRINTF
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
468 n = th_vsnprintf(buf, size, fmt, ap);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
469 #else
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
470 n = vsnprintf(buf, size, fmt, ap);
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
471 #endif
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
472 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
473
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
474 if (n > -1 && n < size)
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
475 return buf;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
476 if (n > -1)
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
477 size = n + 1;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
478 else
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
479 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
480
133
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
481 if ((tmp = th_realloc(buf, size)) == NULL)
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
482 {
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
483 th_free(buf);
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
484 return NULL;
133
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
485 }
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
486 else
84182dab4fca Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
487 buf = tmp;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
488 }
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
489 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
491
43
8c015ac0c556 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
492 char *th_strdup_printf(const char *fmt, ...)
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
493 {
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
494 char *res;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
495 va_list ap;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
496
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
497 va_start(ap, fmt);
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
498 res = th_strdup_vprintf(fmt, ap);
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
499 va_end(ap);
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
500
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
501 return res;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504
30
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
505 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
506 {
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
507 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
508 th_free(*buf);
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
509 *buf = tmp;
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
510 }
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
511
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
512
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
513 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
514 {
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
515 char *tmp;
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
516 va_list ap;
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
517
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
518 va_start(ap, fmt);
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
519 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
520 va_end(ap);
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
521
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
522 th_free(*buf);
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
523 *buf = tmp;
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
524 }
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
525
88
d3d3cd41a95a Slight adjustments.
Matti Hamalainen <ccr@tnsp.org>
parents: 87
diff changeset
526
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527 /* 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
528 */
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
529 int th_strcasecmp(const char *haystack, const char *needle)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
531 const char *s1 = haystack, *s2 = needle;
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
532 assert(haystack != NULL);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
533 assert(needle != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
535 if (haystack == needle)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
536 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
538 while (*s1 && *s2 && th_tolower(*s1) == th_tolower(*s2))
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
539 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
540 s1++;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
541 s2++;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
542 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543
88
d3d3cd41a95a Slight adjustments.
Matti Hamalainen <ccr@tnsp.org>
parents: 87
diff changeset
544 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
545 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
548 int th_strncasecmp(const char *haystack, const char *needle, size_t n)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
550 const char *s1 = haystack, *s2 = needle;
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
551 assert(haystack != NULL);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
552 assert(needle != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
554 if (haystack == needle)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
555 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
556
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
557 while (n > 0 && *s1 && *s2 && th_tolower(*s1) == th_tolower(*s2))
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
558 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
559 s1++;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
560 s2++;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
561 n--;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
562 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
564 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
565 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567
89
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
568 /* Check if end of the given string str matches needle
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
569 * case-insensitively, return pointer to start of the match,
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
570 * if found, NULL otherwise.
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
571 */
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
572 char *th_strrcasecmp(char *str, const char *needle)
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
573 {
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
574 if (str == NULL || needle == NULL)
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
575 return NULL;
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
576
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
577 const size_t
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
578 slen = strlen(str),
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
579 nlen = strlen(needle);
228
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
580
89
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
581 if (slen < nlen)
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
582 return NULL;
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
583
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
584 if (th_strcasecmp(str - nlen - 1, needle) == 0)
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
585 return str - nlen - 1;
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
586 else
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
587 return NULL;
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
588 }
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
589
30690f5c4cae Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 88
diff changeset
590
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
591 /* 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
592 * 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
593 */
43
8c015ac0c556 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
594 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
595 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
596 char *i, *j;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
597 assert(str != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
598
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
599 i = str;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
600 j = str;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
601 while (*i)
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
602 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
603 if (!th_iscntrl(*i))
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
604 *(j++) = *i;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
605 i++;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
606 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
608 *j = 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
609 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
610
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
611
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
612 /* Copy a given string over in *pdst.
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
613 */
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
614 int th_pstrcpy(char **pdst, const char *src)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
615 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
616 assert(pdst != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
617
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
618 if (src == NULL)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
619 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
620
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
621 th_free(*pdst);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
622 if ((*pdst = th_malloc(strlen(src) + 1)) == NULL)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
623 return -2;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
624
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
625 strcpy(*pdst, src);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
626 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
627 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
630 /* Concatenates a given string into string pointed by *pdst.
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 */
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
632 int th_pstrcat(char **pdst, const char *src)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
634 assert(pdst != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
635
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
636 if (src == NULL)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
637 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
638
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
639 if (*pdst != NULL)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
640 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
641 *pdst = th_realloc(*pdst, strlen(*pdst) + strlen(src) + 1);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
642 if (*pdst == NULL)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
643 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
644
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
645 strcat(*pdst, src);
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
646 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
647 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
648 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
649 *pdst = th_malloc(strlen(src) + 1);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
650 if (*pdst == NULL)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
651 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
652
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
653 strcpy(*pdst, src);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
654 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
655
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
656 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
657 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
658
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
659
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
660 /* 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
661 * 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
662 * returns pointer to the string.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
663 */
43
8c015ac0c556 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
664 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
665 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
666 assert(str != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
667
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
668 // Terminating NULL-character is not whitespace!
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
669 while (th_isspace(str[*pos]))
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
670 (*pos)++;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
671
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
672 return &str[*pos];
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
673 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
674
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
675
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
676 /* 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
677 */
43
8c015ac0c556 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
678 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
679 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
680 assert(str != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
681
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
682 while (str[*pos] && str[*pos] != sep)
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
683 (*pos)++;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
684
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
685 return &str[*pos];
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
686 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
687
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
688
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
689 /* 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
690 */
43
8c015ac0c556 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
691 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
692 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
693 assert(str != NULL);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
694
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
695 while (!th_isspace(str[*pos]) && str[*pos] != sep)
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
696 (*pos)++;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
697
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
698 return &str[*pos];
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
699 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
700
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
701
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
702 /* 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
703 * 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
704 * 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
705 * any number of characters.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706 */
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
707 BOOL th_strmatch(const char *haystack, const char *pattern)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
708 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
709 BOOL matched = TRUE, any = FALSE, end = FALSE;
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
710 const char *tmp = NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
711
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
712 // Check given pattern and string
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
713 if (haystack == NULL || pattern == NULL)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
714 return FALSE;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
715
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
716 // Start comparision
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
717 do {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
718 matched = FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
719 switch (*pattern)
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
720 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
721 case '?':
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
722 // Any single character matches
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
723 if (*haystack)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
724 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
725 matched = TRUE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
726 pattern++;
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
727 haystack++;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
728 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
729 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
730
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
731 case '*':
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
732 matched = TRUE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
733 pattern++;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
734 if (!*pattern)
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
735 end = TRUE;
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
736 any = TRUE;
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
737 tmp = pattern;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
738 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
739
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
740 case 0:
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
741 if (any)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
742 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
743 if (*haystack)
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
744 haystack++;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
745 else
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
746 end = TRUE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
747 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
748 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
749 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
750 if (*haystack)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
751 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
752 if (tmp)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
753 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
754 any = TRUE;
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
755 pattern = tmp;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
756 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
757 else
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
758 matched = FALSE;
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
759 }
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
760 else
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
761 end = TRUE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
762 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
763 break;
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
764
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
765 default:
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
766 if (any)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
767 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
768 if (*pattern == *haystack)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
769 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
770 any = FALSE;
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
771 matched = TRUE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
772 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
773 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
774 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
775 if (*haystack)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
776 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
777 matched = TRUE;
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
778 haystack++;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
779 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
780 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
781 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
782 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
783 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
784 if (*pattern == *haystack)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
785 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
786 matched = TRUE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
787 if (*pattern)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
788 pattern++;
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
789 if (*haystack)
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
790 haystack++;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
791 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
792 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
793 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
794 if (tmp)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
795 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
796 matched = TRUE;
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
797 any = TRUE;
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
798 pattern = tmp;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
799 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
800 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
801 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
802
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
803 if (!*haystack && !*pattern)
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
804 end = TRUE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
805 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
806
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
807 } // switch
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
808 } while (matched && !end);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
809
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
810 return matched;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
811 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
812
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
813
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
814 /* 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
815 */
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
816 BOOL th_strcasematch(const char *haystack, const char *pattern)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
817 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
818 BOOL matched = TRUE, any = FALSE, end = FALSE;
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
819 const char *tmp = NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
820
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
821 // Check given pattern and string
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
822 if (haystack == NULL || pattern == NULL)
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
823 return FALSE;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
824
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
825 // Start comparision
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
826 do {
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
827 switch (*pattern) {
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
828 case '?':
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
829 // Any single character matches
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
830 if (*haystack)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
831 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
832 pattern++;
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
833 haystack++;
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
834 }
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
835 else
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
836 matched = FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
837 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
838
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
839 case '*':
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
840 pattern++;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
841 if (!*pattern || *pattern == '?')
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
842 end = TRUE;
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
843 any = TRUE;
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
844 tmp = pattern;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
845 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
846
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
847 case 0:
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
848 if (any)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
849 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
850 if (*haystack)
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
851 haystack++;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
852 else
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
853 end = TRUE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
854 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
855 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
856 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
857 if (*haystack)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
858 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
859 if (tmp)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
860 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
861 any = TRUE;
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
862 pattern = tmp;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
863 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
864 else
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
865 matched = FALSE;
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
866 }
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
867 else
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
868 end = TRUE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
869 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
870 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
871
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
872 default:
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
873 if (any)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
874 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
875 if (th_tolower(*pattern) == th_tolower(*haystack))
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
876 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
877 any = FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
878 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
879 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
880 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
881 if (*haystack)
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
882 haystack++;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
883 else
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
884 matched = FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
885 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
886 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
887 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
888 {
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
889 if (th_tolower(*pattern) == th_tolower(*haystack))
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
890 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
891 if (*pattern)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
892 pattern++;
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
893 if (*haystack)
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
894 haystack++;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
895 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
896 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
897 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
898 if (tmp)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
899 {
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
900 any = TRUE;
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
901 pattern = tmp;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
902 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
903 else
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
904 matched = FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
905 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
906 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
907
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
908 if (!*haystack && !*pattern)
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
909 end = TRUE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
910
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
911 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
912
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
913 } // switch
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
914 } while (matched && !end);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
915
175
3d0a1f87e393 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 174
diff changeset
916 return matched;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
917 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
918
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
919
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
920 int th_get_hex_triplet(const char *str)
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
921 {
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
922 const char *p = str;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
923 int len, val = 0;
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
924
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
925 for (len = 0; *p && len < 6; p++, len++)
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
926 {
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
927 if (*p >= '0' && *p <= '9')
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
928 {
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
929 val *= 16;
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
930 val += (*p - '0');
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
931 }
174
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
932 else
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
933 if (*p >= 'A' && *p <= 'F')
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
934 {
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
935 val *= 16;
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
936 val += (*p - 'A') + 10;
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
937 }
174
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
938 else
7d5707438333 Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 169
diff changeset
939 if (*p >= 'a' && *p <= 'f')
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
940 {
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
941 val *= 16;
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
942 val += (*p - 'a') + 10;
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
943 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 38
diff changeset
944 else
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
945 return -1;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
946 }
44
669d2bc97c57 More cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
947
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
948 return (len == 6) ? val : -1;
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
949 }
162
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
950
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
951
167
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
952 BOOL th_get_boolean(const char *str, BOOL *value)
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
953 {
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
954 if (!th_strcasecmp(str, "yes") ||
169
a06a87b771ce Add on/off to valid boolean values.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
955 !th_strcasecmp(str, "on") ||
167
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
956 !th_strcasecmp(str, "true") ||
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
957 !th_strcasecmp(str, "1"))
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
958 {
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
959 *value = TRUE;
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
960 return TRUE;
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
961 }
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
962 else
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
963 if (!th_strcasecmp(str, "no") ||
169
a06a87b771ce Add on/off to valid boolean values.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
964 !th_strcasecmp(str, "off") ||
167
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
965 !th_strcasecmp(str, "false") ||
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
966 !th_strcasecmp(str, "0"))
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
967 {
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
968 *value = FALSE;
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
969 return TRUE;
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
970 }
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
971 else
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
972 return FALSE;
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
973 }
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
974
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
975
162
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
976 static void th_pad(FILE *outFile, int count)
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
977 {
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
978 while (count--)
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
979 fputc(' ', outFile);
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
980 }
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
981
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
982
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
983 void th_print_wrap(FILE *fh, const char *str, int spad, int rpad, int width)
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
984 {
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
985 size_t pos = 0;
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
986 BOOL first = TRUE;
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
987
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
988 while (str[pos])
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
989 {
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
990 // Pre-pad line
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
991 int linelen = first ? spad : rpad;
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
992 th_pad(fh, first ? 0 : rpad);
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
993 first = FALSE;
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
994
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
995 // Skip whitespace at line start
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
996 while (th_isspace(str[pos]) || str[pos] == '\n') pos++;
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
997
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
998 // Handle each word
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
999 while (str[pos] && str[pos] != '\n')
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1000 {
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1001 size_t next;
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1002 int wlen;
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1003
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1004 // Find word length and next break
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1005 for (wlen = 0, next = pos; str[next] && !th_isspace(str[next]) && str[next] != '\n'; next++, wlen++);
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1006
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1007 // Check if we have too much of text?
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1008 if (linelen + wlen >= width)
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1009 break;
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1010
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1011 // Print what we have
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1012 for (;pos < next; pos++, linelen++)
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1013 fputc(str[pos], fh);
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1014
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1015 // Check if we are at end of input or hard linefeed
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1016 if (str[next] == '\n' || str[next] == 0)
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1017 break;
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1018 else
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1019 {
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1020 fputc(str[pos], fh);
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1021 pos++;
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1022 linelen++;
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1023 }
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1024 }
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1025 fprintf(fh, "\n");
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1026 }
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
1027 }