|
|
|
regExp.HGo to the documentation of this file.00001 /*---------------------------------------------------------------------------*\ 00002 ========= | 00003 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox 00004 \\ / O peration | 00005 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. 00006 \\/ M anipulation | 00007 ------------------------------------------------------------------------------- 00008 License 00009 This file is part of OpenFOAM. 00010 00011 OpenFOAM is free software; you can redistribute it and/or modify it 00012 under the terms of the GNU General Public License as published by the 00013 Free Software Foundation; either version 2 of the License, or (at your 00014 option) any later version. 00015 00016 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT 00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00018 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00019 for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with OpenFOAM; if not, write to the Free Software Foundation, 00023 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00024 00025 Class 00026 Foam::regExp 00027 00028 Description 00029 Wrapper around POSIX extended regular expressions. 00030 00031 SeeAlso 00032 The manpage regex(7) for more information about POSIX regular expressions. 00033 These differ somewhat from @c Perl and @c sed regular expressions. 00034 00035 SourceFiles 00036 regExp.C 00037 00038 \*---------------------------------------------------------------------------*/ 00039 00040 #ifndef regExp_H 00041 #define regExp_H 00042 00043 #include <regex.h> 00044 #include <string> 00045 00046 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00047 00048 namespace Foam 00049 { 00050 00051 // Forward declaration of classes 00052 class string; 00053 template<class T> class List; 00054 00055 /*---------------------------------------------------------------------------*\ 00056 Class regExp Declaration 00057 \*---------------------------------------------------------------------------*/ 00058 00059 class regExp 00060 { 00061 // Private data 00062 00063 //- Precompiled regular expression 00064 mutable regex_t* preg_; 00065 00066 // Private member functions 00067 00068 //- Disallow default bitwise copy construct 00069 regExp(const regExp&); 00070 00071 //- Disallow default bitwise assignment 00072 void operator=(const regExp&); 00073 00074 public: 00075 00076 //- Is character a regular expression meta-character? 00077 // any character: '.' \n 00078 // quantifiers: '*', '+', '?' \n 00079 // grouping: '(', '|', ')' \n 00080 // range: '[', ']' \n 00081 // 00082 // Don't bother checking for '{digit}' bounds 00083 inline static bool meta(char c) 00084 { 00085 return 00086 ( 00087 (c == '.') // any character 00088 || (c == '*' || c == '+' || c == '?') // quantifiers 00089 || (c == '(' || c == ')' || c == '|') // grouping/branching 00090 || (c == '[' || c == ']') // range 00091 ); 00092 } 00093 00094 00095 // Constructors 00096 00097 //- Construct null 00098 regExp(); 00099 00100 //- Construct from character array, optionally ignoring case 00101 regExp(const char*, const bool ignoreCase=false); 00102 00103 //- Construct from std::string (or string), optionally ignoring case 00104 regExp(const std::string&, const bool ignoreCase=false); 00105 00106 // Destructor 00107 00108 ~regExp(); 00109 00110 00111 // Member functions 00112 00113 //- Access 00114 00115 //- Return true if a precompiled expression does not exist 00116 inline bool empty() const 00117 { 00118 return !preg_; 00119 } 00120 00121 //- Does a precompiled expression exist? 00122 inline bool exists() const 00123 { 00124 return preg_ ? true : false; 00125 } 00126 00127 //- Return the number of (groups) 00128 inline int ngroups() const 00129 { 00130 return preg_ ? preg_->re_nsub : 0; 00131 } 00132 00133 00134 //- Editing 00135 00136 //- Compile pattern into a regular expression, optionally ignoring case 00137 void set(const char*, const bool ignoreCase=false) const; 00138 00139 //- Compile pattern into a regular expression, optionally ignoring case 00140 void set(const std::string&, const bool ignoreCase=false) const; 00141 00142 00143 //- Release precompiled expression. 00144 // Returns true if precompiled expression existed before clear 00145 bool clear() const; 00146 00147 00148 //- Searching 00149 00150 //- Find position within string. 00151 // Returns the index where it begins or string::npos if not found 00152 std::string::size_type find(const std::string& str) const; 00153 00154 //- Return true if it matches the entire string 00155 // The begin-of-line (^) and end-of-line ($) anchors are implicit 00156 bool match(const std::string&) const; 00157 00158 //- Return true if it matches and sets the sub-groups matched 00159 // The begin-of-line (^) and end-of-line ($) anchors are implicit 00160 bool match(const string&, List<string>& groups) const; 00161 00162 //- Return true if the regex was found in within string 00163 bool search(const std::string& str) const 00164 { 00165 return std::string::npos != find(str); 00166 } 00167 00168 00169 // Member Operators 00170 00171 //- Assign and compile pattern from a character array 00172 // Always case sensitive 00173 void operator=(const char*); 00174 00175 //- Assign and compile pattern from string 00176 // Always case sensitive 00177 void operator=(const std::string&); 00178 00179 }; 00180 00181 00182 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00183 00184 } // End namespace Foam 00185 00186 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00187 00188 #endif 00189 00190 // ************************************************************************* // |