special_values_parser.hpp 6.1 KB

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