annotate th_string.c @ 354:e96015ed35d0

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