suplibxx  1.3.9
match.cc
1 // --8<--8<--8<--8<--
2 //
3 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
4 //
5 // This file is part of suplibxx
6 //
7 // suplibxx is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // suplibxx is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the
19 // Free Software Foundation, Inc.
20 // 51 Franklin Street, Fifth Floor
21 // Boston, MA 02110-1301, USA
22 //
23 // -->8-->8-->8-->8--
24 
25 #include "colselect.h"
26 
27 namespace suplib {
28 
44 bool
46  const string& str,
47  const string& pattern
48  ) throw ( Exception ) {
49 
50  pcre* re;
51  pcre_extra* pe = NULL;
52  const char* error;
53  int options = 0;
54  int erroffset;
55  int rc;
56  int* ovector = NULL;
57  int ovecsize = 0;
58 
59  if ( NULL == (re = pcre_compile( pattern.c_str( ),
60  options,
61  &error,
62  &erroffset,
63  NULL )) )
64  throw( Exception( error ) );
65 
66  if ( 0 > (rc = pcre_exec( re,
67  pe,
68  str.c_str( ),
69  str.size( ),
70  0,
71  options,
72  ovector,
73  ovecsize
74  )) ) {
75  switch(rc) {
76  case PCRE_ERROR_NOMATCH:
77  break;
78 
79  case PCRE_ERROR_NULL:
80  throw( Exception( "NULL pattern or subject string." ) );
81 
82  case PCRE_ERROR_BADOPTION:
83  throw( Exception( "Bad option." ) );
84 
85  case PCRE_ERROR_BADMAGIC:
86  throw( Exception( "Bad magic number(possible junk regex pointer?)" ) );
87 
88  case PCRE_ERROR_UNKNOWN_NODE:
89  throw( Exception( "Was your compiled pattern overwritten?" ) );
90 
91  case PCRE_ERROR_NOMEMORY:
92  throw( Exception( "Out of memory." ) );
93 
94  default:
95  throw( Exception( "Unforeseen matching error." ) ) ;
96 
97  }
98 
99  return false;
100 
101  }
102 
103  return true;
104 
105 }
106 
107 } // namespace suplib
bool match(const string &str, const string &pattern)
handles Perl regular expression matching.
Definition: match.cc:45
The suplib namespace encompasses all of the functions in the suplib++ library.