inline
 std::basic_istream<CharT, Traits>&
 operator>>(std::basic_istream<CharT, Traits>& is, time_duration& td)
 {
   boost::io::ios_flags_saver iflags(is);
   typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
   if (strm_sentry) {
     try {
       typedef typename date_time::time_input_facet<ptime, CharT> time_input_facet;
       std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
       if(std::has_facet<time_input_facet>(is.getloc())) {
         std::use_facet<time_input_facet>(is.getloc()).get(sit, str_end, is, td);
       }
       else {
         time_input_facet* f = new time_input_facet();
         std::locale l = std::locale(is.getloc(), f);
         is.imbue(l);
         f->get(sit, str_end, is, td);
       }
     }
     catch(...) {
       std::ios_base::iostate exception_mask = is.exceptions();
       if(std::ios_base::failbit & exception_mask) {
         try { is.setstate(std::ios_base::failbit); }
         catch(std::ios_base::failure&) {}
         throw; // rethrow original exception
       }
       else {
         is.setstate(std::ios_base::failbit);
       }
     }
   }
   return is;
 }
示例#2
0
inline
std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is, local_date_time& ldt)
{
    boost::io::ios_flags_saver iflags(is);
    typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
    if (strm_sentry) {
        try {
            typedef typename local_date_time::utc_time_type utc_time_type;
            typedef typename date_time::time_input_facet<utc_time_type, CharT> time_input_facet;

            // intermediate objects
            std::basic_string<CharT> tz_str;
            utc_time_type pt(not_a_date_time);

            std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
            if(std::has_facet<time_input_facet>(is.getloc())) {
                std::use_facet<time_input_facet>(is.getloc()).get_local_time(sit, str_end, is, pt, tz_str);
            }
            else {
                time_input_facet* f = new time_input_facet();
                std::locale l = std::locale(is.getloc(), f);
                is.imbue(l);
                f->get_local_time(sit, str_end, is, pt, tz_str);
            }
            if(tz_str.empty()) {
                time_zone_ptr null_ptr;
                // a null time_zone_ptr creates a local_date_time that is UTC
                ldt = local_date_time(pt, null_ptr);
            }
            else {
                time_zone_ptr tz_ptr(new posix_time_zone(date_time::convert_string_type<CharT,char>(tz_str)));
                // the "date & time" constructor expects the time label to *not* be utc.
                // a posix_tz_string also expects the time label to *not* be utc.
                ldt = local_date_time(pt.date(), pt.time_of_day(), tz_ptr, local_date_time::EXCEPTION_ON_ERROR);
            }
        }
        catch(...) {
            // mask tells us what exceptions are turned on
            std::ios_base::iostate exception_mask = is.exceptions();
            // if the user wants exceptions on failbit, we'll rethrow our
            // date_time exception & set the failbit
            if(std::ios_base::failbit & exception_mask) {
                try {
                    is.setstate(std::ios_base::failbit);
                }
                catch(std::ios_base::failure&) {} // ignore this one
                throw; // rethrow original exception
            }
            else {
                // if the user want's to fail quietly, we simply set the failbit
                is.setstate(std::ios_base::failbit);
            }

        }
    }
    return is;
}
示例#3
0
  inline
  std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_month& m) 
  {
    typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;

    std::basic_string<charT> s;
    is >> s;
    
    if(!std::has_facet<facet_def>(is.getloc())) {
      std::locale loc = is.getloc();
      charT a = '\0';
      is.imbue(generate_locale(loc, a));
    }

    short num = 0;

    try{
      const facet_def& f = std::use_facet<facet_def>(is.getloc());
      num = date_time::find_match(f.get_short_month_names(), 
                                  f.get_long_month_names(), 
                                  (greg_month::max)(), s); 
    }
    /* bad_cast will be thrown if the desired facet is not accessible
     * so we can generate the facet. This has the drawback of using english
     * names as a default. */
    catch(std::bad_cast bc){
      std::cout << "Month exception caught" << std::endl;
      charT a = '\0';
      const facet_def* f = create_facet_def(a);
      num = date_time::find_match(f->get_short_month_names(), 
                                  f->get_long_month_names(), 
                                  (greg_month::max)(), s); 
      delete(f);
    }
    
    num += 1; // months numbered 1-12
    m = greg_month(num); 

    return is;
  }
示例#4
0
 inline
 std::basic_istream<CharT, Traits>&
 operator>>(std::basic_istream<CharT, Traits>& is, ptime& pt)
 {
   boost::io::ios_flags_saver iflags(is);
   typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
   if (strm_sentry) {
     try {
       typedef typename date_time::time_input_facet<ptime, CharT> time_input_facet;
       
       std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
       if(std::has_facet<time_input_facet>(is.getloc())) {
         std::use_facet<time_input_facet>(is.getloc()).get(sit, str_end, is, pt);
       }
       else {
         time_input_facet* f = new time_input_facet();
         std::locale l = std::locale(is.getloc(), f);
         is.imbue(l);
         f->get(sit, str_end, is, pt);
       }
     }
     catch(...) { 
       // mask tells us what exceptions are turned on
       std::ios_base::iostate exception_mask = is.exceptions();
       // if the user wants exceptions on failbit, we'll rethrow our 
       // date_time exception & set the failbit
       if(std::ios_base::failbit & exception_mask) {
         try { is.setstate(std::ios_base::failbit); } 
         catch(std::ios_base::failure&) {} // ignore this one
         throw; // rethrow original exception
       }
       else {
         // if the user want's to fail quietly, we simply set the failbit
         is.setstate(std::ios_base::failbit); 
       } 
           
     }
   }
   return is;
 }
示例#5
0
  inline
  std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_month& m)
  {
    typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;

    std::basic_string<charT> s;
    is >> s;

    if(!std::has_facet<facet_def>(is.getloc())) {
      std::locale loc = is.getloc();
      charT a = '\0';
      is.imbue(generate_locale(loc, a));
    }

    short num = 0;

    try{
      const facet_def& f = std::use_facet<facet_def>(is.getloc());
      num = date_time::find_match(f.get_short_month_names(),
                                  f.get_long_month_names(),
                                  (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
                                                           // which is needed by find_match
    }
    /* bad_cast will be thrown if the desired facet is not accessible
     * so we can generate the facet. This has the drawback of using english
     * names as a default. */
    catch(std::bad_cast&){
      charT a = '\0';
      std::auto_ptr< const facet_def > f(create_facet_def(a));
      num = date_time::find_match(f->get_short_month_names(),
                                  f->get_long_month_names(),
                                  (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
                                                           // which is needed by find_match
    }

    ++num; // months numbered 1-12
    m = greg_month(num);

    return is;
  }
示例#6
0
  inline
  std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_weekday& wd) 
  {
    typedef ndnboost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;

    std::basic_string<charT> s;
    is >> s;

    if(!std::has_facet<facet_def>(is.getloc())) {
      std::locale loc = is.getloc();
      charT a = '\0';
      is.imbue(generate_locale(loc, a));
    }

    short num = 0;
    try{
      const facet_def& f = std::use_facet<facet_def>(is.getloc());
      num = date_time::find_match(f.get_short_weekday_names(), 
                                  f.get_long_weekday_names(), 
                                  (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
                                                                 // to form the array size which is needed by find_match
    }
    /* bad_cast will be thrown if the desired facet is not accessible
     * so we can generate the facet. This has the drawback of using english
     * names as a default. */
    catch(std::bad_cast&){
      charT a = '\0';
      std::auto_ptr< const facet_def > f(create_facet_def(a));
      num = date_time::find_match(f->get_short_weekday_names(), 
                                  f->get_long_weekday_names(), 
                                  (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
                                                                 // to form the array size which is needed by find_match
    }
   
    wd = greg_weekday(num); // weekdays numbered 0-6
    return is;
  }
std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is, duration<Rep, Period>& d)
{
  //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
    typedef duration_punct<CharT> Facet;
    std::locale loc = is.getloc();
    //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
    if (!std::has_facet<Facet>(loc)) {
      //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
        is.imbue(std::locale(loc, new Facet));
    }
    //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
    loc = is.getloc();
    const Facet& f = std::use_facet<Facet>(loc);
    typedef typename chrono_detail::duration_io_intermediate<Rep>::type intermediate_type;
    intermediate_type r;
    std::ios_base::iostate err = std::ios_base::goodbit;
    // read value into r
    //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
    is >> r;
    //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
    if (is.good())
    {
      //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
        // now determine unit
        typedef std::istreambuf_iterator<CharT, Traits> in_iterator;
        in_iterator i(is);
        in_iterator e;
        //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
        if (i != e && *i == ' ')  // mandatory ' ' after value
        {
          //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
            ++i;
            if (i != e)
            {
              //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
                // unit is num / den (yet to be determined)
                unsigned long long num = 0;
                unsigned long long den = 0;
                if (*i == '[')
                {
                  //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
                    // parse [N/D]s or [N/D]seconds format
                    ++i;
                    CharT x;
                    is >> num >> x >> den;
                    if (!is.good() || (x != '/'))
                    {
                      //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
                        is.setstate(is.failbit);
                        return is;
                    }
                    i = in_iterator(is);
                    if (*i != ']')
                    {
                      //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
                        is.setstate(is.failbit);
                        return is;
                    }
                    ++i;
                    const std::basic_string<CharT> units[] =
                    {
                        f.template singular<ratio<1> >(),
                        f.template plural<ratio<1> >(),
                        f.template short_name<ratio<1> >()
                    };
                    //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
                    const std::basic_string<CharT>* k = chrono_detail::scan_keyword(i, e,
                                  units, units + sizeof(units)/sizeof(units[0]),
                                  //~ std::use_facet<std::ctype<CharT> >(loc),
                                  err);
                    //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
                    is.setstate(err);
                    switch ((k - units) / 3)
                    {
                    case 0:
                      //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
                        break;
                    default:
                        is.setstate(err);
                        //std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
                        return is;
                    }
                }
                else
                {
示例#8
0
inline void restore_ws(std::basic_istream<Ch, Tr> &in, std::locale const& old_locale)
{
  in.imbue(old_locale);
}
示例#9
0
inline std::locale change_ws(std::basic_istream<char, Tr> &in, CP pred, int mode = ctype_table::REPLACE)
{
  return in.imbue(std::locale(std::locale(), new ctype_mod_ws(pred, mode)));
}
void use_byte_order_mark (
	std::basic_istream <codepoint, char_traits <codepoint> > & stream)
{
	std::fpos<utf_mbstate> original_position = stream.tellg ();
	if (!stream.good())
	{
		imbue_default (stream);
		return;
	}
	try
	{
		stream.imbue (std::locale (stream.getloc(), new trivial_codecvt));

		/*
		We now start looking for a byte order mark.
		The following must be recognised:
		00 00 FE FF:	UTF-32BE
		FF FE 00 00:	UTF-32LE
		FE FF:			UTF-16BE
		FF FE:			UTF-16LE
		EF BB BF:		UTF-8
		*/

		codepoint byte = stream.rdbuf()->sbumpc();

		switch (byte)
		{
		case 0x00:
			if (stream.rdbuf()->sbumpc() == 0x00 &&
				stream.rdbuf()->sbumpc() == 0xfe &&
				stream.rdbuf()->sbumpc() == 0xff)
			{
				imbue_and_eat_bom (stream, original_position,
					new utf32be_codecvt);
				return;
			}
			break;
			
		case 0xef:
			if (stream.rdbuf()->sbumpc() == 0xbb &&
				stream.rdbuf()->sbumpc() == 0xbf)
			{
				imbue_and_eat_bom (stream, original_position,
					new utf8_codecvt);
				return;
			}
			break;

		case 0xfe:
			if (stream.rdbuf()->sbumpc() == 0xff)
			{
				imbue_and_eat_bom (stream, original_position,
					new utf16be_codecvt);
				return;
			}
			break;

		case 0xff:
			if (stream.rdbuf()->sbumpc() == 0xfe)
			{
				// This is either UTF-16LE or UTF-32LE.
				if (stream.rdbuf()->sbumpc() == 0x00 &&
					stream.rdbuf()->sbumpc() == 0x00)
				{
					imbue_and_eat_bom (stream, original_position,
						new utf32le_codecvt);
				} else
				{
					imbue_and_eat_bom (stream, original_position,
						new utf16le_codecvt);
				}
				return;
			}
			break;
		}
	}
	// If anything has gone wrong, just return to the original position and
	// state and imbue the default codecvt.
	catch (std::ios_base::failure &)
	{}
	stream.seekg (original_position);
	stream.clear();
	imbue_default (stream);
}