bincimap

Log | Files | Refs | LICENSE

regmatch.cc (1803B)


      1 /* -*- Mode: c++; -*- */
      2 /*  --------------------------------------------------------------------
      3  *  Filename:
      4  *    regex.cc
      5  *  
      6  *  Description:
      7  *    Implementation of miscellaneous regexp functions.
      8  *
      9  *  Authors:
     10  *    Andreas Aardal Hanssen <andreas-binc curly bincimap spot org>
     11  *
     12  *  Bugs:
     13  *
     14  *  ChangeLog:
     15  *
     16  *  --------------------------------------------------------------------
     17  *  Copyright 2002-2005 Andreas Aardal Hanssen
     18  *
     19  *  This program is free software; you can redistribute it and/or modify
     20  *  it under the terms of the GNU General Public License as published by
     21  *  the Free Software Foundation; either version 2 of the License, or
     22  *  (at your option) any later version.
     23  *
     24  *  This program is distributed in the hope that it will be useful,
     25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     27  *  GNU General Public License for more details.
     28  *
     29  *  You should have received a copy of the GNU General Public License
     30  *  along with this program; if not, write to the Free Software
     31  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
     32  *  --------------------------------------------------------------------
     33  */
     34 #ifdef HAVE_CONFIG_H
     35 #include <config.h>
     36 #endif
     37 
     38 #include "regmatch.h"
     39 #include <string>
     40 
     41 #include <sys/types.h>
     42 #include <regex.h>
     43 #include <stdio.h>
     44 
     45 using namespace ::std;
     46 
     47 //------------------------------------------------------------------------
     48 int Binc::regexMatch(const string &data_in, const string &p_in)
     49 {
     50   regex_t r;
     51   regmatch_t rm[2];
     52 
     53   if (regcomp(&r, p_in.c_str(), REG_EXTENDED | REG_NOSUB) != 0)
     54     return 2;
     55 
     56   int n = regexec(&r, data_in.c_str(), data_in.length(), rm, 0);
     57   regfree(&r);
     58   if (n == 0)
     59     return 0;
     60   else
     61     return 2;
     62 }