diff --git a/lib/config.cc b/lib/config.cc index fc08886c..b9b74a71 100644 --- a/lib/config.cc +++ b/lib/config.cc @@ -1,6 +1,8 @@ #include "lib/globals.h" #include "lib/config.h" #include "lib/proto.h" +#include +#include static Config config; @@ -24,6 +26,11 @@ Config::operator ConfigProto&() const return globalConfigProto(); } +void Config::clear() +{ + this->clear(); +} + void Config::set(std::string key, std::string value) { setProtoByString(*this, key, value); @@ -33,3 +40,30 @@ std::string Config::get(std::string key) { return getProtoByString(*this, key); } + +static ConfigProto loadSingleConfigFile(std::string filename) +{ + const auto& it = formats.find(filename); + if (it != formats.end()) + return *it->second; + else + { + std::ifstream f(filename, std::ios::out); + if (f.fail()) + error("Cannot open '{}': {}", filename, strerror(errno)); + + std::ostringstream ss; + ss << f.rdbuf(); + + ConfigProto config; + if (!google::protobuf::TextFormat::MergeFromString( + ss.str(), globalConfig())) + error("couldn't load external config proto"); + return config; + } +} + +void Config::readConfigFile(std::string filename) +{ + globalConfig()->MergeFrom(loadSingleConfigFile(filename)); +} diff --git a/lib/config.h b/lib/config.h index eded688a..00e42e3b 100644 --- a/lib/config.h +++ b/lib/config.h @@ -11,6 +11,9 @@ public: operator ConfigProto* () const; operator ConfigProto& () const; + void clear(); + void readConfigFile(std::string filename); + void set(std::string key, std::string value); std::string get(std::string key); }; diff --git a/lib/flags.cc b/lib/flags.cc index 4bbd1d5c..d7087888 100644 --- a/lib/flags.cc +++ b/lib/flags.cc @@ -271,40 +271,11 @@ void FlagGroup::parseFlagsWithConfigFiles(int argc, argv, [&](const auto& filename) { - parseConfigFile(filename, configFiles); + globalConfig().readConfigFile(filename); return true; }); } -ConfigProto FlagGroup::parseSingleConfigFile(const std::string& filename, - const std::map& configFiles) -{ - const auto& it = configFiles.find(filename); - if (it != configFiles.end()) - return *it->second; - else - { - std::ifstream f(filename, std::ios::out); - if (f.fail()) - error("Cannot open '{}': {}", filename, strerror(errno)); - - std::ostringstream ss; - ss << f.rdbuf(); - - ConfigProto config; - if (!google::protobuf::TextFormat::MergeFromString( - ss.str(), globalConfig())) - error("couldn't load external config proto"); - return config; - } -} - -void FlagGroup::parseConfigFile(const std::string& filename, - const std::map& configFiles) -{ - globalConfig()->MergeFrom(parseSingleConfigFile(filename, configFiles)); -} - void FlagGroup::checkInitialised() const { if (!_initialised) diff --git a/lib/flags.h b/lib/flags.h index 43ff3c16..9d2a3f48 100644 --- a/lib/flags.h +++ b/lib/flags.h @@ -42,7 +42,7 @@ public: /* Load a top-level config file (or internal config file), expanding * includes. */ - static void parseConfigFile(const std::string& filename, + static void readConfigFile(const std::string& filename, const std::map& configFiles); /* Modify the current config to engage the named option. */ diff --git a/lib/fluxsource/fl2fluxsource.cc b/lib/fluxsource/fl2fluxsource.cc index fe14c317..ec5c6fb2 100644 --- a/lib/fluxsource/fl2fluxsource.cc +++ b/lib/fluxsource/fl2fluxsource.cc @@ -37,9 +37,9 @@ public: { _proto = loadFl2File(_config.filename()); - DriveProto d = _proto.drive(); - d.MergeFrom(globalConfig()->drive()); - *(globalConfig()->mutable_drive()) = d; + DriveProto d = _proto.drive(); + d.MergeFrom(globalConfig()->drive()); + *(globalConfig()->mutable_drive()) = d; } public: diff --git a/src/fluxengine.cc b/src/fluxengine.cc index 40f9e483..9b14f6b7 100644 --- a/src/fluxengine.cc +++ b/src/fluxengine.cc @@ -138,19 +138,18 @@ void showProfiles(const std::string& command, for (const auto& it : profiles) { - const auto& config = *it.second; - if (!config.is_extension()) - std::cout << fmt::format( - " {}: {}\n", it.first, config.shortname()); + const auto& c = *it.second; + if (!c.is_extension()) + std::cout << fmt::format(" {}: {}\n", it.first, c.shortname()); } std::cout << "Available profile options include:\n"; for (const auto& it : profiles) { - const auto& config = *it.second; - if (config.is_extension() && (it.first[0] != '_')) - std::cout << fmt::format(" {}: {}\n", it.first, config.comment()); + const auto& c = *it.second; + if (c.is_extension() && (it.first[0] != '_')) + std::cout << fmt::format(" {}: {}\n", it.first, c.comment()); } std::cout << "Profiles and extensions may also be textpb files .\n"; diff --git a/src/gui/idlepanel.cc b/src/gui/idlepanel.cc index 631acc28..56e4f82d 100644 --- a/src/gui/idlepanel.cc +++ b/src/gui/idlepanel.cc @@ -180,9 +180,9 @@ public: if (formatSelection == wxNOT_FOUND) error("no format selected"); - globalConfig()->Clear(); + globalConfig().clear(); auto formatName = _formatNames[formatChoice->GetSelection()]; - FlagGroup::parseConfigFile(formatName, formats); + globalConfig().readConfigFile(formatName); /* Apply any format options. */ @@ -261,7 +261,7 @@ public: setProtoByString(globalConfig(), key, value); } else - FlagGroup::parseConfigFile(setting, formats); + globalConfig().readConfigFile(setting); } } @@ -589,10 +589,10 @@ private: formatOptionsContainer, wxID_ANY, "(no format selected)")); else { - globalConfig()->Clear(); + globalConfig().clear(); std::string formatName = _formatNames[formatChoice->GetSelection()]; - FlagGroup::parseConfigFile(formatName, formats); + globalConfig().readConfigFile(formatName); for (auto& group : globalConfig()->option_group()) { diff --git a/tests/build.mk b/tests/build.mk index 9c11a98e..48156fc6 100644 --- a/tests/build.mk +++ b/tests/build.mk @@ -18,6 +18,7 @@ $(call use-library, $(OBJDIR)/tests/$1.exe, $(OBJDIR)/tests/$1.o, FATFS) $(call use-library, $(OBJDIR)/tests/$1.exe, $(OBJDIR)/tests/$1.o, ADFLIB) $(call use-library, $(OBJDIR)/tests/$1.exe, $(OBJDIR)/tests/$1.o, HFSUTILS) $(call use-library, $(OBJDIR)/tests/$1.exe, $(OBJDIR)/tests/$1.o, LIBUSBP) +$(call use-library, $(OBJDIR)/tests/$1.exe, $(OBJDIR)/tests/$1.o, LIBFORMATS) endef @@ -45,7 +46,6 @@ $(call declare-test,vfs) $(call use-library, $(OBJDIR)/tests/agg.exe, $(OBJDIR)/tests/agg.o, AGG) $(call use-library, $(OBJDIR)/tests/agg.exe, $(OBJDIR)/tests/agg.o, STB) -$(call use-library, $(OBJDIR)/tests/configs.exe, $(OBJDIR)/tests/configs.o, LIBFORMATS) $(OBJDIR)/tests/proto.exe: $(OBJDIR)/tests/testproto.o $(OBJDIR)/tests/testproto.cc: $(OBJDIR)/protoencode_TestProto.exe tests/testproto.textpb diff --git a/tests/layout.cc b/tests/layout.cc index cbf53b10..a7efe0a2 100644 --- a/tests/layout.cc +++ b/tests/layout.cc @@ -19,7 +19,7 @@ static std::string cleanup(const std::string& s) static void load_config(const std::string s) { - globalConfig()->Clear(); + globalConfig().clear(); if (!google::protobuf::TextFormat::MergeFromString( cleanup(s), globalConfig())) error("couldn't load test config"); diff --git a/tools/build.mk b/tools/build.mk index 4701c1d5..4938a7b9 100644 --- a/tools/build.mk +++ b/tools/build.mk @@ -18,6 +18,7 @@ $(call use-library, $(OBJDIR)/brother240tool.exe, $(OBJDIR)/tools/brother240tool $(call use-pkgconfig, $(OBJDIR)/upgrade-flux-file.exe, $(OBJDIR)/tools/upgrade-flux-file.o, sqlite3) $(call use-library, $(OBJDIR)/upgrade-flux-file.exe, $(OBJDIR)/tools/upgrade-flux-file.o, LIBFLUXENGINE) +$(call use-library, $(OBJDIR)/upgrade-flux-file.exe, $(OBJDIR)/tools/upgrade-flux-file.o, LIBFORMATS) $(call use-library, $(OBJDIR)/upgrade-flux-file.exe, $(OBJDIR)/tools/upgrade-flux-file.o, PROTO) brother120tool$(EXT): $(OBJDIR)/brother120tool.exe