special_values_parser.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. //! Replace special value strings
  68. void sv_strings(const string_type& nadt_str,
  69. const string_type& neg_inf_str,
  70. const string_type& pos_inf_str,
  71. const string_type& min_dt_str,
  72. const string_type& max_dt_str)
  73. {
  74. collection_type phrases;
  75. phrases.push_back(nadt_str);
  76. phrases.push_back(neg_inf_str);
  77. phrases.push_back(pos_inf_str);
  78. phrases.push_back(min_dt_str);
  79. phrases.push_back(max_dt_str);
  80. m_sv_strings = parse_tree_type(phrases, static_cast<int>(not_a_date_time));
  81. }
  82. //! The parser is expensive to create, and not thread-safe so it cannot be static
  83. //! therefore given a string, determine if it is likely to be a special value.
  84. //! A negative response is a definite no, whereas a positive is only likely and
  85. //! match() should be called and return value checked.
  86. //! \param[in] str the string to check
  87. //! \returns false if it is definitely not a special value
  88. static bool likely(const string_type& str)
  89. {
  90. if (!str.empty()) {
  91. switch (str[0]) {
  92. // See string definitions at the end of this class..
  93. case '+':
  94. case '-':
  95. case 'n':
  96. case 'm':
  97. return true;
  98. default:
  99. break;
  100. }
  101. }
  102. return false;
  103. }
  104. //! Given an input iterator, attempt to match it to a known special value
  105. //! \param[in] sitr the start iterator
  106. //! \param[in] str_end the end iterator
  107. //! \param[out] mr the match result:
  108. //! mr.current_match is set to the corresponding special_value or -1
  109. //! \returns whether something matched
  110. bool match(stream_itr_type& sitr,
  111. stream_itr_type& str_end,
  112. match_results& mr) const
  113. {
  114. unsigned int level = 0;
  115. m_sv_strings.match(sitr, str_end, mr, level);
  116. return (mr.current_match != match_results::PARSE_ERROR);
  117. }
  118. private:
  119. parse_tree_type m_sv_strings;
  120. };
  121. template<class date_type, class CharT>
  122. const typename special_values_parser<date_type, CharT>::char_type
  123. special_values_parser<date_type, CharT>::nadt_string[16] =
  124. {'n','o','t','-','a','-','d','a','t','e','-','t','i','m','e'};
  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>::neg_inf_string[10] =
  128. {'-','i','n','f','i','n','i','t','y'};
  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>::pos_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>::min_date_time_string[18] =
  136. {'m','i','n','i','m','u','m','-','d','a','t','e','-','t','i','m','e'};
  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>::max_date_time_string[18] =
  140. {'m','a','x','i','m','u','m','-','d','a','t','e','-','t','i','m','e'};
  141. } } //namespace
  142. #endif // DATE_TIME_SPECIAL_VALUES_PARSER_HPP__