123456789101112131415161718192021222324252627282930313233343536373839404142 |
- #include "proto_tool.h"
- #include <fcntl.h>
- #include<unistd.h>
- #include <google/protobuf/io/zero_copy_stream_impl.h>
- #include <google/protobuf/text_format.h>
- using google::protobuf::io::FileInputStream;
- using google::protobuf::io::FileOutputStream;
- using google::protobuf::io::ZeroCopyInputStream;
- using google::protobuf::io::CodedInputStream;
- using google::protobuf::io::ZeroCopyOutputStream;
- using google::protobuf::io::CodedOutputStream;
- using google::protobuf::Message;
- //读取protobuf 配置文件,转化为protobuf参数形式
- //input: prototxt_path :prototxt文件路径
- //ouput: parameter: protobuf参数,这里是消息基类,实际调用时传入对应的子类即可。
- bool proto_tool::read_proto_param(std::string prototxt_path, ::google::protobuf::Message& protobuf_parameter)
- {
- int fd = open(prototxt_path.c_str(), O_RDONLY);
- if (fd == -1) return false;
- FileInputStream* input = new FileInputStream(fd);
- bool success = google::protobuf::TextFormat::Parse(input, &protobuf_parameter);
- delete input;
- close(fd);
- return success;
- }
|