簡體   English   中英

我如何為無符號字符向量創建某種istream

[英]How do i make a some sort of istream for a vector of unsigned chars

如何從緩沖區unsigned char *或vector創建一個istream。

基本上我想要:

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

  x >> something;
}

也使用提升....

這可以使用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;

使用Boost的非棄用解決方案:

#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/array.hpp>
using namespace boost::iostreams;

basic_array_source<char> input_source(&my_vector[0], my_vector.size());
stream<basic_array_source<char> > input_stream(input_source);

甚至更簡單:

#include <boost/interprocess/streams/bufferstream.hpp>
using namespace boost::interprocess;

bufferstream input_stream(&my_vector[0], my_vector.size());

您還可以查看std :: stringstream。

我建議:

  1. 使用std::stringchar的奇特向量)而不是std::vector<unsigned char> ,因為它就是它的用途。 然后,您可以在<sstream>標頭中使用隨時可用的std::stringstream

  2. 您可以子類化std::vector<unsigned char> operator>>()以滿足您的需要。

    要么

    (更難但理論上更好)您可以為您的案例創建子類std::iostream ,並告訴它在您使用operator>>()時該怎么做

個人我會選擇1,如果你必須,請使用2a)因為坦率地說,我不知道如何將iostream子類化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM