Jeff Thompson | 86b6d64 | 2013-10-17 15:01:56 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) 1998-2002 |
| 4 | * John Maddock |
| 5 | * |
| 6 | * Use, modification and distribution are subject to the |
| 7 | * Boost Software License, Version 1.0. (See accompanying file |
| 8 | * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * LOCATION: see http://www.boost.org for most recent version. |
| 14 | * FILE cregex.cpp |
| 15 | * VERSION see <ndnboost/version.hpp> |
| 16 | * DESCRIPTION: Declares POSIX API functions |
| 17 | * + ndnboost::RegEx high level wrapper. |
| 18 | */ |
| 19 | |
| 20 | #ifndef NDNBOOST_RE_CREGEX_HPP_INCLUDED |
| 21 | #define NDNBOOST_RE_CREGEX_HPP_INCLUDED |
| 22 | |
| 23 | #ifndef NDNBOOST_REGEX_CONFIG_HPP |
| 24 | #include <ndnboost/regex/config.hpp> |
| 25 | #endif |
| 26 | #include <ndnboost/regex/v4/match_flags.hpp> |
| 27 | #include <ndnboost/regex/v4/error_type.hpp> |
| 28 | |
| 29 | #ifdef __cplusplus |
| 30 | #include <cstddef> |
| 31 | #else |
| 32 | #include <stddef.h> |
| 33 | #endif |
| 34 | |
| 35 | #ifdef NDNBOOST_MSVC |
| 36 | #pragma warning(push) |
| 37 | #pragma warning(disable: 4103) |
| 38 | #endif |
| 39 | #ifdef NDNBOOST_HAS_ABI_HEADERS |
| 40 | # include NDNBOOST_ABI_PREFIX |
| 41 | #endif |
| 42 | #ifdef NDNBOOST_MSVC |
| 43 | #pragma warning(pop) |
| 44 | #endif |
| 45 | |
| 46 | /* include these defs only for POSIX compatablity */ |
| 47 | #ifdef __cplusplus |
| 48 | namespace ndnboost{ |
| 49 | extern "C" { |
| 50 | #endif |
| 51 | |
| 52 | #if defined(__cplusplus) && !defined(NDNBOOST_NO_STDC_NAMESPACE) |
| 53 | typedef std::ptrdiff_t regoff_t; |
| 54 | typedef std::size_t regsize_t; |
| 55 | #else |
| 56 | typedef ptrdiff_t regoff_t; |
| 57 | typedef size_t regsize_t; |
| 58 | #endif |
| 59 | |
| 60 | typedef struct |
| 61 | { |
| 62 | unsigned int re_magic; |
| 63 | #ifdef __cplusplus |
| 64 | std::size_t re_nsub; /* number of parenthesized subexpressions */ |
| 65 | #else |
| 66 | size_t re_nsub; |
| 67 | #endif |
| 68 | const char* re_endp; /* end pointer for REG_PEND */ |
| 69 | void* guts; /* none of your business :-) */ |
| 70 | match_flag_type eflags; /* none of your business :-) */ |
| 71 | } regex_tA; |
| 72 | |
| 73 | #ifndef NDNBOOST_NO_WREGEX |
| 74 | typedef struct |
| 75 | { |
| 76 | unsigned int re_magic; |
| 77 | #ifdef __cplusplus |
| 78 | std::size_t re_nsub; /* number of parenthesized subexpressions */ |
| 79 | #else |
| 80 | size_t re_nsub; |
| 81 | #endif |
| 82 | const wchar_t* re_endp; /* end pointer for REG_PEND */ |
| 83 | void* guts; /* none of your business :-) */ |
| 84 | match_flag_type eflags; /* none of your business :-) */ |
| 85 | } regex_tW; |
| 86 | #endif |
| 87 | |
| 88 | typedef struct |
| 89 | { |
| 90 | regoff_t rm_so; /* start of match */ |
| 91 | regoff_t rm_eo; /* end of match */ |
| 92 | } regmatch_t; |
| 93 | |
| 94 | /* regcomp() flags */ |
| 95 | typedef enum{ |
| 96 | REG_BASIC = 0000, |
| 97 | REG_EXTENDED = 0001, |
| 98 | REG_ICASE = 0002, |
| 99 | REG_NOSUB = 0004, |
| 100 | REG_NEWLINE = 0010, |
| 101 | REG_NOSPEC = 0020, |
| 102 | REG_PEND = 0040, |
| 103 | REG_DUMP = 0200, |
| 104 | REG_NOCOLLATE = 0400, |
| 105 | REG_ESCAPE_IN_LISTS = 01000, |
| 106 | REG_NEWLINE_ALT = 02000, |
| 107 | REG_PERLEX = 04000, |
| 108 | |
| 109 | REG_PERL = REG_EXTENDED | REG_NOCOLLATE | REG_ESCAPE_IN_LISTS | REG_PERLEX, |
| 110 | REG_AWK = REG_EXTENDED | REG_ESCAPE_IN_LISTS, |
| 111 | REG_GREP = REG_BASIC | REG_NEWLINE_ALT, |
| 112 | REG_EGREP = REG_EXTENDED | REG_NEWLINE_ALT, |
| 113 | |
| 114 | REG_ASSERT = 15, |
| 115 | REG_INVARG = 16, |
| 116 | REG_ATOI = 255, /* convert name to number (!) */ |
| 117 | REG_ITOA = 0400 /* convert number to name (!) */ |
| 118 | } reg_comp_flags; |
| 119 | |
| 120 | /* regexec() flags */ |
| 121 | typedef enum{ |
| 122 | REG_NOTBOL = 00001, |
| 123 | REG_NOTEOL = 00002, |
| 124 | REG_STARTEND = 00004 |
| 125 | } reg_exec_flags; |
| 126 | |
| 127 | /* |
| 128 | * POSIX error codes: |
| 129 | */ |
| 130 | typedef unsigned reg_error_t; |
| 131 | typedef reg_error_t reg_errcode_t; /* backwards compatibility */ |
| 132 | |
| 133 | static const reg_error_t REG_NOERROR = 0; /* Success. */ |
| 134 | static const reg_error_t REG_NOMATCH = 1; /* Didn't find a match (for regexec). */ |
| 135 | |
| 136 | /* POSIX regcomp return error codes. (In the order listed in the |
| 137 | standard.) */ |
| 138 | static const reg_error_t REG_BADPAT = 2; /* Invalid pattern. */ |
| 139 | static const reg_error_t REG_ECOLLATE = 3; /* Undefined collating element. */ |
| 140 | static const reg_error_t REG_ECTYPE = 4; /* Invalid character class name. */ |
| 141 | static const reg_error_t REG_EESCAPE = 5; /* Trailing backslash. */ |
| 142 | static const reg_error_t REG_ESUBREG = 6; /* Invalid back reference. */ |
| 143 | static const reg_error_t REG_EBRACK = 7; /* Unmatched left bracket. */ |
| 144 | static const reg_error_t REG_EPAREN = 8; /* Parenthesis imbalance. */ |
| 145 | static const reg_error_t REG_EBRACE = 9; /* Unmatched \{. */ |
| 146 | static const reg_error_t REG_BADBR = 10; /* Invalid contents of \{\}. */ |
| 147 | static const reg_error_t REG_ERANGE = 11; /* Invalid range end. */ |
| 148 | static const reg_error_t REG_ESPACE = 12; /* Ran out of memory. */ |
| 149 | static const reg_error_t REG_BADRPT = 13; /* No preceding re for repetition op. */ |
| 150 | static const reg_error_t REG_EEND = 14; /* unexpected end of expression */ |
| 151 | static const reg_error_t REG_ESIZE = 15; /* expression too big */ |
| 152 | static const reg_error_t REG_ERPAREN = 8; /* = REG_EPAREN : unmatched right parenthesis */ |
| 153 | static const reg_error_t REG_EMPTY = 17; /* empty expression */ |
| 154 | static const reg_error_t REG_E_MEMORY = 15; /* = REG_ESIZE : out of memory */ |
| 155 | static const reg_error_t REG_ECOMPLEXITY = 18; /* complexity too high */ |
| 156 | static const reg_error_t REG_ESTACK = 19; /* out of stack space */ |
| 157 | static const reg_error_t REG_E_PERL = 20; /* Perl (?...) error */ |
| 158 | static const reg_error_t REG_E_UNKNOWN = 21; /* unknown error */ |
| 159 | static const reg_error_t REG_ENOSYS = 21; /* = REG_E_UNKNOWN : Reserved. */ |
| 160 | |
| 161 | NDNBOOST_REGEX_DECL int NDNBOOST_REGEX_CCALL regcompA(regex_tA*, const char*, int); |
| 162 | NDNBOOST_REGEX_DECL regsize_t NDNBOOST_REGEX_CCALL regerrorA(int, const regex_tA*, char*, regsize_t); |
| 163 | NDNBOOST_REGEX_DECL int NDNBOOST_REGEX_CCALL regexecA(const regex_tA*, const char*, regsize_t, regmatch_t*, int); |
| 164 | NDNBOOST_REGEX_DECL void NDNBOOST_REGEX_CCALL regfreeA(regex_tA*); |
| 165 | |
| 166 | #ifndef NDNBOOST_NO_WREGEX |
| 167 | NDNBOOST_REGEX_DECL int NDNBOOST_REGEX_CCALL regcompW(regex_tW*, const wchar_t*, int); |
| 168 | NDNBOOST_REGEX_DECL regsize_t NDNBOOST_REGEX_CCALL regerrorW(int, const regex_tW*, wchar_t*, regsize_t); |
| 169 | NDNBOOST_REGEX_DECL int NDNBOOST_REGEX_CCALL regexecW(const regex_tW*, const wchar_t*, regsize_t, regmatch_t*, int); |
| 170 | NDNBOOST_REGEX_DECL void NDNBOOST_REGEX_CCALL regfreeW(regex_tW*); |
| 171 | #endif |
| 172 | |
| 173 | #ifdef UNICODE |
| 174 | #define regcomp regcompW |
| 175 | #define regerror regerrorW |
| 176 | #define regexec regexecW |
| 177 | #define regfree regfreeW |
| 178 | #define regex_t regex_tW |
| 179 | #else |
| 180 | #define regcomp regcompA |
| 181 | #define regerror regerrorA |
| 182 | #define regexec regexecA |
| 183 | #define regfree regfreeA |
| 184 | #define regex_t regex_tA |
| 185 | #endif |
| 186 | |
| 187 | #ifdef NDNBOOST_MSVC |
| 188 | #pragma warning(push) |
| 189 | #pragma warning(disable: 4103) |
| 190 | #endif |
| 191 | #ifdef NDNBOOST_HAS_ABI_HEADERS |
| 192 | # include NDNBOOST_ABI_SUFFIX |
| 193 | #endif |
| 194 | #ifdef NDNBOOST_MSVC |
| 195 | #pragma warning(pop) |
| 196 | #endif |
| 197 | |
| 198 | #ifdef __cplusplus |
| 199 | } /* extern "C" */ |
| 200 | } /* namespace */ |
| 201 | #endif |
| 202 | |
| 203 | #if defined(__cplusplus) |
| 204 | /* |
| 205 | * C++ high level wrapper goes here: |
| 206 | */ |
| 207 | #include <string> |
| 208 | #include <vector> |
| 209 | namespace ndnboost{ |
| 210 | |
| 211 | #ifdef NDNBOOST_MSVC |
| 212 | #pragma warning(push) |
| 213 | #pragma warning(disable: 4103) |
| 214 | #endif |
| 215 | #ifdef NDNBOOST_HAS_ABI_HEADERS |
| 216 | # include NDNBOOST_ABI_PREFIX |
| 217 | #endif |
| 218 | #ifdef NDNBOOST_MSVC |
| 219 | #pragma warning(pop) |
| 220 | #endif |
| 221 | |
| 222 | class RegEx; |
| 223 | |
| 224 | namespace re_detail{ |
| 225 | |
| 226 | class RegExData; |
| 227 | struct pred1; |
| 228 | struct pred2; |
| 229 | struct pred3; |
| 230 | struct pred4; |
| 231 | |
| 232 | } /* namespace re_detail */ |
| 233 | |
| 234 | #if (defined(NDNBOOST_MSVC) || defined(__BORLANDC__)) && !defined(NDNBOOST_DISABLE_WIN32) |
| 235 | typedef bool (__cdecl *GrepCallback)(const RegEx& expression); |
| 236 | typedef bool (__cdecl *GrepFileCallback)(const char* file, const RegEx& expression); |
| 237 | typedef bool (__cdecl *FindFilesCallback)(const char* file); |
| 238 | #else |
| 239 | typedef bool (*GrepCallback)(const RegEx& expression); |
| 240 | typedef bool (*GrepFileCallback)(const char* file, const RegEx& expression); |
| 241 | typedef bool (*FindFilesCallback)(const char* file); |
| 242 | #endif |
| 243 | |
| 244 | class NDNBOOST_REGEX_DECL RegEx |
| 245 | { |
| 246 | private: |
| 247 | re_detail::RegExData* pdata; |
| 248 | public: |
| 249 | RegEx(); |
| 250 | RegEx(const RegEx& o); |
| 251 | ~RegEx(); |
| 252 | explicit RegEx(const char* c, bool icase = false); |
| 253 | explicit RegEx(const std::string& s, bool icase = false); |
| 254 | RegEx& operator=(const RegEx& o); |
| 255 | RegEx& operator=(const char* p); |
| 256 | RegEx& operator=(const std::string& s){ return this->operator=(s.c_str()); } |
| 257 | unsigned int SetExpression(const char* p, bool icase = false); |
| 258 | unsigned int SetExpression(const std::string& s, bool icase = false){ return SetExpression(s.c_str(), icase); } |
| 259 | std::string Expression()const; |
| 260 | unsigned int error_code()const; |
| 261 | /* |
| 262 | * now matching operators: |
| 263 | */ |
| 264 | bool Match(const char* p, match_flag_type flags = match_default); |
| 265 | bool Match(const std::string& s, match_flag_type flags = match_default) { return Match(s.c_str(), flags); } |
| 266 | bool Search(const char* p, match_flag_type flags = match_default); |
| 267 | bool Search(const std::string& s, match_flag_type flags = match_default) { return Search(s.c_str(), flags); } |
| 268 | unsigned int Grep(GrepCallback cb, const char* p, match_flag_type flags = match_default); |
| 269 | unsigned int Grep(GrepCallback cb, const std::string& s, match_flag_type flags = match_default) { return Grep(cb, s.c_str(), flags); } |
| 270 | unsigned int Grep(std::vector<std::string>& v, const char* p, match_flag_type flags = match_default); |
| 271 | unsigned int Grep(std::vector<std::string>& v, const std::string& s, match_flag_type flags = match_default) { return Grep(v, s.c_str(), flags); } |
| 272 | unsigned int Grep(std::vector<std::size_t>& v, const char* p, match_flag_type flags = match_default); |
| 273 | unsigned int Grep(std::vector<std::size_t>& v, const std::string& s, match_flag_type flags = match_default) { return Grep(v, s.c_str(), flags); } |
| 274 | #ifndef NDNBOOST_REGEX_NO_FILEITER |
| 275 | unsigned int GrepFiles(GrepFileCallback cb, const char* files, bool recurse = false, match_flag_type flags = match_default); |
| 276 | unsigned int GrepFiles(GrepFileCallback cb, const std::string& files, bool recurse = false, match_flag_type flags = match_default) { return GrepFiles(cb, files.c_str(), recurse, flags); } |
| 277 | unsigned int FindFiles(FindFilesCallback cb, const char* files, bool recurse = false, match_flag_type flags = match_default); |
| 278 | unsigned int FindFiles(FindFilesCallback cb, const std::string& files, bool recurse = false, match_flag_type flags = match_default) { return FindFiles(cb, files.c_str(), recurse, flags); } |
| 279 | #endif |
| 280 | |
| 281 | std::string Merge(const std::string& in, const std::string& fmt, |
| 282 | bool copy = true, match_flag_type flags = match_default); |
| 283 | std::string Merge(const char* in, const char* fmt, |
| 284 | bool copy = true, match_flag_type flags = match_default); |
| 285 | |
| 286 | std::size_t Split(std::vector<std::string>& v, std::string& s, match_flag_type flags = match_default, unsigned max_count = ~0); |
| 287 | /* |
| 288 | * now operators for returning what matched in more detail: |
| 289 | */ |
| 290 | std::size_t Position(int i = 0)const; |
| 291 | std::size_t Length(int i = 0)const; |
| 292 | bool Matched(int i = 0)const; |
| 293 | std::size_t Marks()const; |
| 294 | std::string What(int i = 0)const; |
| 295 | std::string operator[](int i)const { return What(i); } |
| 296 | |
| 297 | static const std::size_t npos; |
| 298 | |
| 299 | friend struct re_detail::pred1; |
| 300 | friend struct re_detail::pred2; |
| 301 | friend struct re_detail::pred3; |
| 302 | friend struct re_detail::pred4; |
| 303 | }; |
| 304 | |
| 305 | #ifdef NDNBOOST_MSVC |
| 306 | #pragma warning(push) |
| 307 | #pragma warning(disable: 4103) |
| 308 | #endif |
| 309 | #ifdef NDNBOOST_HAS_ABI_HEADERS |
| 310 | # include NDNBOOST_ABI_SUFFIX |
| 311 | #endif |
| 312 | #ifdef NDNBOOST_MSVC |
| 313 | #pragma warning(pop) |
| 314 | #endif |
| 315 | |
| 316 | } /* namespace ndnboost */ |
| 317 | |
| 318 | #endif /* __cplusplus */ |
| 319 | |
| 320 | #endif /* include guard */ |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |