]> git.sesse.net Git - nageru/commitdiff
Move protobuf text serialization into a shared file.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 6 Jan 2019 12:19:57 +0000 (13:19 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 6 Jan 2019 12:19:57 +0000 (13:19 +0100)
nageru/input_mapping.cpp
nageru/midi_mapper.cpp
shared/meson.build
shared/text_proto.cpp [new file with mode: 0644]
shared/text_proto.h [new file with mode: 0644]

index 45b60095e5af99cadecb01cb88389478974d5cc7..dfcc97ce5e6613cd8c51f12d55a1be49433daf5f 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "audio_mixer.h" 
 #include "state.pb.h"
+#include "shared/text_proto.h"
 
 using namespace std;
 using namespace google::protobuf;
@@ -54,39 +55,15 @@ bool save_input_mapping_to_file(const map<DeviceSpec, DeviceInfo> &devices, cons
                }
        }
 
-       // Save to disk. We use the text format because it's friendlier
-       // for a user to look at and edit.
-       int fd = open(filename.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0666);
-       if (fd == -1) {
-               perror(filename.c_str());
-               return false;
-       }
-       io::FileOutputStream output(fd);  // Takes ownership of fd.
-       if (!TextFormat::Print(mapping_proto, &output)) {
-               // TODO: Don't overwrite the old file (if any) on error.
-               output.Close();
-               return false;
-       }
-
-       output.Close();
-       return true;
+       return save_proto_to_file(mapping_proto, filename);
 }
 
 bool load_input_mapping_from_file(const map<DeviceSpec, DeviceInfo> &devices, const string &filename, InputMapping *new_mapping)
 {
-       // Read and parse the protobuf from disk.
-       int fd = open(filename.c_str(), O_RDONLY);
-       if (fd == -1) {
-               perror(filename.c_str());
-               return false;
-       }
-       io::FileInputStream input(fd);  // Takes ownership of fd.
        InputMappingProto mapping_proto;
-       if (!TextFormat::Parse(&input, &mapping_proto)) {
-               input.Close();
+       if (!load_proto_from_file(filename, &mapping_proto)) {
                return false;
        }
-       input.Close();
 
        // Map devices in the proto to our current ones:
 
index c6d2e9c5f6c6ac8315311e35d5e7412e779d4f9b..7b67fd1d524a71d98c7efc2ec94d898fb22ec306 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "audio_mixer.h"
 #include "midi_mapping.pb.h"
+#include "shared/text_proto.h"
 
 using namespace google::protobuf;
 using namespace std;
@@ -50,39 +51,12 @@ MIDIMapper::~MIDIMapper() {}
 
 bool load_midi_mapping_from_file(const string &filename, MIDIMappingProto *new_mapping)
 {
-       // Read and parse the protobuf from disk.
-       int fd = open(filename.c_str(), O_RDONLY);
-       if (fd == -1) {
-               perror(filename.c_str());
-               return false;
-       }
-       io::FileInputStream input(fd);  // Takes ownership of fd.
-       if (!TextFormat::Parse(&input, new_mapping)) {
-               input.Close();
-               return false;
-       }
-       input.Close();
-       return true;
+       return load_proto_from_file(filename, new_mapping);
 }
 
 bool save_midi_mapping_to_file(const MIDIMappingProto &mapping_proto, const string &filename)
 {
-       // Save to disk. We use the text format because it's friendlier
-       // for a user to look at and edit.
-       int fd = open(filename.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0666);
-       if (fd == -1) {
-               perror(filename.c_str());
-               return false;
-       }
-       io::FileOutputStream output(fd);  // Takes ownership of fd.
-       if (!TextFormat::Print(mapping_proto, &output)) {
-               // TODO: Don't overwrite the old file (if any) on error.
-               output.Close();
-               return false;
-       }
-
-       output.Close();
-       return true;
+       return save_proto_to_file(mapping_proto, filename);
 }
 
 void MIDIMapper::set_midi_mapping(const MIDIMappingProto &new_mapping)
index 9f078428e533739544f46e3169c29a2abd0fd3ed..22d4c19731c0bd6daf382791f4360cab24421f32 100644 (file)
@@ -1,6 +1,7 @@
 qt5 = import('qt5')
 shared_qt5deps = dependency('qt5', modules: ['Core', 'Gui', 'Widgets', 'OpenGL'])
 libmicrohttpddep = dependency('libmicrohttpd')
+protobufdep = dependency('protobuf')
 
 # Preprocess Qt as needed.
 qt_files = qt5.preprocess(
@@ -8,13 +9,13 @@ qt_files = qt5.preprocess(
        ui_files: ['aboutdialog.ui'],
        dependencies: shared_qt5deps)
 
-srcs = ['memcpy_interleaved.cpp', 'metacube2.cpp', 'ffmpeg_raii.cpp', 'mux.cpp', 'metrics.cpp', 'context.cpp', 'httpd.cpp', 'disk_space_estimator.cpp', 'read_file.cpp']
+srcs = ['memcpy_interleaved.cpp', 'metacube2.cpp', 'ffmpeg_raii.cpp', 'mux.cpp', 'metrics.cpp', 'context.cpp', 'httpd.cpp', 'disk_space_estimator.cpp', 'read_file.cpp', 'text_proto.cpp']
 
 # Qt objects.
 srcs += qt_files
 srcs += ['aboutdialog.cpp']
 
-shared = static_library('shared', srcs, include_directories: top_include, dependencies: [shared_qt5deps, libmicrohttpddep])
+shared = static_library('shared', srcs, include_directories: top_include, dependencies: [shared_qt5deps, libmicrohttpddep, protobufdep])
 shareddep = declare_dependency(
    include_directories: top_include,
    link_with: shared)
diff --git a/shared/text_proto.cpp b/shared/text_proto.cpp
new file mode 100644 (file)
index 0000000..39be68e
--- /dev/null
@@ -0,0 +1,47 @@
+#include <fcntl.h>
+#include <stdio.h>
+#include <string>
+#include <google/protobuf/descriptor.h>
+#include <google/protobuf/io/zero_copy_stream_impl.h>
+#include <google/protobuf/message.h>
+#include <google/protobuf/text_format.h>
+
+using namespace std;
+using namespace google::protobuf;
+
+bool load_proto_from_file(const string &filename, Message *msg)
+{
+       // Read and parse the protobuf from disk.
+       int fd = open(filename.c_str(), O_RDONLY);
+       if (fd == -1) {
+               perror(filename.c_str());
+               return false;
+       }
+       io::FileInputStream input(fd);  // Takes ownership of fd.
+       if (!TextFormat::Parse(&input, msg)) {
+               input.Close();
+               return false;
+       }
+       input.Close();
+       return true;
+}
+
+bool save_proto_to_file(const Message &msg, const string &filename)
+{
+       // Save to disk. We use the text format because it's friendlier
+       // for a user to look at and edit.
+       int fd = open(filename.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0666);
+       if (fd == -1) {
+               perror(filename.c_str());
+               return false;
+       }
+       io::FileOutputStream output(fd);  // Takes ownership of fd.
+       if (!TextFormat::Print(msg, &output)) {
+               // TODO: Don't overwrite the old file (if any) on error.
+               output.Close();
+               return false;
+       }
+
+       output.Close();
+       return true;
+}
diff --git a/shared/text_proto.h b/shared/text_proto.h
new file mode 100644 (file)
index 0000000..3a206cf
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef _TEXT_PROTO_H
+#define _TEXT_PROTO_H 1
+
+// Utility functions to serialize protobufs on disk.
+// We use the text format because it's friendlier
+// for a user to look at and edit.
+
+#include <string>
+
+namespace google {
+namespace protobuf {
+class Message;
+}  // namespace protobuf
+}  // namespace google
+
+bool load_proto_from_file(const std::string &filename, google::protobuf::Message *msg);
+bool save_proto_to_file(const google::protobuf::Message &msg, const std::string &filename);
+
+#endif  // !defined(_TEXT_PROTO_H)