From 75bec507b3815f8a101de265bd353cfdb289bc6d Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 21 Nov 2012 08:44:24 +0000 Subject: Configuration file support. Change "jack-client" option to "jack-name". Print more informative info log messages. git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@4841 a436a847-0d15-0410-975c-d299462d15a1 --- src/Configuration.cpp | 118 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 89 insertions(+), 29 deletions(-) (limited to 'src/Configuration.cpp') diff --git a/src/Configuration.cpp b/src/Configuration.cpp index a7f45e7f..d453d43b 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -16,8 +16,14 @@ #include +#include +#include + #include "ingen/Configuration.hpp" #include "raul/fmt.hpp" +#include "sord/sordmm.hpp" + +#define NS_INGEN "http://drobilla.net/ns/ingen#" namespace Ingen { @@ -37,42 +43,36 @@ Configuration::Configuration() " ingen -egl foo.ingen # Run an engine and a GUI and load a graph") , _max_name_length(0) { - add("client-port", 'C', "Client port", INT, Value()); - add("connect", 'c', "Connect to engine URI", STRING, Value("unix:///tmp/ingen.sock")); - add("engine", 'e', "Run (JACK) engine", BOOL, Value(false)); - add("engine-port", 'E', "Engine listen port", INT, Value(16180)); - add("socket", 'S', "Engine socket path", STRING, Value("/tmp/ingen.sock")); - add("gui", 'g', "Launch the GTK graphical interface", BOOL, Value(false)); - add("help", 'h', "Print this help message", BOOL, Value(false)); - add("jack-client", 'n', "JACK client name", STRING, Value("ingen")); - add("jack-server", 's', "JACK server name", STRING, Value("")); - add("uuid", 'u', "JACK session UUID", STRING, Value()); - add("load", 'l', "Load graph", STRING, Value()); - add("packet-size", 'k', "Maximum UDP packet size", INT, Value(4096)); - add("path", 'L', "Target path for loaded graph", STRING, Value()); - add("queue-size", 'q', "Event queue size", INT, Value(4096)); - add("run", 'r', "Run script", STRING, Value()); + add("clientPort", "client-port", 'C', "Client port", INT, Value()); + add("connect", "connect", 'c', "Connect to engine URI", STRING, Value("unix:///tmp/ingen.sock")); + add("engine", "engine", 'e', "Run (JACK) engine", BOOL, Value(false)); + add("enginePort", "engine-port", 'E', "Engine listen port", INT, Value(16180)); + add("socket", "socket", 'S', "Engine socket path", STRING, Value("/tmp/ingen.sock")); + add("gui", "gui", 'g', "Launch the GTK graphical interface", BOOL, Value(false)); + add("", "help", 'h', "Print this help message", BOOL, Value(false)); + add("jackName", "jack-name", 'n', "JACK name", STRING, Value("ingen")); + add("jackServer", "jack-server", 's', "JACK server name", STRING, Value("")); + add("uuid", "uuid", 'u', "JACK session UUID", STRING, Value()); + add("load", "load", 'l', "Load graph", STRING, Value()); + add("path", "path", 'L', "Target path for loaded graph", STRING, Value()); + add("queueSize", "queue-size", 'q', "Event queue size", INT, Value(4096)); + add("run", "run", 'r', "Run script", STRING, Value()); } -/** Add a configuration option. - * - * @param name Long name (without leading "--") - * @param letter Short name (without leading "-") - * @param desc Description - * @param type Type - * @param value Default value - */ Configuration& -Configuration::add( - const std::string& name, - char letter, - const std::string& desc, - const OptionType type, - const Value& value) +Configuration::add(const std::string& key, + const std::string& name, + char letter, + const std::string& desc, + const OptionType type, + const Value& value) { assert(value.type() == type || value.type() == 0); _max_name_length = std::max(_max_name_length, name.length()); _options.insert(make_pair(name, Option(name, letter, desc, type, value))); + if (!key.empty()) { + _keys.insert(make_pair(key, name)); + } if (letter != '\0') { _short_names.insert(make_pair(letter, name)); } @@ -179,6 +179,66 @@ Configuration::parse(int argc, char** argv) throw (Configuration::CommandLineErr } } +bool +Configuration::load(const std::string& path) +{ + if (!Glib::file_test(path, Glib::FILE_TEST_EXISTS)) { + return false; + } + + SerdNode node = serd_node_new_file_uri( + (const uint8_t*)path.c_str(), NULL, NULL, true); + const std::string uri((const char*)node.buf); + + Sord::World world; + Sord::Model model(world, uri, SORD_SPO, false); + SerdEnv* env = serd_env_new(&node); + model.load_file(env, SERD_TURTLE, uri, uri); + + Sord::Node nodemm(world, Sord::Node::URI, (const char*)node.buf); + Sord::Node nil; + for (Sord::Iter i = model.find(nodemm, nil, nil); !i.end(); ++i) { + const Sord::Node& pred = i.get_predicate(); + const Sord::Node& obj = i.get_object(); + if (pred.to_string().substr(0, sizeof(NS_INGEN) - 1) == NS_INGEN) { + const std::string key = pred.to_string().substr(sizeof(NS_INGEN) - 1); + const Keys::iterator k = _keys.find(key); + if (k != _keys.end() && obj.type() == Sord::Node::LITERAL) { + set_value_from_string(_options.find(k->second)->second, + obj.to_string()); + } + } + } + + serd_node_free(&node); + serd_env_free(env); + return true; +} + +std::list +Configuration::load_default(const std::string& app, const std::string& file) +{ + std::list loaded; + + const std::vector dirs = Glib::get_system_config_dirs(); + for (std::vector::const_iterator i = dirs.begin(); + i != dirs.end(); + ++i) { + const std::string path = Glib::build_filename(*i, app, file); + if (load(path)) { + loaded.push_back(path); + } + } + + const std::string path = Glib::build_filename( + Glib::get_user_config_dir(), app, file); + if (load(path)) { + loaded.push_back(path); + } + + return loaded; +} + void Configuration::print(std::ostream& os, const std::string mime_type) const { -- cgit v1.2.1