From e627986266d8fcda1b273c1ecab36400494bf76c Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 2 May 2005 19:14:33 +0000 Subject: [PATCH] Added utilities nmem_strsplit and nmem_strsplit_blank which splits a string into arrays and allocates each stream using NMEM. --- include/yaz/nmem.h | 8 +++++++- src/nmemsdup.c | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/include/yaz/nmem.h b/include/yaz/nmem.h index 6c92e99..da63a1a 100644 --- a/include/yaz/nmem.h +++ b/include/yaz/nmem.h @@ -23,7 +23,7 @@ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THIS SOFTWARE. * - * $Id: nmem.h,v 1.14 2005-01-15 19:47:09 adam Exp $ + * $Id: nmem.h,v 1.15 2005-05-02 19:14:33 adam Exp $ */ /** @@ -72,6 +72,12 @@ YAZ_EXPORT void nmem_reset(NMEM n); YAZ_EXPORT int nmem_total(NMEM n); YAZ_EXPORT char *nmem_strdup (NMEM mem, const char *src); YAZ_EXPORT char *nmem_strdupn (NMEM mem, const char *src, size_t n); +YAZ_EXPORT void nmem_strsplit_blank(NMEM nmem, const char *dstr, + char ***darray, int *num); +YAZ_EXPORT void nmem_strsplit(NMEM nmem, const char *delim, + const char *dstr, + char ***darray, int *num); + YAZ_EXPORT int *nmem_intdup (NMEM mem, int v); YAZ_EXPORT void nmem_transfer (NMEM dst, NMEM src); diff --git a/src/nmemsdup.c b/src/nmemsdup.c index dd9af4f..e680a98 100644 --- a/src/nmemsdup.c +++ b/src/nmemsdup.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2005, Index Data ApS * See the file LICENSE for details. * - * $Id: nmemsdup.c,v 1.3 2005-01-15 19:47:14 adam Exp $ + * $Id: nmemsdup.c,v 1.4 2005-05-02 19:14:33 adam Exp $ */ /** @@ -38,3 +38,43 @@ int *nmem_intdup(NMEM mem, int v) *dst = v; return dst; } + +void nmem_strsplit_blank(NMEM nmem, const char *dstr, char ***darray, int *num) +{ + nmem_strsplit(nmem, " ", dstr, darray, num); +} + +void nmem_strsplit(NMEM nmem, const char *delim, const char *dstr, + char ***darray, int *num) +{ + const char *cp = dstr; + for (*num = 0; *cp; (*num)++) + { + while (*cp && strchr(delim, *cp)) + cp++; + if (!*cp) + break; + while (*cp && !strchr(delim, *cp)) + cp++; + } + if (!*num) + *darray = 0; + else + { + size_t i = 0; + *darray = nmem_malloc(nmem, *num * sizeof(**darray)); + for (cp = dstr; *cp; ) + { + const char *cp0; + while (*cp && strchr(delim, *cp)) + cp++; + if (!*cp) + break; + cp0 = cp; + while (*cp && !strchr(delim, *cp)) + cp++; + (*darray)[i++] = nmem_strdupn(nmem, cp0, cp - cp0); + } + } +} + -- 1.7.10.4