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 ""
24 #define DEFAULT_CSV_EOL "\n"
25 #define CSV_DECIMAL_MODE_1 DEFAULT_CSV_DECIMAL
26 #define CSV_DECIMAL_MODE_2 ","
27 #define DEFAULT_CSV_CONVERSION "double"
28 #define CSV_CONVERSION_MODE_1 DEFAULT_CSV_CONVERSION
29 #define CSV_CONVERSION_MODE_2 "string"
30 #define DEFAULT_CSV_ENCODING "utf-8"
31 #define DEFAULT_CSV_ENCODING_MODE1 "utf-8"
32 #define DEFAULT_CSV_ENCODING_MODE2 "iso-latin"
33 #define CSV_IGNORE_BLANK_LINE_MODE_1 "off"
34 #define CSV_IGNORE_BLANK_LINE_MODE_2 "on"
35 #define DEFAULT_IGNORE_BLANK_LINE CSV_IGNORE_BLANK_LINE_MODE_2
36 // =============================================================================
37 static char *defaultCsvSeparator = NULL;
38 static char *defaultCsvDecimal = NULL;
39 static char *defaultCsvConversion = NULL;
40 static char *defaultCsvPrecision = NULL;
41 static char *defaultCsvCommentsRegExp = NULL;
42 static char *defaultCsvEOL = NULL;
43 static char *defaultCsvEncoding = NULL;
44 static char *defaultCsvIgnoreBlankLine = NULL;
45 // =============================================================================
46 static int initializeCsvDefaultValues(void);
47 // =============================================================================
48 const char *getCsvDefaultSeparator(void)
50 initializeCsvDefaultValues();
51 return defaultCsvSeparator;
53 // =============================================================================
54 const char *getCsvDefaultDecimal(void)
56 initializeCsvDefaultValues();
57 return defaultCsvDecimal;
59 // =============================================================================
60 const char *getCsvDefaultConversion(void)
62 initializeCsvDefaultValues();
63 return defaultCsvConversion;
65 // =============================================================================
66 const char *getCsvDefaultPrecision(void)
68 initializeCsvDefaultValues();
69 return defaultCsvPrecision;
71 // =============================================================================
72 const char *getCsvDefaultCommentsRegExp(void)
74 initializeCsvDefaultValues();
75 return defaultCsvCommentsRegExp;
77 // =============================================================================
78 const char *getCsvDefaultEOL(void)
80 initializeCsvDefaultValues();
83 // =============================================================================
84 const char *getCsvDefaultEncoding(void)
86 initializeCsvDefaultValues();
87 return defaultCsvEncoding;
89 // =============================================================================
90 const char *getCsvDefaultCsvIgnoreBlankLine(void)
92 initializeCsvDefaultValues();
93 return defaultCsvIgnoreBlankLine;
95 // =============================================================================
96 int setCsvDefaultSeparator(const char *separator)
98 if (initializeCsvDefaultValues())
102 if (separator == NULL)
107 if (strcmp(separator, getCsvDefaultDecimal()) == 0)
112 if (defaultCsvSeparator)
114 FREE(defaultCsvSeparator);
116 defaultCsvSeparator = strdup(separator);
117 if (defaultCsvSeparator == NULL)
124 // =============================================================================
125 int setCsvDefaultDecimal(const char *decimal)
127 if (initializeCsvDefaultValues())
136 /* decimal separator supported . and , */
137 if ((strcmp(decimal, CSV_DECIMAL_MODE_1) == 0) ||
138 (strcmp(decimal, CSV_DECIMAL_MODE_2) == 0))
140 if (strcmp(decimal, getCsvDefaultSeparator()) == 0)
144 if (defaultCsvDecimal)
146 FREE(defaultCsvDecimal);
148 defaultCsvDecimal = strdup(decimal);
149 if (defaultCsvDecimal == NULL)
157 // =============================================================================
158 int setCsvDefaultConversion(const char *conversion)
160 if (initializeCsvDefaultValues())
164 if (conversion == NULL)
169 if ((strcmp(conversion, CSV_CONVERSION_MODE_1) == 0) ||
170 (strcmp(conversion, CSV_CONVERSION_MODE_2) == 0))
172 if (defaultCsvConversion)
174 FREE(defaultCsvConversion);
176 defaultCsvConversion = strdup(conversion);
177 if (defaultCsvConversion)
185 // =============================================================================
186 int setCsvDefaultPrecision(const char *precision)
188 if (initializeCsvDefaultValues())
192 if (precision == NULL)
196 if (checkCsvWriteFormat(precision) == 0)
198 if (defaultCsvPrecision)
200 FREE(defaultCsvPrecision);
202 defaultCsvPrecision = strdup(precision);
203 if (defaultCsvPrecision)
210 // =============================================================================
211 int setCsvDefaultCsvIgnoreBlankLine(const char *blankMode)
213 if (initializeCsvDefaultValues())
217 if (blankMode == NULL)
221 if ((strcmp(blankMode, CSV_IGNORE_BLANK_LINE_MODE_1) == 0) ||
222 (strcmp(blankMode, CSV_IGNORE_BLANK_LINE_MODE_2) == 0))
224 if (defaultCsvIgnoreBlankLine)
226 FREE(defaultCsvIgnoreBlankLine);
228 defaultCsvIgnoreBlankLine = strdup(blankMode);
229 if (defaultCsvIgnoreBlankLine)
236 // =============================================================================
237 static int initializeCsvDefaultValues(void)
239 if (defaultCsvSeparator == NULL)
241 defaultCsvSeparator = strdup(DEFAULT_CSV_SEPARATOR);
244 if (defaultCsvDecimal == NULL)
246 defaultCsvDecimal = strdup(DEFAULT_CSV_DECIMAL);
249 if (defaultCsvConversion == NULL)
251 defaultCsvConversion = strdup(DEFAULT_CSV_CONVERSION);
254 if (defaultCsvPrecision == NULL)
256 defaultCsvPrecision = strdup(DEFAULT_CSV_PRECISION);
259 if (defaultCsvCommentsRegExp == NULL)
261 defaultCsvCommentsRegExp = strdup(DEFAULT_CSV_COMMENTS_REGEXP);
264 if (defaultCsvEOL == NULL)
266 defaultCsvEOL = strdup(DEFAULT_CSV_EOL);
269 if (defaultCsvEncoding == NULL)
271 defaultCsvEncoding = strdup(DEFAULT_CSV_ENCODING);
274 if (defaultCsvIgnoreBlankLine == NULL)
276 defaultCsvIgnoreBlankLine = strdup(DEFAULT_IGNORE_BLANK_LINE);
279 if ((defaultCsvSeparator == NULL) ||
280 (defaultCsvDecimal == NULL) ||
281 (defaultCsvConversion == NULL) ||
282 (defaultCsvPrecision == NULL) ||
283 (defaultCsvCommentsRegExp == NULL) ||
284 (defaultCsvEOL == NULL) ||
285 (defaultCsvEncoding == NULL) ||
286 (defaultCsvIgnoreBlankLine == NULL))
293 // =============================================================================
294 int setCsvDefaultReset(void)
296 if (defaultCsvSeparator)
298 FREE(defaultCsvSeparator);
299 defaultCsvSeparator = NULL;
301 if (defaultCsvDecimal)
303 FREE(defaultCsvDecimal);
304 defaultCsvDecimal = NULL;
306 if (defaultCsvConversion)
308 FREE(defaultCsvConversion);
309 defaultCsvConversion = NULL;
311 if (defaultCsvPrecision)
313 FREE(defaultCsvPrecision);
314 defaultCsvPrecision = NULL;
316 if (defaultCsvCommentsRegExp)
318 FREE(defaultCsvCommentsRegExp);
319 defaultCsvCommentsRegExp = NULL;
324 defaultCsvEOL = NULL;
326 if (defaultCsvEncoding)
328 FREE(defaultCsvEncoding);
329 defaultCsvEncoding = NULL;
331 if (defaultCsvIgnoreBlankLine)
333 FREE(defaultCsvIgnoreBlankLine);
334 defaultCsvIgnoreBlankLine = NULL;
336 return initializeCsvDefaultValues();
338 // =============================================================================
339 int setCsvDefaultCommentsRegExp(const char *commentsRegExp)
341 if (initializeCsvDefaultValues())
345 if (commentsRegExp == NULL)
350 if (strcmp(commentsRegExp, getCsvDefaultCommentsRegExp()) == 0)
355 if (defaultCsvCommentsRegExp)
357 FREE(defaultCsvCommentsRegExp);
358 defaultCsvCommentsRegExp = NULL;
361 defaultCsvCommentsRegExp = strdup(commentsRegExp);
363 if (defaultCsvDecimal == NULL)
370 // =============================================================================
371 int setCsvDefaultEOL(const char *eol)
373 if (initializeCsvDefaultValues())
382 if (strcmp(eol, getCsvDefaultEOL()) == 0)
390 defaultCsvEOL = NULL;
393 defaultCsvEOL = strdup(eol);
395 if (defaultCsvEOL == NULL)
402 // =============================================================================
403 int setCsvDefaultEncoding(const char *encoding)
405 if (initializeCsvDefaultValues())
409 if (encoding == NULL)
414 if (strcmp(encoding, getCsvDefaultEncoding()) == 0)
419 if ((strcmp(encoding, DEFAULT_CSV_ENCODING_MODE1) != 0) &&
420 (strcmp(encoding, DEFAULT_CSV_ENCODING_MODE2) != 0))
425 if (defaultCsvEncoding)
427 FREE(defaultCsvEncoding);
428 defaultCsvEncoding = NULL;
431 defaultCsvEncoding = strdup(encoding);
433 if (defaultCsvEncoding == NULL)
440 // =============================================================================