FreeFOAM The Cross-Platform CFD Toolkit
wordRe.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::wordRe
26 
27 Description
28  A wordRe is a word, but can also have a regular expression for matching
29  words.
30 
31  By default the constructors will generally preserve the argument as
32  string literal and the assignment operators will use the wordRe::DETECT
33  compOption to scan the string for regular expression meta characters
34  and/or invalid word characters and react accordingly.
35 
36  The exceptions are when constructing/assigning from another
37  Foam::wordRe (preserve the same type) or from a Foam::word (always
38  literal).
39 
40 Note
41  If the string contents are changed - eg, by the operator+=() or by
42  string::replace(), etc - it will be necessary to use compile() or
43  recompile() to synchronize the regular expression.
44 
45 SourceFiles
46  wordRe.C
47  wordReIO.C
48 
49 \*---------------------------------------------------------------------------*/
50 
51 #ifndef wordRe_H
52 #define wordRe_H
53 
54 #include <OpenFOAM/word.H>
55 #include <OSspecific/regExp.H>
56 
57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58 
59 namespace Foam
60 {
61 
62 // Forward declaration of friend functions and operators
63 class wordRe;
64 
65 class Istream;
66 class Ostream;
67 
68 Istream& operator>>(Istream&, wordRe&);
69 Ostream& operator<<(Ostream&, const wordRe&);
70 
71 
72 /*---------------------------------------------------------------------------*\
73  Class wordRe Declaration
74 \*---------------------------------------------------------------------------*/
75 
76 class wordRe
77 :
78  public word
79 {
80  // Private member data
81 
82  //- The regular expression
83  mutable regExp re_;
84 
85 public:
86 
87  // Public data types
88 
89  //- Enumeration with compile options
90  // Note that 'REGEXP' is implicit if 'NOCASE' is specified alone.
92  {
93  LITERAL = 0,
94  DETECT = 1,
95  REGEXP = 2,
96  NOCASE = 4,
99  };
100 
101 
102  //- Is this a meta character?
103  static inline bool meta(char);
104 
105  //- Test string for regular expression meta characters
106  static inline bool isPattern(const string&);
107 
108  // Constructors
109 
110  //- Construct null
111  inline wordRe();
112 
113  //- Construct as copy
114  inline wordRe(const wordRe&);
115 
116  //- Construct as copy of word
117  inline wordRe(const word&);
118 
119  //- Construct as copy of character array
120  // Optionally specify how it should be treated.
121  inline wordRe(const char*, const compOption=LITERAL);
122 
123  //- Construct as copy of string.
124  // Optionally specify how it should be treated.
125  inline wordRe(const string&, const compOption=LITERAL);
126 
127  //- Construct as copy of std::string
128  // Optionally specify how it should be treated.
129  inline wordRe(const std::string&, const compOption=LITERAL);
130 
131  //- Construct from Istream
132  // Words are treated as literals, strings with an auto-test
133  wordRe(Istream&);
134 
135  // Member functions
136 
137  //- Access
138 
139  //- Should be treated as a match rather than a literal string?
140  inline bool isPattern() const;
141 
142  //- Infrastructure
143 
144  //- Compile the regular expression
145  inline bool compile() const;
146 
147  //- Possibly compile the regular expression, with greater control
148  inline bool compile(const compOption) const;
149 
150  //- Recompile an existing regular expression
151  inline bool recompile() const;
152 
153  //- Frees precompiled regular expression, making wordRe a literal.
154  // Optionally strips invalid word characters
155  inline void uncompile(const bool doStripInvalid=false) const;
156 
157  //- Editing
158 
159  //- Copy string, auto-test for regular expression or other options
160  inline void set(const std::string&, const compOption=DETECT);
161 
162  //- Copy string, auto-test for regular expression or other options
163  inline void set(const char*, const compOption=DETECT);
164 
165  //- Clear string and precompiled regular expression
166  inline void clear();
167 
168  //- Searching
169 
170  //- Smart match as regular expression or as a string
171  // Optionally specify a literal match only
172  inline bool match(const string&, bool literalMatch=false) const;
173 
174  //- Miscellaneous
175 
176  //- Return a string with quoted meta-characters
177  inline string quotemeta() const;
178 
179  //- Output some basic info
180  Ostream& info(Ostream&) const;
181 
182 
183  // Member operators
184 
185  // Assignment
186 
187  //- Assign copy
188  // Always case sensitive
189  inline const wordRe& operator=(const wordRe&);
190 
191  //- Copy word, never a regular expression
192  inline const wordRe& operator=(const word&);
193 
194  //- Copy string, auto-test for regular expression
195  // Always case sensitive
196  inline const wordRe& operator=(const string&);
197 
198  //- Copy string, auto-test for regular expression
199  // Always case sensitive
200  inline const wordRe& operator=(const std::string&);
201 
202  //- Copy string, auto-test for regular expression
203  // Always case sensitive
204  inline const wordRe& operator=(const char*);
205 
206 
207  // IOstream operators
208 
209  friend Istream& operator>>(Istream&, wordRe&);
210  friend Ostream& operator<<(Ostream&, const wordRe&);
211 };
212 
213 
214 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
215 
216 } // End namespace Foam
217 
218 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219 
220 #include "wordReI.H"
221 
222 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
223 
224 #endif
225 
226 // ************************ vim: set sw=4 sts=4 et: ************************ //