a80f523beeb616cf151b7bb74db229f1661f3b13
[alexxy/gromacs.git] / src / gromacs / utility / gmxregex.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013, 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 /*! \libinternal \file
36  * \brief
37  * Declares simple wrapper for regular expression functionality.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_utility
42  */
43 #ifndef GMX_UTILITY_GMXREGEX_H
44 #define GMX_UTILITY_GMXREGEX_H
45
46 #include <string>
47
48 #include "common.h"
49
50 namespace gmx
51 {
52
53 class Regex;
54
55 /*! \cond libapi */
56 /*! \libinternal \brief
57  * Matches a string with a regular expression.
58  *
59  * \param[in] str   String to match.
60  * \param[in] regex Regular expression to match.
61  * \returns   true if \p regex matches the whole \p str.
62  *
63  * Does not throw currently, but this is subject to change if/when better error
64  * handling is implemented (currently, it returns false if the matching fails,
65  * e.g., because of out-of-memory).
66  *
67  * \ingroup module_utility
68  */
69 bool regexMatch(const char *str, const Regex &regex);
70 //! \copydoc regexMatch(const char *, const Regex &)
71 bool regexMatch(const std::string &str, const Regex &regex);
72 //! \endcond
73
74 /*! \libinternal \brief
75  * Represents a regular expression.
76  *
77  * This class provides a simple interface for regular expression construction.
78  * regexMatch() is used to match the regular expression against a string.
79  * POSIX extended regular expression syntax is used.
80  *
81  * Currently, isSupported() will return true if either
82  *
83  *  -# POSIX regular expression header <regex.h> is available, or
84  *  -# C++11 header \<regex> is available (e.g., new enough MSVC has this).
85  *
86  * In other cases, isSupported() returns false and calling other
87  * constructors than the default constructor throws an exception.
88  *
89  * \see regexMatch()
90  *
91  * \inlibraryapi
92  * \ingroup module_utility
93  */
94 class Regex
95 {
96     public:
97         /*! \brief
98          * Returns true if regular expression support has been compiled in.
99          *
100          * Does not throw.
101          */
102         static bool isSupported();
103
104         /*! \brief
105          * Constructs a regular expression that matches nothing.
106          *
107          * Does not throw.
108          */
109         Regex();
110         /*! \brief
111          * Constructs a regular expression from a string.
112          *
113          * \param[in] value  String to compile into a regular expression.
114          * \throws    std::bad_alloc if out of memory.
115          * \throws    InvalidInputError if \p value is not a valid regular
116          *      expression.
117          *
118          * \todo
119          * Consider whether some other exception type would be better.
120          */
121         explicit Regex(const char *value);
122         //! \copydoc Regex(const char *)
123         explicit Regex(const std::string &value);
124         //! Frees memory allocated for the regular expression.
125         ~Regex();
126
127     private:
128         class Impl;
129
130         PrivateImplPointer<Impl> impl_;
131
132         friend bool regexMatch(const char *str, const Regex &regex);
133         friend bool regexMatch(const std::string &str, const Regex &regex);
134 };
135
136 } // namespace gmx
137
138 #endif