Chris@16: // (C) Copyright Howard Hinnant Chris@16: // (C) Copyright 2011 Vicente J. Botet Escriba Chris@16: // Use, modification and distribution are subject to the Boost Software License, Chris@16: // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt). Chris@16: // Chris@16: Chris@16: #ifndef BOOST_CHRONO_IO_TIME_POINT_GET_HPP Chris@16: #define BOOST_CHRONO_IO_TIME_POINT_GET_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: /** Chris@16: * Duration formatting facet for input. Chris@16: */ Chris@16: namespace boost Chris@16: { Chris@16: namespace chrono Chris@16: { Chris@16: Chris@16: template > Chris@16: class time_point_get: public std::locale::facet Chris@16: { Chris@16: public: Chris@16: /** Chris@16: * Type of character the facet is instantiated on. Chris@16: */ Chris@16: typedef CharT char_type; Chris@16: /** Chris@16: * Type of iterator used to scan the character buffer. Chris@16: */ Chris@16: typedef InputIterator iter_type; Chris@16: Chris@16: /** Chris@16: * Construct a @c time_point_get facet. Chris@16: * @param refs Chris@16: * @Effects Construct a @c time_point_get facet. Chris@16: * If the @c refs argument is @c 0 then destruction of the object is Chris@16: * delegated to the @c locale, or locales, containing it. This allows Chris@16: * the user to ignore lifetime management issues. On the other had, Chris@16: * if @c refs is @c 1 then the object must be explicitly deleted; Chris@16: * the @c locale will not do so. In this case, the object can be Chris@16: * maintained across the lifetime of multiple locales. Chris@16: */ Chris@16: Chris@16: explicit time_point_get(size_t refs = 0) : Chris@16: std::locale::facet(refs) Chris@16: { Chris@16: } Chris@16: Chris@16: /** Chris@16: * @param s start input stream iterator Chris@16: * @param end end input stream iterator Chris@16: * @param ios a reference to a ios_base Chris@16: * @param err the ios_base state Chris@16: * @param d the duration Chris@16: * @param pattern begin of the formatting pattern Chris@16: * @param pat_end end of the formatting pattern Chris@16: * Chris@16: * Requires: [pattern,pat_end) shall be a valid range. Chris@16: * Chris@16: * Effects: The function starts by evaluating err = std::ios_base::goodbit. Chris@16: * It then enters a loop, reading zero or more characters from s at Chris@16: * each iteration. Unless otherwise specified below, the loop Chris@16: * terminates when the first of the following conditions holds: Chris@16: * - The expression pattern == pat_end evaluates to true. Chris@16: * - The expression err == std::ios_base::goodbit evaluates to false. Chris@16: * - The expression s == end evaluates to true, in which case the Chris@16: * function evaluates err = std::ios_base::eofbit | std::ios_base::failbit. Chris@16: * - The next element of pattern is equal to '%', followed by a conversion Chris@16: * specifier character, the functions @c get_duration or @c get_epoch are called depending on Chris@16: * whether the format is @c 'd' or @c 'e'. Chris@16: * If the number of elements in the range [pattern,pat_end) is not Chris@16: * sufficient to unambiguously determine whether the conversion Chris@16: * specification is complete and valid, the function evaluates Chris@16: * err = std::ios_base::failbit. Otherwise, the function evaluates Chris@16: * s = do_get(s, end, ios, err, d). If err == std::ios_base::goodbit holds after Chris@16: * the evaluation of the expression, the function increments pattern to Chris@16: * point just past the end of the conversion specification and continues Chris@16: * looping. Chris@16: * - The expression isspace(*pattern, ios.getloc()) evaluates to true, in Chris@16: * which case the function first increments pattern until Chris@16: * pattern == pat_end || !isspace(*pattern, ios.getloc()) evaluates to true, Chris@16: * then advances s until s == end || !isspace(*s, ios.getloc()) is true, Chris@16: * and finally resumes looping. Chris@16: * - The next character read from s matches the element pointed to by Chris@16: * pattern in a case-insensitive comparison, in which case the function Chris@16: * evaluates ++pattern, ++s and continues looping. Otherwise, the function Chris@16: * evaluates err = std::ios_base::failbit. Chris@16: * Chris@16: * Returns: s Chris@16: */ Chris@16: Chris@16: template Chris@16: iter_type get(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err, Chris@16: time_point &tp, const char_type *pattern, const char_type *pat_end) const Chris@16: { Chris@16: if (std::has_facet >(is.getloc())) Chris@16: { Chris@16: time_point_units const &facet = std::use_facet >(is.getloc()); Chris@16: return get(facet, i, e, is, err, tp, pattern, pat_end); Chris@16: } Chris@16: else Chris@16: { Chris@16: time_point_units_default facet; Chris@16: return get(facet, i, e, is, err, tp, pattern, pat_end); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: iter_type get(time_point_units const &facet, iter_type s, iter_type end, std::ios_base& ios, Chris@16: std::ios_base::iostate& err, time_point &tp, const char_type *pattern, Chris@16: const char_type *pat_end) const Chris@16: { Chris@16: Chris@16: Duration d; Chris@16: bool duration_found = false, epoch_found = false; Chris@16: Chris@16: const std::ctype& ct = std::use_facet >(ios.getloc()); Chris@16: err = std::ios_base::goodbit; Chris@16: while (pattern != pat_end && err == std::ios_base::goodbit) Chris@16: { Chris@16: if (s == end) Chris@16: { Chris@16: err |= std::ios_base::eofbit; Chris@16: break; Chris@16: } Chris@16: if (ct.narrow(*pattern, 0) == '%') Chris@16: { Chris@16: if (++pattern == pat_end) Chris@16: { Chris@16: err |= std::ios_base::failbit; Chris@16: return s; Chris@16: } Chris@16: char cmd = ct.narrow(*pattern, 0); Chris@16: switch (cmd) Chris@16: { Chris@16: case 'd': Chris@16: { Chris@16: if (duration_found) Chris@16: { Chris@16: err |= std::ios_base::failbit; Chris@16: return s; Chris@16: } Chris@16: duration_found = true; Chris@16: s = get_duration(s, end, ios, err, d); Chris@16: if (err & (std::ios_base::badbit | std::ios_base::failbit)) Chris@16: { Chris@16: return s; Chris@16: } Chris@16: break; Chris@16: } Chris@16: case 'e': Chris@16: { Chris@16: if (epoch_found) Chris@16: { Chris@16: err |= std::ios_base::failbit; Chris@16: return s; Chris@16: } Chris@16: epoch_found = true; Chris@16: s = get_epoch (facet, s, end, ios, err); Chris@16: if (err & (std::ios_base::badbit | std::ios_base::failbit)) Chris@16: { Chris@16: return s; Chris@16: } Chris@16: break; Chris@16: } Chris@16: default: Chris@16: BOOST_ASSERT(false && "Boost::Chrono internal error."); Chris@16: break; Chris@16: } Chris@16: Chris@16: ++pattern; Chris@16: } Chris@16: else if (ct.is(std::ctype_base::space, *pattern)) Chris@16: { Chris@16: for (++pattern; pattern != pat_end && ct.is(std::ctype_base::space, *pattern); ++pattern) Chris@16: ; Chris@16: for (; s != end && ct.is(std::ctype_base::space, *s); ++s) Chris@16: ; Chris@16: } Chris@16: else if (ct.toupper(*s) == ct.toupper(*pattern)) Chris@16: { Chris@16: ++s; Chris@16: ++pattern; Chris@16: } Chris@16: else Chris@16: { Chris@16: err |= std::ios_base::failbit; Chris@16: } Chris@16: } Chris@16: Chris@16: // Success! Store it. Chris@16: tp = time_point (d); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Chris@16: * @param s an input stream iterator Chris@16: * @param ios a reference to a ios_base Chris@16: * @param d the duration Chris@16: * Stores the duration pattern from the @c duration_unit facet in let say @c str. Last as if Chris@16: * @code Chris@16: * return get(s, end, ios, err, ios, d, str.data(), str.data() + str.size()); Chris@16: * @codeend Chris@16: * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid name Chris@16: */ Chris@16: template Chris@16: iter_type get(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err, Chris@16: time_point &tp) const Chris@16: { Chris@16: if (std::has_facet >(is.getloc())) Chris@16: { Chris@16: time_point_units const &facet = std::use_facet >(is.getloc()); Chris@16: std::basic_string str = facet.get_pattern(); Chris@16: return get(facet, i, e, is, err, tp, str.data(), str.data() + str.size()); Chris@16: } Chris@16: else Chris@16: { Chris@16: time_point_units_default facet; Chris@16: std::basic_string str = facet.get_pattern(); Chris@16: return get(facet, i, e, is, err, tp, str.data(), str.data() + str.size()); Chris@16: } Chris@16: } Chris@16: Chris@16: /** Chris@16: * As if Chris@16: * @code Chris@16: * return facet.get(s, end, ios, err, d); Chris@16: * @endcode Chris@16: * 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. Chris@16: * Chris@16: * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid duration. Chris@16: */ Chris@16: template Chris@16: iter_type get_duration(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err, Chris@16: duration& d) const Chris@16: { Chris@16: if (std::has_facet >(is.getloc())) Chris@16: { Chris@16: duration_get const &facet = std::use_facet >(is.getloc()); Chris@16: return get_duration(facet, i, e, is, err, d); Chris@16: } Chris@16: else Chris@16: { Chris@16: duration_get facet; Chris@16: return get_duration(facet, i, e, is, err, d); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: iter_type get_duration(duration_get const& facet, iter_type s, iter_type end, std::ios_base& ios, Chris@16: std::ios_base::iostate& err, duration& d) const Chris@16: { Chris@16: return facet.get(s, end, ios, err, d); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Chris@16: * @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. Chris@16: * Let @c epoch be the epoch string associated to the Clock using this facet. Chris@16: * Scans @c i to match @c epoch or @c e is reached. Chris@16: * Chris@16: * If not match before the @c e is reached @c std::ios_base::failbit is set in @c err. Chris@16: * If @c e is reached @c std::ios_base::failbit is set in @c err. Chris@16: * Chris@16: * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid epoch. Chris@16: */ Chris@16: template Chris@16: iter_type get_epoch(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err) const Chris@16: { Chris@16: if (std::has_facet >(is.getloc())) Chris@16: { Chris@16: time_point_units const &facet = std::use_facet >(is.getloc()); Chris@16: return get_epoch(facet, i, e, is, err); Chris@16: } Chris@16: else Chris@16: { Chris@16: time_point_units_default facet; Chris@16: return get_epoch(facet, i, e, is, err); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: iter_type get_epoch(time_point_units const &facet, iter_type i, iter_type e, std::ios_base&, Chris@16: std::ios_base::iostate& err) const Chris@16: { Chris@16: const std::basic_string epoch = facet.template get_epoch (); Chris@16: std::ptrdiff_t k = chrono_detail::scan_keyword(i, e, &epoch, &epoch + 1, Chris@16: //~ std::use_facet >(ios.getloc()), Chris@16: err) - &epoch; Chris@16: if (k == 1) Chris@16: { Chris@16: err |= std::ios_base::failbit; Chris@16: return i; Chris@16: } Chris@16: return i; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Unique identifier for this type of facet. Chris@16: */ Chris@16: static std::locale::id id; Chris@16: Chris@16: /** Chris@16: * @Effects Destroy the facet Chris@16: */ Chris@16: ~time_point_get() Chris@16: { Chris@16: } Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Unique identifier for this type of facet. Chris@16: */ Chris@16: template Chris@16: std::locale::id time_point_get::id; Chris@16: Chris@16: } // chrono Chris@16: } Chris@16: // boost Chris@16: Chris@16: #endif // header