| 1 | |
|---|
| 2 | #ifndef DATE_TIME_SPECIAL_VALUES_PARSER_HPP__ |
|---|
| 3 | #define DATE_TIME_SPECIAL_VALUES_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: |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include "boost/date_time/string_parse_tree.hpp" |
|---|
| 15 | #include "boost/date_time/special_defs.hpp" |
|---|
| 16 | #include <string> |
|---|
| 17 | #include <vector> |
|---|
| 18 | |
|---|
| 19 | namespace boost { namespace date_time { |
|---|
| 20 | |
|---|
| 21 | //! Class for special_value parsing |
|---|
| 22 | /*! |
|---|
| 23 | * TODO: add doc-comments for which elements can be changed |
|---|
| 24 | * Parses input stream for strings representing special_values. |
|---|
| 25 | * Special values parsed are: |
|---|
| 26 | * - not_a_date_time |
|---|
| 27 | * - neg_infin |
|---|
| 28 | * - pod_infin |
|---|
| 29 | * - min_date_time |
|---|
| 30 | * - max_date_time |
|---|
| 31 | */ |
|---|
| 32 | template<class date_type, typename charT> |
|---|
| 33 | class special_values_parser |
|---|
| 34 | { |
|---|
| 35 | public: |
|---|
| 36 | typedef std::basic_string<charT> string_type; |
|---|
| 37 | //typedef std::basic_stringstream<charT> stringstream_type; |
|---|
| 38 | typedef std::istreambuf_iterator<charT> stream_itr_type; |
|---|
| 39 | //typedef typename string_type::const_iterator const_itr; |
|---|
| 40 | //typedef typename date_type::year_type year_type; |
|---|
| 41 | //typedef typename date_type::month_type month_type; |
|---|
| 42 | typedef typename date_type::duration_type duration_type; |
|---|
| 43 | //typedef typename date_type::day_of_week_type day_of_week_type; |
|---|
| 44 | //typedef typename date_type::day_type day_type; |
|---|
| 45 | typedef string_parse_tree<charT> parse_tree_type; |
|---|
| 46 | typedef typename parse_tree_type::parse_match_result_type match_results; |
|---|
| 47 | typedef std::vector<std::basic_string<charT> > collection_type; |
|---|
| 48 | |
|---|
| 49 | typedef charT char_type; |
|---|
| 50 | static const char_type nadt_string[16]; |
|---|
| 51 | static const char_type neg_inf_string[10]; |
|---|
| 52 | static const char_type pos_inf_string[10]; |
|---|
| 53 | static const char_type min_date_time_string[18]; |
|---|
| 54 | static const char_type max_date_time_string[18]; |
|---|
| 55 | |
|---|
| 56 | //! Creates a special_values_parser with the default set of "sv_strings" |
|---|
| 57 | special_values_parser() |
|---|
| 58 | { |
|---|
| 59 | sv_strings(string_type(nadt_string), |
|---|
| 60 | string_type(neg_inf_string), |
|---|
| 61 | string_type(pos_inf_string), |
|---|
| 62 | string_type(min_date_time_string), |
|---|
| 63 | string_type(max_date_time_string)); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | //! Creates a special_values_parser using a user defined set of element strings |
|---|
| 67 | special_values_parser(const string_type& nadt_str, |
|---|
| 68 | const string_type& neg_inf_str, |
|---|
| 69 | const string_type& pos_inf_str, |
|---|
| 70 | const string_type& min_dt_str, |
|---|
| 71 | const string_type& max_dt_str) |
|---|
| 72 | { |
|---|
| 73 | sv_strings(nadt_str, neg_inf_str, pos_inf_str, min_dt_str, max_dt_str); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | special_values_parser(typename collection_type::iterator beg, typename collection_type::iterator end) |
|---|
| 77 | { |
|---|
| 78 | collection_type phrases; |
|---|
| 79 | std::copy(beg, end, std::back_inserter(phrases)); |
|---|
| 80 | m_sv_strings = parse_tree_type(phrases, static_cast<int>(not_a_date_time)); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | special_values_parser(const special_values_parser<date_type,charT>& svp) |
|---|
| 84 | { |
|---|
| 85 | this->m_sv_strings = svp.m_sv_strings; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | //! Replace special value strings |
|---|
| 89 | void sv_strings(const string_type& nadt_str, |
|---|
| 90 | const string_type& neg_inf_str, |
|---|
| 91 | const string_type& pos_inf_str, |
|---|
| 92 | const string_type& min_dt_str, |
|---|
| 93 | const string_type& max_dt_str) |
|---|
| 94 | { |
|---|
| 95 | collection_type phrases; |
|---|
| 96 | phrases.push_back(nadt_str); |
|---|
| 97 | phrases.push_back(neg_inf_str); |
|---|
| 98 | phrases.push_back(pos_inf_str); |
|---|
| 99 | phrases.push_back(min_dt_str); |
|---|
| 100 | phrases.push_back(max_dt_str); |
|---|
| 101 | m_sv_strings = parse_tree_type(phrases, static_cast<int>(not_a_date_time)); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | /* Does not return a special_value because if the parsing fails, |
|---|
| 105 | * the return value will always be not_a_date_time |
|---|
| 106 | * (mr.current_match retains its default value of -1 on a failed |
|---|
| 107 | * parse and that casts to not_a_date_time). */ |
|---|
| 108 | //! Sets match_results.current_match to the corresponding special_value or -1 |
|---|
| 109 | bool match(stream_itr_type& sitr, |
|---|
| 110 | stream_itr_type& str_end, |
|---|
| 111 | match_results& mr) const |
|---|
| 112 | { |
|---|
| 113 | unsigned int level = 0; |
|---|
| 114 | m_sv_strings.match(sitr, str_end, mr, level); |
|---|
| 115 | return (mr.current_match != match_results::PARSE_ERROR); |
|---|
| 116 | } |
|---|
| 117 | /*special_values match(stream_itr_type& sitr, |
|---|
| 118 | stream_itr_type& str_end, |
|---|
| 119 | match_results& mr) const |
|---|
| 120 | { |
|---|
| 121 | unsigned int level = 0; |
|---|
| 122 | m_sv_strings.match(sitr, str_end, mr, level); |
|---|
| 123 | if(mr.current_match == match_results::PARSE_ERROR) { |
|---|
| 124 | throw std::ios_base::failure("Parse failed. No match found for '" + mr.cache + "'"); |
|---|
| 125 | } |
|---|
| 126 | return static_cast<special_values>(mr.current_match); |
|---|
| 127 | }*/ |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | private: |
|---|
| 131 | parse_tree_type m_sv_strings; |
|---|
| 132 | |
|---|
| 133 | }; |
|---|
| 134 | |
|---|
| 135 | template<class date_type, class CharT> |
|---|
| 136 | const typename special_values_parser<date_type, CharT>::char_type |
|---|
| 137 | special_values_parser<date_type, CharT>::nadt_string[16] = |
|---|
| 138 | {'n','o','t','-','a','-','d','a','t','e','-','t','i','m','e'}; |
|---|
| 139 | template<class date_type, class CharT> |
|---|
| 140 | const typename special_values_parser<date_type, CharT>::char_type |
|---|
| 141 | special_values_parser<date_type, CharT>::neg_inf_string[10] = |
|---|
| 142 | {'-','i','n','f','i','n','i','t','y'}; |
|---|
| 143 | template<class date_type, class CharT> |
|---|
| 144 | const typename special_values_parser<date_type, CharT>::char_type |
|---|
| 145 | special_values_parser<date_type, CharT>::pos_inf_string[10] = |
|---|
| 146 | {'+','i','n','f','i','n','i','t','y'}; |
|---|
| 147 | template<class date_type, class CharT> |
|---|
| 148 | const typename special_values_parser<date_type, CharT>::char_type |
|---|
| 149 | special_values_parser<date_type, CharT>::min_date_time_string[18] = |
|---|
| 150 | {'m','i','n','i','m','u','m','-','d','a','t','e','-','t','i','m','e'}; |
|---|
| 151 | template<class date_type, class CharT> |
|---|
| 152 | const typename special_values_parser<date_type, CharT>::char_type |
|---|
| 153 | special_values_parser<date_type, CharT>::max_date_time_string[18] = |
|---|
| 154 | {'m','a','x','i','m','u','m','-','d','a','t','e','-','t','i','m','e'}; |
|---|
| 155 | |
|---|
| 156 | } } //namespace |
|---|
| 157 | |
|---|
| 158 | #endif // DATE_TIME_SPECIAL_VALUES_PARSER_HPP__ |
|---|
| 159 | |
|---|