// ============================================================================= csvResult* csvTextScan(const char **lines, int numberOfLines, const char *separator, const char *decimal) { csvResult *result = NULL; int nbRows = 0; int nbColumns = 0; char **cellsStrings = NULL; char **cleanedLines = NULL; int nbLines = numberOfLines; if (strcmp(separator, decimal) == 0) { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_SEPARATOR_DECIMAL_EQUAL; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } return result; } // ticket 472 { const char *blankMode = getCsvDefaultCsvIgnoreBlankLine(); if (strcmp(blankMode, "on") == 0) { char **tmpLines = removeAllBlankLines(lines, &nbLines); if (tmpLines) { freeArrayOfString(cleanedLines, nbLines); cleanedLines = tmpLines; } } else { /* remove last lines empty (bug 7003 in scilab)*/ cleanedLines = removeEmptyLinesAtTheEnd(lines, &nbLines); } } nbColumns = getNumbersOfColumnsInLines((const char **)cleanedLines, nbLines, separator); if (nbColumns == 0) { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_COLUMNS_ERROR; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } FREE(cleanedLines); return result; } else { nbRows = nbLines; } cellsStrings = getStringsFromLines((const char **)cleanedLines, nbLines, separator, decimal, nbColumns, nbRows); if (cleanedLines) { freeArrayOfString(cleanedLines, nbLines); cleanedLines = NULL; } if (cellsStrings) { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_NO_ERROR; result->m = nbRows; result->n = nbColumns; result->pstrValues = cellsStrings; result->pstrComments = NULL; result->nbComments = 0; } else { FREE(cellsStrings); } } else { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_COLUMNS_ERROR; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } } return result; }
/*--------------------------------------------------------------------------*/ fscanfMatResult *fscanfMat(char *filename, char *format, char *separator) { int fd = 0; int f_swap = 0; double res = 0.0; int errMOPEN = MOPEN_INVALID_STATUS; int errMGETL = MGETL_ERROR; int i = 0; int nbLinesTextDetected = 0; int nbColumns = 0; int nbRows = 0; fscanfMatResult *resultFscanfMat = NULL; wchar_t **pwsLines = NULL; char **lines = NULL; int nblines = 0; double *dValues = NULL; wchar_t* filenameW = NULL; if ((filename == NULL) || (format == NULL) || (separator == NULL)) { return NULL; } if (!checkFscanfMatFormat(format)) { resultFscanfMat = (fscanfMatResult*)(MALLOC(sizeof(fscanfMatResult))); if (resultFscanfMat) { resultFscanfMat->err = FSCANFMAT_FORMAT_ERROR; resultFscanfMat->m = 0; resultFscanfMat->n = 0; resultFscanfMat->sizeText = 0; resultFscanfMat->text = NULL; resultFscanfMat->values = NULL; } return resultFscanfMat; } filenameW = to_wide_string(filename); errMOPEN = mopen(filenameW, READ_ONLY_TEXT_MODE, f_swap, &fd); FREE(filenameW); if (errMOPEN != MOPEN_NO_ERROR) { resultFscanfMat = (fscanfMatResult*)(MALLOC(sizeof(fscanfMatResult))); if (resultFscanfMat) { resultFscanfMat->err = FSCANFMAT_MOPEN_ERROR; resultFscanfMat->m = 0; resultFscanfMat->n = 0; resultFscanfMat->sizeText = 0; resultFscanfMat->text = NULL; resultFscanfMat->values = NULL; } return resultFscanfMat; } pwsLines = mgetl(fd, -1, &nblines, &errMGETL); mclose(fd); if (errMGETL != MGETL_NO_ERROR) { resultFscanfMat = (fscanfMatResult*)(MALLOC(sizeof(fscanfMatResult))); if (resultFscanfMat) { resultFscanfMat->err = FSCANFMAT_READLINES_ERROR; resultFscanfMat->m = 0; resultFscanfMat->n = 0; resultFscanfMat->sizeText = 0; resultFscanfMat->text = NULL; resultFscanfMat->values = NULL; } return resultFscanfMat; } lines = (char**)MALLOC(sizeof(char*) * nblines); for (i = 0; i < nblines; i++) { lines[i] = wide_string_to_UTF8(pwsLines[i]); } freeArrayOfWideString(pwsLines, nblines); lines = removeEmptyLinesAtTheEnd(lines, &nblines); lines = removeTextLinesAtTheEnd(lines, &nblines, format, separator); nbLinesTextDetected = getNumbersLinesOfText(lines, nblines, format, separator); nbRows = nblines - nbLinesTextDetected; nbColumns = getNumbersColumnsInLines(lines, nblines, nbLinesTextDetected, format, separator); dValues = getDoubleValuesFromLines(lines, nblines, nbLinesTextDetected, format, separator, nbColumns, nbRows); if (dValues) { resultFscanfMat = (fscanfMatResult*)(MALLOC(sizeof(fscanfMatResult))); if (resultFscanfMat) { if (nbLinesTextDetected > 0) { if (lines) { for (i = nbLinesTextDetected; i < nblines; i++) { if (lines[i]) { FREE(lines[i]); lines[i] = NULL; } } } resultFscanfMat->text = lines; } else { freeArrayOfString(lines, nblines); resultFscanfMat->text = NULL; } resultFscanfMat->sizeText = nbLinesTextDetected; resultFscanfMat->m = nbRows; resultFscanfMat->n = nbColumns; resultFscanfMat->values = dValues; resultFscanfMat->err = FSCANFMAT_NO_ERROR; } else { FREE(dValues); freeArrayOfString(lines, nblines); } } else { freeArrayOfString(lines, nblines); if (nbColumns == 0 || nbRows == 0) { resultFscanfMat = (fscanfMatResult*)(MALLOC(sizeof(fscanfMatResult))); if (resultFscanfMat) { resultFscanfMat->err = FSCANFMAT_READLINES_ERROR; resultFscanfMat->m = 0; resultFscanfMat->n = 0; resultFscanfMat->sizeText = 0; resultFscanfMat->text = NULL; resultFscanfMat->values = NULL; } } } return resultFscanfMat; }