Example #1
0
	inline SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>&
	operator>>(std::basic_istream<Elem, Traits>& lhs, sprout::rational<IntType>& rhs) {
		IntType n = IntType(0);
		IntType d = IntType(1);
		Elem c = 0;
		sprout::detail::io::ios_flags_saver saver(lhs);
		lhs >> n;
		c = lhs.get();
		if (c != Elem('/')) {
			lhs.clear(std::istream::badbit);
		}
		lhs >> std::noskipws;
		lhs >> d;
		if (lhs) {
			rhs.assign(n, d);
		}
		return lhs;
	}
Example #2
0
        std::basic_istream<charT, Traits>&
        operator>>(std::basic_istream<charT, Traits>& in,
            token_matcher<charT, Traits, Allocator> matcher)
        {
            typename std::basic_string<charT, Traits, Allocator>::iterator
                i = matcher.token.begin(),
                end = matcher.token.end();
            typename std::basic_istream<charT, Traits>::char_type c;
            for (; i != end && in.get(c); ++i)
            {
                if (*i != c)
                {
                    in.clear(std::basic_ios<charT, Traits>::failbit);
                    break;
                }
            }

            return in;
        }
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);
}