Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. /*
  9. *
  10. * Copyright (c) 1998-9
  11. * Dr John Maddock
  12. *
  13. * Permission to use, copy, modify, distribute and sell this software
  14. * and its documentation for any purpose is hereby granted without fee,
  15. * provided that the above copyright notice appear in all copies and
  16. * that both that copyright notice and this permission notice appear
  17. * in supporting documentation. Dr John Maddock makes no representations
  18. * about the suitability of this software for any purpose.
  19. * It is provided "as is" without express or implied warranty.
  20. *
  21. */
  22. /*
  23. * FILE cregex.h
  24. * VERSION 2.12
  25. */
  26. #ifndef CREGEX_H
  27. #define CREGEX_H
  28. #include <jm/jm_cfg.h>
  29. /* include these defs only for POSIX compatablity */
  30. typedef int regoff_t;
  31. typedef struct
  32. {
  33. unsigned int re_magic;
  34. unsigned int re_nsub; /* number of parenthesized subexpressions */
  35. const char* re_endp; /* end pointer for REG_PEND */
  36. void* guts; /* none of your business :-) */
  37. unsigned int eflags; /* none of your business :-) */
  38. } regex_tA;
  39. #ifndef JM_NO_WCSTRING
  40. typedef struct
  41. {
  42. unsigned int re_magic;
  43. unsigned int re_nsub; /* number of parenthesized subexpressions */
  44. const wchar_t* re_endp; /* end pointer for REG_PEND */
  45. void* guts; /* none of your business :-) */
  46. unsigned int eflags; /* none of your business :-) */
  47. } regex_tW;
  48. #endif
  49. typedef struct
  50. {
  51. regoff_t rm_so; /* start of match */
  52. regoff_t rm_eo; /* end of match */
  53. } regmatch_t;
  54. /* regcomp() flags */
  55. #define REG_BASIC 0000
  56. #define REG_EXTENDED 0001
  57. #define REG_ICASE 0002
  58. #define REG_NOSUB 0004
  59. #define REG_NEWLINE 0010
  60. #define REG_NOSPEC 0020
  61. #define REG_PEND 0040
  62. #define REG_DUMP 0200
  63. #define REG_NOCOLLATE 0400
  64. #define REG_ASSERT 15
  65. #define REG_INVARG 16
  66. #define REG_ATOI 255 /* convert name to number (!) */
  67. #define REG_ITOA 0400 /* convert number to name (!) */
  68. /* regexec() flags */
  69. #define REG_NOTBOL 00001
  70. #define REG_NOTEOL 00002
  71. #define REG_STARTEND 00004
  72. #ifdef __cplusplus
  73. extern "C" {
  74. #endif
  75. JM_IX_DECL int RE_CCALL regcompA(regex_tA*, const char*, int);
  76. JM_IX_DECL unsigned int RE_CCALL regerrorA(int, const regex_tA*, char*, unsigned int);
  77. JM_IX_DECL int RE_CCALL regexecA(const regex_tA*, const char*, unsigned int, regmatch_t*, int);
  78. JM_IX_DECL void RE_CCALL regfreeA(regex_tA*);
  79. #ifndef JM_NO_WCSTRING
  80. JM_IX_DECL int RE_CCALL regcompW(regex_tW*, const wchar_t*, int);
  81. JM_IX_DECL unsigned int RE_CCALL regerrorW(int, const regex_tW*, wchar_t*, unsigned int);
  82. JM_IX_DECL int RE_CCALL regexecW(const regex_tW*, const wchar_t*, unsigned int, regmatch_t*, int);
  83. JM_IX_DECL void RE_CCALL regfreeW(regex_tW*);
  84. #endif
  85. #ifdef UNICODE
  86. #define regcomp regcompW
  87. #define regerror regerrorW
  88. #define regexec regexecW
  89. #define regfree regfreeW
  90. #define regex_t regex_tW
  91. #else
  92. #define regcomp regcompA
  93. #define regerror regerrorA
  94. #define regexec regexecA
  95. #define regfree regfreeA
  96. #define regex_t regex_tA
  97. #endif
  98. #ifdef __cplusplus
  99. }
  100. #endif
  101. #ifdef __cplusplus
  102. JM_NAMESPACE(__JM)
  103. #endif
  104. /* regerror() flags */
  105. typedef enum
  106. {
  107. REG_NOERROR = 0, /* Success. */
  108. REG_NOMATCH = 1, /* Didn't find a match (for regexec). */
  109. /* POSIX regcomp return error codes. (In the order listed in the
  110. standard.) */
  111. REG_BADPAT = 2, /* Invalid pattern. */
  112. REG_ECOLLATE = 3, /* Undefined collating element. */
  113. REG_ECTYPE = 4, /* Invalid character class name. */
  114. REG_EESCAPE = 5, /* Trailing backslash. */
  115. REG_ESUBREG = 6, /* Invalid back reference. */
  116. REG_EBRACK = 7, /* Unmatched left bracket. */
  117. REG_EPAREN = 8, /* Parenthesis imbalance. */
  118. REG_EBRACE = 9, /* Unmatched \{. */
  119. REG_BADBR = 10, /* Invalid contents of \{\}. */
  120. REG_ERANGE = 11, /* Invalid range end. */
  121. REG_ESPACE = 12, /* Ran out of memory. */
  122. REG_BADRPT = 13, /* No preceding re for repetition op. */
  123. REG_EEND = 14, /* unexpected end of expression */
  124. REG_ESIZE = 15, /* expression too big */
  125. REG_ERPAREN = 16, /* unmatched right parenthesis */
  126. REG_EMPTY = 17, /* empty expression */
  127. REG_E_MEMORY = 18, /* out of memory */
  128. REG_E_UNKNOWN = 19 /* unknown error */
  129. } reg_errcode_t;
  130. enum match_flags
  131. {
  132. match_default = 0,
  133. match_not_bol = 1, // first is not start of line
  134. match_not_eol = match_not_bol << 1, // last is not end of line
  135. match_not_bob = match_not_eol << 1, // first is not start of buffer
  136. match_not_eob = match_not_bob << 1, // last is not end of buffer
  137. match_not_bow = match_not_eob << 1, // first is not start of word
  138. match_not_eow = match_not_bow << 1, // last is not end of word
  139. match_not_dot_newline = match_not_eow << 1, // \n is not matched by '.'
  140. match_not_dot_null = match_not_dot_newline << 1, // '\0' is not matched by '.'
  141. match_prev_avail = match_not_dot_null << 1, // *--first is a valid expression
  142. match_init = match_prev_avail << 1, // internal use
  143. match_any = match_init << 1, // don't care what we match
  144. match_not_null = match_any << 1, // string can't be null
  145. match_continuous = match_not_null << 1, // each grep match must continue from
  146. // uninterupted from the previous one
  147. match_stop = match_continuous << 1 // stop after first match (grep)
  148. };
  149. #ifdef __cplusplus
  150. JM_END_NAMESPACE
  151. #endif
  152. //
  153. // C++ high level wrapper goes here:
  154. //
  155. #if defined(__cplusplus) && !defined(JM_NO_STRING_H)
  156. #include <string>
  157. #include <vector>
  158. JM_NAMESPACE(__JM)
  159. class RegExData;
  160. class RegEx;
  161. struct pred1;
  162. struct pred2;
  163. struct pred3;
  164. struct pred4;
  165. typedef bool (*GrepCallback)(const RegEx& expression);
  166. typedef bool (*GrepFileCallback)(const char* file, const RegEx& expression);
  167. typedef bool (*FindFilesCallback)(const char* file);
  168. class JM_IX_DECL RegEx
  169. {
  170. private:
  171. RegExData* pdata;
  172. public:
  173. RegEx();
  174. RegEx(const RegEx& o);
  175. ~RegEx();
  176. RegEx(const char* c, bool icase = false);
  177. RegEx(const __JM_STD::string& s, bool icase = false);
  178. RegEx& operator=(const RegEx& o);
  179. RegEx& operator=(const char* p);
  180. RegEx& operator=(const __JM_STD::string& s){ return this->operator=(s.c_str()); }
  181. unsigned int SetExpression(const char* p, bool icase = false);
  182. unsigned int SetExpression(const __JM_STD::string& s, bool icase = false){ return SetExpression(s.c_str(), icase); }
  183. __JM_STD::string Expression()const;
  184. //
  185. // now matching operators:
  186. //
  187. bool Match(const char* p, unsigned int flags = match_default);
  188. bool Match(const __JM_STD::string& s, unsigned int flags = match_default) { return Match(s.c_str(), flags); }
  189. bool Search(const char* p, unsigned int flags = match_default);
  190. bool Search(const __JM_STD::string& s, unsigned int flags = match_default) { return Search(s.c_str(), flags); }
  191. unsigned int Grep(GrepCallback cb, const char* p, unsigned int flags = match_default);
  192. unsigned int Grep(GrepCallback cb, const __JM_STD::string& s, unsigned int flags = match_default) { return Grep(cb, s.c_str(), flags); }
  193. unsigned int Grep(__JM_STD::vector<__JM_STD::string>& v, const char* p, unsigned int flags = match_default);
  194. unsigned int Grep(__JM_STD::vector<__JM_STD::string>& v, const __JM_STD::string& s, unsigned int flags = match_default) { return Grep(v, s.c_str(), flags); }
  195. unsigned int Grep(__JM_STD::vector<unsigned int>& v, const char* p, unsigned int flags = match_default);
  196. unsigned int Grep(__JM_STD::vector<unsigned int>& v, const __JM_STD::string& s, unsigned int flags = match_default) { return Grep(v, s.c_str(), flags); }
  197. unsigned int GrepFiles(GrepFileCallback cb, const char* files, bool recurse = false, unsigned int flags = match_default);
  198. unsigned int GrepFiles(GrepFileCallback cb, const __JM_STD::string& files, bool recurse = false, unsigned int flags = match_default) { return GrepFiles(cb, files.c_str(), recurse, flags); }
  199. unsigned int FindFiles(FindFilesCallback cb, const char* files, bool recurse = false, unsigned int flags = match_default);
  200. unsigned int FindFiles(FindFilesCallback cb, const __JM_STD::string& files, bool recurse = false, unsigned int flags = match_default) { return FindFiles(cb, files.c_str(), recurse, flags); }
  201. //
  202. // now operators for returning what matched in more detail:
  203. //
  204. unsigned int Position(int i = 0)const;
  205. unsigned int Length(int i = 0)const;
  206. unsigned int Line()const;
  207. unsigned int Marks()const;
  208. __JM_STD::string What(int i = 0)const;
  209. __JM_STD::string operator[](int i)const { return What(i); }
  210. friend struct pred1;
  211. friend struct pred2;
  212. friend struct pred3;
  213. friend struct pred4;
  214. };
  215. JM_END_NAMESPACE
  216. #if !defined(JM_NO_NAMESPACES) && !defined(JM_NO_USING) && defined(__cplusplus)
  217. using __JM::RegEx;
  218. using __JM::GrepCallback;
  219. using __JM::GrepFileCallback;
  220. using __JM::FindFilesCallback;
  221. #endif
  222. #endif // __cplusplus
  223. #if !defined(JM_NO_NAMESPACES) && !defined(JM_NO_USING) && defined(__cplusplus)
  224. using __JM::match_flags;
  225. using __JM::reg_errcode_t;
  226. using __JM::REG_NOERROR;
  227. using __JM::REG_NOMATCH;
  228. using __JM::REG_BADPAT;
  229. using __JM::REG_ECOLLATE;
  230. using __JM::REG_ECTYPE;
  231. using __JM::REG_EESCAPE;
  232. using __JM::REG_ESUBREG;
  233. using __JM::REG_EBRACK;
  234. using __JM::REG_EPAREN;
  235. using __JM::REG_EBRACE;
  236. using __JM::REG_BADBR;
  237. using __JM::REG_ERANGE;
  238. using __JM::REG_ESPACE;
  239. using __JM::REG_BADRPT;
  240. using __JM::REG_EEND;
  241. using __JM::REG_ESIZE;
  242. using __JM::REG_ERPAREN;
  243. using __JM::REG_EMPTY;
  244. using __JM::REG_E_MEMORY;
  245. using __JM::REG_E_UNKNOWN;
  246. using __JM::match_default;
  247. using __JM::match_not_bol;
  248. using __JM::match_not_eol;
  249. using __JM::match_not_bob;
  250. using __JM::match_not_eob;
  251. using __JM::match_not_bow;
  252. using __JM::match_not_eow;
  253. using __JM::match_not_dot_newline;
  254. using __JM::match_not_dot_null;
  255. using __JM::match_prev_avail;
  256. using __JM::match_init;
  257. using __JM::match_any;
  258. using __JM::match_not_null;
  259. using __JM::match_continuous;
  260. using __JM::match_stop;
  261. #endif
  262. #endif