1 /* 2 * Copyright (c) 1994 Christos Zoulas 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Christos Zoulas. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Counted strings 34 */ 35 #include <stdlib.h> 36 #include <string.h> 37 38 #include "util.h" 39 #include "str.h" 40 41 /* 42 * str_init(): Initialize string 43 */ 44 void 45 str_init(struct string *s) 46 { 47 s->s_str = NULL; 48 s->s_len = 0; 49 } 50 51 52 /* 53 * str_append(): Append string allocating buffer as necessary 54 */ 55 void 56 str_append(struct string *buf, const char *str, int del) 57 { 58 size_t len = strlen(str) + 1; 59 60 if (buf->s_str == NULL) 61 buf->s_str = emalloc(len); 62 else { 63 buf->s_str = erealloc(buf->s_str, buf->s_len + 64 len + (del ? 2 : 1)); 65 if (del) 66 buf->s_str[buf->s_len++] = del; 67 } 68 69 memcpy(&buf->s_str[buf->s_len], str, len); 70 buf->s_len += len - 1; 71 } 72 73 /* 74 * str_prepend(): Prepend string allocating buffer as necessary 75 */ 76 void 77 str_prepend(struct string *buf, const char *str, int del) 78 { 79 char *ptr, *sptr; 80 size_t len = strlen(str) + 1; 81 82 sptr = ptr = emalloc(buf->s_len + len + (del ? 2 : 1)); 83 84 if (del) 85 *ptr++ = del; 86 87 memcpy(ptr, str, len); 88 89 if (buf->s_str) { 90 memcpy(&ptr[len - 1], buf->s_str, buf->s_len + 1); 91 free(buf->s_str); 92 } 93 94 buf->s_str = sptr; 95 buf->s_len += del ? len : len - 1; 96 } 97 98 /* 99 * str_free(): Free a string 100 */ 101 void 102 str_free(struct string *s) 103 { 104 free(s->s_str); 105 s->s_str = NULL; 106 s->s_len = 0; 107 } 108