Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:35:24

0001 
0002 #ifndef DATE_TIME_SPECIAL_VALUES_PARSER_HPP__
0003 #define DATE_TIME_SPECIAL_VALUES_PARSER_HPP__
0004 
0005 /* Copyright (c) 2005 CrystalClear Software, Inc.
0006  * Use, modification and distribution is subject to the 
0007  * Boost Software License, Version 1.0. (See accompanying
0008  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0009  * Author: Jeff Garland, Bart Garst
0010  * $Date: 
0011  */
0012 
0013 
0014 #include "boost/date_time/string_parse_tree.hpp"
0015 #include "boost/date_time/special_defs.hpp"
0016 #include <string>
0017 #include <vector>
0018 
0019 namespace boost { namespace date_time {
0020 
0021   //! Class for special_value parsing 
0022   /*! 
0023    * TODO: add doc-comments for which elements can be changed
0024    * Parses input stream for strings representing special_values. 
0025    * Special values parsed are:
0026    *  - not_a_date_time
0027    *  - neg_infin
0028    *  - pod_infin
0029    *  - min_date_time
0030    *  - max_date_time
0031    */
0032   template<class date_type, typename charT>
0033   class special_values_parser
0034   {
0035    public:
0036     typedef std::basic_string<charT>        string_type;
0037     typedef std::basic_stringstream<charT>  stringstream_type;
0038     typedef std::istreambuf_iterator<charT> stream_itr_type;
0039     typedef typename date_type::duration_type duration_type;
0040     typedef string_parse_tree<charT> parse_tree_type;
0041     typedef typename parse_tree_type::parse_match_result_type match_results;
0042     typedef std::vector<std::basic_string<charT> > collection_type;
0043 
0044     typedef charT char_type;
0045     static const char_type nadt_string[16];
0046     static const char_type neg_inf_string[10];
0047     static const char_type pos_inf_string[10];
0048     static const char_type min_date_time_string[18];
0049     static const char_type max_date_time_string[18];
0050    
0051     //! Creates a special_values_parser with the default set of "sv_strings"
0052     special_values_parser()
0053     {
0054       sv_strings(string_type(nadt_string),
0055                  string_type(neg_inf_string),
0056                  string_type(pos_inf_string),
0057                  string_type(min_date_time_string),
0058                  string_type(max_date_time_string));
0059     }
0060 
0061     //! Creates a special_values_parser using a user defined set of element strings
0062     special_values_parser(const string_type& nadt_str,
0063                           const string_type& neg_inf_str,
0064                           const string_type& pos_inf_str,
0065                           const string_type& min_dt_str,
0066                           const string_type& max_dt_str)
0067     {
0068       sv_strings(nadt_str, neg_inf_str, pos_inf_str, min_dt_str, max_dt_str);
0069     }
0070 
0071     special_values_parser(typename collection_type::iterator beg, typename collection_type::iterator end)
0072     {
0073       collection_type phrases;
0074       std::copy(beg, end, std::back_inserter(phrases));
0075       m_sv_strings = parse_tree_type(phrases, static_cast<int>(not_a_date_time));
0076     }
0077 
0078     //! Replace special value strings
0079     void sv_strings(const string_type& nadt_str,
0080                     const string_type& neg_inf_str,
0081                     const string_type& pos_inf_str,
0082                     const string_type& min_dt_str,
0083                     const string_type& max_dt_str)
0084     {
0085       collection_type phrases;
0086       phrases.push_back(nadt_str);
0087       phrases.push_back(neg_inf_str);
0088       phrases.push_back(pos_inf_str);
0089       phrases.push_back(min_dt_str);
0090       phrases.push_back(max_dt_str);
0091       m_sv_strings = parse_tree_type(phrases, static_cast<int>(not_a_date_time));
0092     }
0093 
0094     //! The parser is expensive to create, and not thread-safe so it cannot be static
0095     //! therefore given a string, determine if it is likely to be a special value.
0096     //! A negative response is a definite no, whereas a positive is only likely and
0097     //! match() should be called and return value checked.
0098     //! \param[in]  str  the string to check
0099     //! \returns  false if it is definitely not a special value
0100     static bool should_call_match(const string_type& str)
0101     {
0102         if (!str.empty()) {
0103             switch (str[0]) {
0104                 // See string definitions at the end of this class..
0105                 case '+':
0106                 case '-':
0107                 case 'n':
0108                 case 'm':
0109                     return true;
0110 
0111                 default:
0112                     break;
0113             }
0114         }
0115 
0116         return false;
0117     }
0118 
0119     //! Given an input iterator, attempt to match it to a known special value
0120     //! \param[in]  sitr  the start iterator
0121     //! \param[in]  str_end  the end iterator
0122     //! \param[out]  mr  the match result:
0123     //!                  mr.current_match is set to the corresponding special_value or -1
0124     //! \returns  whether something matched
0125     bool match(stream_itr_type& sitr, 
0126                         stream_itr_type& str_end,
0127                         match_results& mr) const
0128     {
0129       unsigned int level = 0;
0130       m_sv_strings.match(sitr, str_end, mr, level);
0131       return (mr.current_match != match_results::PARSE_ERROR);
0132     }
0133 
0134    private:
0135     parse_tree_type m_sv_strings;
0136     
0137   };
0138 
0139   template<class date_type, class CharT>
0140   const typename special_values_parser<date_type, CharT>::char_type
0141   special_values_parser<date_type, CharT>::nadt_string[16] =
0142   {'n','o','t','-','a','-','d','a','t','e','-','t','i','m','e'};
0143   template<class date_type, class CharT>
0144   const typename special_values_parser<date_type, CharT>::char_type
0145   special_values_parser<date_type, CharT>::neg_inf_string[10] =
0146   {'-','i','n','f','i','n','i','t','y'};
0147   template<class date_type, class CharT>
0148   const typename special_values_parser<date_type, CharT>::char_type
0149   special_values_parser<date_type, CharT>::pos_inf_string[10] =
0150   {'+','i','n','f','i','n','i','t','y'};
0151   template<class date_type, class CharT>
0152   const typename special_values_parser<date_type, CharT>::char_type
0153   special_values_parser<date_type, CharT>::min_date_time_string[18] =
0154   {'m','i','n','i','m','u','m','-','d','a','t','e','-','t','i','m','e'};
0155   template<class date_type, class CharT>
0156   const typename special_values_parser<date_type, CharT>::char_type
0157   special_values_parser<date_type, CharT>::max_date_time_string[18] =
0158   {'m','a','x','i','m','u','m','-','d','a','t','e','-','t','i','m','e'};
0159 
0160 } } //namespace
0161 
0162 #endif // DATE_TIME_SPECIAL_VALUES_PARSER_HPP__
0163