simple_regex.h

Go to the documentation of this file.
00001 #ifndef REGEX_HEADER_INCLUDED
00002 #define REGEX_HEADER_INCLUDED
00003 
00004 //
00005 // Copyright 2002, 2009 Lowell Boggs Jr.
00006 //
00007 // This file or directory, containing source code for a computer program,
00008 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00009 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00010 // source file without charge or obligation so long as you agree to
00011 // the following:
00012 //
00013 //  1.  You must indemnify Lowell Boggs against any and all financial
00014 //      obligations caused by its use, misuse, function, or malfunction.
00015 //      Further, you acknowledge that there is no warranty of any kind,
00016 //      whatsoever.
00017 //
00018 //  2.  You agree not to attempt to patent any portion of this original
00019 //      work -- though you may attempt to patent your own extensions to
00020 //      it if you so choose.
00021 //
00022 //  3.  You keep this copyright notice with the file and all copies
00023 //      of the file and do not change it anyway except language translation.
00024 //
00025 // You are responsible for enforcing your own compliance with these
00026 // conditions and may not use this source file if you cannot agree to the
00027 // above terms and conditions.
00028 
00029 
00030 
00031 
00032 #include <string>
00033 #include <regex.h>
00034 
00035 namespace cxxtls
00036 {
00037 
00038 //@File
00042 
00043 class SimpleRegex
00054 {
00055 public:
00056 
00057   struct match     
00060   {
00061       int offset;  /* location in the 'string' where the regexp was found */
00062       int length;
00063   };
00064 
00065 private:
00066 
00067   regex_t compiled_expression; 
00068 
00069 
00070   std::string expression_;     
00071   std::string options_;        
00072 
00073   enum constants_
00074   {
00075     max_matches=20
00076   };
00077 
00078   mutable match matches_[max_matches]; 
00079 
00080   mutable char const *error_;          
00081 
00082   bool ok_;                            
00083 
00084   mutable int match_count_;            
00085 
00086                                 
00087   std::string expand(std::string const&, std::string const&) const
00089     ;
00090 
00091 public:
00092 
00093 
00094   SimpleRegex(std::string const &expression="", std::string const &options="")
00103       ;
00104 
00105 
00106   SimpleRegex(char const *expression, char const *options="")
00115       ;
00116 
00117   SimpleRegex(SimpleRegex const &r);              
00118 
00119   SimpleRegex &operator=(SimpleRegex const &r); 
00120   SimpleRegex &operator=(std::string const &r); 
00121   SimpleRegex &operator=(char const *r);        
00122 
00123 
00124   void reset(std::string const &expression, std::string const &options)
00125   {
00126     this->SimpleRegex::~SimpleRegex();
00127 
00128     new(this) SimpleRegex(expression, options);
00129 
00130   }
00131 
00132   // the following operators return the number of matches.
00133   int operator() (char const *string, int stringLength) const;
00136 
00137   int operator() (std::string const &s,
00138                   int offset=0,
00139                   int length=0x7ffffff) const
00142    ;
00143 
00144   bool ok() const { return ok_ && error_ == 0; } 
00145 
00146   std::string const &options() const { return options_; }
00147 
00148   char const *error() const { return error_; } 
00149 
00150   bool anchored() const
00152     ;
00153 
00154   bool empty() const { return expression_.empty(); }
00156 
00157   operator std::string const &() const { return expression_; } 
00158 
00159   ~SimpleRegex();  
00160 
00161   match const *matches() const { return matches_; }  
00162 
00163   std::string substitute(std::string const &original,
00164                          std::string const &replacement) const
00185     ;
00186 
00187   bool replace(std::string       &rv, 
00188                std::string const &source, 
00189                std::string const &replacement, 
00190                int                maxIterations=0x7fffffff
00191               ) const
00195     ;
00196 
00197 };
00198 
00199 } // namespace cxxtls
00200 
00201 #endif
Generated on Wed Feb 29 22:50:04 2012 for CXXUtilities by  doxygen 1.6.3