inline std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_weekday& wd) { 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_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; }
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; }
inline std::basic_istream<Elem, Traits>& operator>>(std::basic_istream<Elem, Traits>& lhs, sprout::uuids::uuid& rhs) { typedef typename std::basic_ios<Elem, Traits>::char_type char_type; typename std::basic_istream<Elem, Traits>::sentry const ok(lhs); typedef std::ctype<Elem> ctype_type; if (ok) { sprout::uuids::uuid::value_type data[16]; ctype_type const& ctype = std::use_facet<ctype_type>(lhs.getloc()); char_type xdigits[16]; { char const szdigits[] = "0123456789ABCDEF"; ctype.widen(szdigits, szdigits + 16, xdigits); } char_type const* const xdigits_end = xdigits + 16; char_type c; for (sprout::uuids::uuid::size_type i = 0, last = rhs.size(); i < last && lhs; ++i) { lhs >> c; c = ctype.toupper(c); char_type const* f = std::find(xdigits, xdigits_end, c); if (f == xdigits_end) { lhs.setstate(std::ios_base::failbit); break; } sprout::uuids::uuid::value_type byte = static_cast<sprout::uuids::uuid::value_type>(std::distance(&xdigits[0], f)); lhs >> c; c = ctype.toupper(c); f = std::find(xdigits, xdigits_end, c); if (f == xdigits_end) { lhs.setstate(std::ios_base::failbit); break; } byte <<= 4; byte |= static_cast<sprout::uuids::uuid::value_type>(std::distance(&xdigits[0], f)); data[i] = byte; if (lhs) { if (i == 3 || i == 5 || i == 7 || i == 9) { lhs >> c; if (c != lhs.widen('-')) { lhs.setstate(std::ios_base::failbit); break; } } } } if (lhs) { std::copy(data, data + 16, rhs.begin()); } }
inline SPROUT_NON_CONSTEXPR std::basic_istream<T, StreamTraits>& operator>>(std::basic_istream<T, StreamTraits>& lhs, sprout::basic_string<T, N, Traits>& rhs) { typedef T elem_type; typedef StreamTraits traits_type; typedef std::basic_istream<T, StreamTraits> istream_type; typedef sprout::basic_string<T, N, Traits> string_type; typedef std::ctype<elem_type> ctype_type; typedef typename string_type::size_type size_type; std::ios_base::iostate state = std::ios_base::goodbit; bool changed = false; size_type current = 0; if (typename istream_type::sentry(lhs)) { ctype_type const& ctype_fac = std::use_facet<ctype_type>(lhs.getloc()); try { size_type remain = 0 < lhs.width() && static_cast<size_type>(lhs.width()) < rhs.max_size() ? static_cast<size_type>(lhs.width()) : rhs.max_size() ; typename traits_type::int_type meta = lhs.rdbuf()->sgetc(); for (; remain; --remain, meta = lhs.rdbuf()->snextc()) if (traits_type::eq_int_type(traits_type::eof(), meta)) { state |= std::ios_base::eofbit; break; } else if (ctype_fac.is(ctype_type::space, traits_type::to_char_type(meta))) { break; } else { rhs[current] = traits_type::to_char_type(meta); changed = true; ++current; } } catch (...) { state |= std::ios_base::badbit; } } lhs.width(0); if (!changed) { state |= std::ios_base::failbit; } lhs.setstate(state); rhs.resize(current); return lhs; }
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 {
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); }
void read_ini(std::basic_istream< typename Ptree::key_type::value_type> &stream, Ptree &pt) { typedef typename Ptree::key_type::value_type Ch; typedef std::basic_string<Ch> Str; const Ch semicolon = stream.widen(';'); const Ch hash = stream.widen('#'); const Ch lbracket = stream.widen('['); const Ch rbracket = stream.widen(']'); Ptree local; unsigned long line_no = 0; Ptree *section = 0; Str line; // For all lines while (stream.good()) { // Get line from stream ++line_no; std::getline(stream, line); if (!stream.good() && !stream.eof()) BOOST_PROPERTY_TREE_THROW(ini_parser_error( "read error", "", line_no)); // If line is non-empty line = property_tree::detail::trim(line, stream.getloc()); if (!line.empty()) { // Comment, section or key? if (line[0] == semicolon || line[0] == hash) { // Ignore comments } else if (line[0] == lbracket) { // If the previous section was empty, drop it again. if (section && section->empty()) local.pop_back(); typename Str::size_type end = line.find(rbracket); if (end == Str::npos) BOOST_PROPERTY_TREE_THROW(ini_parser_error( "unmatched '['", "", line_no)); Str key = property_tree::detail::trim( line.substr(1, end - 1), stream.getloc()); if (local.find(key) != local.not_found()) BOOST_PROPERTY_TREE_THROW(ini_parser_error( "duplicate section name", "", line_no)); section = &local.push_back( std::make_pair(key, Ptree()))->second; } else { Ptree &container = section ? *section : local; typename Str::size_type eqpos = line.find(Ch('=')); if (eqpos == Str::npos) BOOST_PROPERTY_TREE_THROW(ini_parser_error( "'=' character not found in line", "", line_no)); if (eqpos == 0) BOOST_PROPERTY_TREE_THROW(ini_parser_error( "key expected", "", line_no)); Str key = property_tree::detail::trim( line.substr(0, eqpos), stream.getloc()); Str data = property_tree::detail::trim( line.substr(eqpos + 1, Str::npos), stream.getloc()); if (container.find(key) != container.not_found()) BOOST_PROPERTY_TREE_THROW(ini_parser_error( "duplicate key name", "", line_no)); container.push_back(std::make_pair(key, Ptree(data))); } } } // If the last section was empty, drop it again. if (section && section->empty()) local.pop_back(); // Swap local ptree with result ptree pt.swap(local); }
//! The constructor reads parameters from the stream explicit settings(std::basic_istream< char_type >& strm) { typedef typename string_type::iterator str_iterator; std::locale loc = strm.getloc(); typename sections_t::iterator current_section = m_Sections.end(); string_type line; for (unsigned int line_counter = 1; strm.good(); ++line_counter) { line.clear(); std::getline(strm, line); boost::algorithm::trim(line, loc); // Skipping empty lines and comments // NOTE: The comments are only allowed to be the whole line. // Comments beginning in the middle of the line are not supported. if (!line.empty() && line[0] != constants::char_comment) { // Check if the line is a section starter if (line[0] == constants::char_section_bracket_left) { str_iterator it = std::find(line.begin() + 1, line.end(), constants::char_section_bracket_right); string_type section_name(line.begin() + 1, it); boost::algorithm::trim(section_name, loc); if (it != line.end() && !section_name.empty()) { // Creating a new section current_section = m_Sections.insert(std::make_pair(section_name, params_t())).first; } else { // The section starter is broken std::ostringstream descr; descr << "At line " << line_counter << ". The section header is invalid."; boost::log::aux::throw_exception(std::runtime_error(descr.str())); } } else { // Check that we've already started a section if (current_section != m_Sections.end()) { // Find the '=' between the parameter name and value str_iterator it = std::find(line.begin(), line.end(), constants::char_equal); string_type param_name(line.begin(), it); boost::algorithm::trim_right(param_name, loc); if (it != line.end() && !param_name.empty()) { // Put the parameter value into the map string_type param_value(++it, line.end()); boost::algorithm::trim_left(param_value, loc); if (param_value.size() >= 2 && param_value[0] == constants::char_quote && *param_value.rbegin() == constants::char_quote) { param_value = param_value.substr(1, param_value.size() - 2); } current_section->second[param_name] = param_value; } else { // The parameter name is not valid std::ostringstream descr; descr << "At line " << line_counter << ". Parameter description is not valid."; boost::log::aux::throw_exception(std::runtime_error(descr.str())); } } else { // The parameter encountered before any section starter std::ostringstream descr; descr << "At line " << line_counter << ". Parameters are only allowed in sections."; boost::log::aux::throw_exception(std::runtime_error(descr.str())); } } } } }