Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / utility / gmxregex.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements regular expression wrappers.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_utility
41  */
42 #include "gmxregex.h"
43
44 #include "config.h"
45
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
55
56 #include "exceptions.h"
57 #include "stringutil.h"
58
59 namespace gmx
60 {
61
62 // static
63 bool Regex::isSupported()
64 {
65 #if defined(USE_POSIX_REGEX) || defined(USE_CXX11_REGEX)
66     return true;
67 #else
68     return false;
69 #endif
70 }
71
72 #if defined(USE_POSIX_REGEX)
73 class Regex::Impl
74 {
75     public:
76         explicit Impl(const char *value)
77         {
78             compile(value);
79         }
80         explicit Impl(const std::string &value)
81         {
82             compile(value.c_str());
83         }
84         ~Impl()
85         {
86             regfree(&regex_);
87         }
88
89         bool match(const char *value) const
90         {
91             int rc = regexec(&regex_, value, 0, NULL, 0);
92             if (rc != 0 && rc != REG_NOMATCH)
93             {
94                 // TODO: Handle errors.
95             }
96             return (rc == 0);
97         }
98
99     private:
100         void compile(const char *value)
101         {
102             std::string buf(formatString("^%s$", value));
103             int         rc = regcomp(&regex_, buf.c_str(), REG_EXTENDED | REG_NOSUB);
104             if (rc != 0)
105             {
106                 // TODO: Better error messages.
107                 GMX_THROW(InvalidInputError(formatString(
108                                                     "Error in regular expression \"%s\"", value)));
109             }
110         }
111
112         regex_t                 regex_;
113 };
114 #elif defined(USE_CXX11_REGEX)
115 class Regex::Impl
116 {
117     public:
118         explicit Impl(const char *value)
119         try : regex_(value, std::regex::nosubs | std::regex::extended)
120         {
121         }
122         catch (const std::regex_error &)
123         {
124             // TODO: Better error messages.
125             GMX_THROW(InvalidInputError(formatString(
126                                                 "Error in regular expression \"%s\"", value)));
127         }
128         explicit Impl(const std::string &value)
129         try : regex_(value, std::regex::nosubs | std::regex::extended)
130         {
131         }
132         catch (const std::regex_error &)
133         {
134             // TODO: Better error messages.
135             GMX_THROW(InvalidInputError(formatString(
136                                                 "Error in regular expression \"%s\"", value)));
137         }
138
139         bool match(const char *value) const
140         {
141             try
142             {
143                 return std::regex_match(value, regex_);
144             }
145             catch (const std::regex_error &)
146             {
147                 // TODO: Handle errors.
148                 return false;
149             }
150         }
151
152     private:
153         std::regex              regex_;
154 };
155 #else
156 class Regex::Impl
157 {
158     public:
159         explicit Impl(const char * /*value*/)
160         {
161             GMX_THROW(NotImplementedError(
162                               "Gromacs is compiled without regular expression support"));
163         }
164         explicit Impl(const std::string & /*value*/)
165         {
166             GMX_THROW(NotImplementedError(
167                               "Gromacs is compiled without regular expression support"));
168         }
169
170         bool match(const char * /*value*/) const
171         {
172             // Should never be reached.
173             GMX_THROW(NotImplementedError(
174                               "Gromacs is compiled without regular expression support"));
175         }
176 };
177 #endif
178
179 Regex::Regex()
180     : impl_(NULL)
181 {
182 }
183
184 Regex::Regex(const char *value)
185     : impl_(new Impl(value))
186 {
187 }
188
189 Regex::Regex(const std::string &value)
190     : impl_(new Impl(value))
191 {
192 }
193
194 Regex::~Regex()
195 {
196 }
197
198 /*! \cond libapi */
199 bool regexMatch(const char *str, const Regex &regex)
200 {
201     if (regex.impl_.get() == NULL)
202     {
203         return false;
204     }
205     return regex.impl_->match(str);
206 }
207
208 bool regexMatch(const std::string &str, const Regex &regex)
209 {
210     return regexMatch(str.c_str(), regex);
211 }
212 //! \endcond
213
214 } // namespace gmx