Back to home page

EIC code displayed by LXR

 
 

    


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

0001 
0002 #ifndef DATETIME_PERIOD_PARSER_HPP___
0003 #define DATETIME_PERIOD_PARSER_HPP___
0004 
0005 /* Copyright (c) 2002-2004 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 #include <ios>
0014 #include <string>
0015 #include <vector>
0016 #include <iterator>
0017 #include <boost/throw_exception.hpp>
0018 #include <boost/date_time/special_defs.hpp>
0019 #include <boost/date_time/string_parse_tree.hpp>
0020 #include <boost/date_time/string_convert.hpp>
0021 
0022 
0023 namespace boost { namespace date_time {
0024 
0025 
0026   //! Not a facet, but a class used to specify and control period parsing
0027   /*! Provides settings for the following:
0028    *   - period_separator -- default '/'
0029    *   - period_open_start_delimeter -- default '['
0030    *   - period_open_range_end_delimeter -- default ')'
0031    *   - period_closed_range_end_delimeter -- default ']'
0032    *   - display_as_open_range, display_as_closed_range -- default closed_range
0033    *
0034    *  For a typical date_period, the contents of the input stream would be
0035    *@code
0036    *  [2004-Jan-04/2004-Feb-01]
0037    *@endcode
0038    * where the date format is controlled by the date facet
0039    */
0040   template<class date_type, typename CharT>
0041   class period_parser {
0042   public:
0043     typedef std::basic_string<CharT> string_type;
0044     typedef CharT                    char_type;
0045     //typedef typename std::basic_string<char_type>::const_iterator const_itr_type;
0046     typedef std::istreambuf_iterator<CharT> stream_itr_type;
0047     typedef string_parse_tree<CharT> parse_tree_type;
0048     typedef typename parse_tree_type::parse_match_result_type match_results;
0049     typedef std::vector<std::basic_string<CharT> > collection_type;
0050 
0051     static const char_type default_period_separator[2];
0052     static const char_type default_period_start_delimeter[2];
0053     static const char_type default_period_open_range_end_delimeter[2];
0054     static const char_type default_period_closed_range_end_delimeter[2];
0055 
0056     enum period_range_option { AS_OPEN_RANGE, AS_CLOSED_RANGE };
0057 
0058     //! Constructor that sets up period parser options
0059     period_parser(period_range_option range_opt = AS_CLOSED_RANGE,
0060                   const char_type* const period_separator = default_period_separator,
0061                   const char_type* const period_start_delimeter = default_period_start_delimeter,
0062                   const char_type* const period_open_range_end_delimeter = default_period_open_range_end_delimeter,
0063                   const char_type* const period_closed_range_end_delimeter = default_period_closed_range_end_delimeter)
0064       : m_range_option(range_opt)
0065     {
0066       delimiters.push_back(string_type(period_separator));
0067       delimiters.push_back(string_type(period_start_delimeter));
0068       delimiters.push_back(string_type(period_open_range_end_delimeter));
0069       delimiters.push_back(string_type(period_closed_range_end_delimeter));
0070     }
0071 
0072     period_range_option range_option() const
0073     {
0074       return m_range_option;
0075     }
0076     void range_option(period_range_option option)
0077     {
0078       m_range_option = option;
0079     }
0080     collection_type delimiter_strings() const
0081     {
0082       return delimiters;
0083     }
0084     void delimiter_strings(const string_type& separator,
0085                            const string_type& start_delim,
0086                            const string_type& open_end_delim,
0087                            const string_type& closed_end_delim)
0088     {
0089       delimiters.clear();
0090       delimiters.push_back(separator);
0091       delimiters.push_back(start_delim);
0092       delimiters.push_back(open_end_delim);
0093       delimiters.push_back(closed_end_delim);
0094     }
0095 
0096     //! Generic code to parse a period -- no matter the period type.
0097     /*! This generic code will parse any period using a facet to
0098      *  to get the 'elements'.  For example, in the case of a date_period
0099      *  the elements will be instances of a date which will be parsed
0100      *  according the to setup in the passed facet parameter.
0101      *
0102      *  The steps for parsing a period are always the same:
0103      *  - consume the start delimiter
0104      *  - get start element
0105      *  - consume the separator
0106      *  - get either last or end element depending on range settings
0107      *  - consume the end delimeter depending on range settings
0108      *
0109      *  Thus for a typical date period the contents of the input stream
0110      *  might look like this:
0111      *@code
0112      *
0113      *    [March 01, 2004/June 07, 2004]   <-- closed range
0114      *    [March 01, 2004/June 08, 2004)   <-- open range
0115      *
0116      *@endcode
0117      */
0118     template<class period_type, class duration_type, class facet_type>
0119     period_type get_period(stream_itr_type& sitr,
0120                            stream_itr_type& stream_end,
0121                            std::ios_base& a_ios,
0122                            const period_type& /* p */,
0123                            const duration_type& dur_unit,
0124                            const facet_type& facet) const
0125     {
0126       // skip leading whitespace
0127       while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
0128 
0129       typedef typename period_type::point_type point_type;
0130       point_type p1(not_a_date_time), p2(not_a_date_time);
0131 
0132 
0133       consume_delim(sitr, stream_end, delimiters[START]);       // start delim
0134       facet.get(sitr, stream_end, a_ios, p1);                   // first point
0135       consume_delim(sitr, stream_end, delimiters[SEPARATOR]);   // separator
0136       facet.get(sitr, stream_end, a_ios, p2);                   // second point
0137 
0138       // period construction parameters are always open range [begin, end)
0139       if (m_range_option == AS_CLOSED_RANGE) {
0140         consume_delim(sitr, stream_end, delimiters[CLOSED_END]);// end delim
0141         // add 1 duration unit to p2 to make range open
0142         p2 += dur_unit;
0143       }
0144       else {
0145         consume_delim(sitr, stream_end, delimiters[OPEN_END]);  // end delim
0146       }
0147 
0148       return period_type(p1, p2);
0149     }
0150 
0151   private:
0152     collection_type delimiters;
0153     period_range_option m_range_option;
0154 
0155     enum delim_ids { SEPARATOR, START, OPEN_END, CLOSED_END };
0156 
0157     //! throws ios_base::failure if delimiter and parsed data do not match
0158     void consume_delim(stream_itr_type& sitr,
0159                        stream_itr_type& stream_end,
0160                        const string_type& delim) const
0161     {
0162       /* string_parse_tree will not parse a string of punctuation characters
0163        * without knowing exactly how many characters to process
0164        * Ex [2000. Will not parse out the '[' string without knowing
0165        * to process only one character. By using length of the delimiter
0166        * string we can safely iterate past it. */
0167       string_type s;
0168       for(unsigned int i = 0; i < delim.length() && sitr != stream_end; ++i) {
0169         s += *sitr;
0170         ++sitr;
0171       }
0172       if(s != delim) {
0173         boost::throw_exception(std::ios_base::failure("Parse failed. Expected '"
0174           + convert_string_type<char_type,char>(delim) + "' but found '" + convert_string_type<char_type,char>(s) + "'"));
0175       }
0176     }
0177   };
0178 
0179   template <class date_type, class char_type>
0180   const typename period_parser<date_type, char_type>::char_type
0181   period_parser<date_type, char_type>::default_period_separator[2] = {'/'};
0182 
0183   template <class date_type, class char_type>
0184   const typename period_parser<date_type, char_type>::char_type
0185   period_parser<date_type, char_type>::default_period_start_delimeter[2] = {'['};
0186 
0187   template <class date_type, class char_type>
0188   const typename period_parser<date_type, char_type>::char_type
0189   period_parser<date_type, char_type>::default_period_open_range_end_delimeter[2] = {')'};
0190 
0191   template <class date_type, class char_type>
0192   const typename period_parser<date_type, char_type>::char_type
0193   period_parser<date_type, char_type>::default_period_closed_range_end_delimeter[2] = {']'};
0194 
0195  } } //namespace boost::date_time
0196 
0197 #endif // DATETIME_PERIOD_PARSER_HPP___