#include "audio_mixer.h"
#include "state.pb.h"
+#include "shared/text_proto.h"
using namespace std;
using namespace google::protobuf;
}
}
- // 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:
#include "audio_mixer.h"
#include "midi_mapping.pb.h"
+#include "shared/text_proto.h"
using namespace google::protobuf;
using namespace std;
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)
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(
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)
--- /dev/null
+#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;
+}
--- /dev/null
+#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)