initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / primitives / strings / wordRe / wordRe.H
blob741d76e553d73599a92bef534eb866bea2fd7ae9
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::wordRe
28 Description
29     A wordRe is a word, but can also have a regular expression for matching
30     words.
32     By default the constructors will generally preserve the argument as
33     string literal and the assignment operators will use the wordRe::DETECT
34     compOption to scan the string for regular expression meta characters
35     and/or invalid word characters and react accordingly.
37     The exceptions are when constructing/assigning from another
38     Foam::wordRe (preserve the same type) or from a Foam::word (always
39     literal).
41 Note
42     If the string contents are changed - eg, by the operator+=() or by
43     string::replace(), etc - it will be necessary to use compile() or
44     recompile() to synchronize the regular expression.
46 SourceFiles
47     wordRe.C
48     wordReIO.C
50 \*---------------------------------------------------------------------------*/
52 #ifndef wordRe_H
53 #define wordRe_H
55 #include "word.H"
56 #include "regExp.H"
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60 namespace Foam
63 // Forward declaration of friend functions and operators
64 class wordRe;
66 class Istream;
67 class Ostream;
69 Istream& operator>>(Istream&, wordRe&);
70 Ostream& operator<<(Ostream&, const wordRe&);
73 /*---------------------------------------------------------------------------*\
74                            Class wordRe Declaration
75 \*---------------------------------------------------------------------------*/
77 class wordRe
79     public word
81     // Private member data
83         //- The regular expression
84         mutable regExp re_;
86 public:
88     // Public data types
90         //- Enumeration with compile options
91         //  Note that 'REGEXP' is implicit if 'NOCASE' is specified alone.
92         enum compOption
93         {
94             LITERAL = 0, /*!< treat as a string literal */
95             DETECT  = 1, /*!< treat as regular expression */
96             REGEXP  = 2, /*!< detect if the string contains meta-characters */
97             NOCASE  = 4, /*!< ignore case in regular expression */
98             DETECT_NOCASE = DETECT | NOCASE,
99             REGEXP_NOCASE = REGEXP | NOCASE
100         };
103         //- Is this a meta character?
104         static inline bool meta(char);
106         //- Test string for regular expression meta characters
107         static inline bool isPattern(const string&);
109     // Constructors
111         //- Construct null
112         inline wordRe();
114         //- Construct as copy
115         inline wordRe(const wordRe&);
117         //- Construct as copy of word
118         inline wordRe(const word&);
120         //- Construct as copy of character array
121         //  Optionally specify how it should be treated.
122         inline wordRe(const char*, const compOption=LITERAL);
124         //- Construct as copy of string.
125         //  Optionally specify how it should be treated.
126         inline wordRe(const string&, const compOption=LITERAL);
128         //- Construct as copy of std::string
129         //  Optionally specify how it should be treated.
130         inline wordRe(const std::string&, const compOption=LITERAL);
132         //- Construct from Istream
133         //  Words are treated as literals, strings with an auto-test
134         wordRe(Istream&);
136     // Member functions
138     //- Access
140         //- Should be treated as a match rather than a literal string?
141         inline bool isPattern() const;
143     //- Infrastructure
145         //- Compile the regular expression
146         inline bool compile() const;
148         //- Possibly compile the regular expression, with greater control
149         inline bool compile(const compOption) const;
151         //- Recompile an existing regular expression
152         inline bool recompile() const;
154         //- Frees precompiled regular expression, making wordRe a literal.
155         //  Optionally strips invalid word characters
156         inline void uncompile(const bool doStripInvalid=false) const;
158     //- Editing
160         //- Copy string, auto-test for regular expression or other options
161         inline void set(const std::string&, const compOption=DETECT);
163         //- Copy string, auto-test for regular expression or other options
164         inline void set(const char*, const compOption=DETECT);
166         //- Clear string and precompiled regular expression
167         inline void clear();
169     //- Searching
171         //- Smart match as regular expression or as a string
172         //  Optionally specify a literal match only
173         inline bool match(const string&, bool literalMatch=false) const;
175     //- Miscellaneous
177         //- Return a string with quoted meta-characters
178         inline string quotemeta() const;
180         //- Output some basic info
181         Ostream& info(Ostream&) const;
184     // Member operators
186         // Assignment
188             //- Assign copy
189             //  Always case sensitive
190             inline const wordRe& operator=(const wordRe&);
192             //- Copy word, never a regular expression
193             inline const wordRe& operator=(const word&);
195             //- Copy string, auto-test for regular expression
196             //  Always case sensitive
197             inline const wordRe& operator=(const string&);
199             //- Copy string, auto-test for regular expression
200             //  Always case sensitive
201             inline const wordRe& operator=(const std::string&);
203             //- Copy string, auto-test for regular expression
204             //  Always case sensitive
205             inline const wordRe& operator=(const char*);
208     // IOstream operators
210         friend Istream& operator>>(Istream&, wordRe&);
211         friend Ostream& operator<<(Ostream&, const wordRe&);
215 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
217 } // End namespace Foam
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
221 #include "wordReI.H"
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
225 #endif
227 // ************************************************************************* //