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