]> git.sesse.net Git - nageru/blob - shared/text_proto.cpp
Make Futatabi fades apply white balance.
[nageru] / shared / text_proto.cpp
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <string>
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>
8
9 using namespace std;
10 using namespace google::protobuf;
11
12 bool load_proto_from_file(const string &filename, Message *msg)
13 {
14         // Read and parse the protobuf from disk.
15         int fd = open(filename.c_str(), O_RDONLY);
16         if (fd == -1) {
17                 perror(filename.c_str());
18                 return false;
19         }
20         io::FileInputStream input(fd);  // Takes ownership of fd.
21         if (!TextFormat::Parse(&input, msg)) {
22                 input.Close();
23                 return false;
24         }
25         input.Close();
26         return true;
27 }
28
29 bool save_proto_to_file(const Message &msg, const string &filename)
30 {
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);
34         if (fd == -1) {
35                 perror(filename.c_str());
36                 return false;
37         }
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.
41                 output.Close();
42                 return false;
43         }
44
45         output.Close();
46         return true;
47 }