match.cc

00001 // --8<--8<--8<--8<--
00002 //
00003 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
00004 //
00005 // This file is part of suplibxx
00006 //
00007 // suplibxx is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // suplibxx is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the 
00019 //       Free Software Foundation, Inc. 
00020 //       51 Franklin Street, Fifth Floor
00021 //       Boston, MA  02110-1301, USA
00022 //
00023 // -->8-->8-->8-->8--
00024 
00025 #include "colselect.h"
00026 
00027 namespace suplib {
00028 
00044 bool 
00045 match( 
00046   const string& str, 
00047   const string& pattern
00048   ) throw ( Exception ) {
00049 
00050   pcre* re;
00051   pcre_extra* pe = NULL;
00052   const char* error;
00053   int options = 0;
00054   int erroffset;
00055   int rc;
00056   int* ovector = NULL;
00057   int ovecsize = 0;
00058   
00059   if ( NULL == (re = pcre_compile( pattern.c_str( ),
00060                                    options,
00061                                    &error,
00062                                    &erroffset,
00063                                    NULL )) )
00064     throw( Exception( error ) );
00065   
00066   if ( 0 > (rc = pcre_exec( re, 
00067                             pe, 
00068                             str.c_str( ),
00069                             str.size( ),
00070                             0,
00071                             options,
00072                             ovector,
00073                             ovecsize
00074                             )) ) {
00075     switch(rc) {
00076     case PCRE_ERROR_NOMATCH: 
00077       break;
00078       
00079     case PCRE_ERROR_NULL:
00080       throw( Exception( "NULL pattern or subject string." ) );
00081       
00082     case PCRE_ERROR_BADOPTION:
00083       throw( Exception( "Bad option." ) );
00084       
00085     case PCRE_ERROR_BADMAGIC:
00086       throw( Exception( "Bad magic number(possible junk regex pointer?)" ) );
00087       
00088     case PCRE_ERROR_UNKNOWN_NODE:
00089       throw( Exception( "Was your compiled pattern overwritten?" ) ); 
00090       
00091     case PCRE_ERROR_NOMEMORY:  
00092       throw( Exception( "Out of memory." ) ); 
00093       
00094     default: 
00095       throw( Exception( "Unforeseen matching error." ) ) ;
00096       
00097     }
00098     
00099     return false;
00100     
00101   }
00102   
00103   return true;
00104   
00105 }
00106   
00107 } // namespace suplib

Generated on Thu Oct 2 20:49:06 2008 for suplibxx by  doxygen 1.5.6