cregex.hpp 11 KB

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