2 Bullet Continuous Collision Detection and Physics Library
\r
3 Copyright (c) 2003-2012 Erwin Coumans http://bulletphysics.org
\r
5 This software is provided 'as-is', without any express or implied warranty.
\r
6 In no event will the authors be held liable for any damages arising from the use of this software.
\r
7 Permission is granted to anyone to use this software for any purpose,
\r
8 including commercial applications, and to alter it and redistribute it freely,
\r
9 subject to the following restrictions:
\r
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
\r
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
\r
13 3. This notice may not be removed or altered from any source distribution.
\r
17 //#include <stdbool.h>
\r
22 #include "string_split.h"
\r
24 ///todo: remove stl dependency
\r
26 namespace bullet_utils
\r
28 void split( btAlignedObjectArray<std::string>&pieces, const std::string& vector_str, const std::string& separator)
\r
30 char** strArray = str_split(vector_str.c_str(),separator.c_str());
\r
31 int numSubStr = str_array_len(strArray);
\r
32 for (int i=0;i<numSubStr;i++)
\r
33 pieces.push_back(std::string(strArray[i]));
\r
34 str_array_free(strArray);
\r
41 /* Append an item to a dynamically allocated array of strings. On failure,
\r
42 return NULL, in which case the original array is intact. The item
\r
43 string is dynamically copied. If the array is NULL, allocate a new
\r
44 array. Otherwise, extend the array. Make sure the array is always
\r
45 NULL-terminated. Input string might not be '\0'-terminated. */
\r
46 char **str_array_append(char **array, size_t nitems, const char *item,
\r
49 /* Make a dynamic copy of the item. */
\r
54 copy = (char*)malloc(itemlen + 1);
\r
57 memcpy(copy, item, itemlen);
\r
58 copy[itemlen] = '\0';
\r
61 /* Extend array with one element. Except extend it by two elements,
\r
62 in case it did not yet exist. This might mean it is a teeny bit
\r
63 too big, but we don't care. */
\r
64 array = (char**)realloc(array, (nitems + 2) * sizeof(array[0]));
\r
65 if (array == NULL) {
\r
70 /* Add copy of item to array, and return it. */
\r
71 array[nitems] = copy;
\r
72 array[nitems+1] = NULL;
\r
77 /* Free a dynamic array of dynamic strings. */
\r
78 void str_array_free(char **array)
\r
82 for (size_t i = 0; array[i] != NULL; ++i)
\r
88 /* Split a string into substrings. Return dynamic array of dynamically
\r
89 allocated substrings, or NULL if there was an error. Caller is
\r
90 expected to free the memory, for example with str_array_free. */
\r
91 char **str_split(const char *input, const char *sep)
\r
94 char **array = NULL;
\r
95 const char *start = input;
\r
96 const char *next = strstr(start, sep);
\r
97 size_t seplen = strlen(sep);
\r
102 next = strstr(start, sep);
\r
103 if (next == NULL) {
\r
104 /* Add the remaining string (or empty string, if input ends with
\r
106 char **newstr = str_array_append(array, nitems, start, strlen(start));
\r
107 if (newstr == NULL) {
\r
108 str_array_free(array);
\r
114 } else if (next == input) {
\r
115 /* Input starts with separator. */
\r
120 itemlen = next - item;
\r
122 char **newstr = str_array_append(array, nitems, item, itemlen);
\r
123 if (newstr == NULL) {
\r
124 str_array_free(array);
\r
129 start = next + seplen;
\r
133 /* Input does not contain separator at all. */
\r
134 assert(array == NULL);
\r
135 array = str_array_append(array, nitems, input, strlen(input));
\r
142 /* Return length of a NULL-delimited array of strings. */
\r
143 size_t str_array_len(char **array)
\r
147 for (len = 0; array[len] != NULL; ++len)
\r
152 #ifdef UNIT_TEST_STRING
\r
154 #define MAX_OUTPUT 20
\r
162 char *output[MAX_OUTPUT];
\r
164 /* Input is empty string. Output should be a list with an empty
\r
174 /* Input is exactly the separator. Output should be two empty
\r
185 /* Input is non-empty, but does not have separator. Output should
\r
186 be the same string. */
\r
195 /* Input is non-empty, and does have separator. */
\r
197 "foo bar 1 and foo bar 2",
\r
206 const int tab_len = sizeof(tab) / sizeof(tab[0]);
\r
211 for (int i = 0; i < tab_len; ++i) {
\r
212 printf("test %d\n", i);
\r
214 char **output = str_split(tab[i].input, tab[i].sep);
\r
215 if (output == NULL) {
\r
216 fprintf(stderr, "output is NULL\n");
\r
220 size_t num_output = str_array_len(output);
\r
221 printf("num_output %lu\n", (unsigned long) num_output);
\r
223 size_t num_correct = str_array_len(tab[i].output);
\r
224 if (num_output != num_correct) {
\r
225 fprintf(stderr, "wrong number of outputs (%lu, not %lu)\n",
\r
226 (unsigned long) num_output, (unsigned long) num_correct);
\r
229 for (size_t j = 0; j < num_output; ++j) {
\r
230 if (strcmp(tab[i].output[j], output[j]) != 0) {
\r
231 fprintf(stderr, "output[%lu] is '%s' not '%s'\n",
\r
232 (unsigned long) j, output[j], tab[i].output[j]);
\r
239 str_array_free(output);
\r
244 return EXIT_FAILURE;
\r