streambuf

Tricks to pass from a ostringstream to a istringstream

亡梦爱人 提交于 2020-01-05 10:27:44
问题 I try to make a module of compression/decompression and then use istringstream for compression and ostringstream for decompression. My problem is that after filling my istringstream with compression datas, I'm not able to convert this stream into an ostringstream. I try : iss.rdbuf(oss.rdbuf()); as the in and out type match but it doesn't work. Do you have any idea ? Thank in advance. 回答1: stringstream::rdbuf doesn't have an overload that takes a parameter. It does, however, inherit the base

Tricks to pass from a ostringstream to a istringstream

时间秒杀一切 提交于 2020-01-05 10:27:15
问题 I try to make a module of compression/decompression and then use istringstream for compression and ostringstream for decompression. My problem is that after filling my istringstream with compression datas, I'm not able to convert this stream into an ostringstream. I try : iss.rdbuf(oss.rdbuf()); as the in and out type match but it doesn't work. Do you have any idea ? Thank in advance. 回答1: stringstream::rdbuf doesn't have an overload that takes a parameter. It does, however, inherit the base

Custom input stream. Stream buffer and underflow method

北战南征 提交于 2020-01-03 21:07:34
问题 To understand how input streams work I designed 2 of the following classes: #include <iostream> class my_streambuf: public std::streambuf { private: std::streambuf* buffer; char ch; protected: virtual std::streambuf::int_type underflow() { std::streambuf::int_type result = buffer->sbumpc(); if (result != traits_type::eof()) { ch = traits_type::to_char_type(result); setg(&ch, &ch, &ch + 1); } return result; } public: my_streambuf(std::streambuf* buffer) : buffer(buffer) {}; virtual ~my

redirect std::cout to a custom writer

喜欢而已 提交于 2019-12-28 11:57:31
问题 I want to use this snippet from Mr-Edd's iostreams article to print std::clog somewhere. #include <iostream> #include <iomanip> #include <string> #include <sstream> int main() { std::ostringstream oss; // Make clog use the buffer from oss std::streambuf *former_buff = std::clog.rdbuf(oss.rdbuf()); std::clog << "This will appear in oss!" << std::flush; std::cout << oss.str() << '\\n'; // Give clog back its previous buffer std::clog.rdbuf(former_buff); return 0; } so, in a mainloop, I will do

redirect std::cout to a custom writer

試著忘記壹切 提交于 2019-12-28 11:57:00
问题 I want to use this snippet from Mr-Edd's iostreams article to print std::clog somewhere. #include <iostream> #include <iomanip> #include <string> #include <sstream> int main() { std::ostringstream oss; // Make clog use the buffer from oss std::streambuf *former_buff = std::clog.rdbuf(oss.rdbuf()); std::clog << "This will appear in oss!" << std::flush; std::cout << oss.str() << '\\n'; // Give clog back its previous buffer std::clog.rdbuf(former_buff); return 0; } so, in a mainloop, I will do

What is wrong with my implementation of overflow()?

流过昼夜 提交于 2019-12-22 17:10:31
问题 I am trying to implement a stream buffer and I'm having trouble with making overflow() work. I resize the buffer by 10 more characters and reset the buffer using setp . Then I increment the pointer back where we left off. For some reason the output is not right: template <class charT, class traits = std::char_traits<charT>> class stringbuf : public std::basic_stringbuf<charT, traits> { public: using char_type = charT; using traits_type = traits; using int_type = typename traits::int_type;

Reading from serial port with Boost Asio

冷暖自知 提交于 2019-12-18 04:16:40
问题 I'm want to check for incoming data packages on the serial port, using boost.asio . Each data packet will start with a header that is one byte long, and will specify what type of the message has been sent. Each different type of message has its own length. The function I want to write should listen for new incoming messages continually, and when it finds one it should read it, and call some other function to parse it. My current code is as follows: void check_for_incoming_messages() { boost:

Copy a streambuf's contents to a string

与世无争的帅哥 提交于 2019-12-17 07:11:37
问题 Apparently boost::asio::async_read doesn't like strings, as the only overload of boost::asio::buffer allows me to create const_buffer s, so I'm stuck with reading everything into a streambuf. Now I want to copy the contents of the streambuf into a string, but it apparently only supports writing to char* ( sgetn() ), creating an istream with the streambuf and using getline() . Is there any other way to create a string with the streambufs contents without excessive copying? 回答1: I don't know

Taking ownership of streambuf/stringbuf data

半世苍凉 提交于 2019-12-13 20:06:20
问题 I'd like an interface for writing to an automatically resizing array. One way to do this is with a generic std::ostream * . Then consider if ostringstream is the target: void WritePNG(ostream *out, const uint8_t *pixels); void *WritePNGToMemory(uint8_t *pixels) { ostringstream out; WritePng(&out, pixels); uint8_t *copy = new uint8_t[out.tellp()]; memcpy(copy, out.str().c_str(), out.tellp()]; return copy; } But I want to avoid the memcpy(). Is there a way to take ownership of the array in the

Code using boost::asio::streambuf causes segfault

假装没事ソ 提交于 2019-12-12 10:47:05
问题 I've experienced problems using asio::streambuf and am hoping someone can tell me if I'm using the class incorrectly. When I run this example code it segfaults. Why? To make things more confusing, this code works on Windows (Visual Studio 2008), but does not work on Linux (with gcc 4.4.1). #include <boost/asio.hpp> using namespace std; int main() { boost::asio::streambuf Stream; // Put 4 bytes into the streambuf... int SetValue = 0xaabbccdd; Stream.sputn(reinterpret_cast<const char*>(