From 36eca4f9642b540e5c7fdf59861dcdb7a8b622e6 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 6 Jan 2019 13:19:57 +0100 Subject: [PATCH] Move protobuf text serialization into a shared file. --- nageru/input_mapping.cpp | 29 +++---------------------- nageru/midi_mapper.cpp | 32 +++------------------------ shared/meson.build | 5 +++-- shared/text_proto.cpp | 47 ++++++++++++++++++++++++++++++++++++++++ shared/text_proto.h | 19 ++++++++++++++++ 5 files changed, 75 insertions(+), 57 deletions(-) create mode 100644 shared/text_proto.cpp create mode 100644 shared/text_proto.h diff --git a/nageru/input_mapping.cpp b/nageru/input_mapping.cpp index 45b6009..dfcc97c 100644 --- a/nageru/input_mapping.cpp +++ b/nageru/input_mapping.cpp @@ -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 &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 &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: diff --git a/nageru/midi_mapper.cpp b/nageru/midi_mapper.cpp index c6d2e9c..7b67fd1 100644 --- a/nageru/midi_mapper.cpp +++ b/nageru/midi_mapper.cpp @@ -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) diff --git a/shared/meson.build b/shared/meson.build index 9f07842..22d4c19 100644 --- a/shared/meson.build +++ b/shared/meson.build @@ -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 index 0000000..39be68e --- /dev/null +++ b/shared/text_proto.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include + +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 index 0000000..3a206cf --- /dev/null +++ b/shared/text_proto.h @@ -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 + +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) -- 2.39.2