special_values_parser.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 date_type::duration_type duration_type;
  34. typedef string_parse_tree<charT> parse_tree_type;
  35. typedef typename parse_tree_type::parse_match_result_type match_results;
  36. typedef std::vector<std::basic_string<charT> > collection_type;
  37. typedef charT char_type;
  38. static const char_type nadt_string[16];
  39. static const char_type neg_inf_string[10];
  40. static const char_type pos_inf_string[10];
  41. static const char_type min_date_time_string[18];
  42. static const char_type max_date_time_string[18];
  43. //! Creates a special_values_parser with the default set of "sv_strings"
  44. special_values_parser()
  45. {
  46. sv_strings(string_type(nadt_string),
  47. string_type(neg_inf_string),
  48. string_type(pos_inf_string),
  49. string_type(min_date_time_string),
  50. string_type(max_date_time_string));
  51. }
  52. //! Creates a special_values_parser using a user defined set of element strings
  53. special_values_parser(const string_type& nadt_str,
  54. const string_type& neg_inf_str,
  55. const string_type& pos_inf_str,
  56. const string_type& min_dt_str,
  57. const string_type& max_dt_str)
  58. {
  59. sv_strings(nadt_str, neg_inf_str, pos_inf_str, min_dt_str, max_dt_str);
  60. }
  61. special_values_parser(typename collection_type::iterator beg, typename collection_type::iterator end)
  62. {
  63. collection_type phrases;
  64. std::copy(beg, end, std::back_inserter(phrases));
  65. m_sv_strings = parse_tree_type(phrases, static_cast<int>(not_a_date_time));
  66. }
  67. special_values_parser(const special_values_parser<date_type,charT>& svp)
  68. {
  69. this->m_sv_strings = svp.m_sv_strings;
  70. }
  71. //! Replace special value strings
  72. void sv_strings(const string_type& nadt_str,
  73. const string_type& neg_inf_str,
  74. const string_type& pos_inf_str,
  75. const string_type& min_dt_str,
  76. const string_type& max_dt_str)
  77. {
  78. collection_type phrases;
  79. phrases.push_back(nadt_str);
  80. phrases.push_back(neg_inf_str);
  81. phrases.push_back(pos_inf_str);
  82. phrases.push_back(min_dt_str);
  83. phrases.push_back(max_dt_str);
  84. m_sv_strings = parse_tree_type(phrases, static_cast<int>(not_a_date_time));
  85. }
  86. //! The parser is expensive to create, and not thread-safe so it cannot be static
  87. //! therefore given a string, determine if it is likely to be a special value.
  88. //! A negative response is a definite no, whereas a positive is only likely and
  89. //! match() should be called and return value checked.
  90. //! \param[in] str the string to check
  91. //! \returns false if it is definitely not a special value
  92. static bool likely(const string_type& str)
  93. {
  94. if (!str.empty()) {
  95. switch (str[0]) {
  96. // See string definitions at the end of this class..
  97. case '+':
  98. case '-':
  99. case 'n':
  100. case 'm':
  101. return true;
  102. default:
  103. break;
  104. }
  105. }
  106. return false;
  107. }
  108. //! Given an input iterator, attempt to match it to a known special value
  109. //! \param[in] sitr the start iterator
  110. //! \param[in] str_end the end iterator
  111. //! \param[out] mr the match result:
  112. //! mr.current_match is set to the corresponding special_value or -1
  113. //! \returns whether something matched
  114. bool match(stream_itr_type& sitr,
  115. stream_itr_type& str_end,
  116. match_results& mr) const
  117. {
  118. unsigned int level = 0;
  119. m_sv_strings.match(sitr, str_end, mr, level);
  120. return (mr.current_match != match_results::PARSE_ERROR);
  121. }
  122. private:
  123. parse_tree_type m_sv_strings;
  124. };
  125. template<class date_type, class CharT>
  126. const typename special_values_parser<date_type, CharT>::char_type
  127. special_values_parser<date_type, CharT>::nadt_string[16] =
  128. {'n','o','t','-','a','-','d','a','t','e','-','t','i','m','e'};
  129. template<class date_type, class CharT>
  130. const typename special_values_parser<date_type, CharT>::char_type
  131. special_values_parser<date_type, CharT>::neg_inf_string[10] =
  132. {'-','i','n','f','i','n','i','t','y'};
  133. template<class date_type, class CharT>
  134. const typename special_values_parser<date_type, CharT>::char_type
  135. special_values_parser<date_type, CharT>::pos_inf_string[10] =
  136. {'+','i','n','f','i','n','i','t','y'};
  137. template<class date_type, class CharT>
  138. const typename special_values_parser<date_type, CharT>::char_type
  139. special_values_parser<date_type, CharT>::min_date_time_string[18] =
  140. {'m','i','n','i','m','u','m','-','d','a','t','e','-','t','i','m','e'};
  141. template<class date_type, class CharT>
  142. const typename special_values_parser<date_type, CharT>::char_type
  143. special_values_parser<date_type, CharT>::max_date_time_string[18] =
  144. {'m','a','x','i','m','u','m','-','d','a','t','e','-','t','i','m','e'};
  145. } } //namespace
  146. #endif // DATE_TIME_SPECIAL_VALUES_PARSER_HPP__