PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/regex/src/posix_api.cpp

http://hadesmem.googlecode.com/
C++ | 289 lines | 239 code | 29 blank | 21 comment | 48 complexity | ab7b1d6b1fe436d23a61246ad12b0b66 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, Apache-2.0, LGPL-3.0
  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: posix_api.cpp
  14. * VERSION: see <boost/version.hpp>
  15. * DESCRIPTION: Implements the Posix API wrappers.
  16. */
  17. #define BOOST_REGEX_SOURCE
  18. #include <cstdio>
  19. #include <boost/regex.hpp>
  20. #include <boost/cregex.hpp>
  21. #if defined(BOOST_NO_STDC_NAMESPACE)
  22. namespace std{
  23. using ::sprintf;
  24. using ::strcpy;
  25. using ::strcmp;
  26. }
  27. #endif
  28. namespace boost{
  29. namespace{
  30. unsigned int magic_value = 25631;
  31. const char* names[] = {
  32. "REG_NOERROR",
  33. "REG_NOMATCH",
  34. "REG_BADPAT",
  35. "REG_ECOLLATE",
  36. "REG_ECTYPE",
  37. "REG_EESCAPE",
  38. "REG_ESUBREG",
  39. "REG_EBRACK",
  40. "REG_EPAREN",
  41. "REG_EBRACE",
  42. "REG_BADBR",
  43. "REG_ERANGE",
  44. "REG_ESPACE",
  45. "REG_BADRPT",
  46. "REG_EEND",
  47. "REG_ESIZE",
  48. "REG_ERPAREN",
  49. "REG_EMPTY",
  50. "REG_ECOMPLEXITY",
  51. "REG_ESTACK",
  52. "REG_E_PERL",
  53. "REG_E_UNKNOWN",
  54. };
  55. } // namespace
  56. typedef boost::basic_regex<char, c_regex_traits<char> > c_regex_type;
  57. BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompA(regex_tA* expression, const char* ptr, int f)
  58. {
  59. if(expression->re_magic != magic_value)
  60. {
  61. expression->guts = 0;
  62. #ifndef BOOST_NO_EXCEPTIONS
  63. try{
  64. #endif
  65. expression->guts = new c_regex_type();
  66. #ifndef BOOST_NO_EXCEPTIONS
  67. } catch(...)
  68. {
  69. return REG_ESPACE;
  70. }
  71. #else
  72. if(0 == expression->guts)
  73. return REG_E_MEMORY;
  74. #endif
  75. }
  76. // set default flags:
  77. boost::uint_fast32_t flags = (f & REG_PERLEX) ? 0 : ((f & REG_EXTENDED) ? regex::extended : regex::basic);
  78. expression->eflags = (f & REG_NEWLINE) ? match_not_dot_newline : match_default;
  79. // and translate those that are actually set:
  80. if(f & REG_NOCOLLATE)
  81. {
  82. flags |= regex::nocollate;
  83. #ifndef BOOST_REGEX_V3
  84. flags &= ~regex::collate;
  85. #endif
  86. }
  87. if(f & REG_NOSUB)
  88. {
  89. //expression->eflags |= match_any;
  90. flags |= regex::nosubs;
  91. }
  92. if(f & REG_NOSPEC)
  93. flags |= regex::literal;
  94. if(f & REG_ICASE)
  95. flags |= regex::icase;
  96. if(f & REG_ESCAPE_IN_LISTS)
  97. flags &= ~regex::no_escape_in_lists;
  98. if(f & REG_NEWLINE_ALT)
  99. flags |= regex::newline_alt;
  100. const char* p2;
  101. if(f & REG_PEND)
  102. p2 = expression->re_endp;
  103. else p2 = ptr + std::strlen(ptr);
  104. int result;
  105. #ifndef BOOST_NO_EXCEPTIONS
  106. try{
  107. #endif
  108. expression->re_magic = magic_value;
  109. static_cast<c_regex_type*>(expression->guts)->set_expression(ptr, p2, flags);
  110. expression->re_nsub = static_cast<c_regex_type*>(expression->guts)->mark_count() - 1;
  111. result = static_cast<c_regex_type*>(expression->guts)->error_code();
  112. #ifndef BOOST_NO_EXCEPTIONS
  113. }
  114. catch(const boost::regex_error& be)
  115. {
  116. result = be.code();
  117. }
  118. catch(...)
  119. {
  120. result = REG_E_UNKNOWN;
  121. }
  122. #endif
  123. if(result)
  124. regfreeA(expression);
  125. return result;
  126. }
  127. BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int code, const regex_tA* e, char* buf, regsize_t buf_size)
  128. {
  129. std::size_t result = 0;
  130. if(code & REG_ITOA)
  131. {
  132. code &= ~REG_ITOA;
  133. if(code <= (int)REG_E_UNKNOWN)
  134. {
  135. result = std::strlen(names[code]) + 1;
  136. if(buf_size >= result)
  137. re_detail::strcpy_s(buf, buf_size, names[code]);
  138. return result;
  139. }
  140. return result;
  141. }
  142. if(code == REG_ATOI)
  143. {
  144. char localbuf[5];
  145. if(e == 0)
  146. return 0;
  147. for(int i = 0; i <= (int)REG_E_UNKNOWN; ++i)
  148. {
  149. if(std::strcmp(e->re_endp, names[i]) == 0)
  150. {
  151. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
  152. (::sprintf_s)(localbuf, 5, "%d", i);
  153. #else
  154. (std::sprintf)(localbuf, "%d", i);
  155. #endif
  156. if(std::strlen(localbuf) < buf_size)
  157. re_detail::strcpy_s(buf, buf_size, localbuf);
  158. return std::strlen(localbuf) + 1;
  159. }
  160. }
  161. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
  162. (::sprintf_s)(localbuf, 5, "%d", 0);
  163. #else
  164. (std::sprintf)(localbuf, "%d", 0);
  165. #endif
  166. if(std::strlen(localbuf) < buf_size)
  167. re_detail::strcpy_s(buf, buf_size, localbuf);
  168. return std::strlen(localbuf) + 1;
  169. }
  170. if(code <= (int)REG_E_UNKNOWN)
  171. {
  172. std::string p;
  173. if((e) && (e->re_magic == magic_value))
  174. p = static_cast<c_regex_type*>(e->guts)->get_traits().error_string(static_cast< ::boost::regex_constants::error_type>(code));
  175. else
  176. {
  177. p = re_detail::get_default_error_string(static_cast< ::boost::regex_constants::error_type>(code));
  178. }
  179. std::size_t len = p.size();
  180. if(len < buf_size)
  181. {
  182. re_detail::strcpy_s(buf, buf_size, p.c_str());
  183. }
  184. return len + 1;
  185. }
  186. if(buf_size)
  187. *buf = 0;
  188. return 0;
  189. }
  190. BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecA(const regex_tA* expression, const char* buf, regsize_t n, regmatch_t* array, int eflags)
  191. {
  192. #ifdef BOOST_MSVC
  193. #pragma warning(push)
  194. #pragma warning(disable:4267)
  195. #endif
  196. bool result = false;
  197. match_flag_type flags = match_default | expression->eflags;
  198. const char* end;
  199. const char* start;
  200. cmatch m;
  201. if(eflags & REG_NOTBOL)
  202. flags |= match_not_bol;
  203. if(eflags & REG_NOTEOL)
  204. flags |= match_not_eol;
  205. if(eflags & REG_STARTEND)
  206. {
  207. start = buf + array[0].rm_so;
  208. end = buf + array[0].rm_eo;
  209. }
  210. else
  211. {
  212. start = buf;
  213. end = buf + std::strlen(buf);
  214. }
  215. #ifndef BOOST_NO_EXCEPTIONS
  216. try{
  217. #endif
  218. if(expression->re_magic == magic_value)
  219. {
  220. result = regex_search(start, end, m, *static_cast<c_regex_type*>(expression->guts), flags);
  221. }
  222. else
  223. return result;
  224. #ifndef BOOST_NO_EXCEPTIONS
  225. } catch(...)
  226. {
  227. return REG_E_UNKNOWN;
  228. }
  229. #endif
  230. if(result)
  231. {
  232. // extract what matched:
  233. std::size_t i;
  234. for(i = 0; (i < n) && (i < expression->re_nsub + 1); ++i)
  235. {
  236. array[i].rm_so = (m[i].matched == false) ? -1 : (m[i].first - buf);
  237. array[i].rm_eo = (m[i].matched == false) ? -1 : (m[i].second - buf);
  238. }
  239. // and set anything else to -1:
  240. for(i = expression->re_nsub + 1; i < n; ++i)
  241. {
  242. array[i].rm_so = -1;
  243. array[i].rm_eo = -1;
  244. }
  245. return 0;
  246. }
  247. return REG_NOMATCH;
  248. #ifdef BOOST_MSVC
  249. #pragma warning(pop)
  250. #endif
  251. }
  252. BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeA(regex_tA* expression)
  253. {
  254. if(expression->re_magic == magic_value)
  255. {
  256. delete static_cast<c_regex_type*>(expression->guts);
  257. }
  258. expression->re_magic = 0;
  259. }
  260. } // namespace boost