annotate th_string.c @ 317:c532088b4f05

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