time_point_get.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // (C) Copyright Howard Hinnant
  2. // (C) Copyright 2011 Vicente J. Botet Escriba
  3. // Use, modification and distribution are subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt).
  6. //
  7. #ifndef BOOST_CHRONO_IO_TIME_POINT_GET_HPP
  8. #define BOOST_CHRONO_IO_TIME_POINT_GET_HPP
  9. #include <boost/chrono/config.hpp>
  10. #include <boost/chrono/detail/scan_keyword.hpp>
  11. #include <boost/chrono/io/time_point_units.hpp>
  12. #include <boost/chrono/io/duration_get.hpp>
  13. #include <boost/assert.hpp>
  14. #include <locale>
  15. #include <string>
  16. /**
  17. * Duration formatting facet for input.
  18. */
  19. namespace boost
  20. {
  21. namespace chrono
  22. {
  23. template <class CharT, class InputIterator = std::istreambuf_iterator<CharT> >
  24. class time_point_get: public std::locale::facet
  25. {
  26. public:
  27. /**
  28. * Type of character the facet is instantiated on.
  29. */
  30. typedef CharT char_type;
  31. /**
  32. * Type of iterator used to scan the character buffer.
  33. */
  34. typedef InputIterator iter_type;
  35. /**
  36. * Construct a @c time_point_get facet.
  37. * @param refs
  38. * @Effects Construct a @c time_point_get facet.
  39. * If the @c refs argument is @c 0 then destruction of the object is
  40. * delegated to the @c locale, or locales, containing it. This allows
  41. * the user to ignore lifetime management issues. On the other had,
  42. * if @c refs is @c 1 then the object must be explicitly deleted;
  43. * the @c locale will not do so. In this case, the object can be
  44. * maintained across the lifetime of multiple locales.
  45. */
  46. explicit time_point_get(size_t refs = 0) :
  47. std::locale::facet(refs)
  48. {
  49. }
  50. /**
  51. * @param s start input stream iterator
  52. * @param end end input stream iterator
  53. * @param ios a reference to a ios_base
  54. * @param err the ios_base state
  55. * @param d the duration
  56. * @param pattern begin of the formatting pattern
  57. * @param pat_end end of the formatting pattern
  58. *
  59. * Requires: [pattern,pat_end) shall be a valid range.
  60. *
  61. * Effects: The function starts by evaluating err = std::ios_base::goodbit.
  62. * It then enters a loop, reading zero or more characters from s at
  63. * each iteration. Unless otherwise specified below, the loop
  64. * terminates when the first of the following conditions holds:
  65. * - The expression pattern == pat_end evaluates to true.
  66. * - The expression err == std::ios_base::goodbit evaluates to false.
  67. * - The expression s == end evaluates to true, in which case the
  68. * function evaluates err = std::ios_base::eofbit | std::ios_base::failbit.
  69. * - The next element of pattern is equal to '%', followed by a conversion
  70. * specifier character, the functions @c get_duration or @c get_epoch are called depending on
  71. * whether the format is @c 'd' or @c 'e'.
  72. * If the number of elements in the range [pattern,pat_end) is not
  73. * sufficient to unambiguously determine whether the conversion
  74. * specification is complete and valid, the function evaluates
  75. * err = std::ios_base::failbit. Otherwise, the function evaluates
  76. * s = do_get(s, end, ios, err, d). If err == std::ios_base::goodbit holds after
  77. * the evaluation of the expression, the function increments pattern to
  78. * point just past the end of the conversion specification and continues
  79. * looping.
  80. * - The expression isspace(*pattern, ios.getloc()) evaluates to true, in
  81. * which case the function first increments pattern until
  82. * pattern == pat_end || !isspace(*pattern, ios.getloc()) evaluates to true,
  83. * then advances s until s == end || !isspace(*s, ios.getloc()) is true,
  84. * and finally resumes looping.
  85. * - The next character read from s matches the element pointed to by
  86. * pattern in a case-insensitive comparison, in which case the function
  87. * evaluates ++pattern, ++s and continues looping. Otherwise, the function
  88. * evaluates err = std::ios_base::failbit.
  89. *
  90. * Returns: s
  91. */
  92. template <class Clock, class Duration>
  93. iter_type get(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
  94. time_point<Clock, Duration> &tp, const char_type *pattern, const char_type *pat_end) const
  95. {
  96. if (std::has_facet<time_point_units<CharT> >(is.getloc()))
  97. {
  98. time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(is.getloc());
  99. return get(facet, i, e, is, err, tp, pattern, pat_end);
  100. }
  101. else
  102. {
  103. time_point_units_default<CharT> facet;
  104. return get(facet, i, e, is, err, tp, pattern, pat_end);
  105. }
  106. }
  107. template <class Clock, class Duration>
  108. iter_type get(time_point_units<CharT> const &facet, iter_type s, iter_type end, std::ios_base& ios,
  109. std::ios_base::iostate& err, time_point<Clock, Duration> &tp, const char_type *pattern,
  110. const char_type *pat_end) const
  111. {
  112. Duration d;
  113. bool duration_found = false, epoch_found = false;
  114. const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
  115. err = std::ios_base::goodbit;
  116. while (pattern != pat_end && err == std::ios_base::goodbit)
  117. {
  118. if (s == end)
  119. {
  120. err |= std::ios_base::eofbit;
  121. break;
  122. }
  123. if (ct.narrow(*pattern, 0) == '%')
  124. {
  125. if (++pattern == pat_end)
  126. {
  127. err |= std::ios_base::failbit;
  128. return s;
  129. }
  130. char cmd = ct.narrow(*pattern, 0);
  131. switch (cmd)
  132. {
  133. case 'd':
  134. {
  135. if (duration_found)
  136. {
  137. err |= std::ios_base::failbit;
  138. return s;
  139. }
  140. duration_found = true;
  141. s = get_duration(s, end, ios, err, d);
  142. if (err & (std::ios_base::badbit | std::ios_base::failbit))
  143. {
  144. return s;
  145. }
  146. break;
  147. }
  148. case 'e':
  149. {
  150. if (epoch_found)
  151. {
  152. err |= std::ios_base::failbit;
  153. return s;
  154. }
  155. epoch_found = true;
  156. s = get_epoch<Clock> (facet, s, end, ios, err);
  157. if (err & (std::ios_base::badbit | std::ios_base::failbit))
  158. {
  159. return s;
  160. }
  161. break;
  162. }
  163. default:
  164. BOOST_ASSERT(false && "Boost::Chrono internal error.");
  165. break;
  166. }
  167. ++pattern;
  168. }
  169. else if (ct.is(std::ctype_base::space, *pattern))
  170. {
  171. for (++pattern; pattern != pat_end && ct.is(std::ctype_base::space, *pattern); ++pattern)
  172. ;
  173. for (; s != end && ct.is(std::ctype_base::space, *s); ++s)
  174. ;
  175. }
  176. else if (ct.toupper(*s) == ct.toupper(*pattern))
  177. {
  178. ++s;
  179. ++pattern;
  180. }
  181. else
  182. {
  183. err |= std::ios_base::failbit;
  184. }
  185. }
  186. // Success! Store it.
  187. tp = time_point<Clock, Duration> (d);
  188. return s;
  189. }
  190. /**
  191. *
  192. * @param s an input stream iterator
  193. * @param ios a reference to a ios_base
  194. * @param d the duration
  195. * Stores the duration pattern from the @c duration_unit facet in let say @c str. Last as if
  196. * @code
  197. * return get(s, end, ios, err, ios, d, str.data(), str.data() + str.size());
  198. * @codeend
  199. * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid name
  200. */
  201. template <class Clock, class Duration>
  202. iter_type get(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
  203. time_point<Clock, Duration> &tp) const
  204. {
  205. if (std::has_facet<time_point_units<CharT> >(is.getloc()))
  206. {
  207. time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(is.getloc());
  208. std::basic_string<CharT> str = facet.get_pattern();
  209. return get(facet, i, e, is, err, tp, str.data(), str.data() + str.size());
  210. }
  211. else
  212. {
  213. time_point_units_default<CharT> facet;
  214. std::basic_string<CharT> str = facet.get_pattern();
  215. return get(facet, i, e, is, err, tp, str.data(), str.data() + str.size());
  216. }
  217. }
  218. /**
  219. * As if
  220. * @code
  221. * return facet.get(s, end, ios, err, d);
  222. * @endcode
  223. * where @c facet is either the @c duration_get facet associated to the @c ios or an instance of the default @c duration_get facet.
  224. *
  225. * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid duration.
  226. */
  227. template <typename Rep, typename Period>
  228. iter_type get_duration(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
  229. duration<Rep, Period>& d) const
  230. {
  231. if (std::has_facet<duration_get<CharT> >(is.getloc()))
  232. {
  233. duration_get<CharT> const &facet = std::use_facet<duration_get<CharT> >(is.getloc());
  234. return get_duration(facet, i, e, is, err, d);
  235. }
  236. else
  237. {
  238. duration_get<CharT> facet;
  239. return get_duration(facet, i, e, is, err, d);
  240. }
  241. }
  242. template <typename Rep, typename Period>
  243. iter_type get_duration(duration_get<CharT> const& facet, iter_type s, iter_type end, std::ios_base& ios,
  244. std::ios_base::iostate& err, duration<Rep, Period>& d) const
  245. {
  246. return facet.get(s, end, ios, err, d);
  247. }
  248. /**
  249. *
  250. * @Effects Let @c facet be the @c time_point_units facet associated to @c is or a new instance of the default @c time_point_units_default facet.
  251. * Let @c epoch be the epoch string associated to the Clock using this facet.
  252. * Scans @c i to match @c epoch or @c e is reached.
  253. *
  254. * If not match before the @c e is reached @c std::ios_base::failbit is set in @c err.
  255. * If @c e is reached @c std::ios_base::failbit is set in @c err.
  256. *
  257. * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid epoch.
  258. */
  259. template <class Clock>
  260. iter_type get_epoch(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err) const
  261. {
  262. if (std::has_facet<time_point_units<CharT> >(is.getloc()))
  263. {
  264. time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(is.getloc());
  265. return get_epoch<Clock>(facet, i, e, is, err);
  266. }
  267. else
  268. {
  269. time_point_units_default<CharT> facet;
  270. return get_epoch<Clock>(facet, i, e, is, err);
  271. }
  272. }
  273. template <class Clock>
  274. iter_type get_epoch(time_point_units<CharT> const &facet, iter_type i, iter_type e, std::ios_base&,
  275. std::ios_base::iostate& err) const
  276. {
  277. const std::basic_string<CharT> epoch = facet.template get_epoch<Clock> ();
  278. std::ptrdiff_t k = chrono_detail::scan_keyword(i, e, &epoch, &epoch + 1,
  279. //~ std::use_facet<std::ctype<CharT> >(ios.getloc()),
  280. err) - &epoch;
  281. if (k == 1)
  282. {
  283. err |= std::ios_base::failbit;
  284. return i;
  285. }
  286. return i;
  287. }
  288. /**
  289. * Unique identifier for this type of facet.
  290. */
  291. static std::locale::id id;
  292. /**
  293. * @Effects Destroy the facet
  294. */
  295. ~time_point_get()
  296. {
  297. }
  298. };
  299. /**
  300. * Unique identifier for this type of facet.
  301. */
  302. template <class CharT, class InputIterator>
  303. std::locale::id time_point_get<CharT, InputIterator>::id;
  304. } // chrono
  305. }
  306. // boost
  307. #endif // header