// ============================================================================= int sci_csvTextScan(char *fname, unsigned long fname_len) { SciErr sciErr; int iErr = 0; int i = 0; int *piAddressVarOne = NULL; int m1 = 0, n1 = 0; int iType1 = 0; char **text = NULL; int *lengthText = NULL; int nbLines = 0; char *separator = NULL; char *decimal = NULL; char *conversion = NULL; double * dRealValues = NULL; int *iRange = NULL; int haveRange = 0; csvResult *result = NULL; CheckRhs(1, 5); CheckLhs(1, 1); if (Rhs == 5) { int m5 = 0, n5 = 0; iRange = csv_getArgumentAsMatrixofIntFromDouble(pvApiCtx, 5, fname, &m5, &n5, &iErr); if (iErr) { return 0; } if ((m5 * n5 != SIZE_RANGE_SUPPORTED) ) { if (iRange) { FREE(iRange); iRange = NULL; } Scierror(999, _("%s: Wrong size for input argument #%d: Four entries expected.\n"), fname, 5); return 0; } if ((m5 != 1) && (n5 != 1)) { if (iRange) { FREE(iRange); iRange = NULL; } Scierror(999, _("%s: Wrong size for input argument #%d: A column or row vector expected.\n"), fname, 5); return 0; } if (isValidRange(iRange, m5 * n5)) { haveRange = 1; } else { if (iRange) { FREE(iRange); iRange = NULL; } Scierror(999, _("%s: Wrong value for input argument #%d: Inconsistent range.\n"), fname, 5); return 0; } } if (Rhs >= 4) { conversion = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 4, fname, getCsvDefaultConversion(), &iErr); if (iErr) { if (iRange) { FREE(iRange); iRange = NULL; } return 0; } if (!((strcmp(conversion, CONVTOSTR) == 0) || (strcmp(conversion, CONVTODOUBLE) == 0))) { if (iRange) { FREE(iRange); iRange = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' string expected.\n"), fname, 4, "double", "string"); return 0; } } else { conversion = strdup(getCsvDefaultConversion()); } if (Rhs >= 3) { decimal = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 3, fname, getCsvDefaultDecimal(), &iErr); if (iErr) { if (iRange) { FREE(iRange); iRange = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } return 0; } if (decimal[0] != '.' && decimal[0] != ',') { if (iRange) { FREE(iRange); iRange = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' string expected.\n"), fname, 3, ",", "."); return 0; } } else { decimal = strdup(getCsvDefaultDecimal()); } if (Rhs >= 2) { separator = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 2, fname, getCsvDefaultSeparator(), &iErr); if (iErr) { if (iRange) { FREE(iRange); iRange = NULL; } if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } return 0; } } else { separator = strdup(getCsvDefaultSeparator()); } if (!csv_isRowVector(pvApiCtx, 1) && !csv_isColumnVector(pvApiCtx, 1) && !csv_isScalar(pvApiCtx, 1)) { if (iRange) { FREE(iRange); iRange = NULL; } if (separator) { FREE(separator); separator = NULL; } if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } Scierror(999, _("%s: Wrong size for input argument #%d: Vector string expected.\n"), fname, 1); return 0; } text = csv_getArgumentAsMatrixOfString(pvApiCtx, 1, fname, &m1, &n1, &iErr); if (iErr) { if (iRange) { FREE(iRange); iRange = NULL; } if (separator) { FREE(separator); separator = NULL; } if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } return 0; } nbLines = m1 * n1; result = csvTextScan((const char**)text, nbLines, separator, decimal); if (text) { if (separator) { FREE(separator); separator = NULL; } freeArrayOfString(text, nbLines); text = NULL; } if (separator) { FREE(separator); separator = NULL; } if (result) { switch (result->err) { case CSV_READ_SEPARATOR_DECIMAL_EQUAL: { Scierror(999, _("%s: separator and decimal must have different values.\n"), fname); } break; case CSV_READ_NO_ERROR: { if (strcmp(conversion, CONVTOSTR) == 0) { if (haveRange) { int newM = 0; int newN = 0; char **pStrRange = getRangeAsString((const char**)result->pstrValues, result->m, result->n, iRange, &newM, &newN); if (pStrRange) { sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, newM, newN, pStrRange); freeArrayOfString(pStrRange, newM * newN); } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } } else { sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, result->m, result->n, result->pstrValues); } } else /* to double */ { stringToComplexError ierr = STRINGTOCOMPLEX_ERROR; csv_complexArray *ptrCsvComplexArray = stringsToCsvComplexArray((const char**)result->pstrValues, result->m * result->n, decimal, TRUE, &ierr); if (ptrCsvComplexArray == NULL) { freeCsvResult(result); if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } if (iRange) { FREE(iRange); iRange = NULL; } if (ierr == STRINGTOCOMPLEX_ERROR) { Scierror(999, _("%s: can not convert data.\n"), fname); } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } return 0; } switch (ierr) { case STRINGTOCOMPLEX_NOT_A_NUMBER: case STRINGTOCOMPLEX_NO_ERROR: { if (haveRange) { int newM = 0; int newN = 0; csv_complexArray *csvComplexRange = getRangeAsCsvComplexArray(ptrCsvComplexArray, result->m, result->n, iRange, &newM, &newN); if (csvComplexRange) { if (csvComplexRange->isComplex) { sciErr = createComplexMatrixOfDouble(pvApiCtx, Rhs + 1, newM, newN, ptrCsvComplexArray->realPart, ptrCsvComplexArray->imagPart); } else { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, newM, newN, csvComplexRange->realPart); } freeCsvComplexArray(csvComplexRange); csvComplexRange = NULL; } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } } else { if (ptrCsvComplexArray->isComplex) { sciErr = createComplexMatrixOfDouble(pvApiCtx, Rhs + 1, result->m, result->n, ptrCsvComplexArray->realPart, ptrCsvComplexArray->imagPart); } else { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, result->m, result->n, ptrCsvComplexArray->realPart); } } freeCsvComplexArray(ptrCsvComplexArray); ptrCsvComplexArray = NULL; } break; case STRINGTOCOMPLEX_MEMORY_ALLOCATION: { Scierror(999, _("%s: Memory allocation error.\n"), fname); } default: case STRINGTOCOMPLEX_ERROR: { Scierror(999, _("%s: can not convert data.\n"), fname); } } } if (sciErr.iErr) { freeCsvResult(result); if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } if (iRange) { FREE(iRange); iRange = NULL; } printError(&sciErr, 0); Scierror(17, _("%s: Memory allocation error.\n"), fname); return 0; } else { LhsVar(1) = Rhs + 1; PutLhsVar(); } } break; case CSV_READ_MEMORY_ALLOCATION: { Scierror(999, _("%s: Memory allocation error.\n"), fname); } break; case CSV_READ_COLUMNS_ERROR: { Scierror(999, _("%s: can not read text: Error in the column structure\n"), fname); } break; case CSV_READ_READLINES_ERROR: case CSV_READ_ERROR: { Scierror(999, _("%s: can not read text.\n"), fname); } break; } } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } freeCsvResult(result); if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } if (iRange) { FREE(iRange); iRange = NULL; } return 0; }
// ============================================================================= csvResult* csvRead(const char *filename, const char *separator, const char *decimal, const char **toreplace, int sizetoreplace, const char *regexpcomments, int header) { wchar_t *expandedFilename = NULL; wchar_t *wideFilename = NULL; csvResult *result = NULL; int fd = 0; int f_swap = 0; double res = 0.0; int errMOPEN = MOPEN_INVALID_STATUS; int errMGETL = MGETL_ERROR; wchar_t **pwstLines = NULL; char **pstLines = NULL; int nblines = 0; char **replacedInLines = NULL; char **pComments = NULL; int nbComments = 0; if ((filename == NULL) || (separator == NULL) || (decimal == NULL)) { return NULL; } wideFilename = to_wide_string((char*)filename); expandedFilename = expandPathVariableW(wideFilename); FREE(wideFilename); if (!FileExistW(expandedFilename)) { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_FILE_NOT_EXIST; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } FREE(expandedFilename); return result; } errMOPEN = mopen(expandedFilename, L"rt", f_swap, &fd); // rt = read only if (expandedFilename) { FREE(expandedFilename); expandedFilename = NULL; } if (errMOPEN != MOPEN_NO_ERROR) { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_MOPEN_ERROR; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } return result; } if (header != 0) { mgetl(fd, header, &nblines, &errMGETL); } pwstLines = mgetl(fd, -1, &nblines, &errMGETL); pstLines = (char**)MALLOC(sizeof(char*) * nblines); { int i = 0; for (i = 0 ; i < nblines ; i++) { pstLines[i] = wide_string_to_UTF8(pwstLines[i]); } } mclose(fd); if (errMGETL != MGETL_NO_ERROR) { if (pwstLines) { freeArrayOfWideString(pwstLines, nblines); pwstLines = NULL; } result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_READLINES_ERROR; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } return result; } if (regexpcomments) { int iErr = 0; pComments = extractComments((const char**)pstLines, nblines, regexpcomments, &nbComments, &iErr); if ((iErr == CAN_NOT_COMPILE_PATTERN) || (iErr == DELIMITER_NOT_ALPHANUMERIC)) { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { if ((iErr == CAN_NOT_COMPILE_PATTERN) || (iErr == DELIMITER_NOT_ALPHANUMERIC)) { iErr = CSV_READ_REGEXP_ERROR; } result->err = (csvReadError)iErr; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } return result; } if (pComments) { char **pCleanedLines = NULL; int nbCleanedLines = 0; int i = 0; pCleanedLines = removeComments((const char**)pstLines, nblines, (const char*)regexpcomments, &nbCleanedLines, &iErr); if (pCleanedLines) { if (pwstLines) { freeArrayOfWideString(pwstLines, nblines); pwstLines = NULL; } FREE(pstLines); pstLines = pCleanedLines; nblines = nbCleanedLines; } } } if (toreplace && (sizetoreplace > 0)) { replacedInLines = replaceStrings((const char**)pstLines, nblines, toreplace, sizetoreplace); if (replacedInLines) { freeArrayOfString(pstLines, nblines); pstLines = replacedInLines; } } result = csvTextScan((const char**)pstLines, nblines, (const char*)separator, (const char*)decimal); freeArrayOfString(pstLines, nblines); freeArrayOfWideString(pwstLines, nblines); if (result) { result->pstrComments = pComments; result->nbComments = nbComments; } else { freeArrayOfString(pComments, nbComments); } return result; }
// ============================================================================= csvResult* csvRead(const char *filename, const char *separator, const char *decimal, const char **toreplace, int sizetoreplace, const char *regexpcomments) { char *expandedFilename = NULL; csvResult *result = NULL; int fd = 0; int f_swap = 0; double res = 0.0; int errMOPEN = MOPEN_INVALID_STATUS; int errMGETL = MGETL_ERROR; double dErrClose = 0.; char **lines = NULL; int nblines = 0; char **replacedInLines = NULL; char **pComments = NULL; int nbComments = 0; if ((filename == NULL) || (separator == NULL) || (decimal == NULL)) { return NULL; } expandedFilename = expandPathVariable((char*)filename); if (!FileExist(expandedFilename)) { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_FILE_NOT_EXIST; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } if (expandedFilename) { FREE(expandedFilename); expandedFilename = NULL; } return result; } C2F(mopen)(&fd, expandedFilename, (char*)READ_ONLY_TEXT_MODE, &f_swap, &res, &errMOPEN); if (expandedFilename) { FREE(expandedFilename); expandedFilename = NULL; } if (errMOPEN != MOPEN_NO_ERROR) { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_MOPEN_ERROR; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } return result; } lines = mgetl(fd, -1, &nblines, &errMGETL); C2F(mclose)(&fd, &dErrClose); if (errMGETL != MGETL_NO_ERROR) { if (lines) { freeArrayOfString(lines, nblines); lines = NULL; } result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { result->err = CSV_READ_READLINES_ERROR; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } return result; } if (regexpcomments) { int iErr = 0; pComments = extractComments((const char**)lines, nblines, (const char*)regexpcomments, &nbComments, &iErr); if ((iErr == CAN_NOT_COMPILE_PATTERN) || (iErr == DELIMITER_NOT_ALPHANUMERIC)) { result = (csvResult*)(MALLOC(sizeof(csvResult))); if (result) { if ((iErr == CAN_NOT_COMPILE_PATTERN) || (iErr == DELIMITER_NOT_ALPHANUMERIC)) { iErr = CSV_READ_REGEXP_ERROR; } result->err = (csvReadError)iErr; result->m = 0; result->n = 0; result->pstrValues = NULL; result->pstrComments = NULL; result->nbComments = 0; } return result; } if (pComments) { char **pCleanedLines = NULL; int nbCleanedLines = 0; int i = 0; pCleanedLines = removeComments((const char**)lines, nblines, (const char*)regexpcomments, &nbCleanedLines, &iErr); if (pCleanedLines) { FREE(lines); lines = pCleanedLines; nblines = nbCleanedLines; } } } if (toreplace && (sizetoreplace > 0)) { replacedInLines = replaceStrings((const char**)lines, nblines, toreplace, sizetoreplace); if (replacedInLines) { freeArrayOfString(lines, nblines); lines = replacedInLines; } } result = csvTextScan((const char**)lines, nblines, (const char*)separator, (const char*)decimal); if (lines) { freeArrayOfString(lines, nblines); lines = NULL; } if (result) { result->pstrComments = pComments; result->nbComments = nbComments; } return result; }