annotate DEPENDENCIES/generic/include/boost/date_time/date_generator_parser.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1
Chris@16 2 #ifndef DATE_TIME_DATE_GENERATOR_PARSER_HPP__
Chris@16 3 #define DATE_TIME_DATE_GENERATOR_PARSER_HPP__
Chris@16 4
Chris@16 5 /* Copyright (c) 2005 CrystalClear Software, Inc.
Chris@16 6 * Use, modification and distribution is subject to the
Chris@16 7 * Boost Software License, Version 1.0. (See accompanying
Chris@16 8 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
Chris@16 9 * Author: Jeff Garland, Bart Garst
Chris@101 10 * $Date$
Chris@16 11 */
Chris@16 12
Chris@16 13 #include <string>
Chris@16 14 #include <vector>
Chris@16 15 #include <iterator> // istreambuf_iterator
Chris@16 16 #include <boost/throw_exception.hpp>
Chris@16 17 #include <boost/date_time/compiler_config.hpp>
Chris@16 18 #include <boost/date_time/string_parse_tree.hpp>
Chris@16 19 #include <boost/date_time/date_generators.hpp>
Chris@16 20 #include <boost/date_time/format_date_parser.hpp>
Chris@16 21
Chris@16 22 namespace boost { namespace date_time {
Chris@16 23
Chris@16 24 //! Class for date_generator parsing
Chris@16 25 /*! The elements of a date_generator "phrase" are parsed from the input stream in a
Chris@16 26 * particular order. All elements are required and the order in which they appear
Chris@16 27 * cannot change, however, the elements themselves can be changed. The default
Chris@16 28 * elements and their order are as follows:
Chris@16 29 *
Chris@16 30 * - partial_date => "dd Month"
Chris@16 31 * - nth_day_of_the_week_in_month => "nth weekday of month"
Chris@16 32 * - first_day_of_the_week_in_month => "first weekday of month"
Chris@16 33 * - last_day_of_the_week_in_month => "last weekday of month"
Chris@16 34 * - first_day_of_the_week_after => "weekday after"
Chris@16 35 * - first_day_of_the_week_before => "weekday before"
Chris@16 36 *
Chris@16 37 * Weekday and Month names and formats are handled via the date_input_facet.
Chris@16 38 *
Chris@16 39 */
Chris@16 40 template<class date_type, typename charT>
Chris@16 41 class date_generator_parser
Chris@16 42 {
Chris@16 43 public:
Chris@16 44 typedef std::basic_string<charT> string_type;
Chris@16 45 typedef std::istreambuf_iterator<charT> stream_itr_type;
Chris@16 46
Chris@16 47 typedef typename date_type::month_type month_type;
Chris@16 48 typedef typename date_type::day_of_week_type day_of_week_type;
Chris@16 49 typedef typename date_type::day_type day_type;
Chris@16 50
Chris@16 51 typedef string_parse_tree<charT> parse_tree_type;
Chris@16 52 typedef typename parse_tree_type::parse_match_result_type match_results;
Chris@16 53 typedef std::vector<std::basic_string<charT> > collection_type;
Chris@16 54
Chris@16 55 typedef partial_date<date_type> partial_date_type;
Chris@16 56 typedef nth_kday_of_month<date_type> nth_kday_type;
Chris@16 57 typedef first_kday_of_month<date_type> first_kday_type;
Chris@16 58 typedef last_kday_of_month<date_type> last_kday_type;
Chris@16 59 typedef first_kday_after<date_type> kday_after_type;
Chris@16 60 typedef first_kday_before<date_type> kday_before_type;
Chris@16 61
Chris@16 62 typedef charT char_type;
Chris@16 63 static const char_type first_string[6];
Chris@16 64 static const char_type second_string[7];
Chris@16 65 static const char_type third_string[6];
Chris@16 66 static const char_type fourth_string[7];
Chris@16 67 static const char_type fifth_string[6];
Chris@16 68 static const char_type last_string[5];
Chris@16 69 static const char_type before_string[8];
Chris@16 70 static const char_type after_string[6];
Chris@16 71 static const char_type of_string[3];
Chris@16 72
Chris@16 73 enum phrase_elements {first=0, second, third, fourth, fifth, last,
Chris@16 74 before, after, of, number_of_phrase_elements};
Chris@16 75
Chris@16 76 //! Creates a date_generator_parser with the default set of "element_strings"
Chris@16 77 date_generator_parser()
Chris@16 78 {
Chris@16 79 element_strings(string_type(first_string),
Chris@16 80 string_type(second_string),
Chris@16 81 string_type(third_string),
Chris@16 82 string_type(fourth_string),
Chris@16 83 string_type(fifth_string),
Chris@16 84 string_type(last_string),
Chris@16 85 string_type(before_string),
Chris@16 86 string_type(after_string),
Chris@16 87 string_type(of_string));
Chris@16 88 }
Chris@16 89
Chris@16 90 //! Creates a date_generator_parser using a user defined set of element strings
Chris@16 91 date_generator_parser(const string_type& first_str,
Chris@16 92 const string_type& second_str,
Chris@16 93 const string_type& third_str,
Chris@16 94 const string_type& fourth_str,
Chris@16 95 const string_type& fifth_str,
Chris@16 96 const string_type& last_str,
Chris@16 97 const string_type& before_str,
Chris@16 98 const string_type& after_str,
Chris@16 99 const string_type& of_str)
Chris@16 100 {
Chris@16 101 element_strings(first_str, second_str, third_str, fourth_str, fifth_str,
Chris@16 102 last_str, before_str, after_str, of_str);
Chris@16 103 }
Chris@16 104
Chris@16 105 //! Replace strings that determine nth week for generator
Chris@16 106 void element_strings(const string_type& first_str,
Chris@16 107 const string_type& second_str,
Chris@16 108 const string_type& third_str,
Chris@16 109 const string_type& fourth_str,
Chris@16 110 const string_type& fifth_str,
Chris@16 111 const string_type& last_str,
Chris@16 112 const string_type& before_str,
Chris@16 113 const string_type& after_str,
Chris@16 114 const string_type& of_str)
Chris@16 115 {
Chris@16 116 collection_type phrases;
Chris@16 117 phrases.push_back(first_str);
Chris@16 118 phrases.push_back(second_str);
Chris@16 119 phrases.push_back(third_str);
Chris@16 120 phrases.push_back(fourth_str);
Chris@16 121 phrases.push_back(fifth_str);
Chris@16 122 phrases.push_back(last_str);
Chris@16 123 phrases.push_back(before_str);
Chris@16 124 phrases.push_back(after_str);
Chris@16 125 phrases.push_back(of_str);
Chris@16 126 m_element_strings = parse_tree_type(phrases, this->first); // enum first
Chris@16 127 }
Chris@16 128
Chris@16 129 void element_strings(const collection_type& col)
Chris@16 130 {
Chris@16 131 m_element_strings = parse_tree_type(col, this->first); // enum first
Chris@16 132 }
Chris@16 133
Chris@16 134 //! returns partial_date parsed from stream
Chris@16 135 template<class facet_type>
Chris@16 136 partial_date_type
Chris@16 137 get_partial_date_type(stream_itr_type& sitr,
Chris@16 138 stream_itr_type& stream_end,
Chris@16 139 std::ios_base& a_ios,
Chris@16 140 const facet_type& facet) const
Chris@16 141 {
Chris@16 142 // skip leading whitespace
Chris@16 143 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
Chris@16 144
Chris@16 145 day_type d(1);
Chris@16 146 month_type m(1);
Chris@16 147 facet.get(sitr, stream_end, a_ios, d);
Chris@16 148 facet.get(sitr, stream_end, a_ios, m);
Chris@16 149
Chris@16 150 return partial_date_type(d,m);
Chris@16 151 }
Chris@16 152
Chris@16 153 //! returns nth_kday_of_week parsed from stream
Chris@16 154 template<class facet_type>
Chris@16 155 nth_kday_type
Chris@16 156 get_nth_kday_type(stream_itr_type& sitr,
Chris@16 157 stream_itr_type& stream_end,
Chris@16 158 std::ios_base& a_ios,
Chris@16 159 const facet_type& facet) const
Chris@16 160 {
Chris@16 161 // skip leading whitespace
Chris@16 162 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
Chris@16 163
Chris@16 164 typename nth_kday_type::week_num wn;
Chris@16 165 day_of_week_type wd(0); // no default constructor
Chris@16 166 month_type m(1); // no default constructor
Chris@16 167
Chris@16 168 match_results mr = m_element_strings.match(sitr, stream_end);
Chris@16 169 switch(mr.current_match) {
Chris@16 170 case first : { wn = nth_kday_type::first; break; }
Chris@16 171 case second : { wn = nth_kday_type::second; break; }
Chris@16 172 case third : { wn = nth_kday_type::third; break; }
Chris@16 173 case fourth : { wn = nth_kday_type::fourth; break; }
Chris@16 174 case fifth : { wn = nth_kday_type::fifth; break; }
Chris@16 175 default:
Chris@16 176 {
Chris@16 177 boost::throw_exception(std::ios_base::failure("Parse failed. No match found for '" + mr.cache + "'"));
Chris@16 178 BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(wn = nth_kday_type::first);
Chris@16 179 }
Chris@16 180 } // week num
Chris@16 181 facet.get(sitr, stream_end, a_ios, wd); // day_of_week
Chris@16 182 extract_element(sitr, stream_end, of); // "of" element
Chris@16 183 facet.get(sitr, stream_end, a_ios, m); // month
Chris@16 184
Chris@16 185 return nth_kday_type(wn, wd, m);
Chris@16 186 }
Chris@16 187
Chris@16 188 //! returns first_kday_of_week parsed from stream
Chris@16 189 template<class facet_type>
Chris@16 190 first_kday_type
Chris@16 191 get_first_kday_type(stream_itr_type& sitr,
Chris@16 192 stream_itr_type& stream_end,
Chris@16 193 std::ios_base& a_ios,
Chris@16 194 const facet_type& facet) const
Chris@16 195 {
Chris@16 196 // skip leading whitespace
Chris@16 197 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
Chris@16 198
Chris@16 199 day_of_week_type wd(0); // no default constructor
Chris@16 200 month_type m(1); // no default constructor
Chris@16 201
Chris@16 202 extract_element(sitr, stream_end, first); // "first" element
Chris@16 203 facet.get(sitr, stream_end, a_ios, wd); // day_of_week
Chris@16 204 extract_element(sitr, stream_end, of); // "of" element
Chris@16 205 facet.get(sitr, stream_end, a_ios, m); // month
Chris@16 206
Chris@16 207
Chris@16 208 return first_kday_type(wd, m);
Chris@16 209 }
Chris@16 210
Chris@16 211 //! returns last_kday_of_week parsed from stream
Chris@16 212 template<class facet_type>
Chris@16 213 last_kday_type
Chris@16 214 get_last_kday_type(stream_itr_type& sitr,
Chris@16 215 stream_itr_type& stream_end,
Chris@16 216 std::ios_base& a_ios,
Chris@16 217 const facet_type& facet) const
Chris@16 218 {
Chris@16 219 // skip leading whitespace
Chris@16 220 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
Chris@16 221
Chris@16 222 day_of_week_type wd(0); // no default constructor
Chris@16 223 month_type m(1); // no default constructor
Chris@16 224
Chris@16 225 extract_element(sitr, stream_end, last); // "last" element
Chris@16 226 facet.get(sitr, stream_end, a_ios, wd); // day_of_week
Chris@16 227 extract_element(sitr, stream_end, of); // "of" element
Chris@16 228 facet.get(sitr, stream_end, a_ios, m); // month
Chris@16 229
Chris@16 230
Chris@16 231 return last_kday_type(wd, m);
Chris@16 232 }
Chris@16 233
Chris@16 234 //! returns first_kday_of_week parsed from stream
Chris@16 235 template<class facet_type>
Chris@16 236 kday_before_type
Chris@16 237 get_kday_before_type(stream_itr_type& sitr,
Chris@16 238 stream_itr_type& stream_end,
Chris@16 239 std::ios_base& a_ios,
Chris@16 240 const facet_type& facet) const
Chris@16 241 {
Chris@16 242 // skip leading whitespace
Chris@16 243 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
Chris@16 244
Chris@16 245 day_of_week_type wd(0); // no default constructor
Chris@16 246
Chris@16 247 facet.get(sitr, stream_end, a_ios, wd); // day_of_week
Chris@16 248 extract_element(sitr, stream_end, before);// "before" element
Chris@16 249
Chris@16 250 return kday_before_type(wd);
Chris@16 251 }
Chris@16 252
Chris@16 253 //! returns first_kday_of_week parsed from stream
Chris@16 254 template<class facet_type>
Chris@16 255 kday_after_type
Chris@16 256 get_kday_after_type(stream_itr_type& sitr,
Chris@16 257 stream_itr_type& stream_end,
Chris@16 258 std::ios_base& a_ios,
Chris@16 259 const facet_type& facet) const
Chris@16 260 {
Chris@16 261 // skip leading whitespace
Chris@16 262 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
Chris@16 263
Chris@16 264 day_of_week_type wd(0); // no default constructor
Chris@16 265
Chris@16 266 facet.get(sitr, stream_end, a_ios, wd); // day_of_week
Chris@16 267 extract_element(sitr, stream_end, after); // "after" element
Chris@16 268
Chris@16 269 return kday_after_type(wd);
Chris@16 270 }
Chris@16 271
Chris@16 272 private:
Chris@16 273 parse_tree_type m_element_strings;
Chris@16 274
Chris@16 275 //! Extracts phrase element from input. Throws ios_base::failure on error.
Chris@16 276 void extract_element(stream_itr_type& sitr,
Chris@16 277 stream_itr_type& stream_end,
Chris@16 278 typename date_generator_parser::phrase_elements ele) const
Chris@16 279 {
Chris@16 280 // skip leading whitespace
Chris@16 281 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
Chris@16 282 match_results mr = m_element_strings.match(sitr, stream_end);
Chris@16 283 if(mr.current_match != ele) {
Chris@16 284 boost::throw_exception(std::ios_base::failure("Parse failed. No match found for '" + mr.cache + "'"));
Chris@16 285 }
Chris@16 286 }
Chris@16 287
Chris@16 288 };
Chris@16 289
Chris@16 290 template<class date_type, class CharT>
Chris@16 291 const typename date_generator_parser<date_type, CharT>::char_type
Chris@16 292 date_generator_parser<date_type, CharT>::first_string[6] =
Chris@16 293 {'f','i','r','s','t'};
Chris@16 294 template<class date_type, class CharT>
Chris@16 295 const typename date_generator_parser<date_type, CharT>::char_type
Chris@16 296 date_generator_parser<date_type, CharT>::second_string[7] =
Chris@16 297 {'s','e','c','o','n','d'};
Chris@16 298 template<class date_type, class CharT>
Chris@16 299 const typename date_generator_parser<date_type, CharT>::char_type
Chris@16 300 date_generator_parser<date_type, CharT>::third_string[6] =
Chris@16 301 {'t','h','i','r','d'};
Chris@16 302 template<class date_type, class CharT>
Chris@16 303 const typename date_generator_parser<date_type, CharT>::char_type
Chris@16 304 date_generator_parser<date_type, CharT>::fourth_string[7] =
Chris@16 305 {'f','o','u','r','t','h'};
Chris@16 306 template<class date_type, class CharT>
Chris@16 307 const typename date_generator_parser<date_type, CharT>::char_type
Chris@16 308 date_generator_parser<date_type, CharT>::fifth_string[6] =
Chris@16 309 {'f','i','f','t','h'};
Chris@16 310 template<class date_type, class CharT>
Chris@16 311 const typename date_generator_parser<date_type, CharT>::char_type
Chris@16 312 date_generator_parser<date_type, CharT>::last_string[5] =
Chris@16 313 {'l','a','s','t'};
Chris@16 314 template<class date_type, class CharT>
Chris@16 315 const typename date_generator_parser<date_type, CharT>::char_type
Chris@16 316 date_generator_parser<date_type, CharT>::before_string[8] =
Chris@16 317 {'b','e','f','o','r','e'};
Chris@16 318 template<class date_type, class CharT>
Chris@16 319 const typename date_generator_parser<date_type, CharT>::char_type
Chris@16 320 date_generator_parser<date_type, CharT>::after_string[6] =
Chris@16 321 {'a','f','t','e','r'};
Chris@16 322 template<class date_type, class CharT>
Chris@16 323 const typename date_generator_parser<date_type, CharT>::char_type
Chris@16 324 date_generator_parser<date_type, CharT>::of_string[3] =
Chris@16 325 {'o','f'};
Chris@16 326
Chris@16 327 } } //namespace
Chris@16 328
Chris@16 329 #endif // DATE_TIME_DATE_GENERATOR_PARSER_HPP__
Chris@16 330