annotate th_string.h @ 374:1d8ae82304ec

Rename s/th_printf_ctx/th_vprintf_cts/g.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 29 Feb 2016 16:12:10 +0200
parents 22fb23dc8c9b
children 1b3472ba7b23
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Miscellaneous string-handling related utility-functions
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
231
d480cee946de Update copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 228
diff changeset
4 * (C) Copyright 2002-2016 Tecnic Software productions (TNSP)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
8 #ifndef TH_STRING_H
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
9 #define TH_STRING_H
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include "th_util.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include <stdlib.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #include <ctype.h>
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
14 #include <stdarg.h>
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15
91
eca8ffe4e260 Move C++ guard.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
16 #ifdef __cplusplus
eca8ffe4e260 Move C++ guard.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
17 extern "C" {
eca8ffe4e260 Move C++ guard.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
18 #endif
eca8ffe4e260 Move C++ guard.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
19
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 /* Macros
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 */
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
22 #define th_isalnum(c) isalnum((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
23 #define th_isalpha(c) isalpha((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
24 #define th_isascii(c) isascii((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
25 #define th_isblank(c) isblank((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
26 #define th_iscntrl(c) iscntrl((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
27 #define th_isdigit(c) isdigit((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
28 #define th_isgraph(c) isgraph((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
29 #define th_islower(c) islower((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
30 #define th_isprint(c) isprint((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
31 #define th_ispunct(c) ispunct((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
32 #define th_isspace(c) isspace((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
33 #define th_isupper(c) isupper((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
34 #define th_isxdigit(c) isxdigit((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
35 #define th_iscrlf(c) ((c=='\r')||(c=='\n'))
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
37 #define th_isspecial(q) (((q >= 0x5b) && (q <= 0x60)) || ((q >= 0x7b) && (q <= 0x7d)))
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
39 #define th_tolower(c) tolower((int)(unsigned char) c)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
40 #define th_toupper(c) toupper((int)(unsigned char) c)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
43 enum
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
44 {
372
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
45 TH_TRIM_START = 1,
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
46 TH_TRIM_END = 2,
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
47 TH_TRIM_BOTH = 3
90
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
48 };
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
49
2b1f7f1ca8e4 Add new function th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
50
299
0311f139fcf6 Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents: 279
diff changeset
51 enum
0311f139fcf6 Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents: 279
diff changeset
52 {
372
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
53 TH_PF_NONE = 0x0000,
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
54 TH_PF_ALT = 0x0001,
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
55 TH_PF_SIGN = 0x0002,
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
56 TH_PF_SPACE = 0x0004,
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
57 TH_PF_GROUP = 0x0008,
299
0311f139fcf6 Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents: 279
diff changeset
58
372
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
59 TH_PF_ZERO = 0x0100,
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
60 TH_PF_LEFT = 0x0200,
330
b2c4b0b4d44f Add initial support for ll and L printf modifiers.
Matti Hamalainen <ccr@tnsp.org>
parents: 309
diff changeset
61
372
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
62 TH_PF_LONG = 0x1000,
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
63 TH_PF_LONGLONG = 0x2000,
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
64 TH_PF_POINTER = 0x4000,
3d32a0c72dc7 Indentation cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
65 TH_PF_UPCASE = 0x8000,
299
0311f139fcf6 Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents: 279
diff changeset
66 };
0311f139fcf6 Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents: 279
diff changeset
67
0311f139fcf6 Refactor how various printf modifier flags etc. are handled. Still needs
Matti Hamalainen <ccr@tnsp.org>
parents: 279
diff changeset
68
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 232
diff changeset
69 typedef struct
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 232
diff changeset
70 {
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 232
diff changeset
71 char *buf;
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 232
diff changeset
72 size_t size, 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: 277
diff changeset
73 int ipos;
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 232
diff changeset
74 void *data;
374
1d8ae82304ec Rename s/th_printf_ctx/th_vprintf_cts/g.
Matti Hamalainen <ccr@tnsp.org>
parents: 373
diff changeset
75 } th_vprintf_ctx;
251
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 232
diff changeset
76
7b76108248e9 More work on printing functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 232
diff changeset
77
374
1d8ae82304ec Rename s/th_printf_ctx/th_vprintf_cts/g.
Matti Hamalainen <ccr@tnsp.org>
parents: 373
diff changeset
78 typedef int (*th_vprintf_putch)(th_vprintf_ctx *ctx, const char ch);
277
7450d744e633 Rename internal functions and add a typedef for function pointer used for vputch().
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
79
7450d744e633 Rename internal functions and add a typedef for function pointer used for vputch().
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
80
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 /* Normal NUL-terminated string functions
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 */
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
83 char *th_strdup(const char *src);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
84 char *th_strndup(const char *src, const size_t n);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
85 char *th_strdup_trim(const char *, const int flags);
220
099a1a156fd7 Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
86 char *th_strndup_trim(const char *, const size_t n, const int flags);
81
c9c0541bed90 Add th_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
87
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
88 int th_strcasecmp(const char *haystack, const char *needle);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
89 int th_strncasecmp(const char *haystack, const char *needle, size_t n);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
90 char *th_strrcasecmp(char *haystack, const char *needle);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
91 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
92
374
1d8ae82304ec Rename s/th_printf_ctx/th_vprintf_cts/g.
Matti Hamalainen <ccr@tnsp.org>
parents: 373
diff changeset
93 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: 220
diff changeset
94 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: 220
diff changeset
95 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: 220
diff changeset
96 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: 220
diff changeset
97 int th_fprintf(FILE *fh, const char *fmt, ...);
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 220
diff changeset
98
ca9cd98dbcff Initial work on printf() style function family implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 220
diff changeset
99
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
100 char *th_strdup_vprintf(const char *fmt, va_list ap);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
101 char *th_strdup_printf(const char *fmt, ...);
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
102
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
103 void th_pstr_vprintf(char **buf, const char *fmt, va_list ap);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
104 void th_pstr_printf(char **buf, const char *fmt, ...);
30
10d2dc143e4b Add helper functions th_pstr_vprintf() and th_pstr_printf().
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
105
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
106 int th_pstrcpy(char **pdst, const char *src);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
107 int th_pstrcat(char **pdst, const char *src);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
108
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109
78
2579250f2a84 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
110 /* Parsing, matching
2579250f2a84 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
111 */
14
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
112 const char *th_findnext(const char *, size_t *);
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
113 const char *th_findsep(const char *, size_t *, char);
e5e6370217ef Various cleanups, additions and removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
114 const char *th_findseporspace(const char *, size_t *, char);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
116 BOOL th_strmatch(const char *haystack, const char *pattern);
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
117 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
118
186
1ee46a95aa8c Clean up the code, add argument identifiers to function prototypes and
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
119 int th_get_hex_triplet(const char *str);
167
7638fa9d191f Add th_get_boolean() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
120 BOOL th_get_boolean(const char *str, BOOL *value);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121
162
578d9298cc1e Actually, move th_print_wrap() to th_string module.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
122 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
123
21
1f7d8693fda8 Add functions for growing a string/buffer and concatenating characters and/or other strings to it.
Matti Hamalainen <ccr@tnsp.org>
parents: 14
diff changeset
124
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 #ifdef __cplusplus
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 #endif
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 107
diff changeset
128 #endif // TH_STRING_H