Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
source code used to check if a specified command exists (if the command checking...
[simgrid.git] / tools / tesh2 / src / explode.c
1 #include <explode.h>\r
2 \r
3 char** \r
4 explode(char separator, const char *string)\r
5 {\r
6     int pos = 0;\r
7     int i, len = 1;\r
8     int number = 2;\r
9     char **table;\r
10     const char* p = string;\r
11     \r
12     for (i = 0; p[i] != '\0'; i++)\r
13         if (p[i] == separator)\r
14             number++;\r
15     \r
16  \r
17     table = (char**)calloc(number, sizeof(char*));\r
18     \r
19     i = 0;\r
20     \r
21     while (*p++ != '\0')\r
22     {\r
23         if(*p == separator)\r
24         {\r
25             table[i] = (char*)calloc(len + 1, sizeof(char));\r
26             strncpy(table[i], string + pos , len);\r
27             pos += len + 1;\r
28             len = 0;\r
29             i++;\r
30         }\r
31         else\r
32                 len++;\r
33     }\r
34     \r
35     if(len > 1)\r
36         {\r
37         table[i] = (char*)calloc(len + 1, sizeof(char));\r
38         strncpy(table[i], string + pos , len);\r
39     }\r
40     \r
41    \r
42     table[++i] = NULL;\r
43     \r
44     return table;\r
45 }\r