annotate th_string.c @ 529:c44ebf5b08fd

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