1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/* Copyright (C) 2018 by John Schember <john@nachtimwald.com>
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting
* documentation, and that the name of M.I.T. not be used in
* advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is"
* without express or implied warranty.
*/
#include "ares_setup.h"
#include "ares_strsplit.h"
#include "ares.h"
#include "ares_private.h"
static int list_contains(char * const *list, size_t num_elem, const char *str, int insensitive)
{
size_t len;
size_t i;
len = strlen(str);
for (i=0; i<num_elem; i++)
{
if (insensitive)
{
#ifdef WIN32
if (strnicmp(list[i], str, len) == 0)
#else
if (strncasecmp(list[i], str, len) == 0)
#endif
return 1;
}
else
{
if (strncmp(list[i], str, len) == 0)
return 1;
}
}
return 0;
}
static int is_delim(char c, const char *delims, size_t num_delims)
{
size_t i;
for (i=0; i<num_delims; i++)
{
if (c == delims[i])
return 1;
}
return 0;
}
void ares_strsplit_free(char **elms, size_t num_elm)
{
size_t i;
if (elms == NULL)
return;
for (i=0; i<num_elm; i++)
ares_free(elms[i]);
ares_free(elms);
}
char **ares_strsplit(const char *in, const char *delms, int make_set, size_t *num_elm)
{
char *parsestr;
char **temp;
char **out;
size_t cnt;
size_t nelms;
size_t in_len;
size_t num_delims;
size_t i;
if (in == NULL || delms == NULL || num_elm == NULL)
return NULL;
*num_elm = 0;
in_len = strlen(in);
num_delims = strlen(delms);
/* Figure out how many elements. */
nelms = 1;
for (i=0; i<in_len; i++)
{
if (is_delim(in[i], delms, num_delims))
{
nelms++;
}
}
/* Copy of input so we can cut it up. */
parsestr = ares_strdup(in);
if (parsestr == NULL)
return NULL;
/* Temporary array to store locations of start of each element
* within parsestr. */
temp = ares_malloc(nelms * sizeof(*temp));
if (temp == NULL)
{
ares_free(parsestr);
return NULL;
}
temp[0] = parsestr;
cnt = 1;
for (i=0; i<in_len && cnt<nelms; i++)
{
if (!is_delim(parsestr[i], delms, num_delims))
continue;
/* Replace sep with NULL. */
parsestr[i] = '\0';
/* Add the pointer to the array of elements */
temp[cnt] = parsestr+i+1;
cnt++;
}
/* Copy each element to our output array. */
out = ares_malloc(nelms * sizeof(*out));
if (out == NULL)
{
ares_free(parsestr);
ares_free(temp);
return NULL;
}
nelms = 0;
for (i=0; i<cnt; i++)
{
if (temp[i][0] == '\0')
continue;
if (make_set && list_contains(out, nelms, temp[i], 1))
continue;
out[nelms] = ares_strdup(temp[i]);
if (out[nelms] == NULL)
{
ares_strsplit_free(out, nelms);
ares_free(parsestr);
ares_free(temp);
return NULL;
}
nelms++;
}
/* If there are no elements don't return an empty allocated
* array. */
if (nelms == 0)
{
ares_strsplit_free(out, nelms);
out = NULL;
}
/* Get the true number of elements (recalculated because of make_set) */
*num_elm = nelms;
ares_free(parsestr);
ares_free(temp);
return out;
}
|