add regexp function in YaSp and manage <> and == with empty matrix 46/1746/3
antoine ELIAS [Tue, 31 Aug 2010 08:45:11 +0000 (10:45 +0200)]
Change-Id: I7de33b6df7fe09bd56e8223824251606782e7e54

12 files changed:
scilab/modules/abstractSyntaxTree/includes/run_OpExp.hxx
scilab/modules/string/Makefile.am
scilab/modules/string/Makefile.in
scilab/modules/string/includes/gw_string.h
scilab/modules/string/includes/string_gw.hxx
scilab/modules/string/sci_gateway/c/gw_string.c
scilab/modules/string/sci_gateway/c/sci_regexp.c [deleted file]
scilab/modules/string/sci_gateway/cpp/sci_grep.cpp
scilab/modules/string/sci_gateway/cpp/sci_regexp.cpp [new file with mode: 0644]
scilab/modules/string/sci_gateway/cpp/string_gw.cpp
scilab/modules/string/sci_gateway/cpp/string_gw.vcproj
scilab/modules/string/src/c/string.vcproj

index 394e42c..9200e4b 100644 (file)
@@ -385,7 +385,11 @@ void visitprivate(const OpExp &e)
             Double *pL                 = execMeL.result_get()->getAsDouble();
             Double *pR                 = execMeR.result_get()->getAsDouble();
 
-            if(pL->size_get() == 0  && pR->size_get() == 0)
+            if(pR->size_get() == 0 && pL->size_get() == 0)
+            {
+                pResult = new Bool(true);
+            }
+            else if(pL->size_get() == 0  && pR->size_get() == 0)
             {
                 pResult = new Bool(true);
             }
@@ -522,7 +526,11 @@ void visitprivate(const OpExp &e)
             Double *pL                 = execMeL.result_get()->getAsDouble();
             Double *pR                 = execMeR.result_get()->getAsDouble();
 
-            if(pR->size_get() == 1)
+            if(pR->size_get() == 0 && pL->size_get() == 0)
+            {
+                pResult = new Bool(false);
+            }
+            else if(pR->size_get() == 1)
             {
                 pResult                                = new Bool(pL->rows_get(), pL->cols_get());
                 double dblRef  = pR->real_get(0,0);
index 79d3563..a1ec48d 100644 (file)
@@ -58,13 +58,13 @@ sci_gateway/c/sci_strrev.c \
 sci_gateway/c/sci_strspn.c \
 sci_gateway/c/sci_strstr.c \
 sci_gateway/c/sci_strtod.c \
-sci_gateway/c/sci_strtok.c \
-sci_gateway/c/sci_regexp.c
+sci_gateway/c/sci_strtok.c
 
 GATEWAY_CPP_SOURCES = \
     sci_gateway/cpp/sci_stripblanks.cpp \
        sci_gateway/cpp/sci_grep.cpp \
-       sci_gateway/cpp/string_gw.cpp
+       sci_gateway/cpp/string_gw.cpp \
+       sci_gateway/cpp/sci_regexp.cpp
 
 GATEWAY_FORTRAN_SOURCES =  sci_gateway/fortran/sci_string.f \
 sci_gateway/fortran/strops.f
index bb1c9f4..69e9dc9 100644 (file)
@@ -147,11 +147,11 @@ am__objects_4 = libscistring_la-gw_string.lo \
        libscistring_la-sci_strcspn.lo libscistring_la-sci_strncpy.lo \
        libscistring_la-sci_strrchr.lo libscistring_la-sci_strrev.lo \
        libscistring_la-sci_strspn.lo libscistring_la-sci_strstr.lo \
-       libscistring_la-sci_strtod.lo libscistring_la-sci_strtok.lo \
-       libscistring_la-sci_regexp.lo
+       libscistring_la-sci_strtod.lo libscistring_la-sci_strtok.lo
 am__objects_5 = sci_string.lo strops.lo
 am__objects_6 = libscistring_la-sci_stripblanks.lo \
-       libscistring_la-sci_grep.lo libscistring_la-string_gw.lo
+       libscistring_la-sci_grep.lo libscistring_la-string_gw.lo \
+       libscistring_la-sci_regexp.lo
 am_libscistring_la_OBJECTS = $(am__objects_1) $(am__objects_2) \
        $(am__objects_3) $(am__objects_4) $(am__objects_5) \
        $(am__objects_6)
@@ -498,13 +498,13 @@ sci_gateway/c/sci_strrev.c \
 sci_gateway/c/sci_strspn.c \
 sci_gateway/c/sci_strstr.c \
 sci_gateway/c/sci_strtod.c \
-sci_gateway/c/sci_strtok.c \
-sci_gateway/c/sci_regexp.c
+sci_gateway/c/sci_strtok.c
 
 GATEWAY_CPP_SOURCES = \
     sci_gateway/cpp/sci_stripblanks.cpp \
        sci_gateway/cpp/sci_grep.cpp \
-       sci_gateway/cpp/string_gw.cpp
+       sci_gateway/cpp/string_gw.cpp \
+       sci_gateway/cpp/sci_regexp.cpp
 
 GATEWAY_FORTRAN_SOURCES = sci_gateway/fortran/sci_string.f \
 sci_gateway/fortran/strops.f
@@ -1133,13 +1133,6 @@ libscistring_la-sci_strtok.lo: sci_gateway/c/sci_strtok.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(libscistring_la_CFLAGS) $(CFLAGS) -c -o libscistring_la-sci_strtok.lo `test -f 'sci_gateway/c/sci_strtok.c' || echo '$(srcdir)/'`sci_gateway/c/sci_strtok.c
 
-libscistring_la-sci_regexp.lo: sci_gateway/c/sci_regexp.c
-@am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(libscistring_la_CFLAGS) $(CFLAGS) -MT libscistring_la-sci_regexp.lo -MD -MP -MF $(DEPDIR)/libscistring_la-sci_regexp.Tpo -c -o libscistring_la-sci_regexp.lo `test -f 'sci_gateway/c/sci_regexp.c' || echo '$(srcdir)/'`sci_gateway/c/sci_regexp.c
-@am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/libscistring_la-sci_regexp.Tpo $(DEPDIR)/libscistring_la-sci_regexp.Plo
-@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='sci_gateway/c/sci_regexp.c' object='libscistring_la-sci_regexp.lo' libtool=yes @AMDEPBACKSLASH@
-@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
-@am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(libscistring_la_CFLAGS) $(CFLAGS) -c -o libscistring_la-sci_regexp.lo `test -f 'sci_gateway/c/sci_regexp.c' || echo '$(srcdir)/'`sci_gateway/c/sci_regexp.c
-
 .cpp.o:
 @am__fastdepCXX_TRUE@  $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
 @am__fastdepCXX_TRUE@  $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
@@ -1189,6 +1182,13 @@ libscistring_la-string_gw.lo: sci_gateway/cpp/string_gw.cpp
 @AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCXX_FALSE@ $(LIBTOOL)  --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o libscistring_la-string_gw.lo `test -f 'sci_gateway/cpp/string_gw.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/string_gw.cpp
 
+libscistring_la-sci_regexp.lo: sci_gateway/cpp/sci_regexp.cpp
+@am__fastdepCXX_TRUE@  $(LIBTOOL)  --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT libscistring_la-sci_regexp.lo -MD -MP -MF $(DEPDIR)/libscistring_la-sci_regexp.Tpo -c -o libscistring_la-sci_regexp.lo `test -f 'sci_gateway/cpp/sci_regexp.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_regexp.cpp
+@am__fastdepCXX_TRUE@  $(am__mv) $(DEPDIR)/libscistring_la-sci_regexp.Tpo $(DEPDIR)/libscistring_la-sci_regexp.Plo
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     source='sci_gateway/cpp/sci_regexp.cpp' object='libscistring_la-sci_regexp.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(LIBTOOL)  --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o libscistring_la-sci_regexp.lo `test -f 'sci_gateway/cpp/sci_regexp.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_regexp.cpp
+
 .f.o:
        $(F77COMPILE) -c -o $@ $<
 
index 149bc6b..fa8ae26 100644 (file)
@@ -41,7 +41,6 @@ STRING_IMPEXP int sci_strrev(char *fname,unsigned long fname_len);
 STRING_IMPEXP int sci_strtok(char *fname,unsigned long fname_len);
 STRING_IMPEXP int sci_strspn(char *fname,unsigned long fname_len);
 STRING_IMPEXP int sci_strtod(char *fname,unsigned long fname_len);
-STRING_IMPEXP int sci_regexp(char *fname,unsigned long fname_len);
 //STRING_IMPEXP int sci_NumTokens(char *fname,unsigned long fname_len);
 
 //YaSp
index dcf5528..d97b278 100644 (file)
@@ -29,6 +29,7 @@ public :
 
 CPP_GATEWAY_PROTOTYPE(sci_grep);
 CPP_GATEWAY_PROTOTYPE(sci_stripblanks);
+CPP_GATEWAY_PROTOTYPE(sci_regexp);
 
 
 #endif /* !__STRING_GW_HXX__ */
index 106cc16..a490b61 100644 (file)
@@ -48,7 +48,7 @@ static gw_generic_table Tab[] =
        {sci_strtok,"strtok"},
        {sci_strspn,"strspn"},
        {sci_strtod,"strtod"},
-       {sci_regexp,"regexp"}
+       {NULL, ""} //regexp
 };
 /*--------------------------------------------------------------------------*/
 int gw_string(void)
diff --git a/scilab/modules/string/sci_gateway/c/sci_regexp.c b/scilab/modules/string/sci_gateway/c/sci_regexp.c
deleted file mode 100644 (file)
index 0dfa98e..0000000
+++ /dev/null
@@ -1,330 +0,0 @@
-/*
-* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-* Copyright (C) 2008 - INRIA - Cong WU
-* Copyright (C) 2008 - 2009 - DIGITEO - Allan CORNET
-*
-* This file must be used under the terms of the CeCILL.
-* This source file is licensed as described in the file COPYING, which
-* you should have received as part of this distribution.  The terms
-* are also available at
-* http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
-*
-*/
-
-/* desc : search position of a character string in an other string
-using regular expression .                                      */
-/*------------------------------------------------------------------------*/
-#include <string.h>
-#include <stdio.h>
-#include "gw_string.h"
-#include "stack-c.h"
-#include "MALLOC.h"
-#include "localization.h"
-#include "freeArrayOfString.h"
-#include "pcre.h"
-#include "pcre_private.h"
-#include "pcre_error.h"
-#include "Scierror.h"
-#include "os_strdup.h"
-#include "charEncoding.h"
-/*------------------------------------------------------------------------*/
-#define CHAR_S 's'
-#define CHAR_R 'r'
-#define STR_ONCE 'o'
-/*------------------------------------------------------------------------*/
-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 m2 = 0,n2 = 0;
-       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 = os_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 = os_strdup(pointer);
-                                                       char *  strOutput_End =  os_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;}
-
-       C2F(putlhsvar)();
-       return 0;
-}
-/*-----------------------------------------------------------------------------------*/
index 751780a..9665cd0 100644 (file)
@@ -47,7 +47,7 @@ static int GREP_NEW(GREPRESULTS *results,char **Inputs_param_one,int mn_one,char
 static int GREP_OLD(GREPRESULTS *results,char **Inputs_param_one,int mn_one,char **Inputs_param_two,int mn_two);
 /*------------------------------------------------------------------------*/
 
-Function::ReturnValue sci_grep(typed_list &in, int _piRetCount, typed_list &out)
+Function::ReturnValue sci_grep(typed_list &in, int _iRetCount, typed_list &out)
 {
     bool bRegularExpression = false;
 
@@ -152,7 +152,7 @@ Function::ReturnValue sci_grep(typed_list &in, int _piRetCount, typed_list &out)
             
             out.push_back(pD1);
 
-            if (_piRetCount == 2)
+            if (_iRetCount == 2)
             {
                 Double* pD2 = new Double(1, grepresults.currentLength);
                 double* pDbl2 = pD2->real_get();
@@ -193,7 +193,7 @@ Function::ReturnValue sci_grep(typed_list &in, int _piRetCount, typed_list &out)
 
     return Function::OK;
 }
-//Function::ReturnValue sci_grep(typed_list &in, int _piRetCount, typed_list &out)
+//Function::ReturnValue sci_grep(typed_list &in, int _iRetCount, typed_list &out)
 //{
 //     CheckRhs(2,3);
 //     CheckLhs(1,2);
diff --git a/scilab/modules/string/sci_gateway/cpp/sci_regexp.cpp b/scilab/modules/string/sci_gateway/cpp/sci_regexp.cpp
new file mode 100644 (file)
index 0000000..1a7ddb3
--- /dev/null
@@ -0,0 +1,484 @@
+/*
+* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+* Copyright (C) 2008 - INRIA - Cong WU
+* Copyright (C) 2008 - 2009 - DIGITEO - Allan CORNET
+*
+* This file must be used under the terms of the CeCILL.
+* This source file is licensed as described in the file COPYING, which
+* you should have received as part of this distribution.  The terms
+* are also available at
+* http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+*
+*/
+
+/* desc : search position of a character string in an other string
+using regular expression .                                      */
+/*------------------------------------------------------------------------*/
+#include "function.hxx"
+#include "context.hxx"
+#include "types.hxx"
+#include "string_gw.hxx"
+
+extern "C"
+{
+#include "localization.h"
+#include "pcre.h"
+#include "pcre_private.h"
+#include "pcre_error.h"
+#include "Scierror.h"
+#include "charEncoding.h"
+#include "os_strdup.h"
+}
+/*------------------------------------------------------------------------*/
+#define WCHAR_S L's'
+#define WCHAR_R L'r'
+#define WSTR_ONCE L'o'
+/*------------------------------------------------------------------------*/
+Function::ReturnValue sci_regexp(typed_list &in, int _iRetCount, typed_list &out)
+{
+       wchar_t wcType          = WCHAR_S;
+    wchar_t* pwstInput      = NULL;
+    wchar_t* pwstPattern    = NULL;
+
+    char* pstInput          = NULL;
+    char* pstPattern        = NULL;
+
+    int iPcreStatus         = 0;
+    int iStart              = 0;
+    int iStep               = 0;
+    int iEnd                = 0;
+    int* piStart            = NULL;
+    int* piEnd              = NULL;
+    int iOccurs             = 0;
+
+    if(in.size() < 2 || in.size() > 3)
+    {
+        ScierrorW(999,_W("%ls: Wrong number of input arguments: %d or %d expected.\n"), L"regexp", 2, 3);
+        return Function::Error;
+    }
+
+    // check output parameters
+    if(_iRetCount < 1 || _iRetCount > 3)
+    {
+        ScierrorW(999,_W("%ls: Wrong number of output arguments: %d expected.\n"), L"regexp", 1);
+        return Function::Error;
+    }
+
+    if(in[0]->isString() == false || in[0]->getAsString()->size_get() != 1)
+    {
+        ScierrorW(999, _W("%ls: Wrong type for input argument #%d: Single string expected.\n"), L"regexp", 1);
+        return Function::Error;
+    }
+    pwstInput = in[0]->getAsString()->string_get(0);
+
+    if(in[1]->isString() == false || in[1]->getAsString()->size_get() != 1)
+    {
+        ScierrorW(999, _W("%ls: Wrong type for input argument #%d: Single string expected.\n"), L"regexp", 2);
+        return Function::Error;
+    }
+    pwstPattern = in[1]->getAsString()->string_get(0);
+
+    if(in.size() == 3)
+    {
+        if(in[2]->isString() == false || in[2]->getAsString()->size_get() != 1)
+        {
+            ScierrorW(999, _W("%ls: Wrong type for input argument #%d: Single string expected.\n"), L"regexp", 3);
+            return Function::Error;
+        }
+
+        if(in[2]->getAsString()->string_get(0)[0] != WSTR_ONCE)
+        {
+           ScierrorW(999,_W("%ls: Wrong type for input argument #%d: '%ls' expected.\n"), L"regexp", 3, L"o");
+           return Function::Error;
+        }
+        wcType = WSTR_ONCE;
+    }
+
+    pstInput    = wide_string_to_UTF8(pwstInput);
+    pstPattern  = wide_string_to_UTF8(pwstPattern);
+
+    //input is empty
+    if(wcslen(pwstInput) == 0)
+    {
+        Double* pStart = new Double(0,0);
+        out.push_back(pStart);
+        if(_iRetCount > 1)
+        {
+            Double* pEnd = new Double(0,0);
+            out.push_back(pEnd);
+
+            if(_iRetCount > 2)
+            {
+                String* pS = new String(0,0);
+                out.push_back(pS);
+            }
+        }
+        return Function::OK;
+    }
+
+    piStart     = new int[wcslen(pwstInput)];
+    piEnd       = new int[wcslen(pwstInput)];
+
+    do
+    {
+        iPcreStatus = pcre_private(pstInput + iStep, pstPattern, &iStart, &iEnd);
+        if(iPcreStatus == PCRE_FINISHED_OK && iStart != iEnd)
+        {
+            char* pstTempStart  = os_strdup(pstInput);
+            pstTempStart[iStart + iStep] = 0;
+
+            char* pstTempEnd    = os_strdup(pstInput);
+            pstTempEnd[iEnd + iStep]     = 0;
+
+            wchar_t* pwstTempStart  = to_wide_string(pstTempStart);
+            wchar_t* pwstTempEnd    = to_wide_string(pstTempEnd);
+
+
+            piStart[iOccurs] = static_cast<int>(wcslen(pwstTempStart));
+            piEnd[iOccurs]   = static_cast<int>(wcslen(pwstTempEnd));
+
+            FREE(pstTempStart);
+            FREE(pstTempEnd);
+            FREE(pwstTempStart);
+            FREE(pwstTempEnd);
+            //piStart[iOccurs] = iStart + iStep;
+            //piEnd[iOccurs]   = iEnd + iStep;
+            iOccurs++;
+            iStep += iEnd;
+        }
+    }while(iPcreStatus == PCRE_FINISHED_OK && iStart != iEnd && wcType != WSTR_ONCE);
+    FREE(pstInput);
+    FREE(pstPattern);
+
+    Double* pStart = new Double(1, iOccurs);
+    double* pdblStart = pStart->real_get();
+
+    for(int i = 0 ; i < iOccurs ; i++)
+    {
+        pdblStart[i] = piStart[i] + 1; //one indexed
+    }
+
+    out.push_back(pStart);
+
+    if(_iRetCount > 1)
+    {
+        Double* pEnd = new Double(1, iOccurs);
+        double* pdblEnd = pEnd->real_get();
+        for(int i = 0 ; i < iOccurs ; i++)
+        {
+            pdblEnd[i]   = piEnd[i];
+        }
+        out.push_back(pEnd);
+    }
+
+    if(_iRetCount == 3)
+    {
+        String *pS = new String(iOccurs, 1);
+        for(int i = 0 ; i < iOccurs ; i++)
+        {
+            wchar_t* pwstTemp = new wchar_t[piEnd[i] - piStart[i] + 1];
+            wcsncpy(pwstTemp, pwstInput + piStart[i], piEnd[i] - piStart[i]);
+            pwstTemp[piEnd[i] - piStart[i]] = 0;
+            pS->string_set(i, 0, pwstTemp);
+            delete[] pwstTemp;
+        }
+        out.push_back(pS);
+    }
+    delete[] piStart;
+    delete[] piEnd;
+    return Function::OK;
+       //char typ = CHAR_S;
+       //char **Str = NULL;
+       //char **Str2 = NULL;
+       //int i = 0; /* loop indice */
+
+       //int mn = 0; /* dimension parameter 1 m*n */
+
+       //int m2 = 0,n2 = 0;
+       //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
+       //{
+       //      ScierrorW(999, _W("%ls: Wrong type for input argument #%d: Single string expected.\n"), fname,1);
+       //      return 0;
+       //}
+
+       //if (mn != 1)
+       //{
+       //      freeArrayOfString(Str,mn);
+       //      ScierrorW(36, _W("%ls: 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);
+       //      ScierrorW(36, _W("%ls: Wrong type for input argument #%d: Single string expected.\n"), fname,2);
+       //      return 0;
+       //}
+
+       //if (mn2 != 1)
+       //{
+       //      freeArrayOfString(Str2,mn2);
+       //      ScierrorW(36, _W("%ls: 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);
+       //              ScierrorW(999,_W("%ls: 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 = os_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 = os_strdup(pointer);
+       //                                              char *  strOutput_End =  os_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);
+       //                      ScierrorW(999, _W("%ls: 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);
+
+       //                      ScierrorW(999, _W("%ls: 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;}
+
+       //C2F(putlhsvar)();
+       //return 0;
+}
+/*-----------------------------------------------------------------------------------*/
index fa2f4ae..f16224a 100644 (file)
 #include "string_gw.hxx"
 
 #define MODULE_NAME L"string"
+
 bool StringModule::Load()
 {
-  symbol::Context::getInstance()->AddFunction(Function::createFunction(L"grep", &sci_grep, MODULE_NAME));
-  symbol::Context::getInstance()->AddFunction(Function::createFunction(L"stripblanks", &sci_stripblanks, MODULE_NAME));
-
-  return true;
+    symbol::Context::getInstance()->AddFunction(Function::createFunction(L"grep", &sci_grep, MODULE_NAME));
+    symbol::Context::getInstance()->AddFunction(Function::createFunction(L"stripblanks", &sci_stripblanks, MODULE_NAME));
+    symbol::Context::getInstance()->AddFunction(Function::createFunction(L"regexp", &sci_regexp, MODULE_NAME));
+    return true;
 }
index 0e293b0..315902a 100644 (file)
                                >
                        </File>
                        <File
+                               RelativePath=".\sci_regexp.cpp"
+                               >
+                       </File>
+                       <File
                                RelativePath=".\sci_stripblanks.cpp"
                                >
                        </File>
index f1bb863..e60011f 100644 (file)
                                >
                        </File>
                        <File
-                               RelativePath="..\..\sci_gateway\c\sci_regexp.c"
-                               >
-                       </File>
-                       <File
                                RelativePath="..\..\sci_gateway\c\sci_str2code.c"
                                >
                        </File>