Copy from std :: istreambuf_iterator <> to std :: vector <>

I have a Visual Studio 2008 C ++ application where I would like to process a stream as a set of iterators.

For example, if I were to get an array of WIN32_FIND_DATA structures over a stream, I would like to do something like this:

IStreamBuf< WIN32_FIND_DATA > sb( stream );
std::vector< WIN32_FIND_DATA > buffer;
std::copy( std::istreambuf_iterator< WIN32_FIND_DATA >( &sb ), 
           std::istreambuf_iterator< WIN32_FIND_DATA >(),
           std::back_inserter( buffer ) );

For this, I defined a class derived from std::basic_streambuf<>:

template< typename T >
class IStreamBuf : public std::basic_streambuf< byte >
{
public:

    IStreamBuf( IStream* stream ) : stream_( stream )
    {        
    };

protected:

    virtual traits_type::int_type underflow()
    {
        DWORD bytes_read = 0;
        HRESULT hr = stream_->Read( &buffer_, sizeof( T ), &bytes_read );
        if( FAILED( hr ) )
            return traits_type::eof();

        traits_type::char_type* begin = 
            reinterpret_cast< traits_type::char_type* >( &buffer_ );
        setg( begin, begin, begin + bytes_read );   
        return traits_type::to_int_type( *gptr() );
    };

private:

    // buffer to hold current item of type T
    T buffer_;

    // stream used to receive data
    IStream* stream_;
}; // class IStreamBuf

I cannot figure out how to gracefully move from array byteto array WIN32_FIND_DATAs. Since a parameter std::basic_streambuf<>requires a template parameter std::char_traits<>, I get the impression that it can only use built-in types, such as charor byte, and not such a structure as WIN32_FIND_DATA. Right?

Any suggestions on how to make this work?

Thanks PaulH

+3
1

An istreambuf_iterator , . , , , istream_iterator operator>> WIN32_FIND_DATA. / - WIN32_FIND_DATA.

+1

Source: https://habr.com/ru/post/1746904/


All Articles