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