Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

RegExp.H

Go to the documentation of this file.
00001 // Copyright (C) 2003, Kenneth C. Schalk
00002 
00003 // This file is part of Vesta.
00004 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Mon Nov 10 11:29:57 EST 2003 by ken@xorian.net        
00020 
00021 #ifndef _Basics_RegExp
00022 #define _Basics_RegExp
00023 
00024 #include <stddef.h>
00025 #include <regex.h>
00026 
00027 #include "Basics.H"
00028 
00029 namespace Basics
00030 {
00031   // Simplified interface to POSIX regular expressions.  (See the
00032   // regcomp(3) man page.)
00033   class RegExp
00034   {
00035   private:
00036     // Parsed regular expression
00037     regex_t parsed;
00038   public:
00039     // Error parsing regular expression
00040     struct ParseError
00041     {
00042       const int code;
00043       const char *msg;
00044       const char *re;
00045       ParseError(int code, const char *msg, const char *re)
00046         : code(code), msg(msg), re(re) { }
00047     };
00048 
00049     RegExp(const char *re,
00050            // cflags controls the compilation of the regular
00051            // expression.  It's passed directly on to regcomp.  (See
00052            // the regcomp(3) man page.)
00053            int cflags = (REG_EXTENDED | REG_ICASE | REG_NOSUB))
00054       throw(RegExp::ParseError)
00055     {
00056             constructor_inner(re, cflags);
00057     }
00058 
00059     RegExp(const Text &re,
00060            int cflags = (REG_EXTENDED | REG_ICASE | REG_NOSUB))
00061       throw(RegExp::ParseError)
00062     {
00063             constructor_inner(re.cchars(), cflags);
00064     }
00065       
00066     ~RegExp()
00067     {
00068       regfree(&(this->parsed));
00069     }
00070 
00071     inline bool match(const char *string,
00072                // nmatch and pmatch make it possible to get
00073                // information about the location of any matches.  (See
00074                // the regexec(3) man page.)
00075                size_t nmatch = 0, regmatch_t *pmatch = 0,
00076                // eflags controls some aspects of the matching
00077                // behavior.  It's passed directly on to regexec.  (See
00078                // the regexec(3) man page.)
00079                int eflags = 0)
00080     {
00081       return regexec(&(this->parsed), string, nmatch, pmatch, eflags) == 0;
00082     }
00083 
00084     inline bool match(const Text &text,
00085                       size_t nmatch = 0, regmatch_t *pmatch = 0,
00086                       int eflags = 0)
00087     {
00088       return match(text.cchars(), nmatch, pmatch, eflags);
00089     }
00090 
00091     inline bool operator()(const char *string,
00092                            size_t nmatch = 0, regmatch_t *pmatch = 0,
00093                            int eflags = 0)
00094     {
00095       return match(string, nmatch, pmatch, eflags);
00096     }
00097 
00098     inline bool operator()(const Text &text,
00099                            size_t nmatch = 0, regmatch_t *pmatch = 0,
00100                            int eflags = 0)
00101     {
00102       return match(text.cchars(), nmatch, pmatch, eflags);
00103     }
00104 
00105     inline size_t nsubs(void)
00106     {
00107             return parsed.re_nsub;
00108     }
00109 
00110   private:
00111     // Hide copy constructor and assignment operator as there's no way
00112     // to copy the parsed regular expression.
00113     RegExp(const RegExp &);
00114     void operator=(const RegExp &);
00115 
00116     // This does the actual work for the constructors since they're
00117     // both exactly the same other then the type of the first argument
00118     void constructor_inner(const char *re, int cflags)
00119             throw(RegExp::ParseError);
00120   };
00121 }
00122 
00123 #endif

Generated on Mon May 8 00:48:28 2006 for Vesta by  doxygen 1.4.2