Beispiel #1
0
/*-----------------------------------------------------------------------------------*/
static int sci_grep_common(char *fname, BOOL new_grep)
{
    int i = 0;

    int m1 = 0, n1 = 0;
    char **Strings_Input_One = NULL;
    int m1n1 = 0;               /* m1 * n1 */

    int m2 = 0, n2 = 0;
    char **Strings_Input_Two = NULL;
    int m2n2 = 0;               /* m2 * n2 */

    GREPRESULTS grepresults;
    int code_error_grep = GREP_OK;

    GetRhsVar(1, MATRIX_OF_STRING_DATATYPE, &m1, &n1, &Strings_Input_One);
    m1n1 = m1 * n1;
    GetRhsVar(2, MATRIX_OF_STRING_DATATYPE, &m2, &n2, &Strings_Input_Two);
    m2n2 = m2 * n2;

    for (i = 0; i < m2n2; i++)
    {
        if (strlen(Strings_Input_Two[i]) == 0)
        {
            freeArrayOfString(Strings_Input_One, m1n1);
            freeArrayOfString(Strings_Input_Two, m2n2);
            Scierror(249, _("%s: Wrong values for input argument #%d: Non-empty strings expected.\n"), fname, 2);
            return 0;
        }
    }

    grepresults.currentLength = 0;
    grepresults.sizeArraysMax = 0;
    grepresults.positions = NULL;
    grepresults.values = NULL;

    if (new_grep)
    {
        code_error_grep = GREP_NEW(&grepresults, Strings_Input_One, m1n1, Strings_Input_Two, m2n2);
    }
    else
    {
        code_error_grep = GREP_OLD(&grepresults, Strings_Input_One, m1n1, Strings_Input_Two, m2n2);
    }

    freeArrayOfString(Strings_Input_One, m1n1);
    freeArrayOfString(Strings_Input_Two, m2n2);

    switch (code_error_grep)
    {
        case GREP_OK:
        {
            int x = 0;
            int numRow = 1;
            int outIndex = 0;

            CreateVar(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &numRow, &grepresults.currentLength, &outIndex);
            for (x = 0; x < grepresults.currentLength; x++)
            {
                stk(outIndex)[x] = (double)grepresults.values[x];
            }
            LhsVar(1) = Rhs + 1;
            if (Lhs == 2)
            {
                /* Output positions[] */
                numRow = 1;
                outIndex = 0;
                CreateVar(Rhs + 2, MATRIX_OF_DOUBLE_DATATYPE, &numRow, &grepresults.currentLength, &outIndex);
                for (x = 0; x < grepresults.currentLength; x++)
                {
                    stk(outIndex)[x] = (double)grepresults.positions[x];
                }
                LhsVar(2) = Rhs + 2;
            }

            if (grepresults.values)
            {
                FREE(grepresults.values);
                grepresults.values = NULL;
            }
            if (grepresults.positions)
            {
                FREE(grepresults.positions);
                grepresults.positions = NULL;
            }
            PutLhsVar();
        }
        break;

        case MEMORY_ALLOC_ERROR:
        {
            if (grepresults.values)
            {
                FREE(grepresults.values);
                grepresults.values = NULL;
            }
            if (grepresults.positions)
            {
                FREE(grepresults.positions);
                grepresults.positions = NULL;
            }
            Scierror(999, _("%s: No more memory.\n"), fname);
        }
        break;
    }
    return 0;
}
Beispiel #2
0
/*------------------------------------------------------------------------*/
types::Function::ReturnValue sci_grep(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    bool bRegularExpression = false;

    //check input paramters
    if (in.size() < 2 || in.size() > 3)
    {
        Scierror(999, _("%s: Wrong number of input arguments: %d or %d expected.\n"), "grep", 2, 3);
        return types::Function::Error;
    }

    if (in[0]->isDouble() && in[0]->getAs<types::Double>()->getSize() == 0)
    {
        types::Double *pD = types::Double::Empty();
        out.push_back(pD);
        return types::Function::OK;
    }

    if (in.size() == 3)
    {
        //"r" for regular expression
        if (in[2]->isString() == false)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), "grep", 3);
            return types::Function::Error;
        }

        types::String* pS = in[2]->getAs<types::String>();
        if (pS->getSize() != 1)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "grep", 3);
            return types::Function::Error;
        }

        if (pS->get(0)[0] == 'r')
        {
            bRegularExpression = true;
        }
    }

    if (in[0]->isString() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), "grep", 1);
        return types::Function::Error;
    }

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

    types::String* pS1 = in[0]->getAs<types::String>();
    types::String* pS2 = in[1]->getAs<types::String>();


    for (int i = 0 ; i < pS2->getSize() ; i++)
    {
        if (wcslen(pS2->get(i)) == 0)
        {
            Scierror(249, _("%s: Wrong values for input argument #%d: Non-empty strings expected.\n"), "grep", 2);
            return types::Function::Error;
        }
    }

    GREPRESULTS grepresults;
    int code_error_grep = GREP_OK;

    grepresults.currentLength = 0;
    grepresults.sizeArraysMax = 0;
    grepresults.positions = NULL;
    grepresults.values = NULL;

    char** pStr1 = (char**)MALLOC(sizeof(char*) * pS1->getSize());
    for (int i = 0 ; i < pS1->getSize() ; i++)
    {
        pStr1[i] = wide_string_to_UTF8(pS1->get(i));
    }

    char** pStr2 = (char**)MALLOC(sizeof(char*) * pS2->getSize());
    for (int i = 0 ; i < pS2->getSize() ; i++)
    {
        pStr2[i] = wide_string_to_UTF8(pS2->get(i));
    }

    if (bRegularExpression)
    {
        code_error_grep = GREP_NEW(&grepresults, pStr1, pS1->getSize(), pStr2, pS2->getSize());
    }
    else
    {
        code_error_grep = GREP_OLD(&grepresults, pStr1, pS1->getSize(), pStr2, pS2->getSize());
    }

    for (int i = 0; i < pS1->getSize(); i++)
    {
        FREE(pStr1[i]);
    }
    FREE(pStr1);

    for (int i = 0; i < pS2->getSize(); i++)
    {
        FREE(pStr2[i]);
    }
    FREE(pStr2);

    switch (code_error_grep)
    {
        case GREP_OK :
        {
            types::Double* pD1 = NULL;
            if (grepresults.currentLength == 0)
            {
                pD1 = types::Double::Empty();
            }
            else
            {
                pD1 = new types::Double(1, grepresults.currentLength);
                double* pDbl1 = pD1->getReal();
                for (int i = 0 ; i < grepresults.currentLength ; i++ )
                {
                    pDbl1[i] = static_cast<double>(grepresults.values[i]);
                }
            }

            out.push_back(pD1);

            if (_iRetCount == 2)
            {
                types::Double* pD2 = NULL;
                if (grepresults.currentLength == 0)
                {
                    pD2 = types::Double::Empty();
                }
                else
                {
                    pD2 = new types::Double(1, grepresults.currentLength);
                    double* pDbl2 = pD2->getReal();
                    for (int i = 0 ; i < grepresults.currentLength ; i++ )
                    {
                        pDbl2[i] = static_cast<double>(grepresults.positions[i]);
                    }
                }

                out.push_back(pD2);
            }

            if (grepresults.values)
            {
                FREE(grepresults.values);
                grepresults.values = NULL;
            }
            if (grepresults.positions)
            {
                FREE(grepresults.positions);
                grepresults.positions = NULL;
            }
        }
        break;

        case MEMORY_ALLOC_ERROR :
            Scierror(999, _("%s: No more memory.\n"), "grep");
        //no break, to free reserved memory.
        case GREP_ERROR :
        {
            if (grepresults.values)
            {
                FREE(grepresults.values);
                grepresults.values = NULL;
            }
            if (grepresults.positions)
            {
                FREE(grepresults.positions);
                grepresults.positions = NULL;
            }
            return types::Function::Error;
        }
        break;
    }

    return types::Function::OK;
}