Получите istream от char *

У меня есть char * и длина данных, которые я получаю от библиотеки, и мне нужно передать данные функции, которая принимает istream.

Я знаю, что могу создать stringstream, но скопирую все данные. Кроме того, данные, несомненно, будут иметь 0, так как это zip файл, и создание строкового потока будет принимать данные до первого 0, я думаю.

Есть ли способ создать istream из char * и его размер без копирования всех данных?

Ответ 1

Здесь не устаревший метод найденный в Интернете, вы получили свой собственный класс std::streambuf, но он легко работает и, похоже, работает:

#include <iostream>
#include <istream>
#include <streambuf>
#include <string>

struct membuf : std::streambuf
{
    membuf(char* begin, char* end) {
        this->setg(begin, begin, end);
    }
};

int main()
{
    char buffer[] = "I'm a buffer with embedded nulls\0and line\n feeds";

    membuf sbuf(buffer, buffer + sizeof(buffer));
    std::istream in(&sbuf);
    std::string line;
    while (std::getline(in, line)) {
        std::cout << "line: " << line << "\n";
    }
    return 0;
}

Какие выходы:

line: I'm a buffer with embedded nullsand line
line:  feeds

Ответ 2

Не устаревшее решение с использованием Boost:

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

basic_array_source<char> input_source(my_ptr_to_char, byte_count);
stream<basic_array_source<char> > input_stream(input_source);

или даже проще:

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

bufferstream input_stream(my_ptr_to_char, byte_count);

Ответ 3

Мне нужно решение, которое поддерживает tellg и seekg и не требует повышения.

char_array_buffer из Руководства для начинающих по написанию пользовательского потокового буфера (std::streambuf) дало начальную точку.

byte_array_buffer.h:

#include <cstdio>
#include <string>
#include <list>
#include <fstream>
#include <iostream>

//
// http://www.mr-edd.co.uk/blog/beginners_guide_streambuf
//

class byte_array_buffer : public std::streambuf
{
public:
    byte_array_buffer(const uint8_t *begin, const size_t size);

private:
    int_type underflow();
    int_type uflow();
    int_type pbackfail(int_type ch);
    std::streamsize showmanyc();
    std::streampos seekoff ( std::streamoff off, std::ios_base::seekdir way,
                            std::ios_base::openmode which = std::ios_base::in | std::ios_base::out );
    std::streampos seekpos ( std::streampos sp,
                            std::ios_base::openmode which = std::ios_base::in | std::ios_base::out);

    // copy ctor and assignment not implemented;
    // copying not allowed
    byte_array_buffer(const byte_array_buffer &);
    byte_array_buffer &operator= (const byte_array_buffer &);

private:
    const uint8_t * const begin_;
    const uint8_t * const end_;
    const uint8_t * current_;
};

byte_array_buffer.cpp:

#include "byte_array_buffer.h"

#include <cassert>


byte_array_buffer::byte_array_buffer(const uint8_t *begin, const size_t size) :
begin_(begin),
end_(begin + size),
current_(begin_)
{
    assert(std::less_equal<const uint8_t *>()(begin_, end_));
}

byte_array_buffer::int_type byte_array_buffer::underflow()
{
    if (current_ == end_)
        return traits_type::eof();

    return traits_type::to_int_type(*current_);
}

byte_array_buffer::int_type byte_array_buffer::uflow()
{
    if (current_ == end_)
        return traits_type::eof();

    return traits_type::to_int_type(*current_++);
}

byte_array_buffer::int_type byte_array_buffer::pbackfail(int_type ch)
{
    if (current_ == begin_ || (ch != traits_type::eof() && ch != current_[-1]))
        return traits_type::eof();

    return traits_type::to_int_type(*--current_);
}

std::streamsize byte_array_buffer::showmanyc()
{
    assert(std::less_equal<const uint8_t *>()(current_, end_));
    return end_ - current_;
}


std::streampos byte_array_buffer::seekoff ( std::streamoff off, std::ios_base::seekdir way,
                                           std::ios_base::openmode which )
{
    if (way == std::ios_base::beg)
    {
        current_ = begin_ + off;
    }
    else if (way == std::ios_base::cur)
    {
        current_ += off;
    }
    else if (way == std::ios_base::end)
    {
        current_ = end_ + off;
    }

    if (current_ < begin_ || current_ > end_)
        return -1;


    return current_ - begin_;
}

std::streampos byte_array_buffer::seekpos ( std::streampos sp,
                                           std::ios_base::openmode which )
{
    current_ = begin_ + sp;

    if (current_ < begin_ || current_ > end_)
        return -1;

    return current_ - begin_;
}

Ответ 4

Единственный (простой) переносной способ включает создание копии:

std::istringstream ss(std::string(buf,len));

Фактически, это может скопировать данные дважды, один раз, чтобы создать string и один раз, чтобы создать istringstream. (Возможно, С++ 11 может избежать одной из копий с помощью конструктора перемещения, я не уверен.)

Однако, если вам повезет, ваша реализация на С++ позволит вам сделать это:

std::istringstream ss;
ss.rdbuf()->pubsetbuf(buf,len);

В GNU С++ (и, я полагаю, некоторые другие реализации), это создаст stringstream без копирования данных. Но это "поведение, определяемое реализацией" в соответствии со спецификацией. (См. Также этот вопрос.)

Включив параметр len, вы гарантируете, что обе они не будут иметь проблем с нулевыми символами.

Единственный переносимый способ сделать то, что вы хотите, - реализовать свой собственный подкласс stringbuf и использовать его для инициализации строкового потока. Не для слабонервных.

Ответ 6

Расширение принятого ответа, поддерживающее Tellg и seekg:

struct membuf : std::streambuf
{
    membuf(char* begin, char* end)
    {
        this->setg(begin, begin, end);
    }

    pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which = std::ios_base::in) override
    {
        if (dir == std::ios_base::cur)
            gbump(off);
        else if (dir == std::ios_base::end)
            setg(eback(), egptr() + off, egptr());
        else if (dir == std::ios_base::beg)
            setg(eback(), eback() + off, egptr());
        return gptr() - eback();
    }

    pos_type seekpos(pos_type sp, std::ios_base::openmode which) override
    {
        return seekoff(sp - pos_type(off_type(0)), std::ios_base::beg, which);
    }
};

Использование этого класса остается прежним.