cregex.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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_UNKNOWN = 20; /* unknown error */
  141. static const reg_error_t REG_ENOSYS = 20; /* = REG_E_UNKNOWN : Reserved. */
  142. BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompA(regex_tA*, const char*, int);
  143. BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int, const regex_tA*, char*, regsize_t);
  144. BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecA(const regex_tA*, const char*, regsize_t, regmatch_t*, int);
  145. BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeA(regex_tA*);
  146. #ifndef BOOST_NO_WREGEX
  147. BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompW(regex_tW*, const wchar_t*, int);
  148. BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorW(int, const regex_tW*, wchar_t*, regsize_t);
  149. BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecW(const regex_tW*, const wchar_t*, regsize_t, regmatch_t*, int);
  150. BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeW(regex_tW*);
  151. #endif
  152. #ifdef UNICODE
  153. #define regcomp regcompW
  154. #define regerror regerrorW
  155. #define regexec regexecW
  156. #define regfree regfreeW
  157. #define regex_t regex_tW
  158. #else
  159. #define regcomp regcompA
  160. #define regerror regerrorA
  161. #define regexec regexecA
  162. #define regfree regfreeA
  163. #define regex_t regex_tA
  164. #endif
  165. #ifdef BOOST_MSVC
  166. #pragma warning(push)
  167. #pragma warning(disable: 4103)
  168. #endif
  169. #ifdef BOOST_HAS_ABI_HEADERS
  170. # include BOOST_ABI_SUFFIX
  171. #endif
  172. #ifdef BOOST_MSVC
  173. #pragma warning(pop)
  174. #endif
  175. #ifdef __cplusplus
  176. } // extern "C"
  177. } // namespace
  178. #endif
  179. //
  180. // C++ high level wrapper goes here:
  181. //
  182. #if defined(__cplusplus)
  183. #include <string>
  184. #include <vector>
  185. namespace boost{
  186. #ifdef BOOST_MSVC
  187. #pragma warning(push)
  188. #pragma warning(disable: 4103)
  189. #endif
  190. #ifdef BOOST_HAS_ABI_HEADERS
  191. # include BOOST_ABI_PREFIX
  192. #endif
  193. #ifdef BOOST_MSVC
  194. #pragma warning(pop)
  195. #endif
  196. class RegEx;
  197. namespace re_detail{
  198. class RegExData;
  199. struct pred1;
  200. struct pred2;
  201. struct pred3;
  202. struct pred4;
  203. } // namespace re_detail
  204. #if (defined(BOOST_MSVC) || defined(__BORLANDC__)) && !defined(BOOST_DISABLE_WIN32)
  205. typedef bool (__cdecl *GrepCallback)(const RegEx& expression);
  206. typedef bool (__cdecl *GrepFileCallback)(const char* file, const RegEx& expression);
  207. typedef bool (__cdecl *FindFilesCallback)(const char* file);
  208. #else
  209. typedef bool (*GrepCallback)(const RegEx& expression);
  210. typedef bool (*GrepFileCallback)(const char* file, const RegEx& expression);
  211. typedef bool (*FindFilesCallback)(const char* file);
  212. #endif
  213. class BOOST_REGEX_DECL RegEx
  214. {
  215. private:
  216. re_detail::RegExData* pdata;
  217. public:
  218. RegEx();
  219. RegEx(const RegEx& o);
  220. ~RegEx();
  221. explicit RegEx(const char* c, bool icase = false);
  222. explicit RegEx(const std::string& s, bool icase = false);
  223. RegEx& operator=(const RegEx& o);
  224. RegEx& operator=(const char* p);
  225. RegEx& operator=(const std::string& s){ return this->operator=(s.c_str()); }
  226. unsigned int SetExpression(const char* p, bool icase = false);
  227. unsigned int SetExpression(const std::string& s, bool icase = false){ return SetExpression(s.c_str(), icase); }
  228. std::string Expression()const;
  229. unsigned int error_code()const;
  230. //
  231. // now matching operators:
  232. //
  233. bool Match(const char* p, match_flag_type flags = match_default);
  234. bool Match(const std::string& s, match_flag_type flags = match_default) { return Match(s.c_str(), flags); }
  235. bool Search(const char* p, match_flag_type flags = match_default);
  236. bool Search(const std::string& s, match_flag_type flags = match_default) { return Search(s.c_str(), flags); }
  237. unsigned int Grep(GrepCallback cb, const char* p, match_flag_type flags = match_default);
  238. unsigned int Grep(GrepCallback cb, const std::string& s, match_flag_type flags = match_default) { return Grep(cb, s.c_str(), flags); }
  239. unsigned int Grep(std::vector<std::string>& v, const char* p, match_flag_type flags = match_default);
  240. 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); }
  241. unsigned int Grep(std::vector<std::size_t>& v, const char* p, match_flag_type flags = match_default);
  242. 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); }
  243. #ifndef BOOST_REGEX_NO_FILEITER
  244. unsigned int GrepFiles(GrepFileCallback cb, const char* files, bool recurse = false, match_flag_type flags = match_default);
  245. 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); }
  246. unsigned int FindFiles(FindFilesCallback cb, const char* files, bool recurse = false, match_flag_type flags = match_default);
  247. 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); }
  248. #endif
  249. std::string Merge(const std::string& in, const std::string& fmt,
  250. bool copy = true, match_flag_type flags = match_default);
  251. std::string Merge(const char* in, const char* fmt,
  252. bool copy = true, match_flag_type flags = match_default);
  253. std::size_t Split(std::vector<std::string>& v, std::string& s, match_flag_type flags = match_default, unsigned max_count = ~0);
  254. //
  255. // now operators for returning what matched in more detail:
  256. //
  257. std::size_t Position(int i = 0)const;
  258. std::size_t Length(int i = 0)const;
  259. bool Matched(int i = 0)const;
  260. std::size_t Marks()const;
  261. std::string What(int i = 0)const;
  262. std::string operator[](int i)const { return What(i); }
  263. static const std::size_t npos;
  264. friend struct re_detail::pred1;
  265. friend struct re_detail::pred2;
  266. friend struct re_detail::pred3;
  267. friend struct re_detail::pred4;
  268. };
  269. #ifdef BOOST_MSVC
  270. #pragma warning(push)
  271. #pragma warning(disable: 4103)
  272. #endif
  273. #ifdef BOOST_HAS_ABI_HEADERS
  274. # include BOOST_ABI_SUFFIX
  275. #endif
  276. #ifdef BOOST_MSVC
  277. #pragma warning(pop)
  278. #endif
  279. } // namespace boost
  280. #endif
  281. #endif // include guard