Beispiel #1
0
void Polynomial<T>::input(std::istream &in)
{
	T coef;
	int data;
	in.clear();
	in>>coef>>data;
	while(in.good())
	{
		operator+=(Polynomial(coef,data));
		in>>coef>>data;
	}
	in.clear();
	in.get();
}
Beispiel #2
0
/// Determine whether the given stream is gzipped.
bool RibInputBuffer::isGzippedStream(std::istream& in)
{
	bool isZipped = false;
	std::istream::int_type c = in.get();
	// Check whether the magic number matches that for a gzip stream
	const std::istream::int_type gzipMagic[] = {0x1f, 0x8b};
	if(c == gzipMagic[0])
	{
		if(in.peek() == gzipMagic[1])
			isZipped = true;
	}
	in.unget();
	return isZipped;
}
Beispiel #3
0
			static bool isGzip(std::istream & in)
			{
				int const b0 = in.get();
				
				if ( b0 < 0 )
					return false;
				
				int const b1 = in.get();
				
				if ( b1 < 0 )
				{
					in.clear();
					in.putback(b0);
					return false;
				}
				
				in.clear();
				in.putback(b1);
				in.putback(b0);
				
				return b0 == libmaus2::lz::GzipHeaderConstantsBase::ID1 &&
				       b1 == libmaus2::lz::GzipHeaderConstantsBase::ID2;
			}
Beispiel #4
0
void HTMLForm::readUrl(std::istream& istr)
{
	static const int eof = std::char_traits<char>::eof();

	int fields = 0;
	int ch = istr.get();
	while (ch != eof)
	{
		if (_fieldLimit > 0 && fields == _fieldLimit)
			throw HTMLFormException("Too many form fields");
		std::string name;
		std::string value;
		while (ch != eof && ch != '=' && ch != '&')
		{
			if (ch == '+') ch = ' ';
			name += (char) ch;
			ch = istr.get();
		}
		if (ch == '=')
		{
			ch = istr.get();
			while (ch != eof && ch != '&')
			{
				if (ch == '+') ch = ' ';
				value += (char) ch;
				ch = istr.get();
			}
		}
		std::string decodedName;
		std::string decodedValue;
		URI::decode(name, decodedName);
		URI::decode(value, decodedValue);
		add(decodedName, decodedValue);
		++fields;
		if (ch == '&') ch = istr.get();
	}
}
Beispiel #5
0
void HTMLForm::readMultipart(std::istream& istr, PartHandler& handler)
{
	static const int eof = std::char_traits<char>::eof();

	MultipartReader reader(istr, _boundary);
	while (reader.hasNextPart())
	{
		MessageHeader header;
		reader.nextPart(header);
		std::string disp;
		NameValueCollection params;
		if (header.has("Content-Disposition"))
		{
			std::string cd = header.get("Content-Disposition");
			MessageHeader::splitParameters(cd, disp, params);
		}
		if (params.has("filename"))
		{
			handler.handlePart(header, reader.stream());
			// Ensure that the complete part has been read.
			while (reader.stream().good()) reader.stream().get();
		}
		else
		{
			std::string name = params["name"];
			std::string value;
			std::istream& istr = reader.stream();
			int ch = istr.get();
			while (ch != eof)
			{
				value += (char) ch;
				ch = istr.get();
			}
			add(name, value);
		}
	}
}
void JSSPageReader::nextToken(std::istream& istr, std::string& token)
{
	token.clear();
	int ch = istr.get();
	if (ch != -1)
	{
		if (ch == '<' && istr.peek() == '%')
		{
			token += "<%";
			ch = istr.get();
			ch = istr.peek();
			switch (ch)
			{
			case '%':
			case '@':
			case '=':
				ch = istr.get();
				token += (char) ch;
				break;
			case '!':
				ch = istr.get();
				token += (char) ch;
				if (istr.peek() == '!')
				{
					ch = istr.get();
					token += (char) ch;
				}
				break;
			case '-':
				ch = istr.get();
				token += (char) ch;
				if (istr.peek() == '-')
				{
					ch = istr.get();
					token += (char) ch;
				}
				break;
			}
		}
		else if (ch == '%' && istr.peek() == '>')
		{
			token += "%>";
			ch = istr.get();
		}
		else token += (char) ch;
	}
}
// style prompts for and accepts the style from input stream is
//
bool style(std::istream& is, char& s) {
	bool rc = false, ok = false;
	char c;
	do {
		std::cout << " EAN Style ('-', ' ', '\\n' or '0' to quit) : ";
		c = ' ';
		is.get(c);
		if (is.fail()) {
			is.clear();
			is.ignore(2000, '\n');
			std::cerr << " Invalid input. Try again.\n";
        } else if (c != '-' && c != ' ' && c != '\n' && c != '0') {
            is.ignore(2000, '\n');
            std::cerr << " Invalid Character.  Try again.\n";
		} else if (c == '0') {
			if (is.get() != '\n') {
				is.ignore(2000, '\n');
				std::cerr << " Trailing Characters.  Try Again.\n";
			} else {
				ok = true;
			}
		} else if (c == '\n') {
			ok = true;
			s = '\0';
			rc = true;
        } else if (is.get() != '\n') {
            is.ignore(2000, '\n');
            std::cerr << " Trailing Characters.  Try Again.\n";
        } else if (c == '-' || c == ' ') {
            ok = true;
			s = c;
			rc = true;
		}
	} while (!ok);

	return rc;
}
/** returns inner content of a quoted string
 *  Matches pattern "[^"]*"\s* and returns matching part without quotes
 *
 */
std::string 
configparBase::parse_quoted_string(std::istream &in) {

	if ( in.get() != '"' )
		throw configParExceptionParseError("string doesn't start with a quotation mark");

	bool escape = false;
	std::string tmp;
	char c;

	while ( in.get(c) ) {
		if ( escape ) {
			if ( c == '\\' || c == '"' )
				tmp += c;
			else
				throw configParExceptionParseError("invalid escape sequence");

			escape = false;
		}
		else {
			if ( c == '"' )
				break;
			else if ( c == '\\' )
				escape = true;
			else
				tmp += c;
		}
	}

	// we should be on the closing quotation mark
	if ( c != '"' )
		throw configParExceptionParseError("unterminated string");

	skip_rest_of_line(in);

	return tmp;
}
Beispiel #9
0
bool GetField(std::istream &is, std::string &name, std::string &value)
{
	name.resize(0);		// GCC workaround: 2.95.3 doesn't have clear()
	is >> name;
	if (name.empty())
		return false;

	if (name[name.size()-1] != ':')
		SignalTestError();
	name.erase(name.size()-1);

	while (is.peek() == ' ')
		is.ignore(1);

	// VC60 workaround: getline bug
	char buffer[128];
	value.resize(0);	// GCC workaround: 2.95.3 doesn't have clear()
	bool continueLine;

	do
	{
		do
		{
			is.get(buffer, sizeof(buffer));
			value += buffer;
		}
		while (buffer[0] != 0);
		is.clear();
		is.ignore();

		if (!value.empty() && value[value.size()-1] == '\r')
			value.resize(value.size()-1);

		if (!value.empty() && value[value.size()-1] == '\\')
		{
			value.resize(value.size()-1);
			continueLine = true;
		}
		else
			continueLine = false;

		std::string::size_type i = value.find('#');
		if (i != std::string::npos)
			value.erase(i);
	}
	while (continueLine);

	return true;
}
Beispiel #10
0
// Parses a string serialized by serializeJsonStringIfNeeded.
static std::string deSerializeJsonStringIfNeeded(std::istream &is)
{
	std::ostringstream tmp_os;
	bool expect_initial_quote = true;
	bool is_json = false;
	bool was_backslash = false;
	for(;;)
	{
		char c = is.get();
		if(is.eof())
			break;
		if(expect_initial_quote && c == '"')
		{
			tmp_os << c;
			is_json = true;
		}
		else if(is_json)
		{
			tmp_os << c;
			if(was_backslash)
				was_backslash = false;
			else if(c == '\\')
				was_backslash = true;
			else if(c == '"')
				break; // Found end of string
		}
		else
		{
			if(c == ' ')
			{
				// Found end of word
				is.unget();
				break;
			}
			else
			{
				tmp_os << c;
			}
		}
		expect_initial_quote = false;
	}
	if(is_json)
	{
		std::istringstream tmp_is(tmp_os.str(), std::ios::binary);
		return deSerializeJsonString(tmp_is);
	}
	else
		return tmp_os.str();
}
static std::string goto_first_of(std::istream& s, const std::string& str)
{
	std::string result;
	char c;
	while (s.get(c))
	{
		if (str.find(c) != std::string::npos)
		{
			break;
		}
		result += c;
	}
	
	return result;
}
        void read_with_limit(
            std::istream& in, 
            std::string& buffer, 
            int delim = '\n'
        ) 
        {
            using namespace std;
            const size_t max = 16*1024;
            buffer.clear();
            buffer.reserve(300);

            while (in.peek() != delim && in.peek() != EOF && buffer.size() < max)
            {
                buffer += (char)in.get();
            }

            // if we quit the loop because the data is longer than expected or we hit EOF
            if (in.peek() == EOF || buffer.size() == max)
                throw http_parse_error("HTTP field from client is too long", 414);

            // Make sure the last char is the delim.
            if (in.get() != delim) 
            {
                in.setstate(ios::badbit);
                buffer.clear();
            } 
            else 
            {
                // Read the remaining delimiters
                if (delim == ' ') 
                {
                    while (in.peek() == ' ')
                        in.get();
                }
            }
        }
Beispiel #13
0
std::map<char, long long> readHuffmanHeader(std::istream &inFile, int *validBitsLastByte) {
	long long length, frequency;
	char space, character, space2, algorithm;
	map<char, long long> char_freq;
	inFile >> skipws;
	inFile >> (*validBitsLastByte);
	if (DEBUG) cout << " + validBitsLastByte: " << *validBitsLastByte <<endl;
	inFile >> length;
	if (DEBUG) cout << " + length: " << length << endl;

	inFile.get(space);
	if (DEBUG) cout << " - It was suppose to be a space: [" << space << "]\n";

	for (int i = 0; i < length; i++) {
		inFile.get(character);
		inFile.get(space);
		inFile >> frequency;
		inFile.get(space2);
		if (DEBUG) cout << "character: " << character << ", space: "<< space << ", frequency: "<< frequency << ", space2: " << space2 <<endl;
		if (DEBUG) cout << "char_freq["<<character<<"] : " << frequency << endl;
		char_freq[character] = frequency;
	}
	return char_freq;
}
void
PinConfigInfoPacket::read(
        std::istream& inputStream
        )
{
    int pin;
    int direction;

    pin = inputStream.get();
    if (inputStream.good() != true)
    {
        return;
    }
    myPin = (unsigned int)pin;

    direction = inputStream.get();
    if (inputStream.good() != true)
    {
        return;
    }
    myDirection = ((direction == 1) ? DIR_OUTPUT : DIR_INPUT);

    inputStream.get();
}
// throw an exception if the rest of the line doesn't only contain whitespace or a comment starting with #
void 
configparBase::skip_rest_of_line(std::istream &in) {
	char c;
	bool ignore= false;
	while ( in.get(c) ) {
		if (ignore)
			continue;
		else
		if (c == '#')
			ignore= true;
		else
		if ( c != ' ' && c != '\t' )
			throw configParExceptionParseError("junk after value");
	}
}
Beispiel #16
0
bool putback_test_one(std::istream& is)
{
    try {
        do {
            char buf[chunk_size];
            is.read(buf, chunk_size);
            if (is.gcount() < static_cast<std::streamsize>(chunk_size))
                break;
            is.putback('a');
            if (is.get() != 'a')
                return false;
        } while (!is.eof());
        return true;
    } catch (std::exception&) { return false; }
}
Beispiel #17
0
load_line_ret load_line(std::string& s, std::istream& is) {
    s.erase();
    if (is.bad()|| is.eof())
        return FOUND_EOF;

    char c;
    while (is.get(c)) {
        if (c == '\n')
            return FOUND_NL;
        if (c != '\r')
            s.append(1, c);
    }

    return FOUND_END;
}
Beispiel #18
0
bool get_word(std::string& output_string, std::istream& input_stream)
{
	skip_emptyspace(input_stream);
	char c = input_stream.peek();
	while ( !isspace(c) 
			&& '\n' != c 
			&& '\r' != c 
			&& input_stream.good() )
	{
		output_string += c;
		input_stream.get();
		c = input_stream.peek();
	}
	return input_stream.good();
}
Beispiel #19
0
bool input(std::istream &in, std::string fmt)
{
    // Just match fmt
    for(unsigned i = 0; i < fmt.size(); ++i)
    {
        char c;

        in.get(c);
        if(c != fmt[i])
        {
            in.setstate(std::ios::failbit);
            return false;
        }
    }
}
Beispiel #20
0
std::string parseEscapeSequence(std::istream& ss) {
    auto ch = ss.get();
    switch (ch) {
    case '"': return "\"";
    case '\\': return "\\";
    case '/': return "/";
    case 'b': return "\b";
    case 'f': return "\f";
    case 'n': return "\n";
    case 'r': return "\r";
    case 't': return "\t";
    case 'u': return parseUnicodeEscapeSequence(ss);
    default: throw ParseError(std::string("unexpected escape sequence: '\\")+char(ch)+"'");
    }
}
Beispiel #21
0
void parser::parse_expr( std::istream &in )
{
	precondition( in.get() == '[', "missing '[' to start expression" );
	int count = 0;
	while ( !in.eof() && in )
	{
		int c = in.get();
		if ( std::char_traits<char>::not_eof( c ) )
		{
			if ( c == ']' )
			{
				if ( count == 0 )
				{
					_func.push_expr();
					break;
				}
				_func.add( static_cast<char>( c ) );
				--count;
			}
			else if ( c == '[' )
			{
				_func.add( static_cast<char>( c ) );
				++count;
			}
			else if ( c == '"' )
			{
				_func.add( static_cast<char>( c ) );
				parse_string( in );
			}
			else
				_func.add( static_cast<char>( c ) );
		}
	}
	if ( in.get() != ']' )
		throw_runtime( "missing ']' in expression" );
}
Beispiel #22
0
// does the work for the keychange
void keyChange(std::istream& input)
{
   //input.get(); // forward 1;
   while(input)
   {
      char note;
      input >> note;// = input.get();
      if(!input)
         break;
      const char nextChar = input.get();
//      input >> nextChar;// = input.get();
      if (nextChar == 'b') // flatten
      {
         setNote(note, FLAT);
         //input.get(); // forward 1 to skip space
         continue;
      }
      else if(nextChar == '#') // sharpen
      {
         setNote(note, SHARP);
         //input.get(); // forward 1 to skip space
         continue;
      }
      // now nextChar can only equal space
      else if (!std::isspace(nextChar))
      {
         std::cout << "Unrecognised character of '" << nextChar << "' in keychage.\n";
         return;
      }
      const char thirdChar = input.peek();
      if (std::isdigit(thirdChar)) // a pitch is being set
      {
         double newFreq;
         input >> newFreq;
         if(input.fail())
         {
            std::cout << "Failed to read frequency in keychange.\n";
            return;
         }
         //input.get(); // forward 1
         setNote(note,newFreq);
      }
      else // must be reseting to natural
      {
         setNote(note,NATURAL);
         continue;
      }
   }
 inline bool
 AsciiXmlParser::readCharacter()
 /* this reads the next character from textStream into currentChar &
  * increments readNewlines if currentChar was '\n', returning
  * textStream->good().
  */
 {
   streamIsGood = textStream->get( currentChar ).good();
   if( streamIsGood
       &&
       ( '\n' == currentChar ) )
   {
     ++readNewlines;
   }
   return streamIsGood;
 }
 // This puts the next character from xmlStream into currentChar, then puts
 // it into destinationForReadCharacters if it is not NULL, then returns
 // true, unless no character could be read, in which case false is
 // returned.
 inline bool RestrictedXmlParser::ReadCharacter(
                                   std::ostream* destinationForReadCharacter )
 {
   if( xmlStream->get( currentCharacter ).good() )
   {
     if( destinationForReadCharacter != NULL )
     {
       (*destinationForReadCharacter) << currentCharacter;
     }
     return true;
   }
   else
   {
     return false;
   }
 }
Beispiel #25
0
	InputFormat Input_t::detectFormat(std::istream& in) {
		std::istream::int_type x = std::char_traits<char>::eof();
		while (in && (x = in.peek()) != std::char_traits<char>::eof()) {
			unsigned char c = static_cast<unsigned char>(x);
			if (c >= '0' && c <= '9') return Problem_t::LPARSE;
			if (c == 'c' || c == 'p') return Problem_t::DIMACS;
			if (c == '*')             return Problem_t::OPB;
			if (c == ' ' || c == '\t') { in.get(); continue; }
			break;
		}
		char msg[] = "'c': Unrecognized input format!\n";
		msg[1] = (char)(unsigned char)x;
		in && x != std::char_traits<char>::eof()
			? throw ParseError(1, msg)
			: throw ParseError(0, "Bad input stream!\n");
	}
std::string ObjectFactory::GetValue(std::istream& iStream)
{
   char ch = '\0';
   std::string sValue;

   while (ch != '<' && iStream.good())
   {
      iStream.get(ch);
      if (ch!='<')
         sValue += ch;
   }

   if (ch=='<') iStream.putback(ch);

   return sValue;
}
Beispiel #27
0
	void ReadForCharacter(char value, std::istream& stream)
	{
		for (;;)
		{
			char ch = 0;
			stream.get(ch);
			if (value == ch)
			{
				break;
			}
			if (0 == ch)
			{
				throw runtime_error("ReadForCharacter() end of stream has been reached");
			}
		}
	}
Beispiel #28
0
void compressAdaptive(std::istream& in, std::ostream& out) {
	const char* header = "AC\x00";
	out.write(header, 3);

	ArithmeticEncoder encoder(std::make_shared<BitStreamWriter>(&out));
	AdaptiveDataModel dataModel(NUM_SYMBOLS);
	for (;;) {
		int c = in.get();
		if (c == std::char_traits<char>::eof()) {
			encoder.encode(NUM_SYMBOLS - 1, &dataModel);
			break;
		}

		encoder.encode(c, &dataModel);
	}
}
void HTTPBodyResultMapper::ReadBody(String &body, std::istream &is, Context &ctx) {
	StartTrace(HTTPBodyResultMapper.ReadBody);

	long contentLength = ctx.Lookup("Mapper.content-length", -1L);
	Trace("contentLength: " << contentLength);
	if (contentLength > -1) {
		body.Append(is, contentLength);
	} else {
		char c;
		while (is.get(c).good()) {
			body.Append(c);
		}
	}
	Trace("Body[" << body.Length() << "]");
	Trace("<" << body << ">");
}
Beispiel #30
0
		static Value eatNumeric(std::istream& stream, char firstChar)
		{
			std::string numeric(1, firstChar);
			while (!stream.eof())
			{
				char token = stream.peek();
				if ((token >= '0' && token <= '9') || token == '.' || token == '-' || token == 'E')
					numeric += stream.get();
				else
					break;
			}
			if (numeric.find('.') == std::string::npos)
				return Value(atoi(numeric.c_str()));
			else
				return Value((float)atof(numeric.c_str()));
		};