dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This software is distributed under the terms of the GNU Affero Public License. |
| 5 | * See the COPYING file in the main directory for details. |
| 6 | * |
| 7 | * This use of this software may be subject to additional restrictions. |
| 8 | * See the LEGAL file in the main directory for details. |
| 9 | |
| 10 | This program is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU Affero General Public License as published by |
| 12 | the Free Software Foundation, either version 3 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU Affero General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU Affero General Public License |
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #ifndef REGEXPW_H |
| 27 | #define REGEXPW_H |
| 28 | |
| 29 | #include <regex.h> |
| 30 | #include <iostream> |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | |
| 34 | |
| 35 | class Regexp { |
| 36 | |
| 37 | private: |
| 38 | |
| 39 | regex_t mRegex; |
| 40 | |
| 41 | |
| 42 | public: |
| 43 | |
| 44 | Regexp(const char* regexp, int flags=REG_EXTENDED) |
| 45 | { |
| 46 | int result = regcomp(&mRegex, regexp, flags); |
| 47 | if (result) { |
| 48 | char msg[256]; |
| 49 | regerror(result,&mRegex,msg,255); |
| 50 | std::cerr << "Regexp compilation of " << regexp << " failed: " << msg << std::endl; |
| 51 | abort(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | ~Regexp() |
| 56 | { regfree(&mRegex); } |
| 57 | |
| 58 | bool match(const char *text, int flags=0) const |
| 59 | { return regexec(&mRegex, text, 0, NULL, flags)==0; } |
| 60 | |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | #endif |