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