sbuild  1.5.3
sbuild-regex.h
00001 /* Copyright © 2006-2007,2012  Roger Leigh <rleigh@debian.org>
00002  *
00003  * schroot is free software: you can redistribute it and/or modify it
00004  * under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation, either version 3 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * schroot is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>.
00016  *
00017  *********************************************************************/
00018 
00019 #ifndef SBUILD_REGEX_H
00020 #define SBUILD_REGEX_H
00021 
00022 #include <istream>
00023 #include <ostream>
00024 #include <string>
00025 
00026 #include <sbuild/sbuild-config.h>
00027 #ifdef HAVE_REGEX_REGEX
00028 # include <regex>
00029 #else
00030 # include <boost/regex.hpp>
00031 namespace std {
00032   using boost::regex;
00033   using boost::regex_error;
00034   using boost::regex_search;
00035 }
00036 #endif
00037 
00038 namespace sbuild
00039 {
00040 
00055   class regex
00056   {
00057   public:
00059     regex ():
00060       comp(),
00061       rstr()
00062     {}
00063 
00071     regex (std::string const& pattern):
00072       comp(pattern, std::regex::extended),
00073       rstr(pattern)
00074     {}
00075 
00083     regex (const char *pattern):
00084       comp(pattern, std::regex::extended),
00085       rstr(pattern)
00086     {}
00087 
00089     ~regex ()
00090     {}
00091 
00099     regex (const regex& rhs):
00100       comp(rhs.comp),
00101       rstr(rhs.rstr)
00102     {}
00103 
00104     std::string const&
00105     str() const
00106     {
00107       return rstr;
00108     }
00109 
00110     bool
00111     compare (regex const& rhs) const
00112     {
00113       return this->rstr != rhs.rstr;
00114     }
00115 
00116     bool
00117     search (std::string const& str) const
00118     {
00119       return std::regex_search(str, this->comp);
00120     }
00121 
00131     template <class charT, class traits>
00132     friend
00133     std::basic_istream<charT,traits>&
00134     operator >> (std::basic_istream<charT,traits>& stream,
00135                  regex&                            rhs)
00136     {
00137       std::string regex;
00138 
00139       if (std::getline(stream, regex))
00140         {
00141           rhs.comp.assign(regex, std::regex::extended);
00142           rhs.rstr = regex;
00143         }
00144 
00145       return stream;
00146     }
00147 
00155     template <class charT, class traits>
00156     friend
00157     std::basic_ostream<charT,traits>&
00158     operator << (std::basic_ostream<charT,traits>& stream,
00159                  regex const&                rhs)
00160     {
00161       return stream << rhs.str();
00162     }
00163 
00164   private:
00166     std::regex comp;
00168     std::string rstr;
00169   };
00170 
00174   inline bool
00175   regex_search (const std::string& str,
00176                 regex const& regex)
00177   {
00178     return regex.search(str);
00179   }
00180 
00181 }
00182 
00183 #endif /* SBUILD_REGEX_H */
00184 
00185 /*
00186  * Local Variables:
00187  * mode:C++
00188  * End:
00189  */