libzypp  10.5.0
Regex.h
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_BASE_REGEX_H
00013 #define ZYPP_BASE_REGEX_H
00014 
00015 #include <iosfwd>
00016 #include <string>
00017 #include <regex.h>
00018 
00019 #include "zypp/base/Exception.h"
00020 
00022 namespace zypp
00023 {
00027   namespace str
00028   {
00053 
00054     typedef Exception regex_error;
00055 
00056     class smatch;
00057     class regex;
00058 
00067     bool regex_match( const char * s, smatch & matches, const regex & regex );
00068 
00070     inline bool regex_match(const std::string & s, smatch & matches, const regex & regex)
00071     { return regex_match( s.c_str(), matches, regex ); }
00072 
00074     bool regex_match( const char * s, const regex & regex );
00075 
00077     inline bool regex_match( const std::string & s, const regex & regex )
00078     { return regex_match( s.c_str(), regex ); }
00079 
00086     class regex
00087     {
00088     public:
00089 
00090       enum RegFlags {
00091         optimize        = 0,            
00092         match_extra     = 0,            
00093         icase           = REG_ICASE,    
00094         nosubs          = REG_NOSUB,    
00095         match_extended  = REG_EXTENDED, 
00096         normal          = 1<<16         
00097       };
00098 
00099       regex();
00100       regex(const std::string& s,int flags = match_extended);
00101       ~regex() throw();
00102 
00103       regex(const regex & rhs)
00104       { assign(rhs.m_str, rhs.m_flags); }
00105 
00106       regex & operator=(const regex & rhs)
00107       { assign(rhs.m_str, rhs.m_flags); return *this; }
00108 
00112       std::string asString() const
00113       { return m_str; }
00114 
00115     public:
00117       regex_t * get()
00118       { return & m_preg; }
00119 
00120     private:
00121       void assign(const std::string& s,int flags = match_extended);
00122 
00123     private:
00124       friend class smatch;
00125       friend bool regex_match(const char * s, str::smatch& matches, const regex& regex);
00126       friend bool regex_match(const char * s,  const regex& regex);
00127       std::string m_str;
00128       int m_flags;
00129       regex_t m_preg;
00130       bool m_valid;
00131     };
00132 
00134     inline std::ostream & operator<<( std::ostream & str, const regex & obj )
00135     { return str << obj.asString(); }
00136 
00148     class smatch
00149     {
00150     public:
00151       smatch();
00152 
00153       std::string operator[](unsigned i) const;
00154 
00155       unsigned size() const;
00156 
00157       std::string match_str;
00158       regmatch_t pmatch[12];
00159     };
00160 
00161   } // namespace str
00163 } // namespace zypp
00165 #endif // ZYPP_BASE_STRING_H