123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- #ifndef BOOST_CHRONO_IO_TIME_POINT_GET_HPP
- #define BOOST_CHRONO_IO_TIME_POINT_GET_HPP
- #include <boost/chrono/config.hpp>
- #include <boost/chrono/detail/scan_keyword.hpp>
- #include <boost/chrono/io/time_point_units.hpp>
- #include <boost/chrono/io/duration_get.hpp>
- #include <boost/assert.hpp>
- #include <locale>
- #include <string>
- namespace boost
- {
- namespace chrono
- {
- template <class CharT, class InputIterator = std::istreambuf_iterator<CharT> >
- class time_point_get: public std::locale::facet
- {
- public:
-
- typedef CharT char_type;
-
- typedef InputIterator iter_type;
-
- explicit time_point_get(size_t refs = 0) :
- std::locale::facet(refs)
- {
- }
-
- template <class Clock, class Duration>
- iter_type get(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
- time_point<Clock, Duration> &tp, const char_type *pattern, const char_type *pat_end) const
- {
- if (std::has_facet<time_point_units<CharT> >(is.getloc()))
- {
- time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(is.getloc());
- return get(facet, i, e, is, err, tp, pattern, pat_end);
- }
- else
- {
- time_point_units_default<CharT> facet;
- return get(facet, i, e, is, err, tp, pattern, pat_end);
- }
- }
- template <class Clock, class Duration>
- iter_type get(time_point_units<CharT> const &facet, iter_type s, iter_type end, std::ios_base& ios,
- std::ios_base::iostate& err, time_point<Clock, Duration> &tp, const char_type *pattern,
- const char_type *pat_end) const
- {
- Duration d;
- bool duration_found = false, epoch_found = false;
- const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
- err = std::ios_base::goodbit;
- while (pattern != pat_end && err == std::ios_base::goodbit)
- {
- if (s == end)
- {
- err |= std::ios_base::eofbit;
- break;
- }
- if (ct.narrow(*pattern, 0) == '%')
- {
- if (++pattern == pat_end)
- {
- err |= std::ios_base::failbit;
- return s;
- }
- char cmd = ct.narrow(*pattern, 0);
- switch (cmd)
- {
- case 'd':
- {
- if (duration_found)
- {
- err |= std::ios_base::failbit;
- return s;
- }
- duration_found = true;
- s = get_duration(s, end, ios, err, d);
- if (err & (std::ios_base::badbit | std::ios_base::failbit))
- {
- return s;
- }
- break;
- }
- case 'e':
- {
- if (epoch_found)
- {
- err |= std::ios_base::failbit;
- return s;
- }
- epoch_found = true;
- s = get_epoch<Clock> (facet, s, end, ios, err);
- if (err & (std::ios_base::badbit | std::ios_base::failbit))
- {
- return s;
- }
- break;
- }
- default:
- BOOST_ASSERT(false && "Boost::Chrono internal error.");
- break;
- }
- ++pattern;
- }
- else if (ct.is(std::ctype_base::space, *pattern))
- {
- for (++pattern; pattern != pat_end && ct.is(std::ctype_base::space, *pattern); ++pattern)
- ;
- for (; s != end && ct.is(std::ctype_base::space, *s); ++s)
- ;
- }
- else if (ct.toupper(*s) == ct.toupper(*pattern))
- {
- ++s;
- ++pattern;
- }
- else
- {
- err |= std::ios_base::failbit;
- }
- }
-
- tp = time_point<Clock, Duration> (d);
- return s;
- }
-
- template <class Clock, class Duration>
- iter_type get(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
- time_point<Clock, Duration> &tp) const
- {
- if (std::has_facet<time_point_units<CharT> >(is.getloc()))
- {
- time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(is.getloc());
- std::basic_string<CharT> str = facet.get_pattern();
- return get(facet, i, e, is, err, tp, str.data(), str.data() + str.size());
- }
- else
- {
- time_point_units_default<CharT> facet;
- std::basic_string<CharT> str = facet.get_pattern();
- return get(facet, i, e, is, err, tp, str.data(), str.data() + str.size());
- }
- }
-
- template <typename Rep, typename Period>
- iter_type get_duration(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
- duration<Rep, Period>& d) const
- {
- if (std::has_facet<duration_get<CharT> >(is.getloc()))
- {
- duration_get<CharT> const &facet = std::use_facet<duration_get<CharT> >(is.getloc());
- return get_duration(facet, i, e, is, err, d);
- }
- else
- {
- duration_get<CharT> facet;
- return get_duration(facet, i, e, is, err, d);
- }
- }
- template <typename Rep, typename Period>
- iter_type get_duration(duration_get<CharT> const& facet, iter_type s, iter_type end, std::ios_base& ios,
- std::ios_base::iostate& err, duration<Rep, Period>& d) const
- {
- return facet.get(s, end, ios, err, d);
- }
-
- template <class Clock>
- iter_type get_epoch(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err) const
- {
- if (std::has_facet<time_point_units<CharT> >(is.getloc()))
- {
- time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(is.getloc());
- return get_epoch<Clock>(facet, i, e, is, err);
- }
- else
- {
- time_point_units_default<CharT> facet;
- return get_epoch<Clock>(facet, i, e, is, err);
- }
- }
- template <class Clock>
- iter_type get_epoch(time_point_units<CharT> const &facet, iter_type i, iter_type e, std::ios_base&,
- std::ios_base::iostate& err) const
- {
- const std::basic_string<CharT> epoch = facet.template get_epoch<Clock> ();
- std::ptrdiff_t k = chrono_detail::scan_keyword(i, e, &epoch, &epoch + 1,
-
- err) - &epoch;
- if (k == 1)
- {
- err |= std::ios_base::failbit;
- return i;
- }
- return i;
- }
-
- static std::locale::id id;
-
- ~time_point_get()
- {
- }
- };
-
- template <class CharT, class InputIterator>
- std::locale::id time_point_get<CharT, InputIterator>::id;
- }
- }
- #endif
|