2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010-2011 - DIGITEO - Allan CORNET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
14 #include "splitLine.h"
15 #include "csv_strsubst.h"
17 /* ==================================================================== */
18 char **splitLineCSV(const char *str, const char *sep, int *toks, char meta)
20 #define EMPTYFIELD "__EMPTY_FIELD_CSV__"
22 const char *idx = NULL;
23 const char *end = NULL;
24 const char *sep_end = NULL;
25 const char *sep_idx = NULL;
28 char last_char = 0xFF;
30 /* Usually, it should be ,, or ;; */
31 char tokenstring_to_search[64] = "";
32 /* Previous item will be replaced by ;__EMPTY_FIELD_CSV__; */
33 char tokenreplacement_string[64] = "";
34 char *substitutedstring = NULL;
36 sprintf(tokenstring_to_search, "%s%s", sep, sep);
37 sprintf(tokenreplacement_string, "%s%s%s", sep, EMPTYFIELD, sep);
38 substitutedstring = csv_strsubst(str, tokenstring_to_search, tokenreplacement_string);
39 /* in a string like foo;bar;;;, replace all the ;;, not only the first and last one */
40 while (strstr(substitutedstring, tokenstring_to_search) != NULL) {
41 substitutedstring = csv_strsubst(substitutedstring, tokenstring_to_search, tokenreplacement_string);
44 if (strncmp(substitutedstring, sep, strlen(sep)) == 0)
47 size_t l = strlen(substitutedstring) + strlen(EMPTYFIELD) + strlen(sep) + 1;
48 tmp = (char*)MALLOC(sizeof(char) * l);
49 sprintf(tmp, "%s%s%s", EMPTYFIELD, sep, &substitutedstring[1]);
50 FREE(substitutedstring);
51 substitutedstring = tmp;
54 if (substitutedstring[strlen(substitutedstring) - 1] == sep[0])
57 size_t l = strlen(substitutedstring) + strlen(EMPTYFIELD) + 1;
58 tmp = (char*)MALLOC(sizeof(char) * l);
59 sprintf(tmp, "%s%s", substitutedstring, EMPTYFIELD);
60 FREE(substitutedstring);
61 substitutedstring = tmp;
65 sep_end = sep + strlen(sep);
66 end = substitutedstring + strlen(substitutedstring);
69 idx = substitutedstring;
71 if (strstr(substitutedstring, sep) == NULL)
74 FREE(substitutedstring);
75 substitutedstring = NULL;
79 retstr = (char **) MALLOC((sizeof(char *) * (int)strlen(substitutedstring)));
83 FREE(substitutedstring);
84 substitutedstring = NULL;
90 while (sep_idx < sep_end)
92 if ((*idx == *sep_idx) && (last_char != meta))
96 if (curr_str < (int)strlen(substitutedstring))
98 retstr[curr_str] = (char *) MALLOC((sizeof(char) * len) + 1);
100 if (retstr[curr_str] == NULL)
103 FREE(substitutedstring);
104 substitutedstring = NULL;
107 memcpy(retstr[curr_str], (idx - len), len);
108 retstr[curr_str][len] = 0;
109 if (strcmp(retstr[curr_str], EMPTYFIELD) == 0)
111 strcpy(retstr[curr_str], "");
119 if (curr_str >= (int)strlen(substitutedstring))
121 *toks = curr_str + 1;
122 FREE(substitutedstring);
123 substitutedstring = NULL;
149 retstr[curr_str] = (char *) MALLOC((sizeof(char) * len) + 1);
151 if (retstr[curr_str] == NULL)
154 if (substitutedstring)
156 FREE(substitutedstring);
157 substitutedstring = NULL;
162 memcpy(retstr[curr_str], (idx - len), len);
163 retstr[curr_str][len] = 0;
164 if (strcmp(retstr[curr_str], EMPTYFIELD) == 0)
166 strcpy(retstr[curr_str], "");
169 *toks = curr_str + 1;
172 if (substitutedstring)
174 FREE(substitutedstring);
175 substitutedstring = NULL;
180 /* ==================================================================== */