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