使用 ParseDelimitedFromCodedInputStream 从文件中读取?

Using ParseDelimitedFromCodedInputStream for reading from file?

我有一个包含多条 protobuf 消息的文件,查找 table 告诉我文件中哪条消息开始的位置。消息以分隔形式书写。

在 C# 中,我可以做类似的事情

fileStream.Position = message_start_index;
ProtoMessage msg = ProtoMessage.Parser.ParseDelimitedFrom(fileStream);

完成。

对于 C++,ParseDelimitedFromSerializeDelimitedTo 函数终于被添加到库中,所以我很高兴不再需要手动处理分隔符。

但是对于解析定界消息,只有 ParseDelimitedFromZeroCopyStreamParseDelimitedFromCodedStream - google 的一些特殊实现。

如何在其中获得标准 ifstreamistream

我试着传递它,转换它,寻找那些特殊流类型的构造函数来从普通流中读取,但找不到任何有用的东西,所以任何提示都不胜感激。

protobuf API 大量使用继承,因此与其查看 ZeroCopyInputStreamCodedInputStream 的构造函数,不如查看它们的子类。

您要查找的是 google::protobuf::io::IstreamInputStreamgoogle::protobuf::io::ZeroCopyInputStream 的一个子类,改编自 std::istream

你可以这样使用它:

std::ifstream my_file("path/to/file");
google::protobuf::io::IstreamInputStream zc_stream(&my_file);

ParseDelimitedFromZeroCopyStream(&msg, &zc_stream, nullptr);