/* * Ivy, C++ interface * * Copyright (C) 1997-2000 * Centre d'Études de la Navigation Aérienne * * Bind syntax for extracting message comtent * using regexp or other * * Authors: François-Régis Colin * * $Id: ivybind.c,v 1.9.2.7 2006/06/01 12:07:17 bustico Exp $ * * Please refer to file version.h for the * copyright notice regarding this software */ /* Module de gestion de la syntaxe des messages Ivy */ #include "IvyStdAfx.h" #include "IvyBinding.h" static int err_offset; #ifdef USE_PCRE static const char *err_buf; #else /* we don't USE_PCRE */ static ivy::string err_buf; #endif /* USE_PCRE */ /* classes de messages emis par l'application utilise pour le filtrage */ static int messages_classes_count = 0; static const char **messages_classes = 0; /* regexp d'extraction du mot clef des regexp client pour le filtrage des regexp , ca va c'est clair ??? */ static IvyBinding token_extract; IvyBinding::IvyBinding() { nb_match = 0; #ifdef USE_PCRE regexp = NULL; inspect = NULL; ovector = NULL; #else /* we don't USE_PCRE */ regexp = NULL; #endif /* USE_PCRE */ } IvyBinding::~IvyBinding() { #ifdef USE_PCRE if (inspect!=NULL) pcre_free(inspect); if (regexp!=NULL) pcre_free(regexp); if ( ovector != NULL ) delete [] ovector; #else /* we don't USE_PCRE */ delete regexp; #endif /* USE_PCRE */ } bool IvyBinding::Compile( const char * expression, int *erroffset, const char **errmessage ) { int capture_count = 0; bool compile = false; #ifdef USE_PCRE regexp = pcre_compile(expression, PCRE_CASELESS, &err_buf, &err_offset, NULL); if ( regexp != NULL ) { this->inspect = pcre_study(regexp,0,&err_buf); if (err_buf!=NULL) { printf("Error studying %s, message: %s\n",expression,err_buf); } pcre_fullinfo( regexp, inspect, PCRE_INFO_CAPTURECOUNT, &capture_count ); if ( ovector != NULL ) delete [] ovector; // + 1 pour la capture totale ovectorsize = (capture_count+1) * 3; ovector = new int[ovectorsize]; compile = true; } else { *erroffset = err_offset; *errmessage = err_buf; printf("Error compiling '%s', %s\n", expression, err_buf); } #else /* we don't USE_PCRE */ regexp = new Regexp( expression, false ); if ( regexp->CompiledOK() ) { compile = true; } else { err_buf = regexp->GetErrorString(); *erroffset = err_offset; *errmessage = err_buf.c_str(); printf("Error compiling '%s', %s\n", expression, err_buf); } #endif /* USE_PCRE */ return compile; } int IvyBinding::Exec( const char * message ) { #ifdef USE_PCRE nb_match = pcre_exec( regexp, inspect, message, strlen(message), 0, /* debut */ 0, /* no other regexp option */ ovector, ovectorsize); if (nb_match<1) return 0; /* no match */ #else /* we don't USE_PCRE */ if ( !regexp->Match( message ) ) return 0; nb_match = regexp->SubStrings()+1; // +1 first arg is wall string #endif /* USE_PCRE */ return nb_match; } void IvyBinding::Match( const char *message, int argnum, int *arglen, const char **arg) { #ifdef USE_PCRE *arglen = ovector[2*argnum+1]- ovector[2*argnum]; *arg = message + ovector[2*argnum]; #else /* we don't USE_PCRE */ *arglen = regexp->SubLength(argnum); *arg = message + regexp->SubStart(argnum); #endif /* USE_PCRE */ } //filter Expression Bind void IvyBinding::SetFilter( int argc, const char **argv) { const char *errbuf; int erroffset; messages_classes_count = argc; messages_classes = argv; /* compile the token extraction regexp */ if ( !token_extract.Compile("^\\^([a-zA-Z_0-9-]+).*", & erroffset, & errbuf) ) { printf("Error compiling Token Extract regexp: %s\n", errbuf); } } int IvyBinding::Filter(const char *expression) { int i; int err; int regexp_ok = 1; /* accepte tout par default */ int tokenlen; const char *token; if ( *expression =='^' && messages_classes_count !=0 ) { regexp_ok = 0; /* extract token */ err = token_extract.Exec( expression ); if ( err < 1 ) return 1; token_extract.Match( expression , 1, &tokenlen, &token ); for ( i = 0 ; i < messages_classes_count; i++ ) { if (strncmp( messages_classes[i], token, tokenlen ) == 0) { return 1; } // else { //printf ("DBG> %s eliminé [%s]\n", token, expression); //} } } return regexp_ok; }