xref: /titanic_44/usr/src/lib/libcustr/common/libcustr.h (revision e01ec5d5f4fc831c481861bbc5813ed87c953ac4)
1*e01ec5d5SJason King /*
2*e01ec5d5SJason King  * This file and its contents are supplied under the terms of the
3*e01ec5d5SJason King  * Common Development and Distribution License ("CDDL"), version 1.0.
4*e01ec5d5SJason King  * You may only use this file in accordance with the terms of version
5*e01ec5d5SJason King  * 1.0 of the CDDL.
6*e01ec5d5SJason King  *
7*e01ec5d5SJason King  * A full copy of the text of the CDDL should have accompanied this
8*e01ec5d5SJason King  * source.  A copy of the CDDL is also available via the Internet at
9*e01ec5d5SJason King  * http://www.illumos.org/license/CDDL.
10*e01ec5d5SJason King  */
11*e01ec5d5SJason King 
12*e01ec5d5SJason King /*
13*e01ec5d5SJason King  * Copyright 2018, Joyent, Inc.
14*e01ec5d5SJason King  */
15*e01ec5d5SJason King 
16*e01ec5d5SJason King #ifndef _LIBCUSTR_H
17*e01ec5d5SJason King #define	_LIBCUSTR_H
18*e01ec5d5SJason King 
19*e01ec5d5SJason King #include <stdarg.h>
20*e01ec5d5SJason King #include <sys/types.h>
21*e01ec5d5SJason King 
22*e01ec5d5SJason King /* dynamic string utilities */
23*e01ec5d5SJason King 
24*e01ec5d5SJason King #ifdef __cplusplus
25*e01ec5d5SJason King extern "C" {
26*e01ec5d5SJason King #endif
27*e01ec5d5SJason King 
28*e01ec5d5SJason King typedef struct custr custr_t;
29*e01ec5d5SJason King 
30*e01ec5d5SJason King /*
31*e01ec5d5SJason King  * Allocate and free a "custr_t" dynamic string object.  Returns 0 on success
32*e01ec5d5SJason King  * and -1 otherwise.
33*e01ec5d5SJason King  */
34*e01ec5d5SJason King int custr_alloc(custr_t **);
35*e01ec5d5SJason King void custr_free(custr_t *);
36*e01ec5d5SJason King 
37*e01ec5d5SJason King /*
38*e01ec5d5SJason King  * Allocate a "custr_t" dynamic string object that operates on a fixed external
39*e01ec5d5SJason King  * buffer.
40*e01ec5d5SJason King  */
41*e01ec5d5SJason King int custr_alloc_buf(custr_t **, void *, size_t);
42*e01ec5d5SJason King 
43*e01ec5d5SJason King /*
44*e01ec5d5SJason King  * Append a single character, or a NUL-terminated string of characters, to a
45*e01ec5d5SJason King  * dynamic string.  Returns 0 on success and -1 otherwise.  The dynamic string
46*e01ec5d5SJason King  * will be unmodified if the function returns -1.
47*e01ec5d5SJason King  */
48*e01ec5d5SJason King int custr_appendc(custr_t *, char);
49*e01ec5d5SJason King int custr_append(custr_t *, const char *);
50*e01ec5d5SJason King 
51*e01ec5d5SJason King /*
52*e01ec5d5SJason King  * Append a format string and arguments as though the contents were being parsed
53*e01ec5d5SJason King  * through snprintf. Returns 0 on success and -1 otherwise.  The dynamic string
54*e01ec5d5SJason King  * will be unmodified if the function returns -1.
55*e01ec5d5SJason King  */
56*e01ec5d5SJason King int custr_append_printf(custr_t *, const char *, ...);
57*e01ec5d5SJason King int custr_append_vprintf(custr_t *, const char *, va_list);
58*e01ec5d5SJason King 
59*e01ec5d5SJason King /*
60*e01ec5d5SJason King  * Determine the length in bytes, not including the NUL terminator, of the
61*e01ec5d5SJason King  * dynamic string.
62*e01ec5d5SJason King  */
63*e01ec5d5SJason King size_t custr_len(custr_t *);
64*e01ec5d5SJason King 
65*e01ec5d5SJason King /*
66*e01ec5d5SJason King  * Clear the contents of a dynamic string.  Does not free the underlying
67*e01ec5d5SJason King  * memory.
68*e01ec5d5SJason King  */
69*e01ec5d5SJason King void custr_reset(custr_t *);
70*e01ec5d5SJason King 
71*e01ec5d5SJason King /*
72*e01ec5d5SJason King  * Retrieve a const pointer to a NUL-terminated string version of the contents
73*e01ec5d5SJason King  * of the dynamic string.  Storage for this string should not be freed, and
74*e01ec5d5SJason King  * the pointer will be invalidated by any mutations to the dynamic string.
75*e01ec5d5SJason King  */
76*e01ec5d5SJason King const char *custr_cstr(custr_t *str);
77*e01ec5d5SJason King 
78*e01ec5d5SJason King #ifdef __cplusplus
79*e01ec5d5SJason King }
80*e01ec5d5SJason King #endif
81*e01ec5d5SJason King 
82*e01ec5d5SJason King #endif /* _LIBCUSTR_H */
83