4 #include <google/protobuf/descriptor.h>
5 #include <google/protobuf/io/zero_copy_stream_impl.h>
6 #include <google/protobuf/message.h>
7 #include <google/protobuf/text_format.h>
10 using namespace google::protobuf;
12 bool load_proto_from_file(const string &filename, Message *msg)
14 // Read and parse the protobuf from disk.
15 int fd = open(filename.c_str(), O_RDONLY);
17 perror(filename.c_str());
20 io::FileInputStream input(fd); // Takes ownership of fd.
21 if (!TextFormat::Parse(&input, msg)) {
29 bool save_proto_to_file(const Message &msg, const string &filename)
31 // Save to disk. We use the text format because it's friendlier
32 // for a user to look at and edit.
33 int fd = open(filename.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0666);
35 perror(filename.c_str());
38 io::FileOutputStream output(fd); // Takes ownership of fd.
39 if (!TextFormat::Print(msg, &output)) {
40 // TODO: Don't overwrite the old file (if any) on error.