File indexing completed on 2025-01-30 09:35:24
0001
0002 #ifndef DATETIME_PERIOD_PARSER_HPP___
0003 #define DATETIME_PERIOD_PARSER_HPP___
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <ios>
0014 #include <string>
0015 #include <vector>
0016 #include <iterator>
0017 #include <boost/throw_exception.hpp>
0018 #include <boost/date_time/special_defs.hpp>
0019 #include <boost/date_time/string_parse_tree.hpp>
0020 #include <boost/date_time/string_convert.hpp>
0021
0022
0023 namespace boost { namespace date_time {
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 template<class date_type, typename CharT>
0041 class period_parser {
0042 public:
0043 typedef std::basic_string<CharT> string_type;
0044 typedef CharT char_type;
0045
0046 typedef std::istreambuf_iterator<CharT> stream_itr_type;
0047 typedef string_parse_tree<CharT> parse_tree_type;
0048 typedef typename parse_tree_type::parse_match_result_type match_results;
0049 typedef std::vector<std::basic_string<CharT> > collection_type;
0050
0051 static const char_type default_period_separator[2];
0052 static const char_type default_period_start_delimeter[2];
0053 static const char_type default_period_open_range_end_delimeter[2];
0054 static const char_type default_period_closed_range_end_delimeter[2];
0055
0056 enum period_range_option { AS_OPEN_RANGE, AS_CLOSED_RANGE };
0057
0058
0059 period_parser(period_range_option range_opt = AS_CLOSED_RANGE,
0060 const char_type* const period_separator = default_period_separator,
0061 const char_type* const period_start_delimeter = default_period_start_delimeter,
0062 const char_type* const period_open_range_end_delimeter = default_period_open_range_end_delimeter,
0063 const char_type* const period_closed_range_end_delimeter = default_period_closed_range_end_delimeter)
0064 : m_range_option(range_opt)
0065 {
0066 delimiters.push_back(string_type(period_separator));
0067 delimiters.push_back(string_type(period_start_delimeter));
0068 delimiters.push_back(string_type(period_open_range_end_delimeter));
0069 delimiters.push_back(string_type(period_closed_range_end_delimeter));
0070 }
0071
0072 period_range_option range_option() const
0073 {
0074 return m_range_option;
0075 }
0076 void range_option(period_range_option option)
0077 {
0078 m_range_option = option;
0079 }
0080 collection_type delimiter_strings() const
0081 {
0082 return delimiters;
0083 }
0084 void delimiter_strings(const string_type& separator,
0085 const string_type& start_delim,
0086 const string_type& open_end_delim,
0087 const string_type& closed_end_delim)
0088 {
0089 delimiters.clear();
0090 delimiters.push_back(separator);
0091 delimiters.push_back(start_delim);
0092 delimiters.push_back(open_end_delim);
0093 delimiters.push_back(closed_end_delim);
0094 }
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 template<class period_type, class duration_type, class facet_type>
0119 period_type get_period(stream_itr_type& sitr,
0120 stream_itr_type& stream_end,
0121 std::ios_base& a_ios,
0122 const period_type& ,
0123 const duration_type& dur_unit,
0124 const facet_type& facet) const
0125 {
0126
0127 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
0128
0129 typedef typename period_type::point_type point_type;
0130 point_type p1(not_a_date_time), p2(not_a_date_time);
0131
0132
0133 consume_delim(sitr, stream_end, delimiters[START]);
0134 facet.get(sitr, stream_end, a_ios, p1);
0135 consume_delim(sitr, stream_end, delimiters[SEPARATOR]);
0136 facet.get(sitr, stream_end, a_ios, p2);
0137
0138
0139 if (m_range_option == AS_CLOSED_RANGE) {
0140 consume_delim(sitr, stream_end, delimiters[CLOSED_END]);
0141
0142 p2 += dur_unit;
0143 }
0144 else {
0145 consume_delim(sitr, stream_end, delimiters[OPEN_END]);
0146 }
0147
0148 return period_type(p1, p2);
0149 }
0150
0151 private:
0152 collection_type delimiters;
0153 period_range_option m_range_option;
0154
0155 enum delim_ids { SEPARATOR, START, OPEN_END, CLOSED_END };
0156
0157
0158 void consume_delim(stream_itr_type& sitr,
0159 stream_itr_type& stream_end,
0160 const string_type& delim) const
0161 {
0162
0163
0164
0165
0166
0167 string_type s;
0168 for(unsigned int i = 0; i < delim.length() && sitr != stream_end; ++i) {
0169 s += *sitr;
0170 ++sitr;
0171 }
0172 if(s != delim) {
0173 boost::throw_exception(std::ios_base::failure("Parse failed. Expected '"
0174 + convert_string_type<char_type,char>(delim) + "' but found '" + convert_string_type<char_type,char>(s) + "'"));
0175 }
0176 }
0177 };
0178
0179 template <class date_type, class char_type>
0180 const typename period_parser<date_type, char_type>::char_type
0181 period_parser<date_type, char_type>::default_period_separator[2] = {'/'};
0182
0183 template <class date_type, class char_type>
0184 const typename period_parser<date_type, char_type>::char_type
0185 period_parser<date_type, char_type>::default_period_start_delimeter[2] = {'['};
0186
0187 template <class date_type, class char_type>
0188 const typename period_parser<date_type, char_type>::char_type
0189 period_parser<date_type, char_type>::default_period_open_range_end_delimeter[2] = {')'};
0190
0191 template <class date_type, class char_type>
0192 const typename period_parser<date_type, char_type>::char_type
0193 period_parser<date_type, char_type>::default_period_closed_range_end_delimeter[2] = {']'};
0194
0195 } }
0196
0197 #endif