diff options
author | Evan Huus <eapache@gmail.com> | 2013-06-18 21:25:37 +0000 |
---|---|---|
committer | Evan Huus <eapache@gmail.com> | 2013-06-18 21:25:37 +0000 |
commit | d487bba75f4719eff8edc5a946596ecb14c28fa2 (patch) | |
tree | 889b5a4d42e9428ba472cc3455ee5aa09464e8a3 /epan/wmem/wmem_strutl.c | |
parent | ece132a5ce689df9df8ffeb254eb8baf824f40a6 (diff) |
Add wmem_alloc_array, wmem_strsplit, wmem_strconcat.
svn path=/trunk/; revision=50017
Diffstat (limited to 'epan/wmem/wmem_strutl.c')
-rw-r--r-- | epan/wmem/wmem_strutl.c | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/epan/wmem/wmem_strutl.c b/epan/wmem/wmem_strutl.c index 99d13325e8..831d05160f 100644 --- a/epan/wmem/wmem_strutl.c +++ b/epan/wmem/wmem_strutl.c @@ -98,6 +98,115 @@ wmem_strdup_vprintf(wmem_allocator_t *allocator, const gchar *fmt, va_list ap) return dst; } +gchar * +wmem_strconcat(wmem_allocator_t *allocator, const gchar *first, ...) +{ + gsize len; + va_list args; + gchar *s; + gchar *concat; + gchar *ptr; + + if (!first) + return NULL; + + len = 1 + strlen(first); + va_start(args, first); + while ((s = va_arg(args, gchar*))) { + len += strlen(s); + } + va_end(args); + + ptr = concat = (gchar *)wmem_alloc(allocator, len); + + ptr = g_stpcpy(ptr, first); + va_start(args, first); + while ((s = va_arg(args, gchar*))) { + ptr = g_stpcpy(ptr, s); + } + va_end(args); + + return concat; +} + +gchar ** +wmem_strsplit(wmem_allocator_t *allocator, const gchar *src, + const gchar *delimiter, int max_tokens) +{ + gchar* splitted; + gchar* s; + guint tokens; + guint str_len; + guint sep_len; + guint i; + gchar** vec; + enum { AT_START, IN_PAD, IN_TOKEN } state; + guint curr_tok = 0; + + if ( ! src + || ! delimiter + || ! delimiter[0]) + return NULL; + + s = splitted = wmem_strdup(allocator, src); + str_len = (guint) strlen(splitted); + sep_len = (guint) strlen(delimiter); + + if (max_tokens < 1) max_tokens = INT_MAX; + + tokens = 1; + + + while (tokens <= (guint)max_tokens && ( s = strstr(s,delimiter) )) { + tokens++; + + for(i=0; i < sep_len; i++ ) + s[i] = '\0'; + + s += sep_len; + + } + + vec = wmem_alloc_array(allocator, gchar*,tokens+1); + state = AT_START; + + for (i=0; i< str_len; i++) { + switch(state) { + case AT_START: + switch(splitted[i]) { + case '\0': + state = IN_PAD; + continue; + default: + vec[curr_tok] = &(splitted[i]); + curr_tok++; + state = IN_TOKEN; + continue; + } + case IN_TOKEN: + switch(splitted[i]) { + case '\0': + state = IN_PAD; + default: + continue; + } + case IN_PAD: + switch(splitted[i]) { + default: + vec[curr_tok] = &(splitted[i]); + curr_tok++; + state = IN_TOKEN; + case '\0': + continue; + } + } + } + + vec[curr_tok] = NULL; + + return vec; +} + /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * |