Chris@16: Chris@16: #ifndef DATE_TIME_DATE_GENERATOR_PARSER_HPP__ Chris@16: #define DATE_TIME_DATE_GENERATOR_PARSER_HPP__ Chris@16: Chris@16: /* Copyright (c) 2005 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include // istreambuf_iterator Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace date_time { Chris@16: Chris@16: //! Class for date_generator parsing Chris@16: /*! The elements of a date_generator "phrase" are parsed from the input stream in a Chris@16: * particular order. All elements are required and the order in which they appear Chris@16: * cannot change, however, the elements themselves can be changed. The default Chris@16: * elements and their order are as follows: Chris@16: * Chris@16: * - partial_date => "dd Month" Chris@16: * - nth_day_of_the_week_in_month => "nth weekday of month" Chris@16: * - first_day_of_the_week_in_month => "first weekday of month" Chris@16: * - last_day_of_the_week_in_month => "last weekday of month" Chris@16: * - first_day_of_the_week_after => "weekday after" Chris@16: * - first_day_of_the_week_before => "weekday before" Chris@16: * Chris@16: * Weekday and Month names and formats are handled via the date_input_facet. Chris@16: * Chris@16: */ Chris@16: template Chris@16: class date_generator_parser Chris@16: { Chris@16: public: Chris@16: typedef std::basic_string string_type; Chris@16: typedef std::istreambuf_iterator stream_itr_type; Chris@16: Chris@16: typedef typename date_type::month_type month_type; Chris@16: typedef typename date_type::day_of_week_type day_of_week_type; Chris@16: typedef typename date_type::day_type day_type; Chris@16: Chris@16: typedef string_parse_tree parse_tree_type; Chris@16: typedef typename parse_tree_type::parse_match_result_type match_results; Chris@16: typedef std::vector > collection_type; Chris@16: Chris@16: typedef partial_date partial_date_type; Chris@16: typedef nth_kday_of_month nth_kday_type; Chris@16: typedef first_kday_of_month first_kday_type; Chris@16: typedef last_kday_of_month last_kday_type; Chris@16: typedef first_kday_after kday_after_type; Chris@16: typedef first_kday_before kday_before_type; Chris@16: Chris@16: typedef charT char_type; Chris@16: static const char_type first_string[6]; Chris@16: static const char_type second_string[7]; Chris@16: static const char_type third_string[6]; Chris@16: static const char_type fourth_string[7]; Chris@16: static const char_type fifth_string[6]; Chris@16: static const char_type last_string[5]; Chris@16: static const char_type before_string[8]; Chris@16: static const char_type after_string[6]; Chris@16: static const char_type of_string[3]; Chris@16: Chris@16: enum phrase_elements {first=0, second, third, fourth, fifth, last, Chris@16: before, after, of, number_of_phrase_elements}; Chris@16: Chris@16: //! Creates a date_generator_parser with the default set of "element_strings" Chris@16: date_generator_parser() Chris@16: { Chris@16: element_strings(string_type(first_string), Chris@16: string_type(second_string), Chris@16: string_type(third_string), Chris@16: string_type(fourth_string), Chris@16: string_type(fifth_string), Chris@16: string_type(last_string), Chris@16: string_type(before_string), Chris@16: string_type(after_string), Chris@16: string_type(of_string)); Chris@16: } Chris@16: Chris@16: //! Creates a date_generator_parser using a user defined set of element strings Chris@16: date_generator_parser(const string_type& first_str, Chris@16: const string_type& second_str, Chris@16: const string_type& third_str, Chris@16: const string_type& fourth_str, Chris@16: const string_type& fifth_str, Chris@16: const string_type& last_str, Chris@16: const string_type& before_str, Chris@16: const string_type& after_str, Chris@16: const string_type& of_str) Chris@16: { Chris@16: element_strings(first_str, second_str, third_str, fourth_str, fifth_str, Chris@16: last_str, before_str, after_str, of_str); Chris@16: } Chris@16: Chris@16: //! Replace strings that determine nth week for generator Chris@16: void element_strings(const string_type& first_str, Chris@16: const string_type& second_str, Chris@16: const string_type& third_str, Chris@16: const string_type& fourth_str, Chris@16: const string_type& fifth_str, Chris@16: const string_type& last_str, Chris@16: const string_type& before_str, Chris@16: const string_type& after_str, Chris@16: const string_type& of_str) Chris@16: { Chris@16: collection_type phrases; Chris@16: phrases.push_back(first_str); Chris@16: phrases.push_back(second_str); Chris@16: phrases.push_back(third_str); Chris@16: phrases.push_back(fourth_str); Chris@16: phrases.push_back(fifth_str); Chris@16: phrases.push_back(last_str); Chris@16: phrases.push_back(before_str); Chris@16: phrases.push_back(after_str); Chris@16: phrases.push_back(of_str); Chris@16: m_element_strings = parse_tree_type(phrases, this->first); // enum first Chris@16: } Chris@16: Chris@16: void element_strings(const collection_type& col) Chris@16: { Chris@16: m_element_strings = parse_tree_type(col, this->first); // enum first Chris@16: } Chris@16: Chris@16: //! returns partial_date parsed from stream Chris@16: template Chris@16: partial_date_type Chris@16: get_partial_date_type(stream_itr_type& sitr, Chris@16: stream_itr_type& stream_end, Chris@16: std::ios_base& a_ios, Chris@16: const facet_type& facet) const Chris@16: { Chris@16: // skip leading whitespace Chris@16: while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; } Chris@16: Chris@16: day_type d(1); Chris@16: month_type m(1); Chris@16: facet.get(sitr, stream_end, a_ios, d); Chris@16: facet.get(sitr, stream_end, a_ios, m); Chris@16: Chris@16: return partial_date_type(d,m); Chris@16: } Chris@16: Chris@16: //! returns nth_kday_of_week parsed from stream Chris@16: template Chris@16: nth_kday_type Chris@16: get_nth_kday_type(stream_itr_type& sitr, Chris@16: stream_itr_type& stream_end, Chris@16: std::ios_base& a_ios, Chris@16: const facet_type& facet) const Chris@16: { Chris@16: // skip leading whitespace Chris@16: while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; } Chris@16: Chris@16: typename nth_kday_type::week_num wn; Chris@16: day_of_week_type wd(0); // no default constructor Chris@16: month_type m(1); // no default constructor Chris@16: Chris@16: match_results mr = m_element_strings.match(sitr, stream_end); Chris@16: switch(mr.current_match) { Chris@16: case first : { wn = nth_kday_type::first; break; } Chris@16: case second : { wn = nth_kday_type::second; break; } Chris@16: case third : { wn = nth_kday_type::third; break; } Chris@16: case fourth : { wn = nth_kday_type::fourth; break; } Chris@16: case fifth : { wn = nth_kday_type::fifth; break; } Chris@16: default: Chris@16: { Chris@16: boost::throw_exception(std::ios_base::failure("Parse failed. No match found for '" + mr.cache + "'")); Chris@16: BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(wn = nth_kday_type::first); Chris@16: } Chris@16: } // week num Chris@16: facet.get(sitr, stream_end, a_ios, wd); // day_of_week Chris@16: extract_element(sitr, stream_end, of); // "of" element Chris@16: facet.get(sitr, stream_end, a_ios, m); // month Chris@16: Chris@16: return nth_kday_type(wn, wd, m); Chris@16: } Chris@16: Chris@16: //! returns first_kday_of_week parsed from stream Chris@16: template Chris@16: first_kday_type Chris@16: get_first_kday_type(stream_itr_type& sitr, Chris@16: stream_itr_type& stream_end, Chris@16: std::ios_base& a_ios, Chris@16: const facet_type& facet) const Chris@16: { Chris@16: // skip leading whitespace Chris@16: while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; } Chris@16: Chris@16: day_of_week_type wd(0); // no default constructor Chris@16: month_type m(1); // no default constructor Chris@16: Chris@16: extract_element(sitr, stream_end, first); // "first" element Chris@16: facet.get(sitr, stream_end, a_ios, wd); // day_of_week Chris@16: extract_element(sitr, stream_end, of); // "of" element Chris@16: facet.get(sitr, stream_end, a_ios, m); // month Chris@16: Chris@16: Chris@16: return first_kday_type(wd, m); Chris@16: } Chris@16: Chris@16: //! returns last_kday_of_week parsed from stream Chris@16: template Chris@16: last_kday_type Chris@16: get_last_kday_type(stream_itr_type& sitr, Chris@16: stream_itr_type& stream_end, Chris@16: std::ios_base& a_ios, Chris@16: const facet_type& facet) const Chris@16: { Chris@16: // skip leading whitespace Chris@16: while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; } Chris@16: Chris@16: day_of_week_type wd(0); // no default constructor Chris@16: month_type m(1); // no default constructor Chris@16: Chris@16: extract_element(sitr, stream_end, last); // "last" element Chris@16: facet.get(sitr, stream_end, a_ios, wd); // day_of_week Chris@16: extract_element(sitr, stream_end, of); // "of" element Chris@16: facet.get(sitr, stream_end, a_ios, m); // month Chris@16: Chris@16: Chris@16: return last_kday_type(wd, m); Chris@16: } Chris@16: Chris@16: //! returns first_kday_of_week parsed from stream Chris@16: template Chris@16: kday_before_type Chris@16: get_kday_before_type(stream_itr_type& sitr, Chris@16: stream_itr_type& stream_end, Chris@16: std::ios_base& a_ios, Chris@16: const facet_type& facet) const Chris@16: { Chris@16: // skip leading whitespace Chris@16: while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; } Chris@16: Chris@16: day_of_week_type wd(0); // no default constructor Chris@16: Chris@16: facet.get(sitr, stream_end, a_ios, wd); // day_of_week Chris@16: extract_element(sitr, stream_end, before);// "before" element Chris@16: Chris@16: return kday_before_type(wd); Chris@16: } Chris@16: Chris@16: //! returns first_kday_of_week parsed from stream Chris@16: template Chris@16: kday_after_type Chris@16: get_kday_after_type(stream_itr_type& sitr, Chris@16: stream_itr_type& stream_end, Chris@16: std::ios_base& a_ios, Chris@16: const facet_type& facet) const Chris@16: { Chris@16: // skip leading whitespace Chris@16: while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; } Chris@16: Chris@16: day_of_week_type wd(0); // no default constructor Chris@16: Chris@16: facet.get(sitr, stream_end, a_ios, wd); // day_of_week Chris@16: extract_element(sitr, stream_end, after); // "after" element Chris@16: Chris@16: return kday_after_type(wd); Chris@16: } Chris@16: Chris@16: private: Chris@16: parse_tree_type m_element_strings; Chris@16: Chris@16: //! Extracts phrase element from input. Throws ios_base::failure on error. Chris@16: void extract_element(stream_itr_type& sitr, Chris@16: stream_itr_type& stream_end, Chris@16: typename date_generator_parser::phrase_elements ele) const Chris@16: { Chris@16: // skip leading whitespace Chris@16: while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; } Chris@16: match_results mr = m_element_strings.match(sitr, stream_end); Chris@16: if(mr.current_match != ele) { Chris@16: boost::throw_exception(std::ios_base::failure("Parse failed. No match found for '" + mr.cache + "'")); Chris@16: } Chris@16: } Chris@16: Chris@16: }; Chris@16: Chris@16: template Chris@16: const typename date_generator_parser::char_type Chris@16: date_generator_parser::first_string[6] = Chris@16: {'f','i','r','s','t'}; Chris@16: template Chris@16: const typename date_generator_parser::char_type Chris@16: date_generator_parser::second_string[7] = Chris@16: {'s','e','c','o','n','d'}; Chris@16: template Chris@16: const typename date_generator_parser::char_type Chris@16: date_generator_parser::third_string[6] = Chris@16: {'t','h','i','r','d'}; Chris@16: template Chris@16: const typename date_generator_parser::char_type Chris@16: date_generator_parser::fourth_string[7] = Chris@16: {'f','o','u','r','t','h'}; Chris@16: template Chris@16: const typename date_generator_parser::char_type Chris@16: date_generator_parser::fifth_string[6] = Chris@16: {'f','i','f','t','h'}; Chris@16: template Chris@16: const typename date_generator_parser::char_type Chris@16: date_generator_parser::last_string[5] = Chris@16: {'l','a','s','t'}; Chris@16: template Chris@16: const typename date_generator_parser::char_type Chris@16: date_generator_parser::before_string[8] = Chris@16: {'b','e','f','o','r','e'}; Chris@16: template Chris@16: const typename date_generator_parser::char_type Chris@16: date_generator_parser::after_string[6] = Chris@16: {'a','f','t','e','r'}; Chris@16: template Chris@16: const typename date_generator_parser::char_type Chris@16: date_generator_parser::of_string[3] = Chris@16: {'o','f'}; Chris@16: Chris@16: } } //namespace Chris@16: Chris@16: #endif // DATE_TIME_DATE_GENERATOR_PARSER_HPP__ Chris@16: