sl@0: /* sl@0: * sl@0: * Copyright (c) 1998-2002 sl@0: * John Maddock sl@0: * sl@0: * Use, modification and distribution are subject to the sl@0: * Boost Software License, Version 1.0. (See accompanying file sl@0: * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) sl@0: * sl@0: */ sl@0: sl@0: /* sl@0: * LOCATION: see http://www.boost.org for most recent version. sl@0: * FILE cregex.cpp sl@0: * VERSION see sl@0: * DESCRIPTION: Declares POSIX API functions sl@0: * + boost::RegEx high level wrapper. sl@0: */ sl@0: sl@0: #ifndef BOOST_RE_CREGEX_HPP_INCLUDED sl@0: #define BOOST_RE_CREGEX_HPP_INCLUDED sl@0: sl@0: #ifndef BOOST_REGEX_CONFIG_HPP sl@0: #include sl@0: #endif sl@0: #include sl@0: #include sl@0: sl@0: #ifdef __cplusplus sl@0: #include sl@0: #else sl@0: #include sl@0: #endif sl@0: sl@0: #ifdef BOOST_HAS_ABI_HEADERS sl@0: # include BOOST_ABI_PREFIX sl@0: #endif sl@0: sl@0: /* include these defs only for POSIX compatablity */ sl@0: #ifdef __cplusplus sl@0: namespace boost{ sl@0: extern "C" { sl@0: #endif sl@0: sl@0: #if defined(__cplusplus) && !defined(BOOST_NO_STDC_NAMESPACE) sl@0: typedef std::ptrdiff_t regoff_t; sl@0: typedef std::size_t regsize_t; sl@0: #else sl@0: typedef ptrdiff_t regoff_t; sl@0: typedef size_t regsize_t; sl@0: #endif sl@0: sl@0: typedef struct sl@0: { sl@0: unsigned int re_magic; sl@0: #ifdef __cplusplus sl@0: std::size_t re_nsub; /* number of parenthesized subexpressions */ sl@0: #else sl@0: size_t re_nsub; sl@0: #endif sl@0: const char* re_endp; /* end pointer for REG_PEND */ sl@0: void* guts; /* none of your business :-) */ sl@0: match_flag_type eflags; /* none of your business :-) */ sl@0: } regex_tA; sl@0: sl@0: #ifndef BOOST_NO_WREGEX sl@0: typedef struct sl@0: { sl@0: unsigned int re_magic; sl@0: #ifdef __cplusplus sl@0: std::size_t re_nsub; /* number of parenthesized subexpressions */ sl@0: #else sl@0: size_t re_nsub; sl@0: #endif sl@0: const wchar_t* re_endp; /* end pointer for REG_PEND */ sl@0: void* guts; /* none of your business :-) */ sl@0: match_flag_type eflags; /* none of your business :-) */ sl@0: } regex_tW; sl@0: #endif sl@0: sl@0: typedef struct sl@0: { sl@0: regoff_t rm_so; /* start of match */ sl@0: regoff_t rm_eo; /* end of match */ sl@0: } regmatch_t; sl@0: sl@0: /* regcomp() flags */ sl@0: typedef enum{ sl@0: REG_BASIC = 0000, sl@0: REG_EXTENDED = 0001, sl@0: REG_ICASE = 0002, sl@0: REG_NOSUB = 0004, sl@0: REG_NEWLINE = 0010, sl@0: REG_NOSPEC = 0020, sl@0: REG_PEND = 0040, sl@0: REG_DUMP = 0200, sl@0: REG_NOCOLLATE = 0400, sl@0: REG_ESCAPE_IN_LISTS = 01000, sl@0: REG_NEWLINE_ALT = 02000, sl@0: REG_PERLEX = 04000, sl@0: sl@0: REG_PERL = REG_EXTENDED | REG_NOCOLLATE | REG_ESCAPE_IN_LISTS | REG_PERLEX, sl@0: REG_AWK = REG_EXTENDED | REG_ESCAPE_IN_LISTS, sl@0: REG_GREP = REG_BASIC | REG_NEWLINE_ALT, sl@0: REG_EGREP = REG_EXTENDED | REG_NEWLINE_ALT, sl@0: sl@0: REG_ASSERT = 15, sl@0: REG_INVARG = 16, sl@0: REG_ATOI = 255, /* convert name to number (!) */ sl@0: REG_ITOA = 0400 /* convert number to name (!) */ sl@0: } reg_comp_flags; sl@0: sl@0: /* regexec() flags */ sl@0: typedef enum{ sl@0: REG_NOTBOL = 00001, sl@0: REG_NOTEOL = 00002, sl@0: REG_STARTEND = 00004 sl@0: } reg_exec_flags; sl@0: sl@0: // sl@0: // POSIX error codes: sl@0: // sl@0: typedef unsigned reg_error_t; sl@0: typedef reg_error_t reg_errcode_t; // backwards compatibility sl@0: sl@0: static const reg_error_t REG_NOERROR = 0; /* Success. */ sl@0: static const reg_error_t REG_NOMATCH = 1; /* Didn't find a match (for regexec). */ sl@0: sl@0: /* POSIX regcomp return error codes. (In the order listed in the sl@0: standard.) */ sl@0: static const reg_error_t REG_BADPAT = 2; /* Invalid pattern. */ sl@0: static const reg_error_t REG_ECOLLATE = 3; /* Undefined collating element. */ sl@0: static const reg_error_t REG_ECTYPE = 4; /* Invalid character class name. */ sl@0: static const reg_error_t REG_EESCAPE = 5; /* Trailing backslash. */ sl@0: static const reg_error_t REG_ESUBREG = 6; /* Invalid back reference. */ sl@0: static const reg_error_t REG_EBRACK = 7; /* Unmatched left bracket. */ sl@0: static const reg_error_t REG_EPAREN = 8; /* Parenthesis imbalance. */ sl@0: static const reg_error_t REG_EBRACE = 9; /* Unmatched \{. */ sl@0: static const reg_error_t REG_BADBR = 10; /* Invalid contents of \{\}. */ sl@0: static const reg_error_t REG_ERANGE = 11; /* Invalid range end. */ sl@0: static const reg_error_t REG_ESPACE = 12; /* Ran out of memory. */ sl@0: static const reg_error_t REG_BADRPT = 13; /* No preceding re for repetition op. */ sl@0: static const reg_error_t REG_EEND = 14; /* unexpected end of expression */ sl@0: static const reg_error_t REG_ESIZE = 15; /* expression too big */ sl@0: static const reg_error_t REG_ERPAREN = 8; /* = REG_EPAREN : unmatched right parenthesis */ sl@0: static const reg_error_t REG_EMPTY = 17; /* empty expression */ sl@0: static const reg_error_t REG_E_MEMORY = 15; /* = REG_ESIZE : out of memory */ sl@0: static const reg_error_t REG_ECOMPLEXITY = 18; /* complexity too high */ sl@0: static const reg_error_t REG_ESTACK = 19; /* out of stack space */ sl@0: static const reg_error_t REG_E_UNKNOWN = 20; /* unknown error */ sl@0: static const reg_error_t REG_ENOSYS = 20; /* = REG_E_UNKNOWN : Reserved. */ sl@0: sl@0: BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompA(regex_tA*, const char*, int); sl@0: BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int, const regex_tA*, char*, regsize_t); sl@0: BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecA(const regex_tA*, const char*, regsize_t, regmatch_t*, int); sl@0: BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeA(regex_tA*); sl@0: sl@0: #ifndef BOOST_NO_WREGEX sl@0: BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompW(regex_tW*, const wchar_t*, int); sl@0: BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorW(int, const regex_tW*, wchar_t*, regsize_t); sl@0: BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecW(const regex_tW*, const wchar_t*, regsize_t, regmatch_t*, int); sl@0: BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeW(regex_tW*); sl@0: #endif sl@0: sl@0: #ifdef UNICODE sl@0: #define regcomp regcompW sl@0: #define regerror regerrorW sl@0: #define regexec regexecW sl@0: #define regfree regfreeW sl@0: #define regex_t regex_tW sl@0: #else sl@0: #define regcomp regcompA sl@0: #define regerror regerrorA sl@0: #define regexec regexecA sl@0: #define regfree regfreeA sl@0: #define regex_t regex_tA sl@0: #endif sl@0: sl@0: #ifdef BOOST_HAS_ABI_HEADERS sl@0: # include BOOST_ABI_SUFFIX sl@0: #endif sl@0: sl@0: #ifdef __cplusplus sl@0: } // extern "C" sl@0: } // namespace sl@0: #endif sl@0: sl@0: // sl@0: // C++ high level wrapper goes here: sl@0: // sl@0: #if defined(__cplusplus) sl@0: #include sl@0: #include sl@0: namespace boost{ sl@0: sl@0: #ifdef BOOST_HAS_ABI_HEADERS sl@0: # include BOOST_ABI_PREFIX sl@0: #endif sl@0: sl@0: class RegEx; sl@0: sl@0: namespace re_detail{ sl@0: sl@0: class RegExData; sl@0: struct pred1; sl@0: struct pred2; sl@0: struct pred3; sl@0: struct pred4; sl@0: sl@0: } // namespace re_detail sl@0: sl@0: #if (defined(BOOST_MSVC) || defined(__BORLANDC__)) && !defined(BOOST_DISABLE_WIN32) sl@0: typedef bool (__cdecl *GrepCallback)(const RegEx& expression); sl@0: typedef bool (__cdecl *GrepFileCallback)(const char* file, const RegEx& expression); sl@0: typedef bool (__cdecl *FindFilesCallback)(const char* file); sl@0: #else sl@0: typedef bool (*GrepCallback)(const RegEx& expression); sl@0: typedef bool (*GrepFileCallback)(const char* file, const RegEx& expression); sl@0: typedef bool (*FindFilesCallback)(const char* file); sl@0: #endif sl@0: sl@0: class BOOST_REGEX_DECL RegEx sl@0: { sl@0: private: sl@0: re_detail::RegExData* pdata; sl@0: public: sl@0: RegEx(); sl@0: RegEx(const RegEx& o); sl@0: ~RegEx(); sl@0: explicit RegEx(const char* c, bool icase = false); sl@0: explicit RegEx(const std::string& s, bool icase = false); sl@0: RegEx& operator=(const RegEx& o); sl@0: RegEx& operator=(const char* p); sl@0: RegEx& operator=(const std::string& s){ return this->operator=(s.c_str()); } sl@0: unsigned int SetExpression(const char* p, bool icase = false); sl@0: unsigned int SetExpression(const std::string& s, bool icase = false){ return SetExpression(s.c_str(), icase); } sl@0: std::string Expression()const; sl@0: unsigned int error_code()const; sl@0: // sl@0: // now matching operators: sl@0: // sl@0: bool Match(const char* p, match_flag_type flags = match_default); sl@0: bool Match(const std::string& s, match_flag_type flags = match_default) { return Match(s.c_str(), flags); } sl@0: bool Search(const char* p, match_flag_type flags = match_default); sl@0: bool Search(const std::string& s, match_flag_type flags = match_default) { return Search(s.c_str(), flags); } sl@0: unsigned int Grep(GrepCallback cb, const char* p, match_flag_type flags = match_default); sl@0: unsigned int Grep(GrepCallback cb, const std::string& s, match_flag_type flags = match_default) { return Grep(cb, s.c_str(), flags); } sl@0: unsigned int Grep(std::vector& v, const char* p, match_flag_type flags = match_default); sl@0: unsigned int Grep(std::vector& v, const std::string& s, match_flag_type flags = match_default) { return Grep(v, s.c_str(), flags); } sl@0: unsigned int Grep(std::vector& v, const char* p, match_flag_type flags = match_default); sl@0: unsigned int Grep(std::vector& v, const std::string& s, match_flag_type flags = match_default) { return Grep(v, s.c_str(), flags); } sl@0: #ifndef BOOST_REGEX_NO_FILEITER sl@0: unsigned int GrepFiles(GrepFileCallback cb, const char* files, bool recurse = false, match_flag_type flags = match_default); sl@0: 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); } sl@0: unsigned int FindFiles(FindFilesCallback cb, const char* files, bool recurse = false, match_flag_type flags = match_default); sl@0: 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); } sl@0: #endif sl@0: sl@0: std::string Merge(const std::string& in, const std::string& fmt, sl@0: bool copy = true, match_flag_type flags = match_default); sl@0: std::string Merge(const char* in, const char* fmt, sl@0: bool copy = true, match_flag_type flags = match_default); sl@0: sl@0: std::size_t Split(std::vector& v, std::string& s, match_flag_type flags = match_default, unsigned max_count = ~0); sl@0: // sl@0: // now operators for returning what matched in more detail: sl@0: // sl@0: std::size_t Position(int i = 0)const; sl@0: std::size_t Length(int i = 0)const; sl@0: bool Matched(int i = 0)const; sl@0: std::size_t Marks()const; sl@0: std::string What(int i = 0)const; sl@0: std::string operator[](int i)const { return What(i); } sl@0: sl@0: static const std::size_t npos; sl@0: sl@0: friend struct re_detail::pred1; sl@0: friend struct re_detail::pred2; sl@0: friend struct re_detail::pred3; sl@0: friend struct re_detail::pred4; sl@0: }; sl@0: sl@0: #ifdef BOOST_HAS_ABI_HEADERS sl@0: # include BOOST_ABI_SUFFIX sl@0: #endif sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif sl@0: sl@0: #endif // include guard sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: