File indexing completed on 2025-01-18 09:29:47
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_CHRONO_IO_TIME_POINT_GET_HPP
0009 #define BOOST_CHRONO_IO_TIME_POINT_GET_HPP
0010
0011 #include <boost/chrono/config.hpp>
0012 #include <boost/chrono/detail/scan_keyword.hpp>
0013 #include <boost/chrono/io/time_point_units.hpp>
0014 #include <boost/chrono/io/duration_get.hpp>
0015 #include <boost/assert.hpp>
0016 #include <locale>
0017 #include <string>
0018
0019
0020
0021
0022 namespace boost
0023 {
0024 namespace chrono
0025 {
0026
0027 template <class CharT, class InputIterator = std::istreambuf_iterator<CharT> >
0028 class time_point_get: public std::locale::facet
0029 {
0030 public:
0031
0032
0033
0034 typedef CharT char_type;
0035
0036
0037
0038 typedef InputIterator iter_type;
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 explicit time_point_get(size_t refs = 0) :
0053 std::locale::facet(refs)
0054 {
0055 }
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100 template <class Clock, class Duration>
0101 iter_type get(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
0102 time_point<Clock, Duration> &tp, const char_type *pattern, const char_type *pat_end) const
0103 {
0104 if (std::has_facet<time_point_units<CharT> >(is.getloc()))
0105 {
0106 time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(is.getloc());
0107 return get(facet, i, e, is, err, tp, pattern, pat_end);
0108 }
0109 else
0110 {
0111 time_point_units_default<CharT> facet;
0112 return get(facet, i, e, is, err, tp, pattern, pat_end);
0113 }
0114 }
0115
0116 template <class Clock, class Duration>
0117 iter_type get(time_point_units<CharT> const &facet, iter_type s, iter_type end, std::ios_base& ios,
0118 std::ios_base::iostate& err, time_point<Clock, Duration> &tp, const char_type *pattern,
0119 const char_type *pat_end) const
0120 {
0121
0122 Duration d;
0123 bool duration_found = false, epoch_found = false;
0124
0125 const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
0126 err = std::ios_base::goodbit;
0127 while (pattern != pat_end && err == std::ios_base::goodbit)
0128 {
0129 if (s == end)
0130 {
0131 err |= std::ios_base::eofbit;
0132 break;
0133 }
0134 if (ct.narrow(*pattern, 0) == '%')
0135 {
0136 if (++pattern == pat_end)
0137 {
0138 err |= std::ios_base::failbit;
0139 return s;
0140 }
0141 char cmd = ct.narrow(*pattern, 0);
0142 switch (cmd)
0143 {
0144 case 'd':
0145 {
0146 if (duration_found)
0147 {
0148 err |= std::ios_base::failbit;
0149 return s;
0150 }
0151 duration_found = true;
0152 s = get_duration(s, end, ios, err, d);
0153 if (err & (std::ios_base::badbit | std::ios_base::failbit))
0154 {
0155 return s;
0156 }
0157 break;
0158 }
0159 case 'e':
0160 {
0161 if (epoch_found)
0162 {
0163 err |= std::ios_base::failbit;
0164 return s;
0165 }
0166 epoch_found = true;
0167 s = get_epoch<Clock> (facet, s, end, ios, err);
0168 if (err & (std::ios_base::badbit | std::ios_base::failbit))
0169 {
0170 return s;
0171 }
0172 break;
0173 }
0174 default:
0175 BOOST_ASSERT(false && "Boost::Chrono internal error.");
0176 break;
0177 }
0178
0179 ++pattern;
0180 }
0181 else if (ct.is(std::ctype_base::space, *pattern))
0182 {
0183 for (++pattern; pattern != pat_end && ct.is(std::ctype_base::space, *pattern); ++pattern)
0184 ;
0185 for (; s != end && ct.is(std::ctype_base::space, *s); ++s)
0186 ;
0187 }
0188 else if (ct.toupper(*s) == ct.toupper(*pattern))
0189 {
0190 ++s;
0191 ++pattern;
0192 }
0193 else
0194 {
0195 err |= std::ios_base::failbit;
0196 }
0197 }
0198
0199
0200 tp = time_point<Clock, Duration> (d);
0201 return s;
0202 }
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215 template <class Clock, class Duration>
0216 iter_type get(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
0217 time_point<Clock, Duration> &tp) const
0218 {
0219 if (std::has_facet<time_point_units<CharT> >(is.getloc()))
0220 {
0221 time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(is.getloc());
0222 std::basic_string<CharT> str = facet.get_pattern();
0223 return get(facet, i, e, is, err, tp, str.data(), str.data() + str.size());
0224 }
0225 else
0226 {
0227 time_point_units_default<CharT> facet;
0228 std::basic_string<CharT> str = facet.get_pattern();
0229 return get(facet, i, e, is, err, tp, str.data(), str.data() + str.size());
0230 }
0231 }
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 template <typename Rep, typename Period>
0243 iter_type get_duration(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
0244 duration<Rep, Period>& d) const
0245 {
0246 if (std::has_facet<duration_get<CharT> >(is.getloc()))
0247 {
0248 duration_get<CharT> const &facet = std::use_facet<duration_get<CharT> >(is.getloc());
0249 return get_duration(facet, i, e, is, err, d);
0250 }
0251 else
0252 {
0253 duration_get<CharT> facet;
0254 return get_duration(facet, i, e, is, err, d);
0255 }
0256 }
0257
0258 template <typename Rep, typename Period>
0259 iter_type get_duration(duration_get<CharT> const& facet, iter_type s, iter_type end, std::ios_base& ios,
0260 std::ios_base::iostate& err, duration<Rep, Period>& d) const
0261 {
0262 return facet.get(s, end, ios, err, d);
0263 }
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276 template <class Clock>
0277 iter_type get_epoch(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err) const
0278 {
0279 if (std::has_facet<time_point_units<CharT> >(is.getloc()))
0280 {
0281 time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(is.getloc());
0282 return get_epoch<Clock>(facet, i, e, is, err);
0283 }
0284 else
0285 {
0286 time_point_units_default<CharT> facet;
0287 return get_epoch<Clock>(facet, i, e, is, err);
0288 }
0289 }
0290
0291 template <class Clock>
0292 iter_type get_epoch(time_point_units<CharT> const &facet, iter_type i, iter_type e, std::ios_base&,
0293 std::ios_base::iostate& err) const
0294 {
0295 const std::basic_string<CharT> epoch = facet.template get_epoch<Clock> ();
0296 std::ptrdiff_t k = chrono_detail::scan_keyword(i, e, &epoch, &epoch + 1,
0297
0298 err) - &epoch;
0299 if (k == 1)
0300 {
0301 err |= std::ios_base::failbit;
0302 return i;
0303 }
0304 return i;
0305 }
0306
0307
0308
0309
0310 static std::locale::id id;
0311
0312
0313
0314
0315 ~time_point_get()
0316 {
0317 }
0318 };
0319
0320
0321
0322
0323 template <class CharT, class InputIterator>
0324 std::locale::id time_point_get<CharT, InputIterator>::id;
0325
0326 }
0327 }
0328
0329
0330 #endif