strpool.c revision 178825
157429Smarkm/* 260576Skris * Copyright (c) 2005 Kungliga Tekniska H�gskolan 357429Smarkm * (Royal Institute of Technology, Stockholm, Sweden). 460576Skris * All rights reserved. 557429Smarkm * 660576Skris * Redistribution and use in source and binary forms, with or without 757429Smarkm * modification, are permitted provided that the following conditions 857429Smarkm * are met: 960576Skris * 1057429Smarkm * 1. Redistributions of source code must retain the above copyright 1160576Skris * notice, this list of conditions and the following disclaimer. 1257429Smarkm * 1360576Skris * 2. Redistributions in binary form must reproduce the above copyright 1457432Smarkm * notice, this list of conditions and the following disclaimer in the 1557429Smarkm * documentation and/or other materials provided with the distribution. 1657429Smarkm * 1760576Skris * 3. Neither the name of the Institute nor the names of its contributors 1857429Smarkm * may be used to endorse or promote products derived from this software 1957429Smarkm * without specific prior written permission. 2057429Smarkm * 2157429Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 2257429Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2357429Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2457429Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 2557429Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2657429Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2757429Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2857429Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2957429Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3057429Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3157429Smarkm * SUCH DAMAGE. 3257429Smarkm */ 3357429Smarkm 3457429Smarkm#ifdef HAVE_CONFIG_H 3557429Smarkm#include <config.h> 3660576SkrisRCSID("$Id: strpool.c 21005 2007-06-08 01:54:35Z lha $"); 3760576Skris#endif 3857429Smarkm 3957429Smarkm#include <stdarg.h> 4057429Smarkm#include <stdlib.h> 4157429Smarkm#include "roken.h" 4257429Smarkm 4357429Smarkmstruct rk_strpool { 4457429Smarkm char *str; 4557429Smarkm size_t len; 4657429Smarkm}; 4757429Smarkm 4857429Smarkm/* 4957429Smarkm * 5057429Smarkm */ 5157429Smarkm 5257429Smarkmvoid ROKEN_LIB_FUNCTION 5360576Skrisrk_strpoolfree(struct rk_strpool *p) 5460576Skris{ 5560576Skris if (p->str) { 5657429Smarkm free(p->str); 5757429Smarkm p->str = NULL; 5857429Smarkm } 5957429Smarkm free(p); 6057429Smarkm} 6157429Smarkm 6257429Smarkm/* 6360576Skris * 6457429Smarkm */ 6557565Smarkm 6657429Smarkmstruct rk_strpool * ROKEN_LIB_FUNCTION 6757565Smarkmrk_strpoolprintf(struct rk_strpool *p, const char *fmt, ...) 6857429Smarkm{ 6957429Smarkm va_list ap; 7057429Smarkm char *str, *str2; 7157429Smarkm int len; 7257565Smarkm 7357429Smarkm if (p == NULL) { 7457429Smarkm p = malloc(sizeof(*p)); 7557565Smarkm if (p == NULL) 7657565Smarkm return NULL; 7757565Smarkm p->str = NULL; 7857565Smarkm p->len = 0; 7957565Smarkm } 8057429Smarkm va_start(ap, fmt); 8157565Smarkm len = vasprintf(&str, fmt, ap); 8257429Smarkm va_end(ap); 8357429Smarkm if (str == NULL) { 8457429Smarkm rk_strpoolfree(p); 8557429Smarkm return NULL; 8657429Smarkm } 8757429Smarkm str2 = realloc(p->str, len + p->len + 1); 8857429Smarkm if (str2 == NULL) { 8957429Smarkm rk_strpoolfree(p); 9057429Smarkm return NULL; 9157429Smarkm } 9257429Smarkm p->str = str2; 9357429Smarkm memcpy(p->str + p->len, str, len + 1); 9457429Smarkm p->len += len; 9557429Smarkm free(str); 9657429Smarkm return p; 9757429Smarkm} 9857429Smarkm 9957429Smarkm/* 10057429Smarkm * 10157429Smarkm */ 10257432Smarkm 10357432Smarkmchar * ROKEN_LIB_FUNCTION 10457432Smarkmrk_strpoolcollect(struct rk_strpool *p) 10557432Smarkm{ 10657432Smarkm char *str = p->str; 10757432Smarkm p->str = NULL; 10857429Smarkm free(p); 10957429Smarkm return str; 11057429Smarkm} 11157429Smarkm