Chris@16: Chris@16: #ifndef DATE_TIME_SPECIAL_VALUES_PARSER_HPP__ Chris@16: #define DATE_TIME_SPECIAL_VALUES_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@16: * $Date: Chris@16: */ Chris@16: Chris@16: Chris@16: #include "boost/date_time/string_parse_tree.hpp" Chris@16: #include "boost/date_time/special_defs.hpp" Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace date_time { Chris@16: Chris@16: //! Class for special_value parsing Chris@16: /*! Chris@16: * TODO: add doc-comments for which elements can be changed Chris@16: * Parses input stream for strings representing special_values. Chris@16: * Special values parsed are: Chris@16: * - not_a_date_time Chris@16: * - neg_infin Chris@16: * - pod_infin Chris@16: * - min_date_time Chris@16: * - max_date_time Chris@16: */ Chris@16: template Chris@16: class special_values_parser Chris@16: { Chris@16: public: Chris@16: typedef std::basic_string string_type; Chris@16: //typedef std::basic_stringstream stringstream_type; Chris@16: typedef std::istreambuf_iterator stream_itr_type; Chris@16: //typedef typename string_type::const_iterator const_itr; Chris@16: //typedef typename date_type::year_type year_type; Chris@16: //typedef typename date_type::month_type month_type; Chris@16: typedef typename date_type::duration_type duration_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: 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 charT char_type; Chris@16: static const char_type nadt_string[16]; Chris@16: static const char_type neg_inf_string[10]; Chris@16: static const char_type pos_inf_string[10]; Chris@16: static const char_type min_date_time_string[18]; Chris@16: static const char_type max_date_time_string[18]; Chris@16: Chris@16: //! Creates a special_values_parser with the default set of "sv_strings" Chris@16: special_values_parser() Chris@16: { Chris@16: sv_strings(string_type(nadt_string), Chris@16: string_type(neg_inf_string), Chris@16: string_type(pos_inf_string), Chris@16: string_type(min_date_time_string), Chris@16: string_type(max_date_time_string)); Chris@16: } Chris@16: Chris@16: //! Creates a special_values_parser using a user defined set of element strings Chris@16: special_values_parser(const string_type& nadt_str, Chris@16: const string_type& neg_inf_str, Chris@16: const string_type& pos_inf_str, Chris@16: const string_type& min_dt_str, Chris@16: const string_type& max_dt_str) Chris@16: { Chris@16: sv_strings(nadt_str, neg_inf_str, pos_inf_str, min_dt_str, max_dt_str); Chris@16: } Chris@16: Chris@16: special_values_parser(typename collection_type::iterator beg, typename collection_type::iterator end) Chris@16: { Chris@16: collection_type phrases; Chris@16: std::copy(beg, end, std::back_inserter(phrases)); Chris@16: m_sv_strings = parse_tree_type(phrases, static_cast(not_a_date_time)); Chris@16: } Chris@16: Chris@16: special_values_parser(const special_values_parser& svp) Chris@16: { Chris@16: this->m_sv_strings = svp.m_sv_strings; Chris@16: } Chris@16: Chris@16: //! Replace special value strings Chris@16: void sv_strings(const string_type& nadt_str, Chris@16: const string_type& neg_inf_str, Chris@16: const string_type& pos_inf_str, Chris@16: const string_type& min_dt_str, Chris@16: const string_type& max_dt_str) Chris@16: { Chris@16: collection_type phrases; Chris@16: phrases.push_back(nadt_str); Chris@16: phrases.push_back(neg_inf_str); Chris@16: phrases.push_back(pos_inf_str); Chris@16: phrases.push_back(min_dt_str); Chris@16: phrases.push_back(max_dt_str); Chris@16: m_sv_strings = parse_tree_type(phrases, static_cast(not_a_date_time)); Chris@16: } Chris@16: Chris@16: /* Does not return a special_value because if the parsing fails, Chris@16: * the return value will always be not_a_date_time Chris@16: * (mr.current_match retains its default value of -1 on a failed Chris@16: * parse and that casts to not_a_date_time). */ Chris@16: //! Sets match_results.current_match to the corresponding special_value or -1 Chris@16: bool match(stream_itr_type& sitr, Chris@16: stream_itr_type& str_end, Chris@16: match_results& mr) const Chris@16: { Chris@16: unsigned int level = 0; Chris@16: m_sv_strings.match(sitr, str_end, mr, level); Chris@16: return (mr.current_match != match_results::PARSE_ERROR); Chris@16: } Chris@16: /*special_values match(stream_itr_type& sitr, Chris@16: stream_itr_type& str_end, Chris@16: match_results& mr) const Chris@16: { Chris@16: unsigned int level = 0; Chris@16: m_sv_strings.match(sitr, str_end, mr, level); Chris@16: if(mr.current_match == match_results::PARSE_ERROR) { Chris@16: throw std::ios_base::failure("Parse failed. No match found for '" + mr.cache + "'"); Chris@16: } Chris@16: return static_cast(mr.current_match); Chris@16: }*/ Chris@16: Chris@16: Chris@16: private: Chris@16: parse_tree_type m_sv_strings; Chris@16: Chris@16: }; Chris@16: Chris@16: template Chris@16: const typename special_values_parser::char_type Chris@16: special_values_parser::nadt_string[16] = Chris@16: {'n','o','t','-','a','-','d','a','t','e','-','t','i','m','e'}; Chris@16: template Chris@16: const typename special_values_parser::char_type Chris@16: special_values_parser::neg_inf_string[10] = Chris@16: {'-','i','n','f','i','n','i','t','y'}; Chris@16: template Chris@16: const typename special_values_parser::char_type Chris@16: special_values_parser::pos_inf_string[10] = Chris@16: {'+','i','n','f','i','n','i','t','y'}; Chris@16: template Chris@16: const typename special_values_parser::char_type Chris@16: special_values_parser::min_date_time_string[18] = Chris@16: {'m','i','n','i','m','u','m','-','d','a','t','e','-','t','i','m','e'}; Chris@16: template Chris@16: const typename special_values_parser::char_type Chris@16: special_values_parser::max_date_time_string[18] = Chris@16: {'m','a','x','i','m','u','m','-','d','a','t','e','-','t','i','m','e'}; Chris@16: Chris@16: } } //namespace Chris@16: Chris@16: #endif // DATE_TIME_SPECIAL_VALUES_PARSER_HPP__ Chris@16: