c++ - 为 std::istringstream 设置内部缓冲区

标签 c++ macos buffer istream

在升级到 OS X 10.9.4 之前,我有以下用于 OS X 的代码 这是代码:

#include <iostream>
#include <sstream>

int main() {
    // Buffer to be read
    const char* buffer = "0 3";
    std::cout << "buffer: '" << buffer << "'\n";
    size_t len = 3;

    std::istringstream iss;
    iss.clear();
    iss.rdbuf()->pubsetbuf((char*)buffer, len);

    // Did iss got a hold of buffer?
    std::cout << "str: '" << iss.str() << "'\n";
}

思路很简单,已经实现了asked .我希望能够从现有字符串中读取并从中读取而无需复制其内容。正如我已经提到的,这段代码在 OS X 10.9.4 中没有给出正确的输出。

我猜为什么会这样,因为正如本 post 中提到的那样pubsetbuf 的实现什么都不做。

我的问题是:有没有办法在每台机器上都达到预期的效果?我需要做什么才能正确设置内部缓冲区?

如果不清楚,我希望得到以下输出:

buffer: '0 3'
str: '0 3'

我在我的机器上获得的当前输出是

buffer: '0 3'
str: ''

意味着 istringstream 中的内部缓冲区未设置。

最佳答案

而不是篡改 std::istringstream 你可以写你自己的:

#include <algorithm>
#include <ostream>

// BasicInputSequenceBuffer
// ============================================================================

template < typename Char, typename Traits = std::char_traits<Char> >
class BasicInputSequenceBuffer
:   public std::basic_streambuf<Char, Traits>
{
    // Types
    // =====

    private:
    typedef std::basic_streambuf<Char, Traits> Base;

    public:
    typedef typename Base::char_type char_type;
    typedef typename Base::int_type int_type;
    typedef typename Base::pos_type pos_type;
    typedef typename Base::off_type off_type;
    typedef typename Base::traits_type traits_type;
    typedef const char_type* pointer;
    typedef std::size_t size_type;

    // Construction
    // ============

    public:
    BasicInputSequenceBuffer(pointer data, size_type size) {
        // These casts are safe (no modification will take place):
        char* begin = const_cast<char_type*>(data);
        char* end = const_cast<char_type*>(data + size);
        this->setg(begin, begin, end);
    }

    // Stream Buffer Interface
    // =======================

    protected:
    virtual std::streamsize showmanyc();
    virtual std::streamsize xsgetn(char_type*, std::streamsize);
    virtual int_type pbackfail(int_type);

    // Utilities
    // =========

    protected:
    int_type eof() { return traits_type::eof(); }
    bool is_eof(int_type ch) { return ch == eof(); }
};


// Get Area
// ========

template <typename Char, typename Traits>
std::streamsize
BasicInputSequenceBuffer<Char, Traits>::showmanyc() {
    return this->egptr() - this->gptr();
}

template <typename Char, typename Traits>
std::streamsize
BasicInputSequenceBuffer<Char, Traits>::xsgetn(char_type* p, std::streamsize n) {
    std::streamsize result = std::min(n, this->egptr() - this->gptr());
    std::copy(this->gptr(), this->gptr() + result, p);
    this->gbump(result);
    return result;
}

template <typename Char, typename Traits>
typename BasicInputSequenceBuffer<Char, Traits>::int_type
BasicInputSequenceBuffer<Char, Traits>::pbackfail(int_type ch) {
    if(is_eof(ch)) {
        if(this->eback() != this->gptr()) {
            this->gbump(-1);
            return traits_type::to_int_type(*this->gptr());
        }
    }
    return eof();
}

typedef BasicInputSequenceBuffer<char> InputSequenceBuffer;


// BasicInputSequence
//=============================================================================

template < typename Char, typename Traits = std::char_traits<Char> >
class BasicInputSequence
:   public std::basic_istream<Char, Traits>
{
    // Types
    // =====

    private:
    typedef std::basic_istream<Char, Traits> Base;

    public:
    typedef typename Base::char_type char_type;
    typedef typename Base::int_type int_type;
    typedef typename Base::pos_type pos_type;
    typedef typename Base::off_type off_type;
    typedef typename Base::traits_type traits_type;

    private:
    typedef BasicInputSequenceBuffer<Char, Traits> buffer_type;

    public:
    typedef typename buffer_type::pointer pointer;
    typedef typename buffer_type::size_type size_type;

    // Construction
    // ============

    public:
    explicit BasicInputSequence(pointer data, size_type size)
    :   Base(&m_buf), m_buf(data, size)
    {}

    private:
    buffer_type m_buf;
};

typedef BasicInputSequence<char> InputSequence;

#include <iostream>

int main() {
    const char* buffer = "0 3";
    InputSequence stream(buffer, 3);
    std::string a;
    std::string b;
    stream >> a >> b;
    std::cout << "str: '" << a << ' ' << b << "'\n";
    return 0;
}

关于c++ - 为 std::istringstream 设置内部缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25292974/

相关文章:

C++ Rest SDK 卡萨布兰卡 Sigtrap

linux - 我应该将 Linux 驱动程序移植到 Mac OS X 还是应该重写它

java - 如何更改 VBO 中的数据?

c - 我是否正确应用了严格别名规则?

c++ - 使用非默认比较谓词的集合容器

c++ - 命令行参数是一些字符串吗?

c++ - 将类类型传递给 Windows 窗体构造函数并必须将其转换为类指针才能使用它

macos - 如何说服 OS X 打开 txmt ://URLs in MacVim?

macos - 设置 GOPATH 时转到 : go get $GOPATH error,

c++ - 如何在 Linux 中获取命名管道(FIFO)的剩余缓冲区大小