/*- * Copyright (c) 1999, 2000, 2001 Lev Walkin . * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: join.c,v 1.4 2005/05/27 05:34:46 vlm Exp $ */ #include #include #include #include #include #ifdef HAVE_CONFIG_H #include "config.h" #endif #ifdef HAVE_UNISTD_H #include #endif #include #include static char *_sf_sjoin_buf = NULL; char * sjoin(svect *s, const char *delimiter) { char *buf; int flen; int k, need; char *p; if(s == NULL || s->count == 0) { if(_sf_sjoin_buf) free(_sf_sjoin_buf); return (_sf_sjoin_buf = sf_strdup("")); } if(delimiter) { flen = strlen(delimiter); } else { delimiter = ""; flen = 0; } need = 1; for(k = 0; k < s->count; k++) need += ((ssize_t)s->lens[k] >= 0 ? s->lens[k] : strlen(s->list[k])) + (k?flen:0); buf = (char *)sf_malloc(need); if(buf == NULL) /* ENOMEM? */ return NULL; p=buf; for(k = 0; k < s->count; k++) { if(k) { memcpy(p, delimiter, flen); p += flen; } if((need = s->lens[k]) < 0) need = strlen(s->list[k]); memcpy(p, s->list[k], need); p += need; } *p = 0; if(_sf_sjoin_buf) free(_sf_sjoin_buf); return (_sf_sjoin_buf=buf); } static char *_sf_vjoin_buf = NULL; char * vjoin(char **vals, const char *delimiter) { char *buf; int flen; int need; char *p; char **v; if(vals == NULL || *vals == NULL) { if(_sf_vjoin_buf) free(_sf_vjoin_buf); return (_sf_vjoin_buf = sf_strdup("")); } if(delimiter == NULL) { delimiter = ""; flen = 0; } else { flen = strlen(delimiter); } need = 1; for(v = vals; *v; v++) need += strlen(*v) + ((v==vals)?0:flen); buf = (char *)sf_malloc(need); if(buf == NULL) /* ENOMEM? */ return NULL; p=buf; for(v = vals; *v; v++) { if(v != vals) { memcpy(p, delimiter, flen); p += flen; } strcpy(p, *v); p += strlen(*v); } *p = '\0'; if(_sf_vjoin_buf) free(_sf_vjoin_buf); return (_sf_vjoin_buf = buf); }