예제 #1
0
/*--------------------------------------------------------------------------*/
int sci_xls_read(char *fname, unsigned long fname_len)
{
    int m1 = 0, n1 = 0, l1 = 0, zero = 0, ierr = 0;
    double *data = NULL;
    int *ind = NULL;
    int M = 0, N = 0, MN = 0;
    int pos = 0, fd = 0;

    CheckLhs(2, 2);
    CheckRhs(2, 2);

    if (VarType(1) != sci_matrix)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: Scalar expected.\n"), fname, 1);
        return 0;
    }

    if (VarType(2) != sci_matrix)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: Scalar expected.\n"), fname, 2);
        return 0;
    }


    /*  checking variable fd */
    GetRhsVar(1, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &l1);
    fd = (int) * stk(l1);
    /*  checking variable Pos */
    GetRhsVar(2, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &l1);
    pos = (int) * stk(l1);

    xls_read(&fd, &pos, &data, &ind, &N, &M,  &ierr);

    switch (ierr)
    {
        case 1 :
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;
            break;
        case 2 :
            Scierror(999, _("%s: Failed to read expected data, may be invalid xls file.\n"), fname);
            return 0;
            break;
        case 3 :
            Scierror(999, _("%s: End of file.\n"), fname);
            return 0;
            break;
        default :
            /* no error */
            break;
    }

    MN = M * N;

    if (MN == 0)
    {
        CreateVar(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &zero, &zero, &l1);
        CreateVar(Rhs + 2, MATRIX_OF_DOUBLE_DATATYPE, &zero, &zero, &l1);
    }
    else
    {
        CreateVarFromPtr(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &N, &M, &data);
        CreateVarFromPtr(Rhs + 2, MATRIX_OF_INTEGER_DATATYPE, &N, &M, &ind);
        FREE(data);
        data = NULL;
        FREE(ind);
        ind = NULL;
    }
    LhsVar(1) = Rhs + 1;
    LhsVar(2) = Rhs + 2;
    PutLhsVar();
    return 0;
}
예제 #2
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_xls_read(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    // input
    types::Double* pDblId       = NULL;
    types::Double* pDblSheetpos = NULL;

    int iId         = 0;
    int iSheetpos   = 0;
    int rows        = 0;
    int cols        = 0;
    int iErr        = 0;

    int* ind     = NULL;
    double* data = NULL;

    // *** check the minimal number of input args. ***
    if (in.size() != 2)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "xls_read", 2);
        return types::Function::Error;
    }

    // *** check number of output args according the methode. ***
    if (_iRetCount != 2)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "xls_read", 2);
        return types::Function::Error;
    }

    // *** check type of input args and get it. ***
    // file id
    if (in[0]->isDouble() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d : A scalar expected.\n"), "xls_read", 1);
        return types::Function::Error;
    }

    pDblId = in[0]->getAs<types::Double>();

    if (pDblId->isScalar() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d : A scalar expected.\n"), "xls_read", 1);
        return types::Function::Error;
    }

    iId = static_cast<int>(pDblId->get(0));

    // sheetpos
    if (in[1]->isDouble() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d : A scalar expected.\n"), "xls_read", 2);
        return types::Function::Error;
    }

    pDblSheetpos = in[1]->getAs<types::Double>();

    if (pDblSheetpos->isScalar() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d : A scalar expected.\n"), "xls_read", 2);
        return types::Function::Error;
    }

    iSheetpos = static_cast<int>(pDblSheetpos->get(0));

    xls_read(&iId, &iSheetpos, &data, &ind, &rows, &cols, &iErr);
    switch (iErr)
    {
        case 1 :
            Scierror(999, _("%s: No more memory.\n"), "xls_read");
            return types::Function::Error;
        case 2 :
            Scierror(999, _("%s: Failed to read expected data, may be invalid xls file.\n"), "xls_read");
            return types::Function::Error;
        case 3 :
            Scierror(999, _("%s: End of file.\n"), "xls_read");
            return types::Function::Error;
        default :
            /* no error */
            break;
    }

    if (rows * cols)
    {
        types::Double* pDblData = new types::Double(rows, cols);
        pDblData->set(data);
        types::Double* pDblInd = new types::Double(rows, cols);

        for (int i = 0; i < cols; i++)
        {
            for (int j = 0; j < rows; j++)
            {
                pDblInd->set(j, i, static_cast<int>(ind[i * rows + j]));
            }
        }

        out.push_back(pDblData);
        out.push_back(pDblInd);

        free(data);
        free(ind);
    }
    else
    {
        out.push_back(types::Double::Empty());
        out.push_back(types::Double::Empty());
    }

    return types::Function::OK;
}