Пример #1
0
inline std::basic_istream<CharT,Traits>&
operator>>(std::basic_istream<CharT,Traits>& is, uuid& id)
{
    typedef typename std::basic_istream<CharT,Traits>::sentry sentry_t;
    sentry_t ok(is);
    if (ok)
    {
        CharT c;
        if (!is.get(c))
            return is;

        is.unget();

        if (c == is.widen('{'))
        {
            CharT buf[38+1];
            if (!is.read(buf, 38))
                return is;
            buf[38] = CharT();
            id = uuid(buf);
        }
        else
        {
            CharT buf[36+1];
            if (!is.read(buf, 36))
                return is;
            buf[36] = CharT();
            id = uuid(buf);
        }
    }
    return is;
}
Пример #2
0
std::streamsize ignore_line (
    std::basic_istream<CharT>& in, bool always_discard = false )
{
    std::streamsize nread = 0;

    if ( always_discard
            || ( in.rdbuf()->sungetc() != std::char_traits<CharT>::eof()
                 && in.get() != in.widen ( '\n' ) ) )
    {
        // The stream is good, and we haven't
        // read a full line yet, so clear it out
        in.ignore ( std::numeric_limits<std::streamsize>::max(), in.widen ( '\n' ) );
        nread = in.gcount();
    }
    return nread;
}
Пример #3
0
	inline SPROUT_NON_CONSTEXPR std::basic_istream<Char, Traits>&
	operator>>(std::basic_istream<Char, Traits>& lhs, sprout::bitset<N>& rhs) {
		typedef typename Traits::char_type char_type;
		typedef std::basic_istream<Char, Traits> istream_type;
		typedef typename istream_type::ios_base ios_base;

		std::basic_string<Char, Traits> tmp;
		tmp.reserve(N);
		char_type const zero = lhs.widen('0');
		char_type const one = lhs.widen('1');
		typename ios_base::iostate state = ios_base::goodbit;
		typename istream_type::sentry sentry(lhs);
		if (sentry) {
			try {
				for (std::size_t i = N; i > 0; --i) {
					static typename Traits::int_type eof = Traits::eof();
					typename Traits::int_type c1 = lhs.rdbuf()->sbumpc();
					if (Traits::eq_int_type(c1, eof)) {
						state |= ios_base::eofbit;
						break;
					} else {
						char_type const c2 = Traits::to_char_type(c1);
						if (Traits::eq(c2, zero)) {
							tmp.push_back(zero);
						} else if (Traits::eq(c2, one)) {
							tmp.push_back(one);
						} else if (Traits::eq_int_type(lhs.rdbuf()->sputbackc(c2), eof)) {
							state |= ios_base::failbit;
							break;
						}
					}
				}
			} catch(...) {
				lhs.setstate(ios_base::badbit);
			}
		}
		if (tmp.empty() && N) {
			state |= ios_base::failbit;
		} else {
			rhs.copy_from_string(tmp, static_cast<std::size_t>(0), N, zero, one);
			if (state) {
				lhs.setstate(state);
				return lhs;
			}
		}
		return lhs;
	}
Пример #4
0
    std::basic_istream<ch, char_traits>& operator>>(std::basic_istream<ch, char_traits> &is, uuid &u)
{
    const typename std::basic_istream<ch, char_traits>::sentry ok(is);
    if (ok) {
        unsigned char data[16];

        typedef std::ctype<ch> ctype_t;
        ctype_t const& ctype = std::use_facet<ctype_t>(is.getloc());

        ch xdigits[16];
        {
            char szdigits[] = "0123456789ABCDEF";
            ctype.widen(szdigits, szdigits+16, xdigits);
        }
        ch*const xdigits_end = xdigits+16;

        ch c;
        for (std::size_t i=0; i<u.size() && is; ++i) {
            is >> c;
            c = ctype.toupper(c);

            ch* f = std::find(xdigits, xdigits_end, c);
            if (f == xdigits_end) {
                is.setstate(std::ios_base::failbit);
                break;
            }

            unsigned char byte = static_cast<unsigned char>(std::distance(&xdigits[0], f));

            is >> c;
            c = ctype.toupper(c);
            f = std::find(xdigits, xdigits_end, c);
            if (f == xdigits_end) {
                is.setstate(std::ios_base::failbit);
                break;
            }

            byte <<= 4;
            byte |= static_cast<unsigned char>(std::distance(&xdigits[0], f));

            data[i] = byte;

            if (is) {
                if (i == 3 || i == 5 || i == 7 || i == 9) {
                    is >> c;
                    if (c != is.widen('-')) is.setstate(std::ios_base::failbit);
                }
            }
        }

        if (is) {
            std::copy(data, data+16, u.begin());
        }
    }
Пример #5
0
		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());
				}
			}
Пример #6
0
    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);

    }