mirror of
https://github.com/davidgiven/fluxengine.git
synced 2025-10-31 11:17:01 -07:00
Move config file loading into config.cc.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#include "lib/globals.h"
|
||||
#include "lib/config.h"
|
||||
#include "lib/proto.h"
|
||||
#include <fstream>
|
||||
#include <google/protobuf/text_format.h>
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
31
lib/flags.cc
31
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<std::string, const ConfigProto*>& 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<std::string, const ConfigProto*>& configFiles)
|
||||
{
|
||||
globalConfig()->MergeFrom(parseSingleConfigFile(filename, configFiles));
|
||||
}
|
||||
|
||||
void FlagGroup::checkInitialised() const
|
||||
{
|
||||
if (!_initialised)
|
||||
|
||||
@@ -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<std::string, const ConfigProto*>& configFiles);
|
||||
|
||||
/* Modify the current config to engage the named option. */
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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())
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user