[857] | 1 |
|
---|
| 2 | #ifndef DATE_TIME_DATE_GENERATOR_PARSER_HPP__
|
---|
| 3 | #define DATE_TIME_DATE_GENERATOR_PARSER_HPP__
|
---|
| 4 |
|
---|
| 5 | /* Copyright (c) 2005 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 or http://www.boost.org/LICENSE-1.0)
|
---|
| 9 | * Author: Jeff Garland, Bart Garst
|
---|
| 10 | * $Date: 2005/09/07 09:15:03 $
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 | #include "boost/date_time/string_parse_tree.hpp"
|
---|
| 15 | #include "boost/date_time/date_generators.hpp"
|
---|
| 16 | #include "boost/date_time/format_date_parser.hpp"
|
---|
| 17 | #include <string>
|
---|
| 18 | #include <vector>
|
---|
| 19 |
|
---|
| 20 | namespace boost { namespace date_time {
|
---|
| 21 |
|
---|
| 22 | //! Class for date_generator parsing
|
---|
| 23 | /*! The elements of a date_generator "phrase" are parsed from the input stream in a
|
---|
| 24 | * particular order. All elements are required and the order in which they appear
|
---|
| 25 | * cannot change, however, the elements themselves can be changed. The default
|
---|
| 26 | * elements and their order are as follows:
|
---|
| 27 | *
|
---|
| 28 | * - partial_date => "dd Month"
|
---|
| 29 | * - nth_day_of_the_week_in_month => "nth weekday of month"
|
---|
| 30 | * - first_day_of_the_week_in_month => "first weekday of month"
|
---|
| 31 | * - last_day_of_the_week_in_month => "last weekday of month"
|
---|
| 32 | * - first_day_of_the_week_after => "weekday after"
|
---|
| 33 | * - first_day_of_the_week_before => "weekday before"
|
---|
| 34 | *
|
---|
| 35 | * Weekday and Month names and formats are handled via the date_input_facet.
|
---|
| 36 | *
|
---|
| 37 | */
|
---|
| 38 | template<class date_type, typename charT>
|
---|
| 39 | class date_generator_parser
|
---|
| 40 | {
|
---|
| 41 | public:
|
---|
| 42 | typedef std::basic_string<charT> string_type;
|
---|
| 43 | typedef std::istreambuf_iterator<charT> stream_itr_type;
|
---|
| 44 |
|
---|
| 45 | typedef typename date_type::month_type month_type;
|
---|
| 46 | typedef typename date_type::day_of_week_type day_of_week_type;
|
---|
| 47 | typedef typename date_type::day_type day_type;
|
---|
| 48 |
|
---|
| 49 | typedef string_parse_tree<charT> parse_tree_type;
|
---|
| 50 | typedef typename parse_tree_type::parse_match_result_type match_results;
|
---|
| 51 | typedef std::vector<std::basic_string<charT> > collection_type;
|
---|
| 52 |
|
---|
| 53 | typedef partial_date<date_type> partial_date_type;
|
---|
| 54 | typedef nth_kday_of_month<date_type> nth_kday_type;
|
---|
| 55 | typedef first_kday_of_month<date_type> first_kday_type;
|
---|
| 56 | typedef last_kday_of_month<date_type> last_kday_type;
|
---|
| 57 | typedef first_kday_after<date_type> kday_after_type;
|
---|
| 58 | typedef first_kday_before<date_type> kday_before_type;
|
---|
| 59 |
|
---|
| 60 | typedef charT char_type;
|
---|
| 61 | static const char_type first_string[6];
|
---|
| 62 | static const char_type second_string[7];
|
---|
| 63 | static const char_type third_string[6];
|
---|
| 64 | static const char_type fourth_string[7];
|
---|
| 65 | static const char_type fifth_string[6];
|
---|
| 66 | static const char_type last_string[5];
|
---|
| 67 | static const char_type before_string[8];
|
---|
| 68 | static const char_type after_string[6];
|
---|
| 69 | static const char_type of_string[3];
|
---|
| 70 |
|
---|
| 71 | enum phrase_elements {first=0, second, third, fourth, fifth, last,
|
---|
| 72 | before, after, of, number_of_phrase_elements};
|
---|
| 73 |
|
---|
| 74 | //! Creates a date_generator_parser with the default set of "element_strings"
|
---|
| 75 | date_generator_parser()
|
---|
| 76 | {
|
---|
| 77 | element_strings(string_type(first_string),
|
---|
| 78 | string_type(second_string),
|
---|
| 79 | string_type(third_string),
|
---|
| 80 | string_type(fourth_string),
|
---|
| 81 | string_type(fifth_string),
|
---|
| 82 | string_type(last_string),
|
---|
| 83 | string_type(before_string),
|
---|
| 84 | string_type(after_string),
|
---|
| 85 | string_type(of_string));
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | //! Creates a date_generator_parser using a user defined set of element strings
|
---|
| 89 | date_generator_parser(const string_type& first_str,
|
---|
| 90 | const string_type& second_str,
|
---|
| 91 | const string_type& third_str,
|
---|
| 92 | const string_type& fourth_str,
|
---|
| 93 | const string_type& fifth_str,
|
---|
| 94 | const string_type& last_str,
|
---|
| 95 | const string_type& before_str,
|
---|
| 96 | const string_type& after_str,
|
---|
| 97 | const string_type& of_str)
|
---|
| 98 | {
|
---|
| 99 | element_strings(first_str, second_str, third_str, fourth_str, fifth_str,
|
---|
| 100 | last_str, before_str, after_str, of_str);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | //! Replace strings that determine nth week for generator
|
---|
| 104 | void element_strings(const string_type& first_str,
|
---|
| 105 | const string_type& second_str,
|
---|
| 106 | const string_type& third_str,
|
---|
| 107 | const string_type& fourth_str,
|
---|
| 108 | const string_type& fifth_str,
|
---|
| 109 | const string_type& last_str,
|
---|
| 110 | const string_type& before_str,
|
---|
| 111 | const string_type& after_str,
|
---|
| 112 | const string_type& of_str)
|
---|
| 113 | {
|
---|
| 114 | collection_type phrases;
|
---|
| 115 | phrases.push_back(first_str);
|
---|
| 116 | phrases.push_back(second_str);
|
---|
| 117 | phrases.push_back(third_str);
|
---|
| 118 | phrases.push_back(fourth_str);
|
---|
| 119 | phrases.push_back(fifth_str);
|
---|
| 120 | phrases.push_back(last_str);
|
---|
| 121 | phrases.push_back(before_str);
|
---|
| 122 | phrases.push_back(after_str);
|
---|
| 123 | phrases.push_back(of_str);
|
---|
| 124 | m_element_strings = parse_tree_type(phrases, this->first); // enum first
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void element_strings(const collection_type& col)
|
---|
| 128 | {
|
---|
| 129 | m_element_strings = parse_tree_type(col, this->first); // enum first
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | //! returns partial_date parsed from stream
|
---|
| 134 | template<class facet_type>
|
---|
| 135 | partial_date_type
|
---|
| 136 | get_partial_date_type(stream_itr_type& sitr,
|
---|
| 137 | stream_itr_type& stream_end,
|
---|
| 138 | std::ios_base& a_ios,
|
---|
| 139 | const facet_type& facet) const
|
---|
| 140 | {
|
---|
| 141 | // skip leading whitespace
|
---|
| 142 | while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
---|
| 143 |
|
---|
| 144 | day_type d(1);
|
---|
| 145 | month_type m(1);
|
---|
| 146 | facet.get(sitr, stream_end, a_ios, d);
|
---|
| 147 | facet.get(sitr, stream_end, a_ios, m);
|
---|
| 148 |
|
---|
| 149 | return partial_date_type(d,m);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | //! returns nth_kday_of_week parsed from stream
|
---|
| 153 | template<class facet_type>
|
---|
| 154 | nth_kday_type
|
---|
| 155 | get_nth_kday_type(stream_itr_type& sitr,
|
---|
| 156 | stream_itr_type& stream_end,
|
---|
| 157 | std::ios_base& a_ios,
|
---|
| 158 | const facet_type& facet) const
|
---|
| 159 | {
|
---|
| 160 | // skip leading whitespace
|
---|
| 161 | while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
---|
| 162 |
|
---|
| 163 | typename nth_kday_type::week_num wn;
|
---|
| 164 | day_of_week_type wd(0); // no default constructor
|
---|
| 165 | month_type m(1); // no default constructor
|
---|
| 166 |
|
---|
| 167 | match_results mr = m_element_strings.match(sitr, stream_end);
|
---|
| 168 | switch(mr.current_match) {
|
---|
| 169 | case first : { wn = nth_kday_type::first; break; }
|
---|
| 170 | case second : { wn = nth_kday_type::second; break; }
|
---|
| 171 | case third : { wn = nth_kday_type::third; break; }
|
---|
| 172 | case fourth : { wn = nth_kday_type::fourth; break; }
|
---|
| 173 | case fifth : { wn = nth_kday_type::fifth; break; }
|
---|
| 174 | default:
|
---|
| 175 | {
|
---|
| 176 | throw std::ios_base::failure("Parse failed. No match found for '" + mr.cache + "'");
|
---|
| 177 | break;
|
---|
| 178 | }
|
---|
| 179 | } // week num
|
---|
| 180 | facet.get(sitr, stream_end, a_ios, wd); // day_of_week
|
---|
| 181 | extract_element(sitr, stream_end, of); // "of" element
|
---|
| 182 | facet.get(sitr, stream_end, a_ios, m); // month
|
---|
| 183 |
|
---|
| 184 | return nth_kday_type(wn, wd, m);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | //! returns first_kday_of_week parsed from stream
|
---|
| 188 | template<class facet_type>
|
---|
| 189 | first_kday_type
|
---|
| 190 | get_first_kday_type(stream_itr_type& sitr,
|
---|
| 191 | stream_itr_type& stream_end,
|
---|
| 192 | std::ios_base& a_ios,
|
---|
| 193 | const facet_type& facet) const
|
---|
| 194 | {
|
---|
| 195 | // skip leading whitespace
|
---|
| 196 | while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
---|
| 197 |
|
---|
| 198 | day_of_week_type wd(0); // no default constructor
|
---|
| 199 | month_type m(1); // no default constructor
|
---|
| 200 |
|
---|
| 201 | extract_element(sitr, stream_end, first); // "first" element
|
---|
| 202 | facet.get(sitr, stream_end, a_ios, wd); // day_of_week
|
---|
| 203 | extract_element(sitr, stream_end, of); // "of" element
|
---|
| 204 | facet.get(sitr, stream_end, a_ios, m); // month
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 | return first_kday_type(wd, m);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | //! returns last_kday_of_week parsed from stream
|
---|
| 211 | template<class facet_type>
|
---|
| 212 | last_kday_type
|
---|
| 213 | get_last_kday_type(stream_itr_type& sitr,
|
---|
| 214 | stream_itr_type& stream_end,
|
---|
| 215 | std::ios_base& a_ios,
|
---|
| 216 | const facet_type& facet) const
|
---|
| 217 | {
|
---|
| 218 | // skip leading whitespace
|
---|
| 219 | while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
---|
| 220 |
|
---|
| 221 | day_of_week_type wd(0); // no default constructor
|
---|
| 222 | month_type m(1); // no default constructor
|
---|
| 223 |
|
---|
| 224 | extract_element(sitr, stream_end, last); // "last" element
|
---|
| 225 | facet.get(sitr, stream_end, a_ios, wd); // day_of_week
|
---|
| 226 | extract_element(sitr, stream_end, of); // "of" element
|
---|
| 227 | facet.get(sitr, stream_end, a_ios, m); // month
|
---|
| 228 |
|
---|
| 229 |
|
---|
| 230 | return last_kday_type(wd, m);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | //! returns first_kday_of_week parsed from stream
|
---|
| 234 | template<class facet_type>
|
---|
| 235 | kday_before_type
|
---|
| 236 | get_kday_before_type(stream_itr_type& sitr,
|
---|
| 237 | stream_itr_type& stream_end,
|
---|
| 238 | std::ios_base& a_ios,
|
---|
| 239 | const facet_type& facet) const
|
---|
| 240 | {
|
---|
| 241 | // skip leading whitespace
|
---|
| 242 | while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
---|
| 243 |
|
---|
| 244 | day_of_week_type wd(0); // no default constructor
|
---|
| 245 |
|
---|
| 246 | facet.get(sitr, stream_end, a_ios, wd); // day_of_week
|
---|
| 247 | extract_element(sitr, stream_end, before);// "before" element
|
---|
| 248 |
|
---|
| 249 | return kday_before_type(wd);
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | //! returns first_kday_of_week parsed from stream
|
---|
| 253 | template<class facet_type>
|
---|
| 254 | kday_after_type
|
---|
| 255 | get_kday_after_type(stream_itr_type& sitr,
|
---|
| 256 | stream_itr_type& stream_end,
|
---|
| 257 | std::ios_base& a_ios,
|
---|
| 258 | const facet_type& facet) const
|
---|
| 259 | {
|
---|
| 260 | // skip leading whitespace
|
---|
| 261 | while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
---|
| 262 |
|
---|
| 263 | day_of_week_type wd(0); // no default constructor
|
---|
| 264 |
|
---|
| 265 | facet.get(sitr, stream_end, a_ios, wd); // day_of_week
|
---|
| 266 | extract_element(sitr, stream_end, after); // "after" element
|
---|
| 267 |
|
---|
| 268 | return kday_after_type(wd);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | private:
|
---|
| 272 | parse_tree_type m_element_strings;
|
---|
| 273 |
|
---|
| 274 | //! Extracts phrase element from input. Throws ios_base::failure on error.
|
---|
| 275 | void extract_element(stream_itr_type& sitr,
|
---|
| 276 | stream_itr_type& stream_end,
|
---|
| 277 | typename date_generator_parser::phrase_elements ele) const
|
---|
| 278 | {
|
---|
| 279 | // skip leading whitespace
|
---|
| 280 | while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
---|
| 281 | match_results mr = m_element_strings.match(sitr, stream_end);
|
---|
| 282 | if(mr.current_match != ele) {
|
---|
| 283 | throw std::ios_base::failure("Parse failed. No match found for '" + mr.cache + "'");
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | };
|
---|
| 288 |
|
---|
| 289 | template<class date_type, class CharT>
|
---|
| 290 | const typename date_generator_parser<date_type, CharT>::char_type
|
---|
| 291 | date_generator_parser<date_type, CharT>::first_string[6] =
|
---|
| 292 | {'f','i','r','s','t'};
|
---|
| 293 | template<class date_type, class CharT>
|
---|
| 294 | const typename date_generator_parser<date_type, CharT>::char_type
|
---|
| 295 | date_generator_parser<date_type, CharT>::second_string[7] =
|
---|
| 296 | {'s','e','c','o','n','d'};
|
---|
| 297 | template<class date_type, class CharT>
|
---|
| 298 | const typename date_generator_parser<date_type, CharT>::char_type
|
---|
| 299 | date_generator_parser<date_type, CharT>::third_string[6] =
|
---|
| 300 | {'t','h','i','r','d'};
|
---|
| 301 | template<class date_type, class CharT>
|
---|
| 302 | const typename date_generator_parser<date_type, CharT>::char_type
|
---|
| 303 | date_generator_parser<date_type, CharT>::fourth_string[7] =
|
---|
| 304 | {'f','o','u','r','t','h'};
|
---|
| 305 | template<class date_type, class CharT>
|
---|
| 306 | const typename date_generator_parser<date_type, CharT>::char_type
|
---|
| 307 | date_generator_parser<date_type, CharT>::fifth_string[6] =
|
---|
| 308 | {'f','i','f','t','h'};
|
---|
| 309 | template<class date_type, class CharT>
|
---|
| 310 | const typename date_generator_parser<date_type, CharT>::char_type
|
---|
| 311 | date_generator_parser<date_type, CharT>::last_string[5] =
|
---|
| 312 | {'l','a','s','t'};
|
---|
| 313 | template<class date_type, class CharT>
|
---|
| 314 | const typename date_generator_parser<date_type, CharT>::char_type
|
---|
| 315 | date_generator_parser<date_type, CharT>::before_string[8] =
|
---|
| 316 | {'b','e','f','o','r','e'};
|
---|
| 317 | template<class date_type, class CharT>
|
---|
| 318 | const typename date_generator_parser<date_type, CharT>::char_type
|
---|
| 319 | date_generator_parser<date_type, CharT>::after_string[6] =
|
---|
| 320 | {'a','f','t','e','r'};
|
---|
| 321 | template<class date_type, class CharT>
|
---|
| 322 | const typename date_generator_parser<date_type, CharT>::char_type
|
---|
| 323 | date_generator_parser<date_type, CharT>::of_string[3] =
|
---|
| 324 | {'o','f'};
|
---|
| 325 |
|
---|
| 326 | } } //namespace
|
---|
| 327 |
|
---|
| 328 | #endif // DATE_TIME_DATE_GENERATOR_PARSER_HPP__
|
---|
| 329 |
|
---|