3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements regular expression wrappers.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_utility
44 #if defined(HAVE_REGEX_H)
45 // old Mac needs sys/types.h before regex.h
46 #include <sys/types.h>
48 #define USE_POSIX_REGEX
51 #include "exceptions.h"
52 #include "stringutil.h"
58 bool Regex::isSupported()
60 #if defined(USE_POSIX_REGEX)
67 #if defined(USE_POSIX_REGEX)
71 explicit Impl(const char *value)
75 explicit Impl(const std::string &value)
77 compile(value.c_str());
84 bool match(const char *value) const
86 int rc = regexec(®ex_, value, 0, NULL, 0);
87 if (rc != 0 && rc != REG_NOMATCH)
89 // TODO: Handle errors.
95 void compile(const char *value)
97 std::string buf(formatString("^%s$", value));
98 int rc = regcomp(®ex_, buf.c_str(), REG_EXTENDED | REG_NOSUB);
101 // TODO: Better error messages.
102 GMX_THROW(InvalidInputError(formatString(
103 "Error in regular expression \"%s\"", value)));
113 explicit Impl(const char * /*value*/)
115 GMX_THROW(NotImplementedError(
116 "Gromacs is compiled without regular expression support"));
118 explicit Impl(const std::string & /*value*/)
120 GMX_THROW(NotImplementedError(
121 "Gromacs is compiled without regular expression support"));
124 bool match(const char * /*value*/) const
126 // Should never be reached.
127 GMX_THROW(NotImplementedError(
128 "Gromacs is compiled without regular expression support"));
138 Regex::Regex(const char *value)
139 : impl_(new Impl(value))
143 Regex::Regex(const std::string &value)
144 : impl_(new Impl(value))
153 bool regexMatch(const char *str, const Regex ®ex)
155 if (regex.impl_.get() == NULL)
159 return regex.impl_->match(str);
162 bool regexMatch(const std::string &str, const Regex ®ex)
164 return regexMatch(str.c_str(), regex);