Code beautification with uncrustify
[alexxy/gromacs.git] / src / gromacs / utility / gmxregex.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Implements regular expression wrappers.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_utility
37  */
38 #include "gmxregex.h"
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #if defined(HAVE_POSIX_REGEX)
45 // old Mac needs sys/types.h before regex.h
46 #include <sys/types.h>
47 #include <regex.h>
48 #define USE_POSIX_REGEX
49 #elif defined(HAVE_CXX11_REGEX)
50 #include <regex>
51 #define USE_CXX11_REGEX
52 #endif
53
54 #include "exceptions.h"
55 #include "stringutil.h"
56
57 namespace gmx
58 {
59
60 // static
61 bool Regex::isSupported()
62 {
63 #if defined(USE_POSIX_REGEX) || defined(USE_CXX11_REGEX)
64     return true;
65 #else
66     return false;
67 #endif
68 }
69
70 #if defined(USE_POSIX_REGEX)
71 class Regex::Impl
72 {
73     public:
74         explicit Impl(const char *value)
75         {
76             compile(value);
77         }
78         explicit Impl(const std::string &value)
79         {
80             compile(value.c_str());
81         }
82         ~Impl()
83         {
84             regfree(&regex_);
85         }
86
87         bool match(const char *value) const
88         {
89             int rc = regexec(&regex_, value, 0, NULL, 0);
90             if (rc != 0 && rc != REG_NOMATCH)
91             {
92                 // TODO: Handle errors.
93             }
94             return (rc == 0);
95         }
96
97     private:
98         void compile(const char *value)
99         {
100             std::string buf(formatString("^%s$", value));
101             int         rc = regcomp(&regex_, buf.c_str(), REG_EXTENDED | REG_NOSUB);
102             if (rc != 0)
103             {
104                 // TODO: Better error messages.
105                 GMX_THROW(InvalidInputError(formatString(
106                                                     "Error in regular expression \"%s\"", value)));
107             }
108         }
109
110         regex_t                 regex_;
111 };
112 #elif defined(USE_CXX11_REGEX)
113 class Regex::Impl
114 {
115     public:
116         explicit Impl(const char *value)
117         try : regex_(value, std::regex::nosubs | std::regex::extended)
118         {
119         }
120         catch (const std::regex_error &)
121         {
122             // TODO: Better error messages.
123             GMX_THROW(InvalidInputError(formatString(
124                                                 "Error in regular expression \"%s\"", value)));
125         }
126         explicit Impl(const std::string &value)
127         try : regex_(value, std::regex::nosubs | std::regex::extended)
128         {
129         }
130         catch (const std::regex_error &)
131         {
132             // TODO: Better error messages.
133             GMX_THROW(InvalidInputError(formatString(
134                                                 "Error in regular expression \"%s\"", value)));
135         }
136
137         bool match(const char *value) const
138         {
139             try
140             {
141                 return std::regex_match(value, regex_);
142             }
143             catch (const std::regex_error &)
144             {
145                 // TODO: Handle errors.
146                 return false;
147             }
148         }
149
150     private:
151         std::regex              regex_;
152 };
153 #else
154 class Regex::Impl
155 {
156     public:
157         explicit Impl(const char * /*value*/)
158         {
159             GMX_THROW(NotImplementedError(
160                               "Gromacs is compiled without regular expression support"));
161         }
162         explicit Impl(const std::string & /*value*/)
163         {
164             GMX_THROW(NotImplementedError(
165                               "Gromacs is compiled without regular expression support"));
166         }
167
168         bool match(const char * /*value*/) const
169         {
170             // Should never be reached.
171             GMX_THROW(NotImplementedError(
172                               "Gromacs is compiled without regular expression support"));
173         }
174 };
175 #endif
176
177 Regex::Regex()
178     : impl_(NULL)
179 {
180 }
181
182 Regex::Regex(const char *value)
183     : impl_(new Impl(value))
184 {
185 }
186
187 Regex::Regex(const std::string &value)
188     : impl_(new Impl(value))
189 {
190 }
191
192 Regex::~Regex()
193 {
194 }
195
196 /*! \cond libapi */
197 bool regexMatch(const char *str, const Regex &regex)
198 {
199     if (regex.impl_.get() == NULL)
200     {
201         return false;
202     }
203     return regex.impl_->match(str);
204 }
205
206 bool regexMatch(const std::string &str, const Regex &regex)
207 {
208     return regexMatch(str.c_str(), regex);
209 }
210 //! \endcond
211
212 } // namespace gmx