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
13 #include "csvDefault.h"
16 #include "strdup_windows.h"
18 #include "checkCsvWriteFormat.h"
19 // =============================================================================
20 #define DEFAULT_CSV_SEPARATOR ","
21 #define DEFAULT_CSV_DECIMAL "."
22 #define DEFAULT_CSV_PRECISION "%.17lg"
23 #define DEFAULT_CSV_COMMENTS_REGEXP ""
25 #define DEFAULT_CSV_EOL "\r\n"
27 #define DEFAULT_CSV_EOL "\n"
29 #define CSV_DECIMAL_MODE_1 DEFAULT_CSV_DECIMAL
30 #define CSV_DECIMAL_MODE_2 ","
31 #define DEFAULT_CSV_CONVERSION "double"
32 #define CSV_CONVERSION_MODE_1 DEFAULT_CSV_CONVERSION
33 #define CSV_CONVERSION_MODE_2 "string"
34 #define DEFAULT_CSV_ENCODING "utf-8"
35 #define DEFAULT_CSV_ENCODING_MODE1 "utf-8"
36 #define DEFAULT_CSV_ENCODING_MODE2 "iso-latin"
37 #define CSV_IGNORE_BLANK_LINE_MODE_1 "off"
38 #define CSV_IGNORE_BLANK_LINE_MODE_2 "on"
39 #define DEFAULT_IGNORE_BLANK_LINE CSV_IGNORE_BLANK_LINE_MODE_2
40 // =============================================================================
41 static char *defaultCsvSeparator = NULL;
42 static char *defaultCsvDecimal = NULL;
43 static char *defaultCsvConversion = NULL;
44 static char *defaultCsvPrecision = NULL;
45 static char *defaultCsvCommentsRegExp = NULL;
46 static char *defaultCsvEOL = NULL;
47 static char *defaultCsvEncoding = NULL;
48 static char *defaultCsvIgnoreBlankLine = NULL;
49 // =============================================================================
50 static int initializeCsvDefaultValues(void);
51 // =============================================================================
52 const char *getCsvDefaultSeparator(void)
54 initializeCsvDefaultValues();
55 return defaultCsvSeparator;
57 // =============================================================================
58 const char *getCsvDefaultDecimal(void)
60 initializeCsvDefaultValues();
61 return defaultCsvDecimal;
63 // =============================================================================
64 const char *getCsvDefaultConversion(void)
66 initializeCsvDefaultValues();
67 return defaultCsvConversion;
69 // =============================================================================
70 const char *getCsvDefaultPrecision(void)
72 initializeCsvDefaultValues();
73 return defaultCsvPrecision;
75 // =============================================================================
76 const char *getCsvDefaultCommentsRegExp(void)
78 initializeCsvDefaultValues();
79 return defaultCsvCommentsRegExp;
81 // =============================================================================
82 const char *getCsvDefaultEOL(void)
84 initializeCsvDefaultValues();
87 // =============================================================================
88 const char *getCsvDefaultEncoding(void)
90 initializeCsvDefaultValues();
91 return defaultCsvEncoding;
93 // =============================================================================
94 const char *getCsvDefaultCsvIgnoreBlankLine(void)
96 initializeCsvDefaultValues();
97 return defaultCsvIgnoreBlankLine;
99 // =============================================================================
100 int setCsvDefaultSeparator(const char *separator)
102 if (initializeCsvDefaultValues())
106 if (separator == NULL)
111 if (strcmp(separator, getCsvDefaultDecimal()) == 0)
116 if (defaultCsvSeparator)
118 FREE(defaultCsvSeparator);
120 defaultCsvSeparator = strdup(separator);
121 if (defaultCsvSeparator == NULL)
128 // =============================================================================
129 int setCsvDefaultDecimal(const char *decimal)
131 if (initializeCsvDefaultValues())
140 /* decimal separator supported . and , */
141 if ((strcmp(decimal, CSV_DECIMAL_MODE_1) == 0) ||
142 (strcmp(decimal, CSV_DECIMAL_MODE_2) == 0))
144 if (strcmp(decimal, getCsvDefaultSeparator()) == 0)
148 if (defaultCsvDecimal)
150 FREE(defaultCsvDecimal);
152 defaultCsvDecimal = strdup(decimal);
153 if (defaultCsvDecimal == NULL)
161 // =============================================================================
162 int setCsvDefaultConversion(const char *conversion)
164 if (initializeCsvDefaultValues())
168 if (conversion == NULL)
173 if ((strcmp(conversion, CSV_CONVERSION_MODE_1) == 0) ||
174 (strcmp(conversion, CSV_CONVERSION_MODE_2) == 0))
176 if (defaultCsvConversion)
178 FREE(defaultCsvConversion);
180 defaultCsvConversion = strdup(conversion);
181 if (defaultCsvConversion)
189 // =============================================================================
190 int setCsvDefaultPrecision(const char *precision)
192 if (initializeCsvDefaultValues())
196 if (precision == NULL)
200 if (checkCsvWriteFormat(precision) == 0)
202 if (defaultCsvPrecision)
204 FREE(defaultCsvPrecision);
206 defaultCsvPrecision = strdup(precision);
207 if (defaultCsvPrecision)
214 // =============================================================================
215 int setCsvDefaultCsvIgnoreBlankLine(const char *blankMode)
217 if (initializeCsvDefaultValues())
221 if (blankMode == NULL)
225 if ((strcmp(blankMode, CSV_IGNORE_BLANK_LINE_MODE_1) == 0) ||
226 (strcmp(blankMode, CSV_IGNORE_BLANK_LINE_MODE_2) == 0))
228 if (defaultCsvIgnoreBlankLine)
230 FREE(defaultCsvIgnoreBlankLine);
232 defaultCsvIgnoreBlankLine = strdup(blankMode);
233 if (defaultCsvIgnoreBlankLine)
240 // =============================================================================
241 static int initializeCsvDefaultValues(void)
243 if (defaultCsvSeparator == NULL)
245 defaultCsvSeparator = strdup(DEFAULT_CSV_SEPARATOR);
248 if (defaultCsvDecimal == NULL)
250 defaultCsvDecimal = strdup(DEFAULT_CSV_DECIMAL);
253 if (defaultCsvConversion == NULL)
255 defaultCsvConversion = strdup(DEFAULT_CSV_CONVERSION);
258 if (defaultCsvPrecision == NULL)
260 defaultCsvPrecision = strdup(DEFAULT_CSV_PRECISION);
263 if (defaultCsvCommentsRegExp == NULL)
265 defaultCsvCommentsRegExp = strdup(DEFAULT_CSV_COMMENTS_REGEXP);
268 if (defaultCsvEOL == NULL)
270 defaultCsvEOL = strdup(DEFAULT_CSV_EOL);
273 if (defaultCsvEncoding == NULL)
275 defaultCsvEncoding = strdup(DEFAULT_CSV_ENCODING);
278 if (defaultCsvIgnoreBlankLine == NULL)
280 defaultCsvIgnoreBlankLine = strdup(DEFAULT_IGNORE_BLANK_LINE);
283 if ((defaultCsvSeparator == NULL) ||
284 (defaultCsvDecimal == NULL) ||
285 (defaultCsvConversion == NULL) ||
286 (defaultCsvPrecision == NULL) ||
287 (defaultCsvCommentsRegExp == NULL) ||
288 (defaultCsvEOL == NULL) ||
289 (defaultCsvEncoding == NULL) ||
290 (defaultCsvIgnoreBlankLine == NULL))
297 // =============================================================================
298 int setCsvDefaultReset(void)
300 if (defaultCsvSeparator)
302 FREE(defaultCsvSeparator);
303 defaultCsvSeparator = NULL;
305 if (defaultCsvDecimal)
307 FREE(defaultCsvDecimal);
308 defaultCsvDecimal = NULL;
310 if (defaultCsvConversion)
312 FREE(defaultCsvConversion);
313 defaultCsvConversion = NULL;
315 if (defaultCsvPrecision)
317 FREE(defaultCsvPrecision);
318 defaultCsvPrecision = NULL;
320 if (defaultCsvCommentsRegExp)
322 FREE(defaultCsvCommentsRegExp);
323 defaultCsvCommentsRegExp = NULL;
328 defaultCsvEOL = NULL;
330 if (defaultCsvEncoding)
332 FREE(defaultCsvEncoding);
333 defaultCsvEncoding = NULL;
335 if (defaultCsvIgnoreBlankLine)
337 FREE(defaultCsvIgnoreBlankLine);
338 defaultCsvIgnoreBlankLine = NULL;
340 return initializeCsvDefaultValues();
342 // =============================================================================
343 int setCsvDefaultCommentsRegExp(const char *commentsRegExp)
345 if (initializeCsvDefaultValues())
349 if (commentsRegExp == NULL)
354 if (strcmp(commentsRegExp, getCsvDefaultCommentsRegExp()) == 0)
359 if (defaultCsvCommentsRegExp)
361 FREE(defaultCsvCommentsRegExp);
362 defaultCsvCommentsRegExp = NULL;
365 defaultCsvCommentsRegExp = strdup(commentsRegExp);
367 if (defaultCsvDecimal == NULL)
374 // =============================================================================
375 int setCsvDefaultEOL(const char *eol)
377 if (initializeCsvDefaultValues())
386 if (strcmp(eol, getCsvDefaultEOL()) == 0)
394 defaultCsvEOL = NULL;
397 defaultCsvEOL = strdup(eol);
399 if (defaultCsvEOL == NULL)
406 // =============================================================================
407 int setCsvDefaultEncoding(const char *encoding)
409 if (initializeCsvDefaultValues())
413 if (encoding == NULL)
418 if (strcmp(encoding, getCsvDefaultEncoding()) == 0)
423 if ((strcmp(encoding, DEFAULT_CSV_ENCODING_MODE1) != 0) &&
424 (strcmp(encoding, DEFAULT_CSV_ENCODING_MODE2) != 0))
429 if (defaultCsvEncoding)
431 FREE(defaultCsvEncoding);
432 defaultCsvEncoding = NULL;
435 defaultCsvEncoding = strdup(encoding);
437 if (defaultCsvEncoding == NULL)
444 // =============================================================================