comparison th_string.c @ 616:594f197f7005

Slight improvements to the almost non-existing Doxygen documentation.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 17 Jan 2020 03:18:40 +0200
parents 5ec903f366b5
children d3aace9903fa
comparison
equal deleted inserted replaced
615:b87395754c8d 616:594f197f7005
10 10
11 // Include printf implementation 11 // Include printf implementation
12 #include "th_printf.c" 12 #include "th_printf.c"
13 13
14 14
15 /* Implementation of strdup() with a NULL check 15 /**
16 * Implementation of strdup() with a @c NULL check.
17 * @param[in] src string to create a copy of
18 * @returns copy of the given string, or @c NULL if @p src was @c NULL or memory could not be allocated.
16 */ 19 */
17 char *th_strdup(const char *src) 20 char *th_strdup(const char *src)
18 { 21 {
19 char *res; 22 char *res;
20 size_t len; 23 size_t len;
29 memcpy(res, src, len + 1); 32 memcpy(res, src, len + 1);
30 return res; 33 return res;
31 } 34 }
32 35
33 36
34 /* Implementation of strndup() with NULL check 37 /**
38 * Implementation of strndup() with @c NULL check. Copies up to @p n
39 * characters from the source. A NUL terminator is always added,
40 * unless @p src was @c NULL or memory could not be allocated, in which
41 * case @c NULL pointer is returned.
42 * @param[in] src string pointer to be copied from
43 * @param[in] n maximum number of characters to copy
44 * @returns the resulting string or @c NULL pointer
35 */ 45 */
36 char *th_strndup(const char *src, const size_t n) 46 char *th_strndup(const char *src, const size_t n)
37 { 47 {
38 char *res; 48 char *res;
39 size_t len; 49 size_t len;
53 63
54 return res; 64 return res;
55 } 65 }
56 66
57 67
58 /* Like strdup, but trims whitespace from the string according to specified flags. 68 /**
59 * See TH_TRIM_* in th_string.h. If the resulting string would be empty (length 0), 69 * Helper function for th_strdup_trim() and friends. Copies @p len characters from
60 * NULL is returned. 70 * given string, and trims whitespace from it according to specified @p flags.
71 * See TH_TRIM_* in th_string.h. If @p len or the resulting trimmed string would
72 * be empty (length 0), no copy is allocated and a @c NULL pointer is returned.
73 * @param[in] src source string (does not need to be NUL terminated, as length must be specified)
74 * @param[in] len length of the source string, or desired number of characters to copy at maximum
75 * @param[in] flags trimming flags, see TH_TRIM_* in th_string.h
76 * @returns the resulting string or @c NULL pointer
61 */ 77 */
62 static char * th_strdup_trim_do(const char *src, size_t len, const int flags) 78 static char * th_strdup_trim_do(const char *src, size_t len, const int flags)
63 { 79 {
64 char *res; 80 char *res;
65 size_t start, end; 81 size_t start, end;
91 res[len] = 0; 107 res[len] = 0;
92 return res; 108 return res;
93 } 109 }
94 110
95 111
112 /**
113 * Create copy of the given string, but trim the result according to specified @p flags.
114 * See TH_TRIM_* in th_string.h. If length the resulting trimmed string would
115 * be empty (0), no copy is allocated and a NULL pointer is returned.
116 * @param[in] src source string
117 * @param[in] flags trimming flags, see TH_TRIM_* in th_string.h
118 * @returns the resulting string or @c NULL pointer
119 */
96 char *th_strdup_trim(const char *src, const int flags) 120 char *th_strdup_trim(const char *src, const int flags)
97 { 121 {
98 if (src == NULL) 122 if (src == NULL)
99 return NULL; 123 return NULL;
100 124
101 return th_strdup_trim_do(src, strlen(src), flags); 125 return th_strdup_trim_do(src, strlen(src), flags);
102 } 126 }
103 127
104 128
129 /**
130 * Create copy of the given string @p src, up to @p n characters (or less, if the string
131 * is shorter.) The result is trimmed according to specified @p flags.
132 * See TH_TRIM_* in th_string.h. If @p n or the resulting trimmed string would
133 * be empty (length 0), no copy is allocated and a NULL pointer is returned.
134 * @param[in] src source string
135 * @param[in] n maximum number of characters to copy from the source string
136 * @param[in] flags trimming flags, see TH_TRIM_* in th_string.h
137 * @returns the resulting string or @c NULL pointer
138 */
105 char *th_strndup_trim(const char *src, const size_t n, const int flags) 139 char *th_strndup_trim(const char *src, const size_t n, const int flags)
106 { 140 {
107 size_t len; 141 size_t len;
108 if (src == NULL || n == 0) 142 if (src == NULL || n == 0)
109 return NULL; 143 return NULL;
145 return ch; 179 return ch;
146 } 180 }
147 #endif 181 #endif
148 182
149 183
184 /**
185 * Wrapper for either libc vsnprintf() or th-libs internal
186 * vsnprintf() implementation, as determined by a compile-time define.
187 * @param[out] buf pointer to buffer to print to
188 * @param[in] size available space in the buffer in bytes
189 * @param[in] fmt format string
190 * @param[in] ap a va_list variable arguments list structure
191 */
150 int th_vsnprintf(char *buf, size_t size, const char *fmt, va_list ap) 192 int th_vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
151 { 193 {
152 #ifdef TH_USE_INTERNAL_SPRINTF 194 #ifdef TH_USE_INTERNAL_SPRINTF
153 int ret; 195 int ret;
154 th_vprintf_ctx ctx; 196 th_vprintf_ctx ctx;
170 return vsnprintf(buf, size, fmt, ap); 212 return vsnprintf(buf, size, fmt, ap);
171 #endif 213 #endif
172 } 214 }
173 215
174 216
217 /**
218 * Wrapper for either libc snprintf() or th-libs internal
219 * snprintf() implementation, as determined by a compile-time define.
220 * @param[out] buf pointer to buffer to print to
221 * @param[in] size available space in the buffer in bytes
222 * @param[in] fmt format string
223 * @param[in] ... variable arguments
224 */
175 int th_snprintf(char *buf, size_t size, const char *fmt, ...) 225 int th_snprintf(char *buf, size_t size, const char *fmt, ...)
176 { 226 {
177 int n; 227 int n;
178 va_list ap; 228 va_list ap;
179 va_start(ap, fmt); 229 va_start(ap, fmt);
251 return ch; 301 return ch;
252 } 302 }
253 #endif 303 #endif
254 304
255 305
306 /**
307 * Combination of vsprintf() and strdup. Automatically allocates memory
308 * for the resulting string.
309 * @param[in] fmt format string
310 * @param[in] args variable arguments list structure
311 * @returns the resulting string or @c NULL pointer in case of error
312 */
256 char *th_strdup_vprintf(const char *fmt, va_list args) 313 char *th_strdup_vprintf(const char *fmt, va_list args)
257 { 314 {
258 #ifdef TH_USE_INTERNAL_SPRINTF 315 #ifdef TH_USE_INTERNAL_SPRINTF
259 // Using internal printf() implementation 316 // Using internal printf() implementation
260 th_vprintf_ctx ctx; 317 th_vprintf_ctx ctx;
315 } 372 }
316 #endif 373 #endif
317 } 374 }
318 375
319 376
377 /**
378 * Combination of sprintf() and strdup. Automatically allocates memory
379 * for the resulting string.
380 * @param[in] fmt format string
381 * @param[in] ... optional printf arguments
382 * @returns the resulting string or @c NULL pointer in case of error
383 */
320 char *th_strdup_printf(const char *fmt, ...) 384 char *th_strdup_printf(const char *fmt, ...)
321 { 385 {
322 char *res; 386 char *res;
323 va_list ap; 387 va_list ap;
324 388
328 392
329 return res; 393 return res;
330 } 394 }
331 395
332 396
397 /**
398 * A helper function that is given a pointer to a pointer of string,
399 * which will be automatically freed (if necessary) and replaced with
400 * a pointer to the newly created string.
401 * @param[in,out] buf pointer to a char pointer/string
402 * @param[in] fmt format string
403 * @param[in] args variable arguments list structure
404 */
333 void th_pstr_vprintf(char **buf, const char *fmt, va_list ap) 405 void th_pstr_vprintf(char **buf, const char *fmt, va_list ap)
334 { 406 {
335 char *tmp = th_strdup_vprintf(fmt, ap); 407 char *tmp = th_strdup_vprintf(fmt, ap);
336 th_free(*buf); 408 th_free(*buf);
337 *buf = tmp; 409 *buf = tmp;
338 } 410 }
339 411
340 412
413 /**
414 * A helper function that is given a pointer to a pointer of string,
415 * which will be automatically freed (if necessary) and replaced with
416 * a pointer to the newly created string.
417 * @param[in,out] buf pointer to a char pointer/string
418 * @param[in] fmt format string
419 * @param[in] ... optional printf arguments
420 */
341 void th_pstr_printf(char **buf, const char *fmt, ...) 421 void th_pstr_printf(char **buf, const char *fmt, ...)
342 { 422 {
343 char *tmp; 423 char *tmp;
344 va_list ap; 424 va_list ap;
345 425