예제 #1
0
// =============================================================================
static int sci_csvDefault_no_rhs(char *fname, void* pvApiCtx)
{
    int sizeArray = NUMBER_FIELD * 2;
    char **arrayOut = (char**)MALLOC(sizeof(char*) * sizeArray);

    if (arrayOut)
    {
        SciErr sciErr;

        int nbRows = NUMBER_FIELD;
        int nbCols = 2;
        const char *currentEol = getCsvDefaultEOL();

        arrayOut[0] = os_strdup(SEPARATOR_FIELDNAME);
        arrayOut[1] = os_strdup(DECIMAL_FIELDNAME);
        arrayOut[2] = os_strdup(CONVERSION_FIELDNAME);
        arrayOut[3] = os_strdup(PRECISION_FIELDNAME);
        arrayOut[4] = os_strdup(COMMENTSREGEXP_FIELDNAME);
        arrayOut[5] = os_strdup(EOL_FIELDNAME);
        arrayOut[6] = os_strdup(ENCODING_FIELDNAME);
        arrayOut[7] = os_strdup(BLANK_FIELDNAME);

        arrayOut[8] = os_strdup(getCsvDefaultSeparator());
        arrayOut[9] = os_strdup(getCsvDefaultDecimal());
        arrayOut[10] = os_strdup(getCsvDefaultConversion());
        arrayOut[11] = os_strdup(getCsvDefaultPrecision());
        arrayOut[12] = os_strdup(getCsvDefaultCommentsRegExp());

        if (currentEol)
        {
            if (strcmp(currentEol, MACOS9_EOL) == 0)
            {
                arrayOut[13] = os_strdup(MACOS9_EOL_STRING);
            }
            else if (strcmp(currentEol, WINDOWS_EOL) == 0)
            {
                arrayOut[13] = os_strdup(WINDOWS_EOL_STRING);
            }
            else if (strcmp(currentEol, LINUX_EOL) == 0)
            {
                arrayOut[13] = os_strdup(LINUX_EOL_STRING);
            }
            else
            {
                arrayOut[13] = os_strdup("ERROR");
            }
        }
        else
        {
            arrayOut[13] = os_strdup("ERROR");
        }

        arrayOut[14] = os_strdup(getCsvDefaultEncoding());
        arrayOut[15] = os_strdup(getCsvDefaultCsvIgnoreBlankLine());

        sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, nbRows, nbCols, arrayOut);
        freeArrayOfString(arrayOut, sizeArray);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }

        LhsVar(1) = Rhs + 1;
        PutLhsVar();
    }
    else
    {
        Scierror(999, _("%s: Memory allocation error.\n"), fname);
    }

    return 0;
}
예제 #2
0
파일: csvRead.c 프로젝트: scitao/scilab
// =============================================================================
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;
}
예제 #3
0
// =============================================================================
static int sci_csvDefault_one_rhs(char *fname, void* pvApiCtx)
{
    int iErr = 0;

    char *fieldname = NULL;
    char *fieldvalue = NULL;

    fieldname = csv_getArgumentAsString(pvApiCtx, 1, fname, &iErr);
    if (iErr)
    {
        freeVar(&fieldname, &fieldvalue);
        return 0;
    }

    if (strcmp(fieldname, SEPARATOR_FIELDNAME) == 0)
    {
        fieldvalue = os_strdup(getCsvDefaultSeparator());
    }
    else if (strcmp(fieldname, DECIMAL_FIELDNAME) == 0)
    {
        fieldvalue = os_strdup(getCsvDefaultDecimal());
    }
    else if (strcmp(fieldname, CONVERSION_FIELDNAME) == 0)
    {
        fieldvalue = os_strdup(getCsvDefaultConversion());
    }
    else if (strcmp(fieldname, PRECISION_FIELDNAME) == 0)
    {
        fieldvalue = os_strdup(getCsvDefaultPrecision());
    }
    else if (strcmp(fieldname, COMMENTSREGEXP_FIELDNAME) == 0)
    {
        fieldvalue = os_strdup(getCsvDefaultCommentsRegExp());
    }
    else if (strcmp(fieldname, EOL_FIELDNAME) == 0)
    {
        const char *currentEol = getCsvDefaultEOL();
        if (currentEol)
        {
            if (strcmp(currentEol, MACOS9_EOL) == 0)
            {
                fieldvalue = os_strdup(MACOS9_EOL_STRING);
            }
            else if (strcmp(currentEol, WINDOWS_EOL) == 0)
            {
                fieldvalue = os_strdup(WINDOWS_EOL_STRING);
            }
            else if (strcmp(currentEol, LINUX_EOL) == 0)
            {
                fieldvalue = os_strdup(LINUX_EOL_STRING);
            }
            else
            {
                fieldvalue = os_strdup("ERROR");
            }
        }
        else
        {
            fieldvalue = os_strdup("ERROR");
        }
    }
    else if (strcmp(fieldname, ENCODING_FIELDNAME) == 0)
    {
        fieldvalue = os_strdup(getCsvDefaultEncoding());
    }
    else if (strcmp(fieldname, BLANK_FIELDNAME) == 0)
    {
        fieldvalue = os_strdup(getCsvDefaultCsvIgnoreBlankLine());
    }
    else if (strcmp(fieldname, RESET_PARAMATERS) == 0)
    {
        freeVar(&fieldname, &fieldvalue);

        setCsvDefaultReset();

        createEmptyMatrix(pvApiCtx, Rhs + 1);

        LhsVar(1) = Rhs + 1;
        PutLhsVar();
        return 0;
    }
    else
    {
        Scierror(999, _("%s: Wrong value for input argument #%d: '%s', '%s', '%s', '%s', '%s' or '%s' expected.\n"), fname, 1, SEPARATOR_FIELDNAME, DECIMAL_FIELDNAME, CONVERSION_FIELDNAME, COMMENTSREGEXP_FIELDNAME, EOL_FIELDNAME, BLANK_FIELDNAME);
        freeVar(&fieldname, &fieldvalue);
        return 0;
    }

    createSingleString(pvApiCtx, Rhs + 1, fieldvalue);

    freeVar(&fieldname, &fieldvalue);

    LhsVar(1) = Rhs + 1;
    PutLhsVar();

    return 0;
}