Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / Serialize / BulletXmlWorldImporter / string_split.cpp
1 /*\r
2 Bullet Continuous Collision Detection and Physics Library\r
3 Copyright (c) 2003-2012 Erwin Coumans  http://bulletphysics.org\r
4 \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
10 \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
14 */\r
15 \r
16 #include <assert.h>\r
17 //#include <stdbool.h>\r
18 #include <stdio.h>\r
19 #include <stdlib.h>\r
20 #include <string.h>\r
21 \r
22 #include "string_split.h"\r
23 \r
24 ///todo: remove stl dependency\r
25 \r
26 namespace bullet_utils\r
27 {\r
28     void split( btAlignedObjectArray<std::string>&pieces, const std::string& vector_str, const std::string& separator)\r
29         {\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
35         }\r
36 \r
37 };\r
38 \r
39 \r
40 \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
47                         size_t itemlen)\r
48 {\r
49     /* Make a dynamic copy of the item. */\r
50     char *copy;\r
51     if (item == NULL)\r
52         copy = NULL;\r
53     else {\r
54         copy = (char*)malloc(itemlen + 1);\r
55         if (copy == NULL)\r
56             return NULL;\r
57         memcpy(copy, item, itemlen);\r
58         copy[itemlen] = '\0';\r
59     }\r
60         \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
66         free(copy);\r
67         return NULL;\r
68     }\r
69         \r
70     /* Add copy of item to array, and return it. */\r
71     array[nitems] = copy;\r
72     array[nitems+1] = NULL;\r
73     return array;\r
74 }\r
75 \r
76 \r
77 /* Free a dynamic array of dynamic strings. */\r
78 void str_array_free(char **array)\r
79 {\r
80     if (array == NULL)\r
81         return;\r
82     for (size_t i = 0; array[i] != NULL; ++i)\r
83         free(array[i]);\r
84     free(array);\r
85 }\r
86 \r
87 \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
92 {\r
93     size_t nitems = 0;\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
98     const char *item;\r
99     size_t itemlen;\r
100         \r
101     for (;;) {\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
105                          separator. */\r
106             char **newstr = str_array_append(array, nitems, start, strlen(start));\r
107             if (newstr == NULL) {\r
108                 str_array_free(array);\r
109                 return NULL;\r
110             }\r
111             array = newstr;\r
112             ++nitems;\r
113             break;\r
114         } else if (next == input) {\r
115             /* Input starts with separator. */\r
116             item = "";\r
117             itemlen = 0;\r
118         } else {\r
119             item = start;\r
120             itemlen = next - item;\r
121         }\r
122         char **newstr = str_array_append(array, nitems, item, itemlen);\r
123         if (newstr == NULL) {\r
124             str_array_free(array);\r
125             return NULL;\r
126         }\r
127         array = newstr;\r
128         ++nitems;\r
129         start = next + seplen;\r
130     }\r
131         \r
132     if (nitems == 0) {\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
136     }\r
137         \r
138     return array;\r
139 }\r
140 \r
141 \r
142 /* Return length of a NULL-delimited array of strings. */\r
143 size_t str_array_len(char **array)\r
144 {\r
145     size_t len;\r
146         \r
147     for (len = 0; array[len] != NULL; ++len)\r
148         continue;\r
149     return len;\r
150 }\r
151 \r
152 #ifdef UNIT_TEST_STRING\r
153 \r
154 #define MAX_OUTPUT 20\r
155 \r
156 \r
157 int main(void)\r
158 {\r
159     struct {\r
160         const char *input;\r
161         const char *sep;\r
162         char *output[MAX_OUTPUT];\r
163     } tab[] = {\r
164         /* Input is empty string. Output should be a list with an empty\r
165                  string. */\r
166         {\r
167             "",\r
168             "and",\r
169             {\r
170                 "",\r
171                 NULL,\r
172             },\r
173         },\r
174         /* Input is exactly the separator. Output should be two empty\r
175                  strings. */\r
176         {\r
177             "and",\r
178             "and",\r
179             {\r
180                 "",\r
181                 "",\r
182                 NULL,\r
183             },\r
184         },\r
185         /* Input is non-empty, but does not have separator. Output should\r
186                  be the same string. */\r
187         {\r
188             "foo",\r
189             "and",\r
190             {\r
191                 "foo",\r
192                 NULL,\r
193             },\r
194         },\r
195         /* Input is non-empty, and does have separator. */\r
196         {\r
197             "foo bar 1 and foo bar 2",\r
198             " and ",\r
199             {\r
200                 "foo bar 1",\r
201                 "foo bar 2",\r
202                 NULL,\r
203             },\r
204         },\r
205     };\r
206     const int tab_len = sizeof(tab) / sizeof(tab[0]);\r
207     bool errors;\r
208         \r
209     errors = false;\r
210         \r
211     for (int i = 0; i < tab_len; ++i) {\r
212         printf("test %d\n", i);\r
213                 \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
217             errors = true;\r
218             break;\r
219         }\r
220         size_t num_output = str_array_len(output);\r
221         printf("num_output %lu\n", (unsigned long) num_output);\r
222                 \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
227             errors = true;\r
228         } else {\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
233                     errors = true;\r
234                     break;\r
235                 }\r
236             }\r
237         }\r
238                 \r
239         str_array_free(output);\r
240         printf("\n");\r
241     }\r
242         \r
243     if (errors)\r
244         return EXIT_FAILURE;\r
245     return 0;\r
246 }\r
247 \r
248 #endif//\r
249 \r
250 \r