]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/date_time/period_parser.hpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / boost / date_time / period_parser.hpp
1
2 #ifndef DATETIME_PERIOD_PARSER_HPP___
3 #define DATETIME_PERIOD_PARSER_HPP___
4
5 /* Copyright (c) 2002-2004 CrystalClear Software, Inc.
6 * Use, modification and distribution is subject to the
7 * Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
9 * Author: Jeff Garland, Bart Garst
10 * $Date$
11 */
12
13 #include <ios>
14 #include <string>
15 #include <vector>
16 #include <iterator>
17 #include <boost/throw_exception.hpp>
18 #include <boost/date_time/special_defs.hpp>
19 #include <boost/date_time/string_parse_tree.hpp>
20 #include <boost/date_time/string_convert.hpp>
21
22
23 namespace boost { namespace date_time {
24
25
26 //! Not a facet, but a class used to specify and control period parsing
27 /*! Provides settings for the following:
28 * - period_separator -- default '/'
29 * - period_open_start_delimeter -- default '['
30 * - period_open_range_end_delimeter -- default ')'
31 * - period_closed_range_end_delimeter -- default ']'
32 * - display_as_open_range, display_as_closed_range -- default closed_range
33 *
34 * For a typical date_period, the contents of the input stream would be
35 *@code
36 * [2004-Jan-04/2004-Feb-01]
37 *@endcode
38 * where the date format is controlled by the date facet
39 */
40 template<class date_type, typename CharT>
41 class period_parser {
42 public:
43 typedef std::basic_string<CharT> string_type;
44 typedef CharT char_type;
45 //typedef typename std::basic_string<char_type>::const_iterator const_itr_type;
46 typedef std::istreambuf_iterator<CharT> stream_itr_type;
47 typedef string_parse_tree<CharT> parse_tree_type;
48 typedef typename parse_tree_type::parse_match_result_type match_results;
49 typedef std::vector<std::basic_string<CharT> > collection_type;
50
51 static const char_type default_period_separator[2];
52 static const char_type default_period_start_delimeter[2];
53 static const char_type default_period_open_range_end_delimeter[2];
54 static const char_type default_period_closed_range_end_delimeter[2];
55
56 enum period_range_option { AS_OPEN_RANGE, AS_CLOSED_RANGE };
57
58 //! Constructor that sets up period parser options
59 period_parser(period_range_option range_opt = AS_CLOSED_RANGE,
60 const char_type* const period_separator = default_period_separator,
61 const char_type* const period_start_delimeter = default_period_start_delimeter,
62 const char_type* const period_open_range_end_delimeter = default_period_open_range_end_delimeter,
63 const char_type* const period_closed_range_end_delimeter = default_period_closed_range_end_delimeter)
64 : m_range_option(range_opt)
65 {
66 delimiters.push_back(string_type(period_separator));
67 delimiters.push_back(string_type(period_start_delimeter));
68 delimiters.push_back(string_type(period_open_range_end_delimeter));
69 delimiters.push_back(string_type(period_closed_range_end_delimeter));
70 }
71
72 period_parser(const period_parser<date_type,CharT>& p_parser)
73 {
74 this->delimiters = p_parser.delimiters;
75 this->m_range_option = p_parser.m_range_option;
76 }
77
78 period_range_option range_option() const
79 {
80 return m_range_option;
81 }
82 void range_option(period_range_option option)
83 {
84 m_range_option = option;
85 }
86 collection_type delimiter_strings() const
87 {
88 return delimiters;
89 }
90 void delimiter_strings(const string_type& separator,
91 const string_type& start_delim,
92 const string_type& open_end_delim,
93 const string_type& closed_end_delim)
94 {
95 delimiters.clear();
96 delimiters.push_back(separator);
97 delimiters.push_back(start_delim);
98 delimiters.push_back(open_end_delim);
99 delimiters.push_back(closed_end_delim);
100 }
101
102 //! Generic code to parse a period -- no matter the period type.
103 /*! This generic code will parse any period using a facet to
104 * to get the 'elements'. For example, in the case of a date_period
105 * the elements will be instances of a date which will be parsed
106 * according the to setup in the passed facet parameter.
107 *
108 * The steps for parsing a period are always the same:
109 * - consume the start delimiter
110 * - get start element
111 * - consume the separator
112 * - get either last or end element depending on range settings
113 * - consume the end delimeter depending on range settings
114 *
115 * Thus for a typical date period the contents of the input stream
116 * might look like this:
117 *@code
118 *
119 * [March 01, 2004/June 07, 2004] <-- closed range
120 * [March 01, 2004/June 08, 2004) <-- open range
121 *
122 *@endcode
123 */
124 template<class period_type, class duration_type, class facet_type>
125 period_type get_period(stream_itr_type& sitr,
126 stream_itr_type& stream_end,
127 std::ios_base& a_ios,
128 const period_type& /* p */,
129 const duration_type& dur_unit,
130 const facet_type& facet) const
131 {
132 // skip leading whitespace
133 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
134
135 typedef typename period_type::point_type point_type;
136 point_type p1(not_a_date_time), p2(not_a_date_time);
137
138
139 consume_delim(sitr, stream_end, delimiters[START]); // start delim
140 facet.get(sitr, stream_end, a_ios, p1); // first point
141 consume_delim(sitr, stream_end, delimiters[SEPARATOR]); // separator
142 facet.get(sitr, stream_end, a_ios, p2); // second point
143
144 // period construction parameters are always open range [begin, end)
145 if (m_range_option == AS_CLOSED_RANGE) {
146 consume_delim(sitr, stream_end, delimiters[CLOSED_END]);// end delim
147 // add 1 duration unit to p2 to make range open
148 p2 += dur_unit;
149 }
150 else {
151 consume_delim(sitr, stream_end, delimiters[OPEN_END]); // end delim
152 }
153
154 return period_type(p1, p2);
155 }
156
157 private:
158 collection_type delimiters;
159 period_range_option m_range_option;
160
161 enum delim_ids { SEPARATOR, START, OPEN_END, CLOSED_END };
162
163 //! throws ios_base::failure if delimiter and parsed data do not match
164 void consume_delim(stream_itr_type& sitr,
165 stream_itr_type& stream_end,
166 const string_type& delim) const
167 {
168 /* string_parse_tree will not parse a string of punctuation characters
169 * without knowing exactly how many characters to process
170 * Ex [2000. Will not parse out the '[' string without knowing
171 * to process only one character. By using length of the delimiter
172 * string we can safely iterate past it. */
173 string_type s;
174 for(unsigned int i = 0; i < delim.length() && sitr != stream_end; ++i) {
175 s += *sitr;
176 ++sitr;
177 }
178 if(s != delim) {
179 boost::throw_exception(std::ios_base::failure("Parse failed. Expected '"
180 + convert_string_type<char_type,char>(delim) + "' but found '" + convert_string_type<char_type,char>(s) + "'"));
181 }
182 }
183 };
184
185 template <class date_type, class char_type>
186 const typename period_parser<date_type, char_type>::char_type
187 period_parser<date_type, char_type>::default_period_separator[2] = {'/'};
188
189 template <class date_type, class char_type>
190 const typename period_parser<date_type, char_type>::char_type
191 period_parser<date_type, char_type>::default_period_start_delimeter[2] = {'['};
192
193 template <class date_type, class char_type>
194 const typename period_parser<date_type, char_type>::char_type
195 period_parser<date_type, char_type>::default_period_open_range_end_delimeter[2] = {')'};
196
197 template <class date_type, class char_type>
198 const typename period_parser<date_type, char_type>::char_type
199 period_parser<date_type, char_type>::default_period_closed_range_end_delimeter[2] = {']'};
200
201 } } //namespace boost::date_time
202
203 #endif // DATETIME_PERIOD_PARSER_HPP___