34

既製のものが見つからなかったので、次のことを思いつきました。

class membuf : public basic_streambuf<char>
{
public:
  membuf(char* p, size_t n) {
    setg(p, p, p + n);
    setp(p, p + n);
  }
}

使用法:

char *mybuffer;
size_t length;
// ... allocate "mybuffer", put data into it, set "length"

membuf mb(mybuffer, length);
istream reader(&mb);
// use "reader"

私は知っstringstreamていますが、指定された長さのバイナリデータを処理できないようです。

ここで自分のホイールを発明していますか?

編集

  • 入力データをコピーしてはならず、データを反復処理するものを作成するだけです。
  • ポータブルである必要があります。少なくとも、gccとMSVCの両方で機能する必要があります。
4

4 に答える 4

31

入力データが(テキストではなく)バイナリであり、そこからバイナリデータのチャンクを抽出したいとします。入力データのコピーを作成せずにすべて。

boost::iostreams::basic_array_sourceおよびboost::iostreams::stream_bufferBoost.Iostreamsから)とboost::archive::binary_iarchive(Boost.Serializationからを組み合わせて、便利な抽出>>演算子を使用してバイナリデータのチャンクを読み取ることができます。

#include <stdint.h>
#include <iostream>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/archive/binary_iarchive.hpp>

int main()
{
    uint16_t data[] = {1234, 5678};
    char* dataPtr = (char*)&data;

    typedef boost::iostreams::basic_array_source<char> Device;
    boost::iostreams::stream_buffer<Device> buffer(dataPtr, sizeof(data));
    boost::archive::binary_iarchive archive(buffer, boost::archive::no_header);

    uint16_t word1, word2;
    archive >> word1 >> word2;
    std::cout << word1 << "," << word2 << std::endl;
    return 0;
}

AMD64上のGCC4.4.1では、次のように出力されます。

1234,5678

Boost.Serializationは非常に強力で、すべての基本タイプ、文字列、さらにはSTLコンテナをシリアル化する方法を知っています。タイプを簡単にシリアル化できます。ドキュメントを参照してください。Boost.Serializationソースのどこかに隠されているのは、マシンのエンディアンに対して適切なスワッピングを実行する方法を知っているポータブルバイナリアーカイブの例です。これはあなたにも役立つかもしれません。

Boost.Serializationの空想を必要とせず、fread()タイプの方法でバイナリデータを読み取れる場合は、次basic_array_sourceのように簡単に使用できます。

#include <stdint.h>
#include <iostream>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>

int main()
{
    uint16_t data[] = {1234, 5678};
    char* dataPtr = (char*)&data;

    typedef boost::iostreams::basic_array_source<char> Device;
    boost::iostreams::stream<Device> stream(dataPtr, sizeof(data));

    uint16_t word1, word2;
    stream.read((char*)&word1, sizeof(word1));
    stream.read((char*)&word2, sizeof(word2));
    std::cout << word1 << "," << word2 << std::endl;

    return 0;
}

このプログラムでも同じ出力が得られます。

于 2010-01-17T05:20:49.243 に答える
5

何が必要かわかりませんが、これで何ができますか?

char *mybuffer;
size_t length;
// allocate, fill, set length, as before

std::string data(mybuffer, length);
std::istringstream mb(data);
//use mb
于 2010-01-17T04:22:32.270 に答える
4

標準ストリームバッファにはこの機能があります。
ストリームを作成します。バッファを取得してからオーバーライドします。

#include <sstream>
#include <iostream>
#include <algorithm>
#include <iterator>

int main()
{
    // Your imaginary buffer
    char    buffer[]    = "A large buffer we don't want to copy but use in a stream";

    // An ordinary stream.
    std::stringstream   str;

    // Get the streams buffer object. Reset the actual buffer being used.
    str.rdbuf()->pubsetbuf(buffer,sizeof(buffer));

    std::copy(std::istreambuf_iterator<char>(str),
              std::istreambuf_iterator<char>(),
              std::ostream_iterator<char>(std::cout)
             );
}
于 2010-01-17T05:17:51.160 に答える
2

質問者はデータをコピーしないものを望んでいました、そして彼の解決策はうまくいきます。私の貢献はそれを少しクリーンアップすることです。そうすれば、メモリ内のデータの入力ストリームである単一のオブジェクトを作成することができます。私はこれをテストしました、そしてそれは働きます。

class MemoryInputStream: public std::istream
    {
    public:
    MemoryInputStream(const uint8_t* aData,size_t aLength):
        std::istream(&m_buffer),
        m_buffer(aData,aLength)
        {
        rdbuf(&m_buffer); // reset the buffer after it has been properly constructed
        }

    private:
    class MemoryBuffer: public std::basic_streambuf<char>
        {
        public:
        MemoryBuffer(const uint8_t* aData,size_t aLength)
            {
            setg((char*)aData,(char*)aData,(char*)aData + aLength);
            }
        };

    MemoryBuffer m_buffer;
    };
于 2012-05-16T09:18:32.773 に答える