Manually sort some includes in src/gromacs
[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 "gmxpre.h"
43
44 #include "gmxregex.h"
45
46 #include "config.h"
47
48 #if defined(HAVE_POSIX_REGEX)
49 #include <sys/types.h>
50 // old Mac needs sys/types.h before regex.h
51 #include <regex.h>
52 #define USE_POSIX_REGEX
53 #elif defined(HAVE_CXX11_REGEX)
54 #include <regex>
55 #define USE_CXX11_REGEX
56 #endif
57
58 #include "gromacs/utility/exceptions.h"
59 #include "gromacs/utility/stringutil.h"
60
61 namespace gmx
62 {
63
64 // static
65 bool Regex::isSupported()
66 {
67 #if defined(USE_POSIX_REGEX) || defined(USE_CXX11_REGEX)
68     return true;
69 #else
70     return false;
71 #endif
72 }
73
74 #if defined(USE_POSIX_REGEX)
75 class Regex::Impl
76 {
77     public:
78         explicit Impl(const char *value)
79         {
80             compile(value);
81         }
82         explicit Impl(const std::string &value)
83         {
84             compile(value.c_str());
85         }
86         ~Impl()
87         {
88             regfree(&regex_);
89         }
90
91         bool match(const char *value) const
92         {
93             int rc = regexec(&regex_, value, 0, NULL, 0);
94             if (rc != 0 && rc != REG_NOMATCH)
95             {
96                 // TODO: Handle errors.
97             }
98             return (rc == 0);
99         }
100
101     private:
102         void compile(const char *value)
103         {
104             std::string buf(formatString("^%s$", value));
105             int         rc = regcomp(&regex_, buf.c_str(), REG_EXTENDED | REG_NOSUB);
106             if (rc != 0)
107             {
108                 // TODO: Better error messages.
109                 GMX_THROW(InvalidInputError(formatString(
110                                                     "Error in regular expression \"%s\"", value)));
111             }
112         }
113
114         regex_t                 regex_;
115 };
116 #elif defined(USE_CXX11_REGEX)
117 class Regex::Impl
118 {
119     public:
120         explicit Impl(const char *value)
121         try : regex_(value, std::regex::nosubs | std::regex::extended)
122         {
123         }
124         catch (const std::regex_error &)
125         {
126             // TODO: Better error messages.
127             GMX_THROW(InvalidInputError(formatString(
128                                                 "Error in regular expression \"%s\"", value)));
129         }
130         explicit Impl(const std::string &value)
131         try : regex_(value, std::regex::nosubs | std::regex::extended)
132         {
133         }
134         catch (const std::regex_error &)
135         {
136             // TODO: Better error messages.
137             GMX_THROW(InvalidInputError(formatString(
138                                                 "Error in regular expression \"%s\"", value)));
139         }
140
141         bool match(const char *value) const
142         {
143             try
144             {
145                 return std::regex_match(value, regex_);
146             }
147             catch (const std::regex_error &)
148             {
149                 // TODO: Handle errors.
150                 return false;
151             }
152         }
153
154     private:
155         std::regex              regex_;
156 };
157 #else
158 class Regex::Impl
159 {
160     public:
161         explicit Impl(const char * /*value*/)
162         {
163             GMX_THROW(NotImplementedError(
164                               "Gromacs is compiled without regular expression support"));
165         }
166         explicit Impl(const std::string & /*value*/)
167         {
168             GMX_THROW(NotImplementedError(
169                               "Gromacs is compiled without regular expression support"));
170         }
171
172         bool match(const char * /*value*/) const
173         {
174             // Should never be reached.
175             GMX_THROW(NotImplementedError(
176                               "Gromacs is compiled without regular expression support"));
177         }
178 };
179 #endif
180
181 Regex::Regex()
182     : impl_(NULL)
183 {
184 }
185
186 Regex::Regex(const char *value)
187     : impl_(new Impl(value))
188 {
189 }
190
191 Regex::Regex(const std::string &value)
192     : impl_(new Impl(value))
193 {
194 }
195
196 Regex::~Regex()
197 {
198 }
199
200 /*! \cond libapi */
201 bool regexMatch(const char *str, const Regex &regex)
202 {
203     if (regex.impl_.get() == NULL)
204     {
205         return false;
206     }
207     return regex.impl_->match(str);
208 }
209
210 bool regexMatch(const std::string &str, const Regex &regex)
211 {
212     return regexMatch(str.c_str(), regex);
213 }
214 //! \endcond
215
216 } // namespace gmx