initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OSspecific / POSIX / regExp.H
blob59804082e6a31f0e2a7807ede3d68b947257bbc0
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::regExp
28 Description
29     Wrapper around POSIX extended regular expressions.
31 SeeAlso
32     The manpage regex(7) for more information about POSIX regular expressions.
33     These differ somewhat from @c Perl and @c sed regular expressions.
35 SourceFiles
36     regExp.C
38 \*---------------------------------------------------------------------------*/
40 #ifndef regExp_H
41 #define regExp_H
43 #include <regex.h>
44 #include <string>
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 namespace Foam
51 // Forward declaration of classes
52 class string;
53 template<class T> class List;
55 /*---------------------------------------------------------------------------*\
56                            Class regExp Declaration
57 \*---------------------------------------------------------------------------*/
59 class regExp
61     // Private data
63         //- Precompiled regular expression
64         mutable regex_t* preg_;
66     // Private member functions
68         //- Disallow default bitwise copy construct
69         regExp(const regExp&);
71         //- Disallow default bitwise assignment
72         void operator=(const regExp&);
74 public:
76         //- Is character a regular expression meta-character?
77         //  any character: '.' \n
78         //  quantifiers: '*', '+', '?' \n
79         //  grouping: '(', '|', ')' \n
80         //  range: '[', ']' \n
81         //
82         //  Don't bother checking for '{digit}' bounds
83         inline static bool meta(char c)
84         {
85             return
86             (
87                 (c == '.')                           // any character
88              || (c == '*' || c == '+' || c == '?')   // quantifiers
89              || (c == '(' || c == ')' || c == '|')   // grouping/branching
90              || (c == '[' || c == ']')               // range
91             );
92         }
95     // Constructors
97         //- Construct null
98         regExp();
100         //- Construct from character array, optionally ignoring case
101         regExp(const char*, const bool ignoreCase=false);
103         //- Construct from std::string (or string), optionally ignoring case
104         regExp(const std::string&, const bool ignoreCase=false);
106     // Destructor
108         ~regExp();
111     // Member functions
113     //- Access
115         //- Return true if a precompiled expression does not exist
116         inline bool empty() const
117         {
118             return !preg_;
119         }
121         //- Does a precompiled expression exist?
122         inline bool exists() const
123         {
124             return preg_ ? true : false;
125         }
127         //- Return the number of (groups)
128         inline int ngroups() const
129         {
130             return preg_ ? preg_->re_nsub : 0;
131         }
134     //- Editing
136         //- Compile pattern into a regular expression, optionally ignoring case
137         void set(const char*, const bool ignoreCase=false) const;
139         //- Compile pattern into a regular expression, optionally ignoring case
140         void set(const std::string&, const bool ignoreCase=false) const;
143         //- Release precompiled expression.
144         //  Returns true if precompiled expression existed before clear
145         bool clear() const;
148     //- Searching
150         //- Find position within string.
151         //  Returns the index where it begins or string::npos if not found
152         std::string::size_type find(const std::string& str) const;
154         //- Return true if it matches the entire string
155         //  The begin-of-line (^) and end-of-line ($) anchors are implicit
156         bool match(const std::string&) const;
158         //- Return true if it matches and sets the sub-groups matched
159         //  The begin-of-line (^) and end-of-line ($) anchors are implicit
160         bool match(const string&, List<string>& groups) const;
162         //- Return true if the regex was found in within string
163         bool search(const std::string& str) const
164         {
165             return std::string::npos != find(str);
166         }
169     // Member Operators
171         //- Assign and compile pattern from a character array
172         //  Always case sensitive
173         void operator=(const char*);
175         //- Assign and compile pattern from string
176         //  Always case sensitive
177         void operator=(const std::string&);
182 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184 } // End namespace Foam
186 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
188 #endif
190 // ************************************************************************* //