_CRTIMP2 int strstreambuf::underflow()
	{	// read only if read position available
	if (gptr() == 0)
		return (EOF);
	else if (gptr() < egptr())
		return ((unsigned char)*gptr());
	else if (pptr() == 0
		|| pptr() <= gptr() && _Seekhigh <= gptr())
		return (EOF);
	else
		{	// update _Seekhigh and expand read region
		if (_Seekhigh < pptr())
			_Seekhigh = pptr();
		setg(eback(), gptr(), _Seekhigh);
		return ((unsigned char)*gptr());
		}
	}
int string_parsebuf::underflow()
{
    register char* ptr = egptr(); // Point to end of current_line
    do {
	int i = right() - ptr;
	if (i <= 0)
	    return EOF;
	ptr++; i--; // Skip '\n'.
	char *line_start = ptr;
	while (ptr < right() && *ptr == '\n') ptr++;
	setg(line_start-1, line_start, ptr + (ptr < right()));
	pos_at_line_start = line_start - left();
	_line_length = ptr - line_start;
	__line_number++;
    } while (gptr() == ptr);
    return *gptr();
}
Exemple #3
0
	std::streampos streambuf::seekoff(	std::streamoff off,
						std::ios_base::seekdir seekdir,
						std::ios_base::openmode /*m*/)
	{
		if(sync())
			return -1;
		if(!buffer_in_.empty()) {
			setg(0,0,0);
		}
		if(seekdir == std::ios_base::cur)
			return device().seek(off,io_device::cur);
		if(seekdir == std::ios_base::beg)
			return device().seek(off,io_device::set);
		if(seekdir == std::ios_base::end)
			return device().seek(off,io_device::end);
		return -1;
	}
			int_type underflow()
			{
				// if there is still data, then return it
				if ( gptr() < egptr() )
					return static_cast<int_type>(*(reinterpret_cast<uint8_t const *>(gptr())));

				assert ( gptr() == egptr() );

				// number of bytes for putback buffer
				uint64_t const putbackcopy = std::min(
					static_cast<uint64_t>(gptr() - eback()),
					putbackspace
				);
				
				// copy bytes
				#if 0
				std::copy(
					gptr()-putbackcopy,
					gptr(),
					buffer.begin() + putbackspace - putbackcopy
				);
				#endif
				std::memmove(
					buffer.begin() + putbackspace - putbackcopy,
					gptr()-putbackcopy,
					putbackcopy
				);
				
				// load data
				uint64_t const uncompressedsize = stream.readPart(
						buffer.begin()+putbackspace,
						buffer.size()-putbackspace
					);

				// set buffer pointers
				setg(
					buffer.begin()+putbackspace-putbackcopy,
					buffer.begin()+putbackspace,
					buffer.begin()+putbackspace+uncompressedsize
				);

				if ( uncompressedsize )
					return static_cast<int_type>(*(reinterpret_cast<uint8_t const *>(gptr())));
				else
					return traits_type::eof();
			}
Exemple #5
0
ProcReader*
ProcReader::open(const char* fileName, const char* mode)
{
	std::string fullName = "/proc/";
	fullName.append(fileName);

	fFD = ::open(fullName.c_str(), O_RDONLY);
	if (fFD < 0)
		return NULL;

	fBuffer = new char[512];
	if (fBuffer == NULL)
		return NULL;

	setg(fBuffer, fBuffer, fBuffer);
	return this;
}
 /// Move-assign a basic_socket_streambuf from another.
 basic_socket_streambuf& operator=(basic_socket_streambuf&& other)
 {
   this->close();
   socket() = std::move(other.socket());
   detail::socket_streambuf_io_context::operator=(other);
   ec_ = other.ec_;
   expiry_time_ = other.expiry_time_;
   get_buffer_.swap(other.get_buffer_);
   put_buffer_.swap(other.put_buffer_);
   setg(other.eback(), other.gptr(), other.egptr());
   setp(other.pptr(), other.epptr());
   other.ec_ = asio::error_code();
   other.expiry_time_ = max_expiry_time();
   other.put_buffer_.resize(buffer_size);
   other.init_buffers();
   return *this;
 }
Exemple #7
0
int strstreambuf::underflow()
{
    char * tptr;
    if (gptr() >= egptr())
        {
        // try to grow get area if we can...
        if (egptr()<pptr())
            {
            tptr = base() + (gptr()-eback());
            setg(base(),tptr,pptr());
            }
        if (gptr() >= egptr())
            return EOF;
        }

    return (int)(unsigned char) *gptr();
}
Exemple #8
0
sockbuf::int_type sockbuf::underflow ()
{
  if (gptr () == 0)
    return eof; // input stream has been disabled

  if (gptr () < egptr ())
    return (unsigned char) *gptr (); // eof is a -ve number; make it
                                     // unsigned to be diff from eof

  int rlen = read (eback (), (char*) rep->gend - (char*) eback ());

  if (rlen == 0)
    return eof;

  setg (eback (), eback (), eback () + rlen);
  return (unsigned char) *gptr ();
}
Exemple #9
0
void BinaryStreamBuffer::reserve(size_t newSize)
{
	size_t getPos = 0;
	size_t putPos = 0;
	size_t readEndPos = 0;
	if(!m_dataBuf.empty()){
		getPos = gptr() - eback();
		readEndPos = egptr() - eback();
		putPos = pptr() - buffer();
	}

	if(newSize > m_dataBuf.size())
		m_dataBuf.resize(newSize);

	setg(buffer(), buffer() + getPos, buffer() + readEndPos);
	setp(buffer() + putPos, end());
}
Exemple #10
0
LoggerMessage::LoggerMessage(LoggerMessage&& other)
  : std::ostream(nullptr)
  , std::streambuf()
  , category(other.category)
  , logLevel(other.logLevel)
  , logger(other.logger)
  , message(other.message)
  , timestamp(boost::posix_time::microsec_clock::local_time())
  , gotText(false) {
  if (this != &other) {
    _M_tie = nullptr;
    _M_streambuf = nullptr;

    //ios_base swap
    std::swap(_M_streambuf_state, other._M_streambuf_state);
    std::swap(_M_exception, other._M_exception);
    std::swap(_M_flags, other._M_flags);
    std::swap(_M_precision, other._M_precision);
    std::swap(_M_width, other._M_width);

    std::swap(_M_callbacks, other._M_callbacks);
    std::swap(_M_ios_locale, other._M_ios_locale);
    //ios_base swap

    //streambuf swap
    char *_Pfirst = pbase();
    char *_Pnext = pptr();
    char *_Pend = epptr();
    char *_Gfirst = eback();
    char *_Gnext = gptr();
    char *_Gend = egptr();

    setp(other.pbase(), other.epptr());
    other.setp(_Pfirst, _Pend);

    setg(other.eback(), other.gptr(), other.egptr());
    other.setg(_Gfirst, _Gnext, _Gend);

    std::swap(_M_buf_locale, other._M_buf_locale);
    //streambuf swap

    std::swap(_M_fill, other._M_fill);
    std::swap(_M_tie, other._M_tie);
  }
  _M_streambuf = this;
}
Exemple #11
0
ERW_Result CRWStreambuf::x_Pushback(void)
{
    if ( !m_Reader ) {
        _ASSERT(!gptr()  &&  !egptr());
        return eRW_Success;
    }

    ERW_Result result;
    const CT_CHAR_TYPE* ptr = gptr();
    size_t count = (size_t)(egptr() - ptr);
    setg(0, 0, 0);
    if ( !count )
        result = eRW_Success;
    else if ((result = m_Reader->Pushback(ptr, count, m_pBuf)) == eRW_Success)
        m_pBuf = 0;
    return result;
}
Exemple #12
0
stringbuf::pos_type stringbuf::seekoff(off_type __off, ios_base::seekdir __dir, ios_base::openmode __mode /* = ios_base::in | ios_base::out */) 
{
    __mode &= __M_mode;
    bool __imode = (__mode & ios_base::in) != 0;
    bool __omode = (__mode & ios_base::out) != 0;

    if ((__imode == false) && (__omode == false)) return pos_type(-1);
    if ((__imode && (gptr() == 0)) || (__omode && (pptr() == 0))) return pos_type(-1);
    STL_ASSERT((eback() <= gptr()) && (gptr() <= egptr()) && (pbase() <= pptr()) && (pptr() <= epptr()));

    streamoff __newoff;
    switch (__dir) 
    {
    case ios_base::beg:
        __newoff = 0;
        break;
    case ios_base::end:
        __newoff = __M_str.size();
        break;
    case ios_base::cur:
        __newoff = __imode ? (gptr() - eback()) : (pptr() - pbase());
        if (__off == 0) return pos_type(__newoff);
        break;
    default:
        return pos_type(-1);
    }

    __off += __newoff;

    if (__imode) 
    {
        ptrdiff_t __size = egptr() - eback();
        if ((__off < 0) || (__off > __size)) return pos_type(-1);
        setg(eback(), eback() + static_cast<ptrdiff_t>(__off), eback() + static_cast<ptrdiff_t>(__size));
    }

    if (__omode) 
    {
        ptrdiff_t __size = epptr() - pbase();
        if ((__off < 0) || (__off > __size)) return pos_type(-1);
        setp(pbase(), pbase() + __size);
        pbump(static_cast<int>(__off));
    }

    return pos_type(__off);
}
Exemple #13
0
streamsize stringbuf::xsputn(const char_type* __string, streamsize __length) 
{
    if (((__M_mode & ios_base::out) == 0) || (__length <= 0)) return 0;

    streamsize __nwritten = 0;

    // If the put pointer is somewhere in the middle of the string, then overwrite instead of append.
    if (!__M_str.empty() && (pbase() == __M_str.begin()))
    {
        ptrdiff_t __avail = __M_str.end() - pptr();
        if (__avail >= __length)
        {
            VOS_memcpy_s(pptr(), epptr() - pptr(), __string, static_cast<size_t>(__length));
            pbump(static_cast<int>(__length));
            return __length;
        }
        else 
        {
            VOS_memcpy_s(pptr(), epptr() - pptr(), __string, __avail);
            __nwritten += __avail;
            __length -= __avail;
            __string += __avail;
        }
    }

    // At this point we know we're appending.
    char_type* __data_ptr;
    if (__M_mode & ios_base::in) 
    {
        ptrdiff_t __get_offset = gptr() - eback();
        __M_str.append(__string, __string + static_cast<ptrdiff_t>(__length));
        __data_ptr = __M_str.begin();
        setg(__data_ptr, __data_ptr + __get_offset, __M_str.end());
    }
    else 
    {
        __M_str.append(__string, __string + static_cast<ptrdiff_t>(__length));
        __data_ptr = __M_str.begin();
    }

    setp(__data_ptr, __M_str.end());
    pbump(static_cast<int>(__M_str.size()));
    __nwritten += __length;
    return __nwritten;
}
Exemple #14
0
streamsize stringbuf::_M_xsputnc(char_type __char, streamsize __count) 
{
    if (((__M_mode & ios_base::out) == 0) || (__count <= 0)) return 0;

    streamsize __nwritten = 0;

    // If the put pointer is somewhere in the middle of the string, then overwrite instead of append.
    if (pbase() == __M_str.begin()) 
    {
        ptrdiff_t __avail = __M_str.end() - pptr();
        if (__avail >= __count) 
        {
            VOS_memset_s(pptr(), epptr() - pptr(), __char, static_cast<size_t>(__count));
            pbump(static_cast<int>(__count));
            return __count;
        }
        else 
        {
            VOS_memset_s(pptr(), epptr() - pptr(), __char, __avail);
            __nwritten += __avail;
            __count -= __avail;
        }
    }

    // At this point we know we're appending.
    size_t __app_size = static_cast<size_t>(__count);
    char_type* __data_ptr;
    if (__M_mode & ios_base::in) 
    {
        ptrdiff_t __get_offset = gptr() - eback();
        __M_str.append(__app_size, __char);
        __data_ptr = __M_str.begin();
        setg(__data_ptr, __data_ptr + __get_offset, __M_str.end());
    }
    else 
    {
        __M_str.append(__app_size, __char);
        __data_ptr = __M_str.begin();
    }

    setp(__data_ptr, __M_str.end());
    pbump(static_cast<int>(__M_str.size()));
    __nwritten += __app_size;
    return __nwritten;
}
EIO_Status CConn_Streambuf::x_Close(bool close)
{
    if (!m_Conn)
        return close ? eIO_Closed : eIO_Success;
 
    EIO_Status status;
    // flush only if some data pending
    if (pbase() < pptr()) {
        if ((status = CONN_Status(m_Conn, eIO_Write)) != eIO_Success) {
            m_Status = status;
            if (CONN_Status(m_Conn, eIO_Open) == eIO_Success) {
                _TRACE(x_Message("Close():  Cannot finalize implicitly"
                                 ", data loss may result"));
            }
        } else if (sync() != 0)
            status = m_Status != eIO_Success ? m_Status : eIO_Unknown;
    } else
        status = eIO_Success;

    setg(0, 0, 0);
    setp(0, 0);

    CONN c = m_Conn;
    m_Conn = 0;  // NB: no re-entry

    if (close) {
        // here: not called from the close callback x_OnClose
        if (m_CbValid) {
            SCONN_Callback cb;
            CONN_SetCallback(c, eCONN_OnClose, &m_Cb, &cb);
            if ((void*) cb.func != (void*) x_OnClose  ||  cb.data != this)
                CONN_SetCallback(c, eCONN_OnClose, &cb, 0);
        }
        if (m_Close  &&  (m_Status = CONN_Close(c)) != eIO_Success) {
            _TRACE(x_Message("Close():  CONN_Close() failed"));
            if (status == eIO_Success)
                status  = m_Status;
        }
    } else if (m_CbValid  &&  m_Cb.func) {
        EIO_Status cbstat = m_Cb.func(c, eCONN_OnClose, m_Cb.data);
        if (cbstat != eIO_Success)
            status  = cbstat;
    }
    return status;
}
Exemple #16
0
    istreambuf::istreambuf(std::streambuf * sb)
    : common(sb), end(false) {
        LOG("bz::istreambuf");

        int cret =::BZ2_bzDecompressInit(z_strm,
            0, //verbosity
            0  //no small memory
        );

        if (BZ_OK != cret) {
            LOG("\terror creating zstream " << cret);
            raise_error(cret);
        }
        //initialize streambuf interface functions
        //first call will call uflow and this will set the buffer accordingly
        //no buffering
        setg(out.buf, out.buf, out.buf);
    }
// Read characters from input buffer.
// This invokes wide_buffer_.xsgetn() which requires a patch for
// console (keyboard) input on Windows, so ensure this class is
// initialized with a patched std::wcin when std::wcin is used.
std::streambuf::int_type unicode_streambuf::underflow()
{
    // Read from the wide input buffer.
    const auto read = wide_buffer_->sgetn(wide_, wide_size_);

    // Handle read termination.
    if (read == 0)
        return traits_type::eof();

    // Convert utf16 to utf8, returning bytes written.
    const auto bytes = to_utf8(narrow_, narrow_size_, wide_, read);

    // Reset gptr and egptr, eback never changes.
    setg(narrow_, narrow_, &narrow_[bytes]);

    // Return the first character in the input sequence.
    return traits_type::to_int_type(*gptr());
}
Exemple #18
0
 void init (
 )
 {
     try
     {
         out_buffer = new char[out_buffer_size];
         in_buffer = new char[in_buffer_size];
     }
     catch (...)
     {
         if (out_buffer) delete [] out_buffer;
         throw;
     }
     setp(out_buffer, out_buffer + (out_buffer_size-1));
     setg(in_buffer+max_putback, 
         in_buffer+max_putback, 
         in_buffer+max_putback);
 }
Exemple #19
0
void sockbuf::shutdown (shuthow sh)
{
    switch (sh) {
    case shut_read:
        delete [] eback ();
        setg (0, 0, 0);
        break;
    case shut_write:
        delete [] pbase ();
        setp (0, 0);
        break;
    case shut_readwrite:
        shutdown (shut_read);
        shutdown (shut_write);
        break;
    }
    if (::shutdown(rep->sock, sh) == -1) throw sockerr (errno, "sockbuf::shutdown", sockname.text.c_str());
}
Exemple #20
0
LoggerBuffer::LoggerBuffer(YAC_LoggerRollPtr roll, size_t buffer_len) : _roll(roll), _buffer(NULL), _buffer_len(buffer_len)
{
    //设置get buffer, 无效, 不适用
    setg(NULL, NULL, NULL);

    //设置put buffer
    if (_roll)
    {
        //分配空间
        _buffer = new char[_buffer_len];
        setp(_buffer, _buffer + _buffer_len);
    }
    else
    {
        setp(NULL, NULL);
        _buffer_len = 0;
    }
}
// ---------
pos_type CEEStdStreamBufProxy::seekoff(off_type offset, seekdir dir, openmode mode)
{
	// sync in case writing to stream
	sync();

    pos_type pos = 
		m_EEStream->SeekTo(offset, 
            (dir ==  ios::beg) ? SEEK_SET :
            (dir ==  ios::cur) ? SEEK_CUR :
                                        SEEK_END
        );

    if (pos < 0)
        return -1;

    setg(m_Buffer, m_Buffer + 1, m_Buffer + 1);
    return pos;
}
Exemple #22
0
void stringbuf::_M_set_ptrs()
{
    char_type* __data_ptr = __M_str.begin();
    char_type* __data_end = __M_str.end();

    // The initial read position is the beginning of the string.
    if (__M_mode & ios_base::in) 
    {
        setg(__data_ptr, (__M_mode & ios_base::ate) ? __data_end : __data_ptr, __data_end);
    }

    // The initial write position is the beginning of the string.
    if (__M_mode & ios_base::out) 
    {
        setp(__data_ptr, __data_end);
        pbump(static_cast<int>(__M_str.size()));   // initial write position, if we initialized with string 
    }
}
	int HashStream::underflow()
	{
		// TODO: add seek mechanisms to reset the istream

		if (!final_)
		{
			finalize(hash_buf_, hash_size_);
			final_ = true;

			// Since the input buffer content is now valid (or is new)
			// the get pointer should be initialized (or reset).
			setg(hash_buf_, hash_buf_, hash_buf_ + hash_size_);

			return std::char_traits<char>::not_eof((unsigned char) hash_buf_[0]);
		}

		return std::char_traits<char>::eof();
	}
Exemple #24
0
int gzfilebuf::fillbuf() {

  int required;
  char *p;

  p = base();

  required = blen();

  int t = gzread( file, p, required );

  if ( t <= 0) return EOF;

  setg( base(), base(), base()+t);

  return t;

}
Exemple #25
0
			PacDecoderBuffer(
				std::string const & filename, 
				::std::size_t rbuffersize,
				bool const raddterm = false
			)
			: stream(filename),
			  b(2),
			  n(getNumberOfSymbols(stream)),
			  // smallest multiple aligning to bitsperentity bits
			  alignmult(1ull << (loglog-::libmaus2::bitio::Ctz::ctz(b))),
			  // make buffersize multiple of alignmult
			  buffersize(((rbuffersize + alignmult-1)/alignmult)*alignmult),
			  C((buffersize*b+7)/8),
			  buffer(buffersize,false),
			  symsread(0),
			  addterm(raddterm)
			{
				setg(buffer.end(), buffer.end(), buffer.end());	
			}
Exemple #26
0
std::ios::pos_type
no_copy_membuf::seekoff (std::ios::off_type pos, std::ios::seekdir dir, std::ios::openmode which) {

    // TODO: This logic isn't great. Need to account for 'which'

    // Based on the direction, move pos bytes.
    if (dir == std::ios::beg) {
        pos = clamp((std::ptrdiff_t)pos, (std::ptrdiff_t)0, m_size); // Make sure we're not overreaching.
    }
    else if (dir == std::ios::cur) {
        pos = clamp((std::ptrdiff_t)(m_current_pos + pos), (std::ptrdiff_t)0, m_size); // Make sure it's not too big/small
    }
    else if (dir == std::ios::end) {
        pos = clamp((std::ptrdiff_t)(m_size - pos), (std::ptrdiff_t)0, m_size); // Make sure it's not too big/small
    }
    setg(m_p, m_p + pos, m_p + m_size);
    m_current_pos = pos;
    return pos;
}
std::streambuf::int_type multifstreambuf::underflow()
{
  if (mglob.gl_pathv == 0 || mglob.gl_pathv[current] == 0)
    open_next();

  int_type r;
  do
  {
    r = file.sbumpc();
  } while (r == traits_type::eof() && open_next());

  if (r != traits_type::eof())
  {
    ch = static_cast<char>(r);
    setg(&ch, &ch, &ch + 1);
  }

  return r;
}
Exemple #28
0
void streambuf::seekg(zim::offset_type off)
{
  setg(0, 0, 0);
  currentPos = off;

  zim::offset_type o = off;
  FilesType::iterator it;
  for (it = files.begin(); it != files.end() && (*it)->fsize < o; ++it)
    o -= (*it)->fsize;

  if (it == files.end())
  {
    std::ostringstream msg;
    msg << "error seeking to "<< off;
    throw std::runtime_error(msg.str());
  }

  setCurrentFile((*it)->fname, o);
}
Exemple #29
0
  void gzstreambuf::Init(FILE *f_, bool write_)
  {
    write = write_;
    Open(f_);
    if (error)
      return;

    buf = (char *)malloc(sizeof(char) * bufsize);
    if (!buf)
      throw AllocError("Out of memory");
    //Assert(setbuf(buf, bufsize));

    if (write)
      setp(buf, buf + bufsize - 1);
    else
      setg(buf, buf + bufsize, buf + bufsize);

    error = false;
  }
Exemple #30
0
strstreambuf::int_type
strstreambuf::overflow(int_type __c)
{
    if (__c == EOF)
        return int_type(0);
    if (pptr() == epptr())
    {
        if ((__strmode_ & __dynamic) == 0 || (__strmode_ & __frozen) != 0)
            return int_type(EOF);
        size_t old_size = static_cast<size_t> ((epptr() ? epptr() : egptr()) - eback());
        size_t new_size = max<size_t>(static_cast<size_t>(__alsize_), 2*old_size);
        if (new_size == 0)
            new_size = __default_alsize;
        char* buf = nullptr;
        if (__palloc_)
            buf = static_cast<char*>(__palloc_(new_size));
        else
            buf = new char[new_size];
        if (buf == nullptr)
            return int_type(EOF);
        if (old_size != 0) {
            _LIBCPP_ASSERT(eback(), "overflow copying from NULL");
            memcpy(buf, eback(), static_cast<size_t>(old_size));
        }
        ptrdiff_t ninp = gptr()  - eback();
        ptrdiff_t einp = egptr() - eback();
        ptrdiff_t nout = pptr()  - pbase();
        if (__strmode_ & __allocated)
        {
            if (__pfree_)
                __pfree_(eback());
            else
                delete [] eback();
        }
        setg(buf, buf + ninp, buf + einp);
        setp(buf + einp, buf + new_size);
        pbump(static_cast<int>(nout));
        __strmode_ |= __allocated;
    }
    *pptr() = static_cast<char>(__c);
    pbump(1);
    return int_type(static_cast<unsigned char>(__c));
}