Beispiel #1
0
/*-----------------------------------------------------------------------------------*/
static int GREP_NEW(GREPRESULTS * results, char **Inputs_param_one, int mn_one, char **Inputs_param_two, int mn_two)
{
    int x = 0, y = 0;
    char *save = NULL;
    pcre_error_code answer = PCRE_FINISHED_OK;

    for (x = 0; x < mn_one; x++)
    {
        results->sizeArraysMax = results->sizeArraysMax + (int)strlen(Inputs_param_one[x]);
    }

    results->values = (int *)MALLOC(sizeof(int) * (3 * results->sizeArraysMax + 1));
    results->positions = (int *)MALLOC(sizeof(int) * (3 * results->sizeArraysMax + 1));

    if ((results->values == NULL) || (results->positions == NULL))
    {
        if (results->values)
        {
            FREE(results->values);
            results->values = NULL;
        }
        if (results->positions)
        {
            FREE(results->positions);
            results->positions = NULL;
        }
        return MEMORY_ALLOC_ERROR;
    }

    results->currentLength = 0;
    for (y = 0; y < mn_one; ++y)
    {
        for (x = 0; x < mn_two; ++x)
        {
            int Output_Start = 0;
            int Output_End = 0;

            save = strdup(Inputs_param_two[x]);
            answer = pcre_private(Inputs_param_one[y], save, &Output_Start, &Output_End, NULL, NULL);

            if (answer == PCRE_FINISHED_OK)
            {
                if (results->currentLength < results->sizeArraysMax)
                {
                    results->values[results->currentLength] = y + 1;
                    results->positions[results->currentLength] = x + 1;
                    results->currentLength++;
                }
            }
            else
            {
                pcre_error("grep", answer);
            }
            if (save)
            {
                FREE(save);
                save = NULL;
            }
        }
    }

    if (results->currentLength > results->sizeArraysMax)
    {
        results->currentLength = results->sizeArraysMax;
    }

    return GREP_OK;
}
Beispiel #2
0
/*------------------------------------------------------------------------*/
int sci_regexp(char *fname,unsigned long fname_len)
{
	char typ = CHAR_S;
	char **Str = NULL;
	char **Str2 = NULL;
	int i = 0; /* loop indice */

	int mn = 0; /* dimension parameter 1 m*n */

	int mn2 = 0; /* m2*n2 */

	int outIndex = 0;
	int numRow = 1;

	int *values_start = NULL;
	int *values_end = NULL;

	int *wcvalues_start = NULL;
	int *wcvalues_end = NULL;


	int nbValues_start = 0;
	int nbValues_end=0;
	int nbposition = 0;

	CheckRhs(2,3);
	CheckLhs(1,3);

	if (VarType(1) == sci_strings)
	{
		int m1 = 0;
		int n1 = 0;

		GetRhsVar(1,MATRIX_OF_STRING_DATATYPE,&m1,&n1,&Str);
		mn = m1*n1;
	}
	else
	{
		Scierror(999, _("%s: Wrong type for input argument #%d: Single string expected.\n"), fname,1);
		return 0;
	}

	if (mn != 1)
	{
		freeArrayOfString(Str,mn);
		Scierror(36, _("%s: Wrong size for input argument #%d: Single string expected.\n"), fname,1);
		return 0;
	}

	if (VarType(2) == sci_strings)
	{
		int m2 = 0,n2 = 0;
		GetRhsVar(2,MATRIX_OF_STRING_DATATYPE,&m2,&n2,&Str2);
		mn2 = m2*n2;
	}
	else
	{
		freeArrayOfString(Str,mn);
		Scierror(36, _("%s: Wrong type for input argument #%d: Single string expected.\n"), fname,2);
		return 0;
	}

	if (mn2 != 1)
	{
		freeArrayOfString(Str2,mn2);
		Scierror(36, _("%s: Wrong size for input argument #%d: Single string expected.\n"), fname,2);
		return 0;
	}

	if ( (int)strlen(Str[0]) == 0 )
	{
		values_start = (int *)MALLOC(sizeof(int));
		values_end = (int *)MALLOC(sizeof(int));

		wcvalues_start = (int *)MALLOC(sizeof(int));
		wcvalues_end = (int *)MALLOC(sizeof(int));
	}
	else
	{
		values_start = (int *)MALLOC( sizeof(int) * ( strlen(Str[0]) ) );
		values_end = (int *)MALLOC( sizeof(int) * ( strlen(Str[0]) ) );

		wcvalues_start = (int *)MALLOC( sizeof(int) * ( strlen(Str[0]) ) );
		wcvalues_end = (int *)MALLOC( sizeof(int) * ( strlen(Str[0]) ) );
	}

	if (Rhs == 2 )
	{
		typ = CHAR_S;
	}
	else /* Rhs == 3 */
	{
		int m3 = 0, n3 = 0, l3 = 0;

		GetRhsVar(3,STRING_DATATYPE,&m3,&n3,&l3);

		if ( m3*n3 != 0) typ = cstk(l3)[0];
		if (typ != STR_ONCE)
		{
			freeArrayOfString(Str,mn);
			freeArrayOfString(Str2,mn2);
			Scierror(999,_("%s: Wrong type for input argument #%d: '%s' expected.\n"),fname,3,"o");
			return 0;
		}
	}

	{
		int x = 0;
		pcre_error_code answer = PCRE_FINISHED_OK;

		int start_point = 0;
		int Output_Start = 0;
		int Output_End = 0;

		int wcOutput_Start = 0;
		int wcstart_point = 0;
		int wcOutput_End = 0;

		for (x = 0; x < mn2; ++x)
		{
			char *save = strdup(Str2[x]);
			if (save)
			{
				char *pointer = Str[0];
				start_point = 0;
				wcstart_point = 0;
				do
				{
					strcpy(save,Str2[x]);
					Output_Start = 0;
					Output_End = 0;

					answer = pcre_private(pointer,save,&Output_Start,&Output_End);
					if ( answer == PCRE_FINISHED_OK )
					{
						/* Start = End means that we matched a position and 0 characters.
						* Matching 0 characters, for us, means no match.
						*/
						if (Output_Start != Output_End)
						{
							char *	strOutput_Start = strdup(pointer);
							char *  strOutput_End =  strdup(pointer);

							wchar_t *wcstrOutput_Start = NULL;
							wchar_t *wcstrOutput_End = NULL;

							/* calculates positions with wide characters */
							strOutput_Start[Output_Start] = '\0';
							strOutput_End[Output_End] = '\0';

							wcstrOutput_Start = to_wide_string(strOutput_Start);
							wcstrOutput_End = to_wide_string(strOutput_End);

							if (wcstrOutput_Start)
							{
								wcOutput_Start = (int)wcslen(wcstrOutput_Start);
								FREE(wcstrOutput_Start);wcstrOutput_Start = NULL;
							}
							else
							{
								wcOutput_Start = 0;
							}

							if (wcstrOutput_End)
							{
								wcOutput_End = (int)wcslen(wcstrOutput_End);
								FREE(wcstrOutput_End);wcstrOutput_End = NULL;
							}
							else
							{
								wcOutput_End = 0;
							}

							if (strOutput_Start) {FREE(strOutput_Start); strOutput_Start = NULL;}
							if (strOutput_End) {FREE(strOutput_End); strOutput_End = NULL;}

							/*adding the answer into the outputmatrix*/
							values_start[nbValues_start] = Output_Start + start_point + 1;
							values_end[nbValues_end] = Output_End + start_point;

							wcvalues_start[nbValues_start] = wcOutput_Start + wcstart_point + 1;
							wcvalues_end[nbValues_end] = wcOutput_End + wcstart_point;

							nbValues_start++;
							nbValues_end++;

							/*The number according to the str2 matrix*/
							nbposition++;
						}
						else if(Output_End == 0 && *pointer != '\0')
						{
							/* Avoid an infinite loop */
							pointer++;
						}

						pointer = &pointer[Output_End];
						start_point = start_point + Output_End ;
						wcstart_point = wcstart_point + wcOutput_End ;
					}
					else
					{
						if (answer != NO_MATCH)
						{
							pcre_error(fname,answer);
							freeArrayOfString(Str,mn);
							freeArrayOfString(Str2,mn2);
							return 0;
						}
					}
				}
				while( (answer == PCRE_FINISHED_OK) && (*pointer != '\0') &&  (typ != STR_ONCE) );

				if (save) {FREE(save);save=NULL;}
			}
			else
			{
				freeArrayOfString(Str,mn);
				freeArrayOfString(Str2,mn2);
				Scierror(999, _("%s: No more memory.\n"),fname);
				return 0;
			}
		}
	}

	numRow   = 1; /* Output values[] */
	outIndex = 0;
	CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,&numRow,&nbValues_start,&outIndex);
	for ( i = 0 ; i < nbValues_start ; i++ )
	{
		stk(outIndex)[i] = (double)wcvalues_start[i] ;
	}
	LhsVar(1) = Rhs+1 ;

	if (Lhs >= 2)
	{
		numRow   = 1;
		outIndex = 0;
		CreateVar(Rhs+2,MATRIX_OF_DOUBLE_DATATYPE,&numRow,&nbValues_end,&outIndex);
		for ( i = 0 ; i < nbposition ; i++ )
		{
			stk(outIndex)[i] = (double)wcvalues_end[i] ;
		}
		LhsVar(2) = Rhs+2;
	}

	if (Lhs == 3)
	{
		if (nbValues_start != 0)
		{
			char **match = (char**)MALLOC(sizeof(char*)*(nbValues_start));

			if (match == NULL)
			{
				freeArrayOfString(Str, mn);
				freeArrayOfString(Str2, mn2);

				Scierror(999, _("%s: No more memory.\n"),fname);
				return 0;
			}

			for( i = 0; i < nbValues_start; i++)
			{
				int len = values_end[i] - values_start[i] + 1;
				match[i] = (char*)MALLOC(sizeof(char)*(len + 1));
				strncpy(match[i], Str[0] + values_start[i] - 1, len);

				/* A char* always finished by \0 */
				match[i][len] = '\0';
			}

			numRow =  nbValues_start;
			outIndex = 1 ;
			CreateVarFromPtr(Rhs + 3,MATRIX_OF_STRING_DATATYPE, &numRow, &outIndex, match );
			LhsVar(3) = Rhs + 3 ;
			freeArrayOfString(match,nbValues_start);
		}
		else
		{
			int m3 = 0, n3 = 0, l3 = 0;
			CreateVar(Rhs+3,STRING_DATATYPE,  &m3, &n3, &l3);
			LhsVar(3) = Rhs + 3 ;
		}
	}

	freeArrayOfString(Str, mn);
	freeArrayOfString(Str2, mn2);
	if (values_start) {FREE(values_start); values_start = NULL;}
	if (values_end) {FREE(values_end); values_end = NULL;}
	if (wcvalues_start) {FREE(wcvalues_start); wcvalues_start = NULL;}
	if (wcvalues_end) {FREE(wcvalues_end); wcvalues_end = NULL;}

	PutLhsVar();
	return 0;
}
/*------------------------------------------------------------------------*/
types::Function::ReturnValue sci_strindex(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    bool bRegExp = false;
    if (in.size() < 2 || in.size() > 3)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "strindex", 2, 3);
        return types::Function::Error;
    }

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

        if (in[2]->getAs<types::String>()->get(0)[0] == WCHAR_R)
        {
            bRegExp = true;
        }
        else if (in[2]->getAs<types::String>()->get(0)[0] == WCHAR_S)
        {
            bRegExp = false;
        }
        else
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: 's' or 'r' expected.\n"), "strindex", 3);
            return types::Function::Error;
        }
    }

    if (in[1]->isString() == false || (in[1]->getAs<types::String>()->getRows() != 1 && in[1]->getAs<types::String>()->getCols() != 1))
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: string or string vector expected.\n"), "strindex", 2);
        return types::Function::Error;
    }

    types::String* pS = in[1]->getAs<types::String>();
    wchar_t** pwstSearch = pS->get();

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

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

    wchar_t* pwstData = in[0]->getAs<types::String>()->get()[0];
    if (wcslen(pwstData) == 0)
    {
        out.push_back(types::Double::Empty());
        if (_iRetCount == 2)
        {
            out.push_back(types::Double::Empty());
        }

        return types::Function::OK;
    }

    In* pstrResult = new In[wcslen(pwstData)];

    //number of occurances
    int iValues = 0;
    if (bRegExp)
    {
        //pcre
        pcre_error_code iPcreStatus = PCRE_FINISHED_OK;
        for (int i = 0 ; i < pS->getSize() ; i++)
        {
            int iStart      = 0;
            int iEnd        = 0;
            int iStep       = 0;

            do
            {
                iPcreStatus = wide_pcre_private(pwstData + iStep, pwstSearch[i], &iStart, &iEnd, NULL, NULL);
                if (iPcreStatus == PCRE_FINISHED_OK)
                {
                    pstrResult[iValues].data        = iStart + iStep + 1;
                    pstrResult[iValues].position    = i + 1;
                    iStep                           += iEnd;
                    iValues++;
                }
                else
                {
                    if (iPcreStatus != NO_MATCH)
                    {
                        pcre_error("strindex", iPcreStatus);
                        delete[] pstrResult;
                        return types::Function::Error;
                    }
                    break;
                }
            }
            while (iPcreStatus == PCRE_FINISHED_OK && iStart != iEnd);
        }
    }
    else
    {
        for (int i = 0 ; i < pS->getSize() ; i++)
        {
            wchar_t* pCur = pwstData;
            do
            {
                pCur = wcsstr(pCur, pwstSearch[i]);
                if (pCur != NULL)
                {
                    pstrResult[iValues].data      = (int)(pCur - pwstData + 1);
                    pstrResult[iValues].position  = i + 1;
                    pCur++;
                    iValues++;
                }
            }
            while (pCur != NULL && pCur[0] != L'\0');
        }
    }

    qsort(pstrResult, iValues, sizeof(In), ComparaisonCallback);

    types::Double* pIndex = NULL;
    if (iValues == 0)
    {
        pIndex = types::Double::Empty();
    }
    else
    {
        pIndex = new types::Double(1, iValues);
        for (int i = 0 ; i < iValues ; i++)
        {
            pIndex->set(0, i, pstrResult[i].data);
        }
    }
    out.push_back(pIndex);

    if (_iRetCount == 2)
    {
        types::Double* pPos = NULL;
        if (iValues == 0)
        {
            pPos = types::Double::Empty();
        }
        else
        {
            pPos = new types::Double(1, iValues);
            for (int i = 0 ; i < iValues ; i++)
            {
                pPos->set(0, i, pstrResult[i].position);
            }
        }
        out.push_back(pPos);
    }

    delete[] pstrResult;
    return types::Function::OK;
}
Beispiel #4
0
/*------------------------------------------------------------------------*/
int sci_strindex(char *fname, unsigned long fname_len)
{
    BOOL bStrindex_with_pattern = FALSE;
    int outIndex = 0;
    int numRow = 1;
    int *next = NULL;

    CheckRhs(2, 3);
    CheckLhs(1, 2);

    if (Rhs == 3)
    {
        int m3 = 0;
        int n3 = 0;
        char **Strings_Input3 = NULL;
        int m3n3 = 0; /* m3 * n3 */

        if (VarType(3) != sci_strings)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: Character expected.\n"), fname, 3);
            return 0;
        }
        GetRhsVar(3, MATRIX_OF_STRING_DATATYPE, &m3, &n3, &Strings_Input3);
        m3n3 = m3 * n3;

        if (m3n3 != 1)
        {
            freeArrayOfString(Strings_Input3, m3n3);
            Scierror(999, _("%s: Wrong type for input argument #%d: Character expected.\n"), fname, 3);
            return 0;
        }

        if ( (strcmp(Strings_Input3[0], CHAR_R) == 0) || (strcmp(Strings_Input3[0], CHAR_S) == 0) )
        {
            if (strcmp(Strings_Input3[0], CHAR_R) == 0)
            {
                bStrindex_with_pattern = TRUE;
            }
            else
            {
                bStrindex_with_pattern = FALSE;
            }
            freeArrayOfString(Strings_Input3, m3n3);
        }
        else
        {
            freeArrayOfString(Strings_Input3, m3n3);
            Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 3, CHAR_S, CHAR_R);
            return 0;
        }
    }

    if (VarType(1) == sci_matrix)
    {
        int m1 = 0;
        int n1 = 0;
        int l1 = 0;

        GetRhsVar(1, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &l1);
        if ((m1 == 0) && (n1 == 0))
        {
            CreateVar(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &l1);
            LhsVar(1) = Rhs + 1 ;
            PutLhsVar();
            return 0;
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: Matrix of strings or empty matrix expected.\n"), fname, 1);
            return 0;
        }
    }

    if ( (VarType(1) == sci_strings) && (VarType(2) == sci_strings) )
    {
        int m1 = 0, n1 = 0;
        char **Strings_Input1 = NULL;
        wchar_t *wStrings_Input1 = NULL;
        int m1n1 = 0; /* m1 * n1 */

        int m2 = 0, n2 = 0;
        char **Strings_Input2 = NULL;
        wchar_t **wStrings_Input2 = NULL;
        int m2n2 = 0; /* m2 * n2 */

        In *values = NULL;

        int nbValues = 0;
        int nbposition = 0;

        int i = 0;

        GetRhsVar(1, MATRIX_OF_STRING_DATATYPE, &m1, &n1, &Strings_Input1);
        m1n1 = m1 * n1;

        if (m1n1 != 1)
        {
            freeArrayOfString(Strings_Input1, m1n1);
            Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
            return 0;
        }

        wStrings_Input1 = to_wide_string(Strings_Input1[0]);
        if (wStrings_Input1 == NULL)
        {
            /* string conversion fails */
            freeArrayOfString(Strings_Input1, m1n1);
            freeArrayOfString(Strings_Input2, m2n2);
            Scierror(999, _("%s: Wrong value for input argument #%d: A valid string expected (UTF-8 Encoding problem).\n"), fname, 1);
            return 0;
        }

        GetRhsVar(2, MATRIX_OF_STRING_DATATYPE, &m2, &n2, &Strings_Input2);
        m2n2 = m2 * n2;

        if ( (m2 != 1) && (n2 != 1) )
        {
            freeArrayOfString(Strings_Input1, m1n1);
            freeArrayOfString(Strings_Input2, m2n2);
            Scierror(999, _("%s: Wrong type for input argument #%d: Row vector of strings or column vector of strings expected.\n"), fname, 2);
            return 0;
        }

        wStrings_Input2 = (wchar_t**)MALLOC(m2n2 * sizeof(wchar_t*));
        for (i = 0 ; i < m2n2 ; i++)
        {
            wStrings_Input2[i] = to_wide_string(Strings_Input2[i]);
        }

        if ( (int)wcslen(wStrings_Input1) == 0 )
        {
            values = (In*)MALLOC(sizeof(In));
        }
        else
        {
            values = (In *)MALLOC( sizeof(In) * ( wcslen(wStrings_Input1) ) * m2n2);
        }

        if (bStrindex_with_pattern)
        {
            int x = 0;
            pcre_error_code answer = PCRE_FINISHED_OK;

            int Output_Start = 0;
            int Output_End = 0;

            int wcOutput_Start = 0;
            int wcstart_point = 0;
            int wcOutput_End = 0;

            for (x = 0; x < m2n2; ++x)
            {
                char *save = strdup(Strings_Input2[x]);
                if (save)
                {
                    char *pointer = Strings_Input1[0];
                    wcstart_point = 0;

                    do
                    {
                        strcpy(save, Strings_Input2[x]);
                        Output_Start = 0;
                        Output_End = 0;

                        answer = pcre_private(pointer, save, &Output_Start, &Output_End, NULL, NULL);
                        if ( answer == PCRE_FINISHED_OK )
                        {
                            /* Start = End means that we matched a position and 0 characters.
                            * Matching 0 characters, for us, means no match.
                            */
                            if (Output_Start != Output_End)
                            {
                                char *	strOutput_Start = strdup(pointer);
                                char *  strOutput_End =  strdup(pointer);

                                wchar_t *wcstrOutput_Start = NULL;
                                wchar_t *wcstrOutput_End = NULL;

                                /* calculates positions with wide characters */
                                strOutput_Start[Output_Start] = '\0';
                                strOutput_End[Output_End] = '\0';

                                wcstrOutput_Start = to_wide_string(strOutput_Start);
                                wcstrOutput_End = to_wide_string(strOutput_End);

                                if (wcstrOutput_Start)
                                {
                                    wcOutput_Start = (int)wcslen(wcstrOutput_Start);
                                    FREE(wcstrOutput_Start);
                                }
                                else
                                {
                                    wcOutput_Start = 0;
                                }

                                if (wcstrOutput_End)
                                {
                                    wcOutput_End = (int)wcslen(wcstrOutput_End);
                                    FREE(wcstrOutput_End);
                                }
                                else
                                {
                                    wcOutput_End = 0;
                                }

                                FREE(strOutput_Start);
                                FREE(strOutput_End);

                                /*adding the answer into the outputmatrix*/
                                values[nbValues].data = wcOutput_Start + wcstart_point + 1;
                                values[nbValues].position = x + 1;
                                nbValues++;
                            }
                            else if (Output_End == 0 && *pointer != '\0')
                            {
                                /* Avoid an infinite loop */
                                pointer++;
                            }

                            pointer = &pointer[Output_End];
                            wcstart_point = wcstart_point + wcOutput_End;
                        }
                        else
                        {
                            if (answer != NO_MATCH)
                            {
                                pcre_error(fname, answer);
                                freeArrayOfString(Strings_Input1, m1n1);
                                freeArrayOfString(Strings_Input2, m2n2);
                                return 0;
                            }
                        }
                    }
                    while ( (answer == PCRE_FINISHED_OK) && (*pointer != '\0'));

                    if (save)
                    {
                        FREE(save);
                        save = NULL;
                    }
                }
                else
                {
                    freeArrayOfString(Strings_Input1, m1n1);
                    freeArrayOfString(Strings_Input2, m2n2);
                    Scierror(999, _("%s: No more memory.\n"), fname);
                    return 0;
                }
            }

            sort_inert(values, values + nbValues, cmp);
        }
        else
        {
            /* We don't use pcre library */
            int x = 0;

            for (x = 0; x < m2n2 ; ++x)
            {
                if ( wcslen(wStrings_Input2[x]) == 0 )
                {
                    freeArrayOfWideString(wStrings_Input2, m2n2);
                    freeArrayOfString(Strings_Input2, m2n2);
                    freeArrayOfString(Strings_Input1, m1n1);
                    if (next)
                    {
                        FREE(next);
                        next = NULL;
                    }
                    if (values)
                    {
                        FREE(values);
                        values = NULL;
                    }
                    Scierror(999, _("%s: Wrong size for input argument #%d: Non-empty string expected.\n"), fname, 2);
                    return 0;
                }
                if (Strings_Input2)
                {
                    wchar_t *pCur = wStrings_Input1;
                    do
                    {
                        pCur = wcsstr(pCur, wStrings_Input2[x]);
                        if (pCur != NULL)
                        {
                            pCur++;
                            values[nbValues++].data = (int)(pCur - wStrings_Input1);
                            values[nbposition++].position = x + 1;
                        }
                    }
                    while (pCur != NULL && *pCur != 0); //Plus tard

                    /* values are sorted */
                    sort_inert(values, values + nbValues, cmp);
                }
            }
        }

        FREE(wStrings_Input1);
        freeArrayOfWideString(wStrings_Input2, m2n2);
        freeArrayOfString(Strings_Input1, m1n1);
        freeArrayOfString(Strings_Input2, m2n2);

        numRow   = 1;
        outIndex = 0;
        CreateVar(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &numRow, &nbValues, &outIndex);
        for ( i = 0 ; i < nbValues ; i++ )
        {
            stk(outIndex)[i] = (double)values[i].data ;
        }
        LhsVar(1) = Rhs + 1 ;

        if (Lhs == 2)
        {
            numRow   = 1;
            outIndex = 0;
            CreateVar(Rhs + 2, MATRIX_OF_DOUBLE_DATATYPE, &numRow, &nbValues, &outIndex);
            for ( i = 0 ; i < nbValues ; i++ )
            {
                stk(outIndex)[i] = (double)values[i].position ;
            }
            LhsVar(2) = Rhs + 2;
        }

        if (values)
        {
            FREE(values);
            values = NULL;
        }
        PutLhsVar();
    }
    else
    {
        if (VarType(1) != sci_strings)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: Row vector of strings or column vector of strings expected.\n"), fname, 2);
        }
        return 0;
    }
    return 0;
}
/*------------------------------------------------------------------------*/
int sci_strindex(char *fname,unsigned long fname_len)
{
	BOOL bStrindex_with_pattern = FALSE;
	int outIndex = 0;
	int numRow = 1;
    int *next = NULL;

	CheckRhs(2,3);
	CheckLhs(1,2);

	if (Rhs == 3)
	{
		int m3 = 0;
		int n3 = 0;
		char **Strings_Input3 = NULL;
		int m3n3 = 0; /* m3 * n3 */

		if (VarType(3) != sci_strings)
		{
			Scierror(999,_("%s: Wrong type for input argument #%d: Character expected.\n"),fname,3);
			return 0;
		}
		GetRhsVar(3, MATRIX_OF_STRING_DATATYPE, &m3, &n3, &Strings_Input3);
		m3n3 = m3 * n3;

		if (m3n3 != 1)
		{
			freeArrayOfString(Strings_Input3, m3n3);
			Scierror(999,_("%s: Wrong type for input argument #%d: Character expected.\n"), fname, 3);
			return 0;
		}

		if ( (strcmp(Strings_Input3[0],CHAR_R) == 0) || (strcmp(Strings_Input3[0],CHAR_S) == 0) )
		{
			if (strcmp(Strings_Input3[0],CHAR_R) == 0)
			{
				bStrindex_with_pattern = TRUE;
			}
			else
			{
				bStrindex_with_pattern = FALSE;
			}
			freeArrayOfString(Strings_Input3, m3n3);
		}
		else
		{
			freeArrayOfString(Strings_Input3, m3n3);
			Scierror(999,_("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"),fname,3,CHAR_S,CHAR_R);
			return 0;
		}
	}

	if (VarType(1) == sci_matrix)
	{
		int m1 = 0;
		int n1 = 0;
		int l1 = 0;

		GetRhsVar(1,MATRIX_OF_DOUBLE_DATATYPE,&m1,&n1,&l1);
		if ((m1 == 0) && (n1 == 0))
		{
			CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,&m1,&n1,&l1);
			LhsVar(1) = Rhs+1 ;
			PutLhsVar();
			return 0;
		}
		else
		{
			Scierror(999,_("%s: Wrong type for input argument #%d: Matrix of strings or empty matrix expected.\n"),fname,3);
			return 0;
		}
	}

	if ( (VarType(1) == sci_strings) && (VarType(2) == sci_strings) )
	{
		int m1 = 0, n1 = 0;
		char **Strings_Input1 = NULL;
		wchar_t *wStrings_Input1 = NULL;
		int m1n1 = 0; /* m1 * n1 */

		int m2 = 0, n2 = 0;
		char **Strings_Input2 = NULL;
		wchar_t **wStrings_Input2 = NULL;
		int m2n2 = 0; /* m2 * n2 */

		struct In *values=NULL;

		int nbValues = 0;
		int nbposition = 0;

		int i = 0;

		GetRhsVar(1,MATRIX_OF_STRING_DATATYPE,&m1,&n1,&Strings_Input1);
		m1n1 = m1*n1;

		if (m1n1 != 1)
		{
			freeArrayOfString(Strings_Input1,m1n1);
			Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1);
			return 0;
		}

		wStrings_Input1 = to_wide_string(Strings_Input1[0]);
		if (wStrings_Input1 == NULL)
		{
			/* string conversion fails */
			freeArrayOfString(Strings_Input1,m1n1);
			freeArrayOfString(Strings_Input2,m2n2);
			Scierror(999,_("%s: Wrong value for input argument #%d: A valid string expected (UTF-8 Encoding problem).\n"),fname,1);
			return 0;
		}

		GetRhsVar(2,MATRIX_OF_STRING_DATATYPE,&m2,&n2,&Strings_Input2);
		m2n2 = m2*n2;

		if ( (m2 != 1) && (n2 != 1) )
		{
			freeArrayOfString(Strings_Input1,m1n1);
			freeArrayOfString(Strings_Input2,m2n2);
			Scierror(999,_("%s: Wrong type for input argument #%d: Row vector of strings or column vector of strings expected.\n"),fname,2);
			return 0;
		}

		wStrings_Input2 = (wchar_t**)MALLOC(m2n2 * sizeof(wchar_t*));
		for(i = 0 ; i < m2n2 ; i++)
		{
			wStrings_Input2[i] = to_wide_string(Strings_Input2[i]);
		}

		if ( (int)wcslen(wStrings_Input1) == 0 )
		{
			values= (struct In*)MALLOC(sizeof(struct In));
		}
		else
		{
			values = (struct In *)MALLOC( sizeof(struct In) * ( wcslen(wStrings_Input1) ) * m2n2);
		}

		if (bStrindex_with_pattern)
		{
			int x = 0;
			pcre_error_code w = PCRE_FINISHED_OK;

			int Output_Start = 0;
			int Output_End = 0;

			/* We use pcre library */
			for (x = 0; x < m2n2; ++x)
			{
				w = pcre_private(Strings_Input1[0],Strings_Input2[x],&Output_Start,&Output_End);
				if ( w == PCRE_FINISHED_OK)
				{
					char *partStr = strdup(Strings_Input1[0]);
					wchar_t *wcpartStr = NULL;
					partStr[Output_Start] = '\0';
					wcpartStr = to_wide_string(partStr);
					values[nbValues++].data = (int)wcslen(wcpartStr) + 1; /* adding the answer into the outputmatrix */
					values[nbposition++].position = x+1;        /* The number according to the str2 matrix */

					if (partStr) {FREE(partStr); partStr = NULL;}
					if (wcpartStr) {FREE(wcpartStr); wcpartStr = NULL;}
				}
				else
				{
					if (w != NO_MATCH)
					{
						freeArrayOfString(Strings_Input1,m1n1);
						freeArrayOfString(Strings_Input2,m2n2);
						pcre_error(fname,w);
						return 0;
					}
					break;
				}
			}

			qsort(values,nbValues,sizeof(values[0]),cmp);

		}
		else
		{
			/* We don't use pcre library */
			int x = 0;

			for (x=0; x < m2n2 ;++x)
			{
				if ( wcslen(wStrings_Input2[x]) == 0 )
				{
					freeArrayOfString(Strings_Input2,m2n2);
					freeArrayOfString(Strings_Input1,m1n1);
					if (next) {FREE(next); next = NULL;}
					if (values) {FREE(values); values = NULL;}
					Scierror(999, _("%s: Wrong size for input argument #%d: Non-empty string expected.\n"), fname,2);
					return 0;
				}
				if (Strings_Input2)
				{
					wchar_t *pCur = wStrings_Input1;
					do
					{
						pCur = wcsstr(pCur, wStrings_Input2[x]);
						if (pCur != NULL)
						{
							pCur++;
							values[nbValues++].data = (int)(pCur - wStrings_Input1);
							values[nbposition++].position = x+1;
						}
					}
					while(pCur != NULL && *pCur != 0);//Plus tard

					/* values are sorted */
					qsort(values,nbValues,sizeof(values[0]),cmp);
				}
			}
		}

		FREE(wStrings_Input1);
		freeArrayOfWideString(wStrings_Input2, m2n2);
		freeArrayOfString(Strings_Input1,m1n1);
		freeArrayOfString(Strings_Input2,m2n2);

		numRow   = 1;
		outIndex = 0;
		CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,&numRow,&nbValues,&outIndex);
		for ( i = 0 ; i < nbValues ; i++ )
		{
			stk(outIndex)[i] = (double)values[i].data ;
		}
		LhsVar(1) = Rhs+1 ;

		if (Lhs == 2)
		{
			numRow   = 1;
			outIndex = 0;
			CreateVar(Rhs+2,MATRIX_OF_DOUBLE_DATATYPE,&numRow,&nbposition,&outIndex);
			for ( i = 0 ; i < nbposition ; i++ )
			{
				stk(outIndex)[i] = (double)values[i].position ;
			}
			LhsVar(2) = Rhs+2;
		}

        if (values) {FREE(values); values = NULL;}
		PutLhsVar();
	}
	else
	{
		if(VarType(1) != sci_strings)
		{
			Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,1);
		}
		else
		{
			Scierror(999,_("%s: Wrong type for input argument #%d: Row vector of strings or column vector of strings expected.\n"),fname,2);
		}
		return 0;
	}
	return 0;
}