00001 /*---------------------------------------------------------------------------*\ 00002 ========= | 00003 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox 00004 \\ / O peration | 00005 \\ / A nd | Copyright (C) 1991-2010 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 00013 the Free Software Foundation, either version 3 of the License, or 00014 (at your 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, see <http://www.gnu.org/licenses/>. 00023 00024 Class 00025 Foam::regExp 00026 00027 Description 00028 Wrapper around POSIX extended regular expressions. 00029 00030 SeeAlso 00031 The manpage regex(7) for more information about POSIX regular expressions. 00032 These differ somewhat from @c Perl and @c sed regular expressions. 00033 00034 SourceFiles 00035 regExp.C 00036 00037 \*---------------------------------------------------------------------------*/ 00038 00039 #ifndef regExp_H 00040 #define regExp_H 00041 00042 #include <regex.h> 00043 #include <string> 00044 00045 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00046 00047 namespace Foam 00048 { 00049 00050 // Forward declaration of classes 00051 class string; 00052 template<class T> class List; 00053 00054 /*---------------------------------------------------------------------------*\ 00055 Class regExp Declaration 00056 \*---------------------------------------------------------------------------*/ 00057 00058 class regExp 00059 { 00060 // Private data 00061 00062 //- Precompiled regular expression 00063 mutable regex_t* preg_; 00064 00065 // Private member functions 00066 00067 //- Disallow default bitwise copy construct 00068 regExp(const regExp&); 00069 00070 //- Disallow default bitwise assignment 00071 void operator=(const regExp&); 00072 00073 public: 00074 00075 //- Is character a regular expression meta-character? 00076 // any character: '.' \n 00077 // quantifiers: '*', '+', '?' \n 00078 // grouping: '(', '|', ')' \n 00079 // range: '[', ']' \n 00080 // 00081 // Don't bother checking for '{digit}' bounds 00082 inline static bool meta(char c) 00083 { 00084 return 00085 ( 00086 (c == '.') // any character 00087 || (c == '*' || c == '+' || c == '?') // quantifiers 00088 || (c == '(' || c == ')' || c == '|') // grouping/branching 00089 || (c == '[' || c == ']') // range 00090 ); 00091 } 00092 00093 00094 // Constructors 00095 00096 //- Construct null 00097 regExp(); 00098 00099 //- Construct from character array, optionally ignoring case 00100 regExp(const char*, const bool ignoreCase=false); 00101 00102 //- Construct from std::string (or string), optionally ignoring case 00103 regExp(const std::string&, const bool ignoreCase=false); 00104 00105 // Destructor 00106 00107 ~regExp(); 00108 00109 00110 // Member functions 00111 00112 //- Access 00113 00114 //- Return true if a precompiled expression does not exist 00115 inline bool empty() const 00116 { 00117 return !preg_; 00118 } 00119 00120 //- Does a precompiled expression exist? 00121 inline bool exists() const 00122 { 00123 return preg_ ? true : false; 00124 } 00125 00126 //- Return the number of (groups) 00127 inline int ngroups() const 00128 { 00129 return preg_ ? preg_->re_nsub : 0; 00130 } 00131 00132 00133 //- Editing 00134 00135 //- Compile pattern into a regular expression, optionally ignoring case 00136 void set(const char*, const bool ignoreCase=false) const; 00137 00138 //- Compile pattern into a regular expression, optionally ignoring case 00139 void set(const std::string&, const bool ignoreCase=false) const; 00140 00141 00142 //- Release precompiled expression. 00143 // Returns true if precompiled expression existed before clear 00144 bool clear() const; 00145 00146 00147 //- Searching 00148 00149 //- Find position within string. 00150 // Returns the index where it begins or string::npos if not found 00151 std::string::size_type find(const std::string& str) const; 00152 00153 //- Return true if it matches the entire string 00154 // The begin-of-line (^) and end-of-line ($) anchors are implicit 00155 bool match(const std::string&) const; 00156 00157 //- Return true if it matches and sets the sub-groups matched 00158 // The begin-of-line (^) and end-of-line ($) anchors are implicit 00159 bool match(const string&, List<string>& groups) const; 00160 00161 //- Return true if the regex was found in within string 00162 bool search(const std::string& str) const 00163 { 00164 return std::string::npos != find(str); 00165 } 00166 00167 00168 // Member Operators 00169 00170 //- Assign and compile pattern from a character array 00171 // Always case sensitive 00172 void operator=(const char*); 00173 00174 //- Assign and compile pattern from string 00175 // Always case sensitive 00176 void operator=(const std::string&); 00177 00178 }; 00179 00180 00181 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00182 00183 } // End namespace Foam 00184 00185 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00186 00187 #endif 00188 00189 // ************************ vim: set sw=4 sts=4 et: ************************ //