/* File: perlpattern.c -- interface to Perl's match() and substitute() ** Author(s): Jin Yu ** Contact: xsb-contact@cs.sunysb.edu ** ** Copyright (C) The Research Foundation of SUNY, 1998 ** ** XSB is free software; you can redistribute it and/or modify it under the ** terms of the GNU Library General Public License as published by the Free ** Software Foundation; either version 2 of the License, or (at your option) ** any later version. ** ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ** FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for ** more details. ** ** You should have received a copy of the GNU Library General Public License ** along with XSB; if not, write to the Free Software Foundation, ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ** ** $Id: perlpattern.c,v 1.4 2001/12/30 05:58:06 kifer Exp $ ** */ /*---------------------------------------------------------------------------- int match(SV *string, char *pattern) Try to find the match pattern in the given string. If found, save the submatch strings in the global string array "matchResults". input: char *string: text string char *pattern: match pattern output: if match, return SUCCESS(1), otherwise return FAILURE(0). ----------------------------------------------------------------------------*/ int match(SV *string, char *pattern) { SV *command = newSV(0), *retval; /*allocate space for SV data*/ SV *buffer = newSV(0); SV *string_buff = newSV(0); AV *matchArray; /* AV storage for submatch lists */ int number,i; int returnCode = FAILURE; /*return code*/ /*------------------------------------------------------------------------- allocate the space for the match pattern string, and initial it -----------------------------------------------------------------------*/ if ( matchPattern!=NULL )free(matchPattern); matchPattern = malloc(strlen(pattern)+1); strcpy(matchPattern, pattern); /*------------------------------------------------------------------------- initialize the seaching string -----------------------------------------------------------------------*/ sv_setpvf(command, "$__text__='%s'", SvPV(string,PL_na)); my_perl_eval_sv(command, TRUE); /* execute the perl command */ /*------------------------------------------------------------------------- do the perl pattern matching ------------------------------------------------------------------------*/ /* use an unlikely variable name __match__ to avoid name clashes*/ sv_setpvf(command, "$__text__=~%s; @__match__=%s", matchPattern, subMatchSpec); retval=my_perl_eval_sv(command, TRUE); if (retval == NULL ) return returnCode; /*------------------------------------------------------------------------- get submatch from @__match__ -----------------------------------------------------------------------*/ matchArray = perl_get_av("__match__", FALSE); /* +1 because av_len gives the last index, and indices start with 0 */ number = av_len(matchArray) + 1; /* MAX_TOTAL_MATCH is also the size of subMatchSpec, so they must be equal */ if ( number != MAX_TOTAL_MATCH ) { return returnCode; } for ( i=0;i