Beispiel #1
0
void StarsNode::unpackState(std::streambuf & in) {
    uint32_t size;
    in.sgetn((char *)&size, 4);
    msgpack::unpacker upk;
    upk.reserve_buffer(size);
    in.sgetn(upk.buffer(), size);
    upk.buffer_consumed(size);
    MsgpackInArchive ar(upk);
    ar & power & mem & disk & static_cast<SimOverlayBranch &>(getBranch()) & static_cast<SimOverlayLeaf &>(getLeaf());
    switch (Configuration::getInstance().getPolicy()) {
        case Configuration::IBPolicy:
            static_cast<IBPDispatcher &>(getDisp()).serializeState(ar);
            break;
        case Configuration::MMPolicy:
            static_cast<MMPDispatcher &>(getDisp()).serializeState(ar);
            break;
        case Configuration::DPolicy:
            static_cast<DPDispatcher &>(getDisp()).serializeState(ar);
            break;
        case Configuration::FSPolicy:
            static_cast<FSPDispatcher &>(getDisp()).serializeState(ar);
            break;
        default:
            break;
    }
}
	std::streambuf::int_type SolidusEscaper::operator()(std::streambuf &destination,
	                                                    std::streambuf::int_type character) {
		bool notEscaped = true;
		std::streambuf::char_type tmpChar = std::streambuf::traits_type::to_char_type(character);

		// If we encounter a quotation mark.
		if (tmpChar == Strings::Json::Escape::QUOTATION_MARK) {
			// If we're not in a string, we change that. If we're in a string,
			// we change that only if we're not after an escape back slash.
			inString = !inString || (afterBackSlash);

		} else if (inString && !afterBackSlash) {
			// If we are in a string definition and we're not after a backslash
			// escape.
			if (tmpChar == Strings::Std::SOLIDUS) {
				destination.sputn(Strings::Json::SOLIDUS.c_str(), Strings::Json::SOLIDUS.size());
				notEscaped = false;

			}

		}

		// We determine if we start a backslash escape or not.
		afterBackSlash = inString && !afterBackSlash && (tmpChar == Strings::Json::Escape::BEGIN_ESCAPE);
		return (notEscaped) ? (destination.sputc(tmpChar)) : (0);
	}
Beispiel #3
0
void StarsNode::packState(std::streambuf & out) {
    msgpack::sbuffer buffer;
    msgpack::packer<msgpack::sbuffer> pk(&buffer);
    MsgpackOutArchive ar(pk);
    ar & power & mem & disk & static_cast<SimOverlayBranch &>(getBranch()) & static_cast<SimOverlayLeaf &>(getLeaf());
    switch (Configuration::getInstance().getPolicy()) {
        case Configuration::IBPolicy:
            static_cast<IBPDispatcher &>(getDisp()).serializeState(ar);
            break;
        case Configuration::MMPolicy:
            static_cast<MMPDispatcher &>(getDisp()).serializeState(ar);
            break;
        case Configuration::DPolicy:
            static_cast<DPDispatcher &>(getDisp()).serializeState(ar);
            break;
        case Configuration::FSPolicy:
            static_cast<FSPDispatcher &>(getDisp()).serializeState(ar);
            break;
        default:
            break;
    }
    uint32_t size = buffer.size();
    out.sputn((const char *)&size, 4);
    out.sputn(buffer.data(), size);
}
Beispiel #4
0
    static std::string parseXMLIdentifier(std::streambuf& buffer)
    {
        std::stringstream identifier;
        {
            char c=buffer.sgetc();
            if((c<'A' || c>'Z') && (c<'a' || c>'z'))
            {
                throw XMLParseException();
            }
            buffer.snextc();
            identifier<<c;
        }
        for(;;)
        {
            char c=buffer.sgetc();
            if((c<'0' || c>'9') && (c<'A' || c>'Z') && (c<'a' || c>'z'))
            {
                break;
            }
            buffer.snextc();
            identifier<<c;
        }
        return identifier.str();

    }
Beispiel #5
0
 static void parseXMLSkipSpace(std::streambuf& buffer)
 {
     while(buffer.sgetc()==' ' || buffer.sgetc()=='\n' || buffer.sgetc()=='\r')
     {
         buffer.snextc();
     }
 }
Beispiel #6
0
/**
   read whole contents of streambuf 'buf'

   \return size of data malloced and copied into \param[out] buffer unless size
   is 0 (in which case buffer is set to NULL for convenience, so you can blindly free)

   requires put pointer is at end for determining size (e.g. just-written iostream)
*/
inline std::size_t read_streambuf_malloc(std::streambuf& buf, void*& buffer) {
  typedef std::streambuf::pos_type Pos;
  typedef std::streambuf::off_type Off;
  Pos sz = buf.pubseekoff(0, std::ios::end, std::ios_base::out);
  if (sz > 0 && seek_ok(sz) && seek_ok(buf.pubseekpos(0, std::ios_base::in)))
    return buf.sgetn((char*)(buffer = std::malloc(sz)), sz);
  else {
    buffer = 0;
    return 0;
  }
}
Beispiel #7
0
	std::streambuf::int_type Indenter::operator()(std::streambuf &destination,
	                                              std::streambuf::int_type character) {
		std::streambuf::char_type tmpChar = std::streambuf::traits_type::to_char_type(character);

		if (atStartOfLine && tmpChar != Whitespace::NEW_LINE) {
			destination.sputc(Whitespace::HORIZONTAL_TAB);
		}

		atStartOfLine = (tmpChar == Whitespace::NEW_LINE);
		return destination.sputc(tmpChar);
	}
Beispiel #8
0
    std::size_t HeaderParser::advance(std::streambuf& sb)
    {
        std::size_t ret = 0;

        while (sb.in_avail() > 0)
        {
            ++ret;
            if (parse(sb.sbumpc()))
                return ret;
        }

        return ret;
    }
    virtual std::streampos seekoff (std::streamoff off, std::ios_base::seekdir way,
                   std::ios_base::openmode which = std::ios_base::in)
    {
        std::streamoff newpos;
        if ( way == std::ios_base::beg )
        {
            newpos = off;
        }
        else if ( way == std::ios_base::cur )
        {
            newpos = _curPos + off;
        }
        else if ( way == std::ios_base::end )
        {
            newpos = _numChars + off;
        }
        else
        {
            return -1;
        }

        if ( newpos<0 || newpos>_numChars ) return -1;
        if ( ARCHIVE_POS(_streambuf->pubseekpos( STREAM_POS(_startPos+newpos), which)) < 0 ) return -1;
        _curPos = newpos;
        return _curPos;
    }
Beispiel #10
0
 XMLNode_SPtr parseXML(std::streambuf& buffer)
 {
     std::cout<<"Parse Start"<<std::endl;
     parseXMLSkipSpace(buffer);
     char c=buffer.sgetc();
     if(c=='<')
     {
         buffer.snextc();
         std::cout<<"Parse Node"<<std::endl;
         XMLNode_SPtr current=parseXMLNode(buffer);
         std::cout<<"Something"<<std::endl;
         return current;
     }
     std::cout<<"Nothing"<<std::endl;
     return XMLNode_SPtr();
 }
Beispiel #11
0
 void reset() {
   if (size) {
     if (resetbuf)
       resetbuf->pubsetbuf(0, 0); // this should flush if old buffer wasn't flushed yet
     ::operator delete(buf);
     size = 0;
   }
 }
Beispiel #12
0
Ttamanio Bloque::serializar(std::streambuf&salida)const{
	unsigned char nrocomponetes=componentes.size();
	Ttamanio offset=sizeof(unsigned char);
	salida.sputn((char*)&nrocomponetes,offset);
	for(Ttamanio i=0;i<nrocomponetes;i++){
		offset+=componentes.at(i)->serializar(salida);
	}
	return offset;
}
Beispiel #13
0
 int operator()(std::streambuf& sbuf, int c)
 {
     int result = eof();
     if (c != eof() && new_line)
     {
         std::ostream(&sbuf) << ++line_number << ": ";
     }
     new_line = (c == '\n');
     
     return result != eof() ? result : sbuf.sputc(c);
 }
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_single_scenario_campaign_file_header::read(
	std::streambuf &		stream,
	t_progress_handler *	progress_handler_ptr )
{
	m_map_data_start = stream.pubseekoff( 0, std::ios::cur );
	m_map_data_end = stream.pubseekoff( 0, std::ios::end );
	stream.pubseekpos( m_map_data_start );

	try
	{
		t_inflate_filter inflater( stream );
		if ( !::read( inflater, *m_map_header_ptr, progress_handler_ptr ) ) 
			return false;
	}
	catch ( t_inflate_filter::t_data_error const & )
	{
		return false;
	}
	return true;
}
//////////////////////////////////////////////////////////////////////////////
// STATIC
void
read(std::streambuf & istrm, void * dataIn, size_t dataInLen)
{
	std::streamsize cnt = dataInLen;
#ifdef BLOCXX_WIN32
// VC10 warns that sgetn is unsafe.
#pragma warning(push)
#pragma warning(disable:4996)
#endif
	if (istrm.sgetn(static_cast<char *>(dataIn), cnt) != cnt)
#ifdef BLOCXX_WIN32
#pragma warning(pop)
#endif
	{
		BLOCXX_THROW(IOException, "Failed reading data");
	}
}
Beispiel #16
0
	std::streambuf::int_type Escaper::operator()(std::streambuf &destination,
	                                             std::streambuf::int_type character) {
		bool notEscaped = true;
		std::streambuf::char_type tmpChar = std::streambuf::traits_type::to_char_type(character);

		// If we encounter a quotation mark.
		if (tmpChar == Structural::BEGIN_END_STRING) {
			// If we're not in a string, we change that. If we're in a string,
			// we change that only if we're not after an escape back slash.
			inString = !inString || (afterBackSlash);

		} else if (inString && !afterBackSlash) {
			// If we are in a string definition and we're not after a backslash
			// escape.
			if (tmpChar == Strings::Std::REVERSE_SOLIDUS) {
				destination.sputn(Strings::Json::REVERSE_SOLIDUS.c_str(), Strings::Json::REVERSE_SOLIDUS.size());
				notEscaped = false;

			} else if (tmpChar == Strings::Std::BACKSPACE) {
				destination.sputn(Strings::Json::BACKSPACE.c_str(), Strings::Json::BACKSPACE.size());
				notEscaped = false;

			} else if (tmpChar == Strings::Std::FORM_FEED) {
				destination.sputn(Strings::Json::FORM_FEED.c_str(), Strings::Json::FORM_FEED.size());
				notEscaped = false;

			} else if (tmpChar == Strings::Std::LINE_FEED) {
				destination.sputn(Strings::Json::LINE_FEED.c_str(), Strings::Json::LINE_FEED.size());
				notEscaped = false;

			} else if (tmpChar == Strings::Std::TAB) {
				destination.sputn(Strings::Json::TAB.c_str(), Strings::Json::TAB.size());
				notEscaped = false;

			} else if (tmpChar >= '\0' && tmpChar <= '\x1f') {
				std::string tmp(Value::escapeToUnicode(tmpChar));
				destination.sputn(tmp.c_str(), tmp.size());
				notEscaped = false;
			}

		}

		// We determine if we start a backslash escape or not.
		afterBackSlash = inString && !afterBackSlash && (tmpChar == Strings::Json::Escape::BEGIN_ESCAPE);
		return (notEscaped) ? (destination.sputc(tmpChar)) : (0);
	}
    virtual int_type underflow()
    {
        // Return current character.

        if ( gptr() == &_oneChar ) return traits_type::to_int_type(_oneChar);

        // Get another character from the archive stream, if available.

        if ( _curPos==_numChars ) return traits_type::eof();
         _curPos += 1;

        int_type next_value = _streambuf->sbumpc();

        if ( !traits_type::eq_int_type(next_value,traits_type::eof()) )
        {
            setg(&_oneChar, &_oneChar, (&_oneChar)+1);
            _oneChar = traits_type::to_char_type(next_value);
        }

        return next_value;
    }
Beispiel #18
0
Ttamanio Bloque::deserializar(std::streambuf&entrada){
	unsigned char nrocomponetes;
	Ttamanio offset=sizeof(unsigned char);
	entrada.sgetn((char*)&nrocomponetes,offset);
	Ttamanio i=0;
	while(i<nrocomponetes and i<componentes.size()){
		offset+=componentes.at(i)->deserializar(entrada);
		i++;
	}
	/*le sobran componentes*/
	while(i<componentes.size()){
		delete componentes.at(i);
		componentes.erase(componentes.begin()+i);
	}
	/*le faltan componentes*/
	Componente* auxiliar;
	while(i<nrocomponetes){
		auxiliar=componentes.at(0)->clonar();
		offset+=auxiliar->deserializar(entrada);
		componentes.push_back(auxiliar);
		i++;
	}
	return offset;
}
Beispiel #19
0
    static XMLNode_SPtr parseXMLNode(std::streambuf& buffer)
    {
        // 1. Read Symbol name
        parseXMLSkipSpace(buffer);
        std::string id=parseXMLIdentifier(buffer);
        std::cout<<"Parse Id:"<<id<<std::endl;
        std::list<XMLNode_SPtr> children;
        std::string text;

        parseXMLSkipSpace(buffer);

        for(;;)
        {
            char c=buffer.sgetc();
            char c2=buffer.snextc();
            if((c=='/') && (c2=='>'))
            {
                buffer.snextc();
                goto completeNode;
            }
            if(c=='>')
            {
                goto parseText;
            }
            // TODO: Parse attribute
        }
    parseText:
        std::cout<<"Parse Text"<<std::endl;
        {
            std::stringstream textbuffer;
            parseXMLSkipSpace(buffer);
            for(;;)
            {
                char c=buffer.sgetc();
                char c2=buffer.snextc();
                if(c!='<')
                {
                    textbuffer<<c;
                }
                else
                {
                    if(c2=='/')
                    {
                        buffer.snextc();
                        break;
                    }
                    // TODO: There may be other cases.
                    children.push_back(parseXMLNode(buffer));
                }
            }
            text=textbuffer.str();
        }
        {
            std::string::reverse_iterator trimpos=text.rbegin();
            while((trimpos!=text.rend()) && (*trimpos==' ' || *trimpos=='\n' || *trimpos=='\r'))
            {
                trimpos++;
            }
            text.erase(trimpos.base(),text.end());
        }
        std::cout<<"Parse end"<<std::endl;
        {
            std::string endidentifier=parseXMLIdentifier(buffer);
            if(endidentifier!=id)
            {
                throw XMLParseException();
            }
            parseXMLSkipSpace(buffer);
            char c=buffer.sgetc();
            if(c!='>')
            {
                throw XMLParseException();
            }
            buffer.snextc();
        }
    completeNode:
        std::cout<<"Complete"<<std::endl;
        // Complete...just build and return it.
        XMLNode_SPtr node(new XMLNode(id, children, text));
        return node;
    }
Beispiel #20
0
 virtual int_type underflow() override {
   ASSERT(_streambuf);
   return _streambuf->sgetc();
 }
Beispiel #21
0
 virtual std::streamsize xsputn(const char *pBuffer, std::streamsize sz) override {
   ASSERT(_streambuf);
   return _streambuf->sputn(pBuffer, sz);
 }
Beispiel #22
0
 virtual pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode mode) override {
   ASSERT(_streambuf);
   return _streambuf->pubseekoff(off, way, mode);
 }
Beispiel #23
0
 virtual pos_type seekpos(pos_type pos, std::ios_base::openmode mode) override {
   ASSERT(_streambuf);
   return _streambuf->pubseekpos(pos, mode);
 }
Beispiel #24
0
 virtual int sync() override {
   ASSERT(_streambuf);
   return _streambuf->pubsync();
 }
Beispiel #25
0
 virtual int uflow() override {
   ASSERT(_streambuf);
   return _streambuf->sbumpc();
 }
Beispiel #26
0
bool Responder::advance(std::streambuf& in)
{
    std::streambuf::int_type chi;
    while ((chi = in.sgetc()) != std::streambuf::traits_type::eof())
    {
        char ch = std::streambuf::traits_type::to_char_type(chi);
        switch (_state)
        {
            case state_0:
                log_debug("new rpc request");

                if (ch == '\xc0')
                    _state = state_method;
                else if (ch == '\xc3')
                    _state = state_domain;
                else
                    throw std::runtime_error("domain or method name expected");
                in.sbumpc();
                break;

            case state_domain:
                if (ch == '\0')
                {
                    log_info_if(!_domain.empty(), "rpc method domain \"" << _domain << '"');
                    _state = state_method;
                }
                else
                    _domain += ch;
                in.sbumpc();
                break;

            case state_method:
                if (ch == '\0')
                {
                    log_info("rpc method \"" << _methodName << '"');

                    _proc = _serviceRegistry.getProcedure(_domain.empty() ? _methodName : _domain + '\0' + _methodName);

                    if (_proc)
                    {
                        _args = _proc->beginCall();
                        _state = state_params;
                    }
                    else
                    {
                        _failed = true;
                        _errorMessage = "unknown method \"" + _methodName + '"';
                        _state = state_params_skip;
                    }

                    _methodName.clear();
                    _domain.clear();
                }
                else
                    _methodName += ch;
                in.sbumpc();
                break;

            case state_params:
                if (ch == '\xff')
                {
                    if (_args && *_args)
                    {
                        _failed = true;
                        _errorMessage = "argument expected";
                    }

                    in.sbumpc();
                    return true;
                }
                else
                {
                    if (_args == 0 || *_args == 0)
                    {
                        _failed = true;
                        _errorMessage = "too many arguments";
                        _state = state_params_skip;
                    }
                    else
                    {
                        _deserializer.begin(false);
                        _state = state_param;
                    }
                }
                break;

            case state_params_skip:
                if (ch == '\xff')
                {
                    in.sbumpc();
                    return true;
                }
                else
                {
                    _deserializer.skip();
                    _state = state_param_skip;
                }

                break;

            case state_param:
                if (_deserializer.advance(in))
                {
                    try
                    {
                        (*_args)->fixup(_deserializer.si());
                        ++_args;
                        _state = state_params;
                    }
                    catch (const std::exception& e)
                    {
                        _failed = true;
                        _errorMessage = e.what();
                        _state = state_params_skip;
                    }
                }
                break;

            case state_param_skip:
                if (_deserializer.advance(in))
                    _state = state_params_skip;

                break;
        }
    }

    return false;
}
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_multi_scenario_campaign_file_header::read(
	std::streambuf &		stream,
	t_progress_handler *	progress_handler_ptr )
{
	int const k_current_format_version = 1;

	int format_version = get< t_uint16 >( stream );
	if ( format_version < 0 || format_version > k_current_format_version )
		return false;

	std::string new_name;
	std::string new_description;
	if (	!read_string16( stream, new_name )
		||	( format_version >= 1 && !read_string16( stream, new_description ) ) )
		return false;

	int map_count = get< t_uint16 >( stream );
	if ( map_count <= 0 )
		return false;

	std::vector< std::streambuf::off_type > map_data_sizes( map_count );
	std::vector< t_map_info > new_map_info_vector( map_count );

	int map_num;
	for ( map_num = 0; map_num < map_count; ++map_num )
		map_data_sizes[ map_num ] = get< t_uint32 >( stream );

	std::streambuf::pos_type start_pos = stream.pubseekoff( 0, std::ios::cur );
	map_num = 0;
	while( true )
	{
		std::streambuf::pos_type end_pos = start_pos + map_data_sizes[ map_num ];

		t_map_info & map_info = new_map_info_vector[ map_num ];
		map_info.data_start = start_pos;
		map_info.data_end = end_pos;
		map_info.header_ptr = new t_map_header;

		stream.pubseekpos( start_pos );
		try
		{
			t_inflate_filter inflater( stream );
			if ( !::read( inflater, *map_info.header_ptr, progress_handler_ptr ) ) 
				return false;
		}
		catch ( t_inflate_filter::t_data_error const & )
		{
			return false;
		}

		if ( ++map_num >= map_count )
			break;

		start_pos = end_pos;
	}

	m_name.swap( new_name );
	m_description.swap( new_description );
	m_map_info_vector.swap( new_map_info_vector );

	return true;
}
 /**
  * Flushes destination streambuf
  * 
  * @return number of bytes flushed
  */
 std::streamsize flush() {
     return streambuf->pubsync();
 }
	std::streambuf::int_type IndentCanceller::operator()(std::streambuf &destination,
	                                                     std::streambuf::int_type character) {
		std::streambuf::char_type tmpChar = std::streambuf::traits_type::to_char_type(character);

		// If we encounter a quotation mark.
		if (tmpChar == Structural::BEGIN_END_STRING) {
			// If we're not in a string, we change that. If we're in a string,
			// we change that only if we're not after an escape back slash.
			inString = !inString || (afterBackSlash);
		}

		// We determine if we start a backslash escape or not.
		afterBackSlash = inString && !afterBackSlash && (tmpChar == Strings::Json::Escape::BEGIN_ESCAPE);

		return (tmpChar != Whitespace::NEW_LINE && tmpChar != Whitespace::HORIZONTAL_TAB && tmpChar != Whitespace::CARRIAGE_RETURN && (inString || tmpChar != Whitespace::SPACE)) ? (destination.sputc(tmpChar)) : (0);
	}
 /**
  * Write implementation delegated to the underlying streambuf
  * 
  * @param buffer source buffer
  * @param length number of bytes to process
  * @return number of bytes processed
  */
 std::streamsize write(const char* buffer, std::streamsize length) {
     return streambuf->sputn(buffer, length);
 }