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