suplibxx  1.3.13
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 <string>
26 #include <pcre.h>
27 
28 #include <Exception/Exception.h>
29 
30 #include "colselect.h"
31 
32 namespace suplib {
33 
49 bool
51  const std::string& str,
52  const std::string& pattern
53  ){
54 
55  pcre* re;
56  pcre_extra* pe = NULL;
57  const char* error;
58  int options = 0;
59  int erroffset;
60  int rc;
61  int* ovector = NULL;
62  int ovecsize = 0;
63 
64  if ( NULL == (re = pcre_compile( pattern.c_str( ),
65  options,
66  &error,
67  &erroffset,
68  NULL )) )
69  throw( Exception( error ) );
70 
71  if ( 0 > (rc = pcre_exec( re,
72  pe,
73  str.c_str( ),
74  str.size( ),
75  0,
76  options,
77  ovector,
78  ovecsize
79  )) ) {
80  switch(rc) {
81  case PCRE_ERROR_NOMATCH:
82  break;
83 
84  case PCRE_ERROR_NULL:
85  throw( Exception( "NULL pattern or subject string." ) );
86 
87  case PCRE_ERROR_BADOPTION:
88  throw( Exception( "Bad option." ) );
89 
90  case PCRE_ERROR_BADMAGIC:
91  throw( Exception( "Bad magic number(possible junk regex pointer?)" ) );
92 
93  case PCRE_ERROR_UNKNOWN_NODE:
94  throw( Exception( "Was your compiled pattern overwritten?" ) );
95 
96  case PCRE_ERROR_NOMEMORY:
97  throw( Exception( "Out of memory." ) );
98 
99  default:
100  throw( Exception( "Unforeseen matching error." ) ) ;
101 
102  }
103 
104  return false;
105 
106  }
107 
108  return true;
109 
110 }
111 
112 } // namespace suplib
The suplib namespace encompasses all of the functions in the suplib++ library.
bool match(const std::string &str, const std::string &pattern)
handles Perl regular expression matching.
Definition: match.cc:50