Remove all unnecessary HAVE_CONFIG_H
[gromacs.git] / src / gromacs / utility / gmxregex.cpp
bloba5674e02ded98fa2f4ddba901700dbbf58e1da44
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \internal \file
36 * \brief
37 * Implements regular expression wrappers.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_utility
42 #include "gmxregex.h"
44 #include "config.h"
46 #if defined(HAVE_POSIX_REGEX)
47 // old Mac needs sys/types.h before regex.h
48 #include <sys/types.h>
49 #include <regex.h>
50 #define USE_POSIX_REGEX
51 #elif defined(HAVE_CXX11_REGEX)
52 #include <regex>
53 #define USE_CXX11_REGEX
54 #endif
56 #include "exceptions.h"
57 #include "stringutil.h"
59 namespace gmx
62 // static
63 bool Regex::isSupported()
65 #if defined(USE_POSIX_REGEX) || defined(USE_CXX11_REGEX)
66 return true;
67 #else
68 return false;
69 #endif
72 #if defined(USE_POSIX_REGEX)
73 class Regex::Impl
75 public:
76 explicit Impl(const char *value)
78 compile(value);
80 explicit Impl(const std::string &value)
82 compile(value.c_str());
84 ~Impl()
86 regfree(&regex_);
89 bool match(const char *value) const
91 int rc = regexec(&regex_, value, 0, NULL, 0);
92 if (rc != 0 && rc != REG_NOMATCH)
94 // TODO: Handle errors.
96 return (rc == 0);
99 private:
100 void compile(const char *value)
102 std::string buf(formatString("^%s$", value));
103 int rc = regcomp(&regex_, buf.c_str(), REG_EXTENDED | REG_NOSUB);
104 if (rc != 0)
106 // TODO: Better error messages.
107 GMX_THROW(InvalidInputError(formatString(
108 "Error in regular expression \"%s\"", value)));
112 regex_t regex_;
114 #elif defined(USE_CXX11_REGEX)
115 class Regex::Impl
117 public:
118 explicit Impl(const char *value)
119 try : regex_(value, std::regex::nosubs | std::regex::extended)
122 catch (const std::regex_error &)
124 // TODO: Better error messages.
125 GMX_THROW(InvalidInputError(formatString(
126 "Error in regular expression \"%s\"", value)));
128 explicit Impl(const std::string &value)
129 try : regex_(value, std::regex::nosubs | std::regex::extended)
132 catch (const std::regex_error &)
134 // TODO: Better error messages.
135 GMX_THROW(InvalidInputError(formatString(
136 "Error in regular expression \"%s\"", value)));
139 bool match(const char *value) const
143 return std::regex_match(value, regex_);
145 catch (const std::regex_error &)
147 // TODO: Handle errors.
148 return false;
152 private:
153 std::regex regex_;
155 #else
156 class Regex::Impl
158 public:
159 explicit Impl(const char * /*value*/)
161 GMX_THROW(NotImplementedError(
162 "Gromacs is compiled without regular expression support"));
164 explicit Impl(const std::string & /*value*/)
166 GMX_THROW(NotImplementedError(
167 "Gromacs is compiled without regular expression support"));
170 bool match(const char * /*value*/) const
172 // Should never be reached.
173 GMX_THROW(NotImplementedError(
174 "Gromacs is compiled without regular expression support"));
177 #endif
179 Regex::Regex()
180 : impl_(NULL)
184 Regex::Regex(const char *value)
185 : impl_(new Impl(value))
189 Regex::Regex(const std::string &value)
190 : impl_(new Impl(value))
194 Regex::~Regex()
198 /*! \cond libapi */
199 bool regexMatch(const char *str, const Regex &regex)
201 if (regex.impl_.get() == NULL)
203 return false;
205 return regex.impl_->match(str);
208 bool regexMatch(const std::string &str, const Regex &regex)
210 return regexMatch(str.c_str(), regex);
212 //! \endcond
214 } // namespace gmx