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