Mercurial > hg > th-libs
annotate th_string.c @ 386:2974ce76da8a
Remove debug variable.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 03 Mar 2016 13:16:45 +0200 |
parents | 65c42f8b39e0 |
children | 56ec224421a6 |
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, |
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
|
199 const BOOL f_unsig, char *(f_alt)(const char *buf, 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]; |
361
ad9719373fe3
Simplify th_printf_vbuf*() helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
360
diff
changeset
|
202 int f_len = 0, ret = 0, vret, nwidth; |
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 f_flags &= ~TH_PF_ZERO; |
f6b9991d76ed
Some work on pointer formatter %p.
Matti Hamalainen <ccr@tnsp.org>
parents:
369
diff
changeset
|
233 } |
f6b9991d76ed
Some work on pointer formatter %p.
Matti Hamalainen <ccr@tnsp.org>
parents:
369
diff
changeset
|
234 else |
f6b9991d76ed
Some work on pointer formatter %p.
Matti Hamalainen <ccr@tnsp.org>
parents:
369
diff
changeset
|
235 if (f_prec != 0) |
f6b9991d76ed
Some work on pointer formatter %p.
Matti Hamalainen <ccr@tnsp.org>
parents:
369
diff
changeset
|
236 { |
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 buf[f_len] = 0; |
f6b9991d76ed
Some work on pointer formatter %p.
Matti Hamalainen <ccr@tnsp.org>
parents:
369
diff
changeset
|
239 } |
361
ad9719373fe3
Simplify th_printf_vbuf*() helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
360
diff
changeset
|
240 } |
ad9719373fe3
Simplify th_printf_vbuf*() helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
360
diff
changeset
|
241 |
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
|
242 f_altstr = vret != 0 && (f_flags & TH_PF_ALT) && f_alt != NULL ? f_alt(buf, 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 |
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
|
250 if (f_sign) |
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
|
251 f_width--; |
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 |
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
|
253 if ((f_flags & TH_PF_ZERO) && f_prec < 0 && (f_flags & TH_PF_LEFT) == 0) |
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
|
254 f_prec = f_width; |
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 |
324
0084618812ee
Fix th_printf_vput_intstr() so that most tests pass. Not certain how
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
256 f_prec = (f_prec > f_len) ? f_prec - f_len : 0; |
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
|
257 nwidth = f_width - f_len - f_prec - (f_altstr ? strlen(f_altstr) : 0); |
383
39464ba52032
Add debug prints when TH_PRINTF_DEBUG is defined.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
258 #ifdef TH_PRINTF_DEBUG |
386 | 259 printf("W2: vret=%3d, f_flags=[%s], f_width=%3d, f_prec=%3d, f_sign='%c', nwidth=%d\n", |
260 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
|
261 #endif |
317
c532088b4f05
Fix float tests (type was int) :D
Matti Hamalainen <ccr@tnsp.org>
parents:
314
diff
changeset
|
262 |
241 | 263 // 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
|
264 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
|
265 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
|
266 |
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
|
267 // Do we want a sign prefix? Not for unsigned values |
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
|
268 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
|
269 return ret; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
270 |
369
f26290f8d35e
Rename some internal functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
368
diff
changeset
|
271 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
|
272 return ret; |
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
273 |
369
f26290f8d35e
Rename some internal functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
368
diff
changeset
|
274 if (f_prec > 0 && (ret = th_vprintf_put_repch(ctx, vputch, f_prec, '0')) == EOF) |
357
77201824790c
Use th_printf_vput_repch() where appropriate.
Matti Hamalainen <ccr@tnsp.org>
parents:
355
diff
changeset
|
275 return ret; |
317
c532088b4f05
Fix float tests (type was int) :D
Matti Hamalainen <ccr@tnsp.org>
parents:
314
diff
changeset
|
276 |
229 | 277 // 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
|
278 while (f_len-- > 0) |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
279 { |
323 | 280 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
|
281 return ret; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
282 } |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
283 |
229 | 284 // 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
|
285 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
|
286 } |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
299
diff
changeset
|
287 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
299
diff
changeset
|
288 |
374
1d8ae82304ec
Rename s/th_printf_ctx/th_vprintf_cts/g.
Matti Hamalainen <ccr@tnsp.org>
parents:
373
diff
changeset
|
289 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
|
290 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
|
291 { |
322 | 292 int nwidth, f_len, ret = 0; |
317
c532088b4f05
Fix float tests (type was int) :D
Matti Hamalainen <ccr@tnsp.org>
parents:
314
diff
changeset
|
293 |
248
3a2a29f801ed
Add check for NULL strings.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
294 // Check for null strings |
3a2a29f801ed
Add check for NULL strings.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
295 if (str == NULL) |
3a2a29f801ed
Add check for NULL strings.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
296 str = "(null)"; |
3a2a29f801ed
Add check for NULL strings.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
297 |
322 | 298 f_len = strlen(str); |
299 if (f_prec >= 0 && f_len > f_prec) | |
300 f_len = f_prec; | |
335 | 301 |
322 | 302 nwidth = f_width - f_len; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
303 |
241 | 304 // 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
|
305 if ((ret = th_printf_pad_pre(ctx, vputch, nwidth, f_flags)) == EOF) |
335 | 306 return ret; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
307 |
322 | 308 while (*str && f_len--) |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
309 { |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
310 if ((ret = vputch(ctx, *str++)) == EOF) |
335 | 311 return ret; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
312 } |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
313 |
241 | 314 // 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
|
315 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
|
316 } |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
317 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
318 |
375
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
319 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
|
320 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
|
321 { |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
322 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
|
323 int64_t val; |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
324 |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
325 // 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
|
326 // 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
|
327 // 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
|
328 memcpy(&val, &pval, sizeof(int64_t)); |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
329 |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
330 // We have sign, exponent and mantissa |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
331 BOOL f_sign = (val >> 63) & 0x01; |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
332 int64_t d_exp = (val >> 52) & 0x7ff; |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
333 uint64_t d_man = val & 0x0fffffffffffff; |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
334 |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
335 return 0; |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
336 } |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
337 |
04a273cce4fa
Initial th_vprintf_put_float() stub.
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
338 |
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
|
339 static char * th_printf_altfmt_oct(const char *buf, const int vret, const int flags) |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
340 { |
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
|
341 (void) vret; |
337 | 342 (void) flags; |
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
|
343 return (buf[0] != '0') ? "0" : ""; |
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
|
344 ; |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
345 } |
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
346 |
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
347 |
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
|
348 static char * th_printf_altfmt_hex(const char *buf, const int vret, const int flags) |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
349 { |
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
|
350 (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
|
351 (void) vret; |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
352 return (flags & TH_PF_UPCASE) ? "0X" : "0x"; |
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
353 } |
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
354 |
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
355 |
374
1d8ae82304ec
Rename s/th_printf_ctx/th_vprintf_cts/g.
Matti Hamalainen <ccr@tnsp.org>
parents:
373
diff
changeset
|
356 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
|
357 { |
238
d3ab9d263409
Initialize return value variable.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
358 int ret = 0; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
359 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
360 while (*fmt) |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
361 { |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
362 if (*fmt != '%') |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
363 { |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
364 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
|
365 goto out; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
366 } |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
367 else |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
368 { |
314
7bce1e9fa397
Add f_prec arguments to helpers and call points.
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
369 int f_width = 0, f_prec = -1, f_flags = 0; |
299
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
370 BOOL end = FALSE; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
371 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
372 fmt++; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
373 |
299
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
374 // Check for flags |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
375 while (!end) |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
376 { |
299
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
377 switch (*fmt) |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
378 { |
299
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
379 case '#': |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
380 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
|
381 break; |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
382 |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
383 case '+': |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
384 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
|
385 break; |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
386 |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
387 case '0': |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
388 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
|
389 break; |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
390 |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
391 case '-': |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
392 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
|
393 break; |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
394 |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
395 case ' ': |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
396 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
|
397 break; |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
398 |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
399 case '\'': |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
400 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
|
401 break; |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
402 |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
403 default: |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
404 end = TRUE; |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
405 break; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
406 } |
299
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
407 if (!end) fmt++; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
408 } |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
409 |
284 | 410 // Get field width |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
411 while (th_isdigit(*fmt)) |
285
e1414cf8fef8
Rename some function arguments and variables.
Matti Hamalainen <ccr@tnsp.org>
parents:
284
diff
changeset
|
412 f_width = f_width * 10 + (*fmt++ - '0'); |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
413 |
298 | 414 // Check for field precision |
240
5e781dba6136
Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
415 if (*fmt == '.') |
5e781dba6136
Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
416 { |
5e781dba6136
Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
417 fmt++; |
298 | 418 // If no digit after '.', precision is to be 0 |
285
e1414cf8fef8
Rename some function arguments and variables.
Matti Hamalainen <ccr@tnsp.org>
parents:
284
diff
changeset
|
419 f_prec = 0; |
240
5e781dba6136
Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
420 while (th_isdigit(*fmt)) |
285
e1414cf8fef8
Rename some function arguments and variables.
Matti Hamalainen <ccr@tnsp.org>
parents:
284
diff
changeset
|
421 f_prec = f_prec * 10 + (*fmt++ - '0'); |
240
5e781dba6136
Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
422 } |
298 | 423 |
287
e214637b0645
Add check for (currently unsupported) length modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
286
diff
changeset
|
424 // Check for length modifiers (NOT SUPPORTED CURRENTLY) |
e214637b0645
Add check for (currently unsupported) length modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
286
diff
changeset
|
425 switch (*fmt) |
e214637b0645
Add check for (currently unsupported) length modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
286
diff
changeset
|
426 { |
e214637b0645
Add check for (currently unsupported) length modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
286
diff
changeset
|
427 case 'l': |
330
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
428 if (*++fmt == 'l') |
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
429 { |
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
430 f_flags |= TH_PF_LONGLONG; |
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
431 fmt++; |
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
432 } |
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
433 else |
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
434 f_flags |= TH_PF_LONG; |
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
435 break; |
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
436 |
287
e214637b0645
Add check for (currently unsupported) length modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
286
diff
changeset
|
437 case 'L': |
330
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
438 fmt++; |
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
439 f_flags |= TH_PF_LONGLONG; |
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
440 break; |
b2c4b0b4d44f
Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
329
diff
changeset
|
441 |
287
e214637b0645
Add check for (currently unsupported) length modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
286
diff
changeset
|
442 case 'h': |
e214637b0645
Add check for (currently unsupported) length modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
286
diff
changeset
|
443 case 'j': |
e214637b0645
Add check for (currently unsupported) length modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
286
diff
changeset
|
444 case 'z': |
e214637b0645
Add check for (currently unsupported) length modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
286
diff
changeset
|
445 case 't': |
e214637b0645
Add check for (currently unsupported) length modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
286
diff
changeset
|
446 return -202; |
e214637b0645
Add check for (currently unsupported) length modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
286
diff
changeset
|
447 } |
240
5e781dba6136
Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
448 |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
449 switch (*fmt) |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
450 { |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
451 case 0: |
247
2fc282c365a8
Use special error codes for syntax errors in format string.
Matti Hamalainen <ccr@tnsp.org>
parents:
246
diff
changeset
|
452 return -104; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
453 |
249 | 454 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
|
455 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
|
456 goto out; |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
457 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
|
458 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
|
459 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
|
460 goto out; |
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
461 break; |
250 | 462 |
299
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
463 case 'o': |
369
f26290f8d35e
Rename some internal functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
368
diff
changeset
|
464 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
|
465 goto out; |
249 | 466 break; |
467 | |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
468 case 'u': |
299
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
469 case 'i': |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
470 case 'd': |
369
f26290f8d35e
Rename some internal functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
368
diff
changeset
|
471 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
|
472 goto out; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
473 break; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
474 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
475 case 'x': |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
476 case 'X': |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
477 if (*fmt == 'X') |
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
478 f_flags |= TH_PF_UPCASE; |
369
f26290f8d35e
Rename some internal functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
368
diff
changeset
|
479 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
|
480 goto out; |
299
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
481 break; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
482 |
299
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
483 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
|
484 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
|
485 return -120; |
bc5567f5e305
Check %p for ll and L modifiers and error out on them.
Matti Hamalainen <ccr@tnsp.org>
parents:
370
diff
changeset
|
486 |
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
299
diff
changeset
|
487 #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
|
488 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
|
489 #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
|
490 f_flags |= TH_PF_LONGLONG; |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
335
diff
changeset
|
491 #endif |
370
f6b9991d76ed
Some work on pointer formatter %p.
Matti Hamalainen <ccr@tnsp.org>
parents:
369
diff
changeset
|
492 f_flags |= TH_PF_ALT | TH_PF_POINTER; |
369
f26290f8d35e
Rename some internal functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
368
diff
changeset
|
493 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
|
494 goto out; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
495 break; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
496 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
497 case 'f': |
299
0311f139fcf6
Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
498 case 'F': |
373
22fb23dc8c9b
Rename functions s/th_printf_vput/th_vprintf_put/g.
Matti Hamalainen <ccr@tnsp.org>
parents:
371
diff
changeset
|
499 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
|
500 f_flags, f_width, f_prec)) == EOF) |
c532088b4f05
Fix float tests (type was int) :D
Matti Hamalainen <ccr@tnsp.org>
parents:
314
diff
changeset
|
501 goto out; |
240
5e781dba6136
Some preparation for %f support .. if it ever happens.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
502 break; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
503 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
504 case 's': |
369
f26290f8d35e
Rename some internal functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
368
diff
changeset
|
505 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
|
506 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
|
507 goto out; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
508 break; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
509 |
278
a6088507317b
Comment out case '%', it's superfluous here.
Matti Hamalainen <ccr@tnsp.org>
parents:
277
diff
changeset
|
510 //case '%': |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
511 default: |
251
7b76108248e9
More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
512 if ((ret = vputch(ctx, *fmt)) == EOF) |
7b76108248e9
More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
513 goto out; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
514 break; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
515 } |
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 fmt++; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
518 } |
243 | 519 |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
520 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
|
521 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
|
522 } |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
523 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
524 |
239
10f596441e75
Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
525 #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
|
526 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
|
527 { |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
528 if (ctx->pos < ctx->size) |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
529 ctx->buf[ctx->pos] = ch; |
283 | 530 |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
531 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
|
532 ctx->ipos++; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
533 return ch; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
534 } |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
535 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
536 |
374
1d8ae82304ec
Rename s/th_printf_ctx/th_vprintf_cts/g.
Matti Hamalainen <ccr@tnsp.org>
parents:
373
diff
changeset
|
537 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
|
538 { |
251
7b76108248e9
More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
539 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
|
540 ctx->ipos++; |
251
7b76108248e9
More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
541 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
|
542 } |
10f596441e75
Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
543 #endif |
10f596441e75
Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
544 |
10f596441e75
Silence unused function warning when not using internal implementations.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
545 |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
546 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
|
547 { |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
548 #ifdef TH_USE_INTERNAL_SPRINTF |
252 | 549 int ret; |
374
1d8ae82304ec
Rename s/th_printf_ctx/th_vprintf_cts/g.
Matti Hamalainen <ccr@tnsp.org>
parents:
373
diff
changeset
|
550 th_vprintf_ctx ctx; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
551 ctx.buf = buf; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
552 ctx.size = size; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
553 ctx.pos = 0; |
281
0c70dcfb6796
Remember to initialize th_printf_ctx.ipos to 0!
Matti Hamalainen <ccr@tnsp.org>
parents:
280
diff
changeset
|
554 ctx.ipos = 0; |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
555 |
252 | 556 ret = th_vprintf_do(&ctx, th_pbuf_vputch, fmt, ap); |
557 | |
558 if (ctx.pos < size) | |
559 buf[ctx.pos] = 0; | |
560 else | |
561 if (size > 0) | |
562 buf[size - 1] = 0; | |
563 | |
564 return ret; | |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
565 #else |
237
c55ebc438243
Oops, s/vsnpintf/vsnprintf/.
Matti Hamalainen <ccr@tnsp.org>
parents:
236
diff
changeset
|
566 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
|
567 #endif |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
568 } |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
569 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
570 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
571 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
|
572 { |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
573 int n; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
574 va_list ap; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
575 va_start(ap, fmt); |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
576 #ifdef TH_USE_INTERNAL_SPRINTF |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
577 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
|
578 #else |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
579 n = vsnprintf(buf, size, fmt, ap); |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
580 #endif |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
581 va_end(ap); |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
582 return n; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
583 } |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
584 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
585 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
586 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
|
587 { |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
588 #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
|
589 th_vprintf_ctx ctx; |
251
7b76108248e9
More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
590 ctx.data = (void *) fh; |
7b76108248e9
More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
591 ctx.pos = 0; |
281
0c70dcfb6796
Remember to initialize th_printf_ctx.ipos to 0!
Matti Hamalainen <ccr@tnsp.org>
parents:
280
diff
changeset
|
592 ctx.ipos = 0; |
251
7b76108248e9
More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
593 |
252 | 594 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
|
595 #else |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
596 return vfprintf(fh, fmt, ap); |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
597 #endif |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
598 } |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
599 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
600 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
601 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
|
602 { |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
603 int ret; |
251
7b76108248e9
More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
604 #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
|
605 th_vprintf_ctx ctx; |
251
7b76108248e9
More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
606 #endif |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
607 va_list ap; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
608 va_start(ap, fmt); |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
609 #ifdef TH_USE_INTERNAL_SPRINTF |
251
7b76108248e9
More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
610 ctx.data = (void *) fh; |
7b76108248e9
More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
611 ctx.pos = 0; |
281
0c70dcfb6796
Remember to initialize th_printf_ctx.ipos to 0!
Matti Hamalainen <ccr@tnsp.org>
parents:
280
diff
changeset
|
612 ctx.ipos = 0; |
0c70dcfb6796
Remember to initialize th_printf_ctx.ipos to 0!
Matti Hamalainen <ccr@tnsp.org>
parents:
280
diff
changeset
|
613 |
252 | 614 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
|
615 #else |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
616 ret = fprintf(fh, fmt, ap); |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
617 #endif |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
618 va_end(ap); |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
619 return ret; |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
620 } |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
621 |
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
622 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
623 /* Simulate a sprintf() that allocates memory |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
624 */ |
43 | 625 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
|
626 { |
133 | 627 int size = 64; |
628 char *buf, *tmp; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
629 |
260 | 630 if (fmt == NULL) |
631 return NULL; | |
632 | |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
633 if ((buf = th_malloc(size)) == NULL) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
634 return NULL; |
44 | 635 |
39 | 636 while (1) |
637 { | |
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
|
638 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
|
639 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
|
640 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
|
641 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
|
642 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
|
643 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
644 if (n > -1 && n < size) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
645 return buf; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
646 if (n > -1) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
647 size = n + 1; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
648 else |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
649 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
|
650 |
133 | 651 if ((tmp = th_realloc(buf, size)) == NULL) |
652 { | |
653 th_free(buf); | |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
654 return NULL; |
133 | 655 } |
656 else | |
657 buf = tmp; | |
10 | 658 } |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
659 } |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
660 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
661 |
43 | 662 char *th_strdup_printf(const char *fmt, ...) |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
663 { |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
664 char *res; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
665 va_list ap; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
666 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
667 va_start(ap, fmt); |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
668 res = th_strdup_vprintf(fmt, ap); |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
669 va_end(ap); |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
670 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
671 return res; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
672 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
673 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
674 |
30
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
675 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
|
676 { |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
677 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
|
678 th_free(*buf); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
679 *buf = tmp; |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
680 } |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
681 |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
682 |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
683 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
|
684 { |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
685 char *tmp; |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
686 va_list ap; |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
687 |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
688 va_start(ap, fmt); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
689 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
|
690 va_end(ap); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
691 |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
692 th_free(*buf); |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
693 *buf = tmp; |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
694 } |
10d2dc143e4b
Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
695 |
88 | 696 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
697 /* 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
|
698 */ |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
699 int th_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
|
700 { |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
701 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
|
702 assert(haystack != NULL); |
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
703 assert(needle != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
704 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
705 if (haystack == needle) |
10 | 706 return 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
707 |
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
|
708 while (*s1 && *s2) |
39 | 709 { |
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
|
710 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
|
711 if (k != 0) |
283 | 712 return k; |
10 | 713 s1++; |
714 s2++; | |
715 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
716 |
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
|
717 return 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
718 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
719 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
720 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
721 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
|
722 { |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
723 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
|
724 assert(haystack != NULL); |
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
725 assert(needle != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
726 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
727 if (haystack == needle) |
10 | 728 return 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
729 |
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
|
730 while (n > 0 && *s1 && *s2) |
39 | 731 { |
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
|
732 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
|
733 if (k != 0) |
283 | 734 return k; |
10 | 735 s1++; |
736 s2++; | |
737 n--; | |
738 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
739 |
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
|
740 return 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
741 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
742 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
743 |
89
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
744 /* 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
|
745 * case-insensitively, return pointer to start of the match, |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
746 * if found, NULL otherwise. |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
747 */ |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
748 char *th_strrcasecmp(char *str, const char *needle) |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
749 { |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
750 if (str == NULL || needle == NULL) |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
751 return NULL; |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
752 |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
753 const size_t |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
754 slen = strlen(str), |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
755 nlen = strlen(needle); |
228
ca9cd98dbcff
Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
756 |
89
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
757 if (slen < nlen) |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
758 return NULL; |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
759 |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
760 if (th_strcasecmp(str - nlen - 1, needle) == 0) |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
761 return str - nlen - 1; |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
762 else |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
763 return NULL; |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
764 } |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
765 |
30690f5c4cae
Add new function th_strrcasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
766 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
767 /* 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
|
768 * 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
|
769 */ |
43 | 770 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
|
771 { |
10 | 772 char *i, *j; |
773 assert(str != NULL); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
774 |
10 | 775 i = str; |
776 j = str; | |
39 | 777 while (*i) |
778 { | |
10 | 779 if (!th_iscntrl(*i)) |
780 *(j++) = *i; | |
781 i++; | |
782 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
783 |
10 | 784 *j = 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
785 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
786 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
787 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
788 /* 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
|
789 */ |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
790 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
|
791 { |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
792 assert(pdst != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
793 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
794 if (src == NULL) |
10 | 795 return -1; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
796 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
797 th_free(*pdst); |
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
798 if ((*pdst = th_malloc(strlen(src) + 1)) == NULL) |
10 | 799 return -2; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
800 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
801 strcpy(*pdst, src); |
10 | 802 return 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
803 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
804 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
805 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
806 /* 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
|
807 */ |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
808 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
|
809 { |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
810 assert(pdst != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
811 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
812 if (src == NULL) |
10 | 813 return -1; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
814 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
815 if (*pdst != NULL) |
39 | 816 { |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
817 *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
|
818 if (*pdst == NULL) |
10 | 819 return -1; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
820 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
821 strcat(*pdst, src); |
39 | 822 } |
823 else | |
824 { | |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
825 *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
|
826 if (*pdst == NULL) |
10 | 827 return -1; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
828 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
829 strcpy(*pdst, src); |
10 | 830 } |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
831 |
10 | 832 return 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
833 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
834 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
835 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
836 /* 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
|
837 * 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
|
838 * returns pointer to the string. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
839 */ |
43 | 840 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
|
841 { |
10 | 842 assert(str != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
843 |
129 | 844 // Terminating NULL-character is not whitespace! |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
845 while (th_isspace(str[*pos])) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
846 (*pos)++; |
39 | 847 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
848 return &str[*pos]; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
849 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
850 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
851 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
852 /* 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
|
853 */ |
43 | 854 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
|
855 { |
10 | 856 assert(str != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
857 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
858 while (str[*pos] && str[*pos] != sep) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
859 (*pos)++; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
860 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
861 return &str[*pos]; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
862 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
863 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
864 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
865 /* 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
|
866 */ |
43 | 867 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
|
868 { |
10 | 869 assert(str != NULL); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
870 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
871 while (!th_isspace(str[*pos]) && str[*pos] != sep) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
872 (*pos)++; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
873 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
874 return &str[*pos]; |
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 /* 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
|
879 * 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
|
880 * 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
|
881 * any number of characters. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
882 */ |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
883 BOOL th_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
|
884 { |
175 | 885 BOOL matched = TRUE, any = FALSE, end = FALSE; |
886 const char *tmp = NULL; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
887 |
129 | 888 // Check given pattern and string |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
889 if (haystack == NULL || pattern == NULL) |
10 | 890 return FALSE; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
891 |
129 | 892 // Start comparision |
10 | 893 do { |
175 | 894 matched = FALSE; |
39 | 895 switch (*pattern) |
896 { | |
10 | 897 case '?': |
129 | 898 // 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
|
899 if (*haystack) |
39 | 900 { |
175 | 901 matched = TRUE; |
10 | 902 pattern++; |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
903 haystack++; |
10 | 904 } |
905 break; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
906 |
10 | 907 case '*': |
175 | 908 matched = TRUE; |
10 | 909 pattern++; |
910 if (!*pattern) | |
175 | 911 end = TRUE; |
912 any = TRUE; | |
913 tmp = pattern; | |
10 | 914 break; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
915 |
10 | 916 case 0: |
175 | 917 if (any) |
39 | 918 { |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
919 if (*haystack) |
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
920 haystack++; |
10 | 921 else |
175 | 922 end = TRUE; |
39 | 923 } |
924 else | |
925 { | |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
926 if (*haystack) |
39 | 927 { |
175 | 928 if (tmp) |
39 | 929 { |
175 | 930 any = TRUE; |
931 pattern = tmp; | |
39 | 932 } |
933 else | |
175 | 934 matched = FALSE; |
44 | 935 } |
936 else | |
175 | 937 end = TRUE; |
10 | 938 } |
939 break; | |
175 | 940 |
10 | 941 default: |
175 | 942 if (any) |
39 | 943 { |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
944 if (*pattern == *haystack) |
39 | 945 { |
175 | 946 any = FALSE; |
947 matched = TRUE; | |
39 | 948 } |
949 else | |
950 { | |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
951 if (*haystack) |
39 | 952 { |
175 | 953 matched = TRUE; |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
954 haystack++; |
10 | 955 } |
956 } | |
39 | 957 } |
958 else | |
959 { | |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
960 if (*pattern == *haystack) |
39 | 961 { |
175 | 962 matched = TRUE; |
10 | 963 if (*pattern) |
964 pattern++; | |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
965 if (*haystack) |
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
966 haystack++; |
39 | 967 } |
968 else | |
969 { | |
175 | 970 if (tmp) |
39 | 971 { |
175 | 972 matched = TRUE; |
973 any = TRUE; | |
974 pattern = tmp; | |
10 | 975 } |
976 } | |
977 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
978 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
979 if (!*haystack && !*pattern) |
175 | 980 end = TRUE; |
10 | 981 break; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
982 |
129 | 983 } // switch |
175 | 984 } while (matched && !end); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
985 |
175 | 986 return matched; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
987 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
988 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
989 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
990 /* 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
|
991 */ |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
992 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
|
993 { |
175 | 994 BOOL matched = TRUE, any = FALSE, end = FALSE; |
995 const char *tmp = NULL; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
996 |
129 | 997 // 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
|
998 if (haystack == NULL || pattern == NULL) |
10 | 999 return FALSE; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1000 |
129 | 1001 // Start comparision |
10 | 1002 do { |
1003 switch (*pattern) { | |
1004 case '?': | |
129 | 1005 // 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
|
1006 if (*haystack) |
39 | 1007 { |
10 | 1008 pattern++; |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
1009 haystack++; |
44 | 1010 } |
1011 else | |
175 | 1012 matched = FALSE; |
10 | 1013 break; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1014 |
10 | 1015 case '*': |
1016 pattern++; | |
1017 if (!*pattern || *pattern == '?') | |
175 | 1018 end = TRUE; |
1019 any = TRUE; | |
1020 tmp = pattern; | |
10 | 1021 break; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1022 |
10 | 1023 case 0: |
175 | 1024 if (any) |
39 | 1025 { |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
1026 if (*haystack) |
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
1027 haystack++; |
10 | 1028 else |
175 | 1029 end = TRUE; |
39 | 1030 } |
1031 else | |
1032 { | |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
1033 if (*haystack) |
39 | 1034 { |
175 | 1035 if (tmp) |
39 | 1036 { |
175 | 1037 any = TRUE; |
1038 pattern = tmp; | |
39 | 1039 } |
1040 else | |
175 | 1041 matched = FALSE; |
44 | 1042 } |
1043 else | |
175 | 1044 end = TRUE; |
10 | 1045 } |
1046 break; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1047 |
10 | 1048 default: |
175 | 1049 if (any) |
39 | 1050 { |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
1051 if (th_tolower(*pattern) == th_tolower(*haystack)) |
39 | 1052 { |
175 | 1053 any = FALSE; |
39 | 1054 } |
1055 else | |
1056 { | |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
1057 if (*haystack) |
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
1058 haystack++; |
10 | 1059 else |
175 | 1060 matched = FALSE; |
10 | 1061 } |
39 | 1062 } |
1063 else | |
1064 { | |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
1065 if (th_tolower(*pattern) == th_tolower(*haystack)) |
39 | 1066 { |
10 | 1067 if (*pattern) |
1068 pattern++; | |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
1069 if (*haystack) |
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
1070 haystack++; |
39 | 1071 } |
1072 else | |
1073 { | |
175 | 1074 if (tmp) |
39 | 1075 { |
175 | 1076 any = TRUE; |
1077 pattern = tmp; | |
39 | 1078 } |
1079 else | |
175 | 1080 matched = FALSE; |
10 | 1081 } |
1082 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1083 |
186
1ee46a95aa8c
Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
1084 if (!*haystack && !*pattern) |
175 | 1085 end = TRUE; |
39 | 1086 |
10 | 1087 break; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1088 |
129 | 1089 } // switch |
175 | 1090 } while (matched && !end); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1091 |
175 | 1092 return matched; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1093 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1094 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
1095 |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
1096 int th_get_hex_triplet(const char *str) |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
1097 { |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
1098 const char *p = str; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
1099 int len, val = 0; |
44 | 1100 |
39 | 1101 for (len = 0; *p && len < 6; p++, len++) |
1102 { | |
1103 if (*p >= '0' && *p <= '9') | |
1104 { | |
1105 val *= 16; | |
1106 val += (*p - '0'); | |
1107 } | |
174 | 1108 else |
1109 if (*p >= 'A' && *p <= 'F') | |
39 | 1110 { |
1111 val *= 16; | |
1112 val += (*p - 'A') + 10; | |
1113 } | |
174 | 1114 else |
1115 if (*p >= 'a' && *p <= 'f') | |
39 | 1116 { |
1117 val *= 16; | |
1118 val += (*p - 'a') + 10; | |
1119 } | |
1120 else | |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
1121 return -1; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
1122 } |
44 | 1123 |
14
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
1124 return (len == 6) ? val : -1; |
e5e6370217ef
Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
1125 } |
162
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1126 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1127 |
167
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1128 BOOL th_get_boolean(const char *str, BOOL *value) |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1129 { |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1130 if (!th_strcasecmp(str, "yes") || |
169
a06a87b771ce
Add on/off to valid boolean values.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
1131 !th_strcasecmp(str, "on") || |
167
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1132 !th_strcasecmp(str, "true") || |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1133 !th_strcasecmp(str, "1")) |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1134 { |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1135 *value = TRUE; |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1136 return TRUE; |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1137 } |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1138 else |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1139 if (!th_strcasecmp(str, "no") || |
169
a06a87b771ce
Add on/off to valid boolean values.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
1140 !th_strcasecmp(str, "off") || |
167
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1141 !th_strcasecmp(str, "false") || |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1142 !th_strcasecmp(str, "0")) |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1143 { |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1144 *value = FALSE; |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1145 return TRUE; |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1146 } |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1147 else |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1148 return FALSE; |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1149 } |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1150 |
7638fa9d191f
Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
1151 |
162
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1152 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
|
1153 { |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1154 while (count--) |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1155 fputc(' ', outFile); |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1156 } |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1157 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1158 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1159 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
|
1160 { |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1161 size_t pos = 0; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1162 BOOL first = TRUE; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1163 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1164 while (str[pos]) |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1165 { |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1166 // Pre-pad line |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1167 int linelen = first ? spad : rpad; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1168 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
|
1169 first = FALSE; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1170 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1171 // Skip whitespace at line start |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1172 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
|
1173 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1174 // Handle each word |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1175 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
|
1176 { |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1177 size_t next; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1178 int wlen; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1179 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1180 // 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
|
1181 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
|
1182 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1183 // 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
|
1184 if (linelen + wlen >= width) |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1185 break; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1186 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1187 // Print what we have |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1188 for (;pos < next; pos++, linelen++) |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1189 fputc(str[pos], fh); |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1190 |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1191 // 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
|
1192 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
|
1193 break; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1194 else |
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 fputc(str[pos], fh); |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1197 pos++; |
578d9298cc1e
Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
1198 linelen++; |
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 fprintf(fh, "\n"); |
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 } |