|
| 1 | +/* vim: set et ts=4 sts=4 sw=4 : */ |
| 2 | +/********************************************************************\ |
| 3 | + * This program is free software; you can redistribute it and/or * |
| 4 | + * modify it under the terms of the GNU General Public License as * |
| 5 | + * published by the Free Software Foundation; either version 2 of * |
| 6 | + * the License, or (at your option) any later version. * |
| 7 | + * * |
| 8 | + * This program is distributed in the hope that it will be useful, * |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 11 | + * GNU General Public License for more details. * |
| 12 | + * * |
| 13 | + * You should have received a copy of the GNU General Public License* |
| 14 | + * along with this program; if not, contact: * |
| 15 | + * * |
| 16 | + * Free Software Foundation Voice: +1-617-542-5942 * |
| 17 | + * 59 Temple Place - Suite 330 Fax: +1-617-542-2652 * |
| 18 | + * Boston, MA 02111-1307, USA [email protected] * |
| 19 | + * * |
| 20 | +\********************************************************************/ |
| 21 | + |
| 22 | +/** @file pstring.h |
| 23 | + @brief Simple pascal string like strings |
| 24 | + @author Copyright (C) 2015 Alexandre Carmel-Veilleux <[email protected]> |
| 25 | +*/ |
| 26 | + |
| 27 | +#include <string.h> |
| 28 | +#include <stdlib.h> |
| 29 | + |
| 30 | +#include "safe.h" |
| 31 | +#include "pstring.h" |
| 32 | +#include "common.h" |
| 33 | + |
| 34 | +static void _pstr_grow(pstr_t *); |
| 35 | + |
| 36 | +/** |
| 37 | + * Create a new pascal-string like pstr struct and allocate initial buffer. |
| 38 | + * @param None. |
| 39 | + * @return A pointer to an opaque pstr_t string, think java StringBuilder |
| 40 | + */ |
| 41 | +pstr_t * |
| 42 | +pstr_new(void) |
| 43 | +{ |
| 44 | + pstr_t *new; |
| 45 | + |
| 46 | + new = (pstr_t *)safe_malloc(sizeof(pstr_t)); |
| 47 | + new->len = 0; |
| 48 | + new->size = MAX_BUF; |
| 49 | + new->buf = (char *)safe_malloc(MAX_BUF); |
| 50 | + |
| 51 | + return new; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Convert the pstr_t pointer to a char * pointer, freeing the pstr_t in the |
| 56 | + * process. Note that the char * is the buffer of the pstr_t and must be freed |
| 57 | + * by the called. |
| 58 | + * @param pstr A pointer to a pstr_t struct. |
| 59 | + * @return A char * pointer |
| 60 | + */ |
| 61 | +char * |
| 62 | +pstr_to_string(pstr_t *pstr) |
| 63 | +{ |
| 64 | + char *ret = pstr->buf; |
| 65 | + free(pstr); |
| 66 | + return ret; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Grow a pstr_t's buffer by MAX_BUF chars in length. |
| 71 | + * Program terminates if realloc() fails. |
| 72 | + * @param pstr A pointer to a pstr_t struct. |
| 73 | + * @return void |
| 74 | + */ |
| 75 | +static void |
| 76 | +_pstr_grow(pstr_t *pstr) |
| 77 | +{ |
| 78 | + pstr->buf = (char *)safe_realloc((void *)pstr->buf, (pstr->size + MAX_BUF)); |
| 79 | + pstr->size += MAX_BUF; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Append a char string to pstr_t, allocating more memory if needed. |
| 84 | + * If allocation is needed but fails, program terminates. |
| 85 | + * @param pstr A pointer to a pstr_t struct. |
| 86 | + * @param string A pointer to char string to append. |
| 87 | + */ |
| 88 | +void |
| 89 | +pstr_cat(pstr_t *pstr, const char *string) |
| 90 | +{ |
| 91 | + size_t inlen = strlen(string); |
| 92 | + while ((pstr->len + inlen + 1) > pstr->size) { |
| 93 | + _pstr_grow(pstr); |
| 94 | + } |
| 95 | + strncat((pstr->buf + pstr->len), string, (pstr->size - pstr->len - 1)); |
| 96 | + pstr->len += inlen; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Append a printf-like formatted char string to a pstr_t string. |
| 101 | + * If allocation fails, program terminates. |
| 102 | + * @param pstr A pointer to a pstr_t struct. |
| 103 | + * @param fmt A char string specifying the format. |
| 104 | + * @param ... A va_arg list of argument, like for printf/sprintf. |
| 105 | + * @return int Number of bytes added. |
| 106 | + */ |
| 107 | +int |
| 108 | +pstr_append_sprintf(pstr_t *pstr, const char *fmt, ...) |
| 109 | +{ |
| 110 | + va_list ap; |
| 111 | + char *str; |
| 112 | + int retval; |
| 113 | + |
| 114 | + va_start(ap, fmt); |
| 115 | + retval = safe_vasprintf(&str, fmt, ap); |
| 116 | + va_end(ap); |
| 117 | + |
| 118 | + if (retval >= 0) { |
| 119 | + pstr_cat(pstr, str); |
| 120 | + free(str); |
| 121 | + } |
| 122 | + |
| 123 | + return retval; |
| 124 | +} |
0 commit comments