c++ - 我如何为无符号字符的 vector 制作某种 istream

标签 c++ boost stl

我如何从缓冲区 unsigned char* 或 vector 创建一个 istream。

基本上我想要:

void Func(vector<unsigned char> data)
{
  someSortOfIstream x (data);

  x >> something;
}

也在使用 boost....

最佳答案

这可以使用 Boost.IOStreams 来完成:

#include <iosfwd>                          // streamsize
#include <boost/iostreams/categories.hpp>  // seekable_device_tag 
#include <boost/iostreams/positioning.hpp> // stream_offset

template<typename Container>
class container_device 
{
public:
    typedef typename Container::value_type char_type;
    typedef boost::iostreams::seekable_device_tag category;

    container_device(Container& container) 
      : container_(container), pos_(0) {}

    /// Read up to n characters from the underlying data source into the 
    /// buffer s, returning the number of characters read; return -1 to 
    /// indicate EOF
    std::streamsize read(char_type* s, std::streamsize n)
    {
        std::streamsize amt = 
            static_cast<std::streamsize>(container_.size() - pos_);
        std::streamsize result = (std::min)(n, amt);
        if (result != 0) {
            std::copy(container_.begin() + pos_, 
                      container_.begin() + pos_ + result, s);
            pos_ += result;
            return result;
        } 
        else {
            return -1;  // EOF
        }
    }

    /// Write up to n characters to the underlying data sink into the 
    /// buffer s, returning the number of characters written
    std::streamsize write(const char_type* s, std::streamsize n)
    {
        std::streamsize result = 0;
        if (pos_ != container_.size()) {
            std::streamsize amt = 
                static_cast<std::streamsize>(container_.size() - pos_);
            std::streamsize result = (std::min)(n, amt);
            std::copy(s, s + result, container_.begin() + pos_);
            pos_ += result;
        }
        if (result < n) {
            container_.insert(container_.end(), s, s + n);
            pos_ = container_.size();
        }
        return n;
    }

    /// Seek to position off and return the new stream position. The 
    /// argument 'way' indicates how off is interpreted:
    ///    - std::ios_base::beg indicates an offset from the sequence 
    ///      beginning 
    ///    - std::ios_base::cur indicates an offset from the current 
    ///      character position 
    ///    - std::ios_base::end indicates an offset from the sequence end
    boost::iostreams::stream_offset seek(
        boost::iostreams::stream_offset off, std::ios_base::seekdir way)
    {
        // Determine new value of pos_
        boost::iostreams::stream_offset next;
        if (way == std::ios_base::beg) {
            next = off;
        } 
        else if (way == std::ios_base::cur) {
            next = pos_ + off;
        } 
        else if (way == std::ios_base::end) {
            next = container_.size() + off - 1;
        }

        // Check for errors
        if (next < ((boost::iostreams::stream_offset)0) 
         || next >= ((boost::iostreams::stream_offset)container_.size()))
            throw std::ios_base::failure("bad seek offset");

        pos_ = (size_type)next;
        return pos_;
    }

    Container& container() { return container_; }

private:
    typedef typename Container::size_type size_type;
    Container& container_;
    size_type pos_;
};

可以用作:

std::vector<char> data;
boost::iostreams::stream<container_device<std::vector<char> > > io(data);

和:

io << foo;
io >> bar;

关于c++ - 我如何为无符号字符的 vector 制作某种 istream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3175159/

相关文章:

c++ - 使用按位运算符相乘

c++ - cout 和 printf

不同编译器中的 C++ 标准库实现

c++ - 在具有预分配 block 的自定义 STL 分配器中重新绑定(bind)

c++ - 为什么当字符改变时这个 std::string 的大小会改变?

c++ - 当我定义一个具有该类的(非引用)返回类型的函数时,该类的哪个——默认构造函数或复制构造函数——被调用?

c++ - boost 的虚拟析构函数 :noncopyable classes?

c++ - BOOST 中的属性映射是什么?

c++ - 使用 boost::posix_time 计算时间差

c++ - 当 T 是 std::pair<std::hash 也支持的两种更简单的类型> 时,std::hash<T> 应该工作吗?