summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--raul/Configuration.hpp195
-rw-r--r--src/Configuration.cpp178
-rw-r--r--wscript1
3 files changed, 0 insertions, 374 deletions
diff --git a/raul/Configuration.hpp b/raul/Configuration.hpp
deleted file mode 100644
index 3449767..0000000
--- a/raul/Configuration.hpp
+++ /dev/null
@@ -1,195 +0,0 @@
-/*
- This file is part of Raul.
- Copyright 2007-2012 David Robillard <http://drobilla.net>
-
- Raul is free software: you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation, either version 3 of the License, or any later version.
-
- Raul is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Raul. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#ifndef RAUL_CONFIGURATION_HPP
-#define RAUL_CONFIGURATION_HPP
-
-#include <assert.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <list>
-#include <map>
-#include <ostream>
-#include <string>
-
-#include "raul/Exception.hpp"
-
-namespace Raul {
-
-/** Program configuration (command line options and/or configuration file).
- *
- * \ingroup raul
- */
-class Configuration {
-public:
- Configuration(const std::string& shortdesc,
- const std::string& desc)
- : _shortdesc(shortdesc)
- , _desc(desc)
- , _max_name_length(0)
- {}
-
- enum OptionType {
- NOTHING,
- BOOL,
- INT,
- STRING
- };
-
- class Value {
- public:
- Value() : _type(NOTHING) { _val._string = NULL; }
- Value(bool v) : _type(BOOL) { _val._bool = v; }
- Value(int v) : _type(INT) { _val._int = v; }
-
- Value(const char* v) : _type(STRING) {
- const size_t len = strlen(v);
- _val._string = (char*)calloc(len + 1, 1);
- memcpy(_val._string, v, len + 1);
- }
-
- Value(const std::string& v) : _type(STRING) {
- _val._string = (char*)calloc(v.length() + 1, 1);
- memcpy(_val._string, v.c_str(), v.length() + 1);
- }
-
- Value(const Value& copy)
- : _type(copy._type)
- , _val(copy._val)
- {
- if (_type == STRING) {
- const size_t len = strlen(copy.get_string());
- _val._string = (char*)malloc(len + 1);
- memcpy(_val._string, copy.get_string(), len + 1);
- }
- }
-
- Value& operator=(const Value& other)
- {
- if (&other == this) {
- return *this;
- }
- if (_type == STRING) {
- free(_val._string);
- }
- _type = other._type;
- _val = other._val;
- if (_type == STRING) {
- const size_t len = strlen(other.get_string());
- _val._string = (char*)malloc(len + 1);
- memcpy(_val._string, other.get_string(), len + 1);
- }
- return *this;
- }
-
- ~Value() {
- if (_type == STRING) {
- free(_val._string);
- }
- }
-
- inline OptionType type() const { return _type; }
- inline bool is_valid() const { return _type != NOTHING; }
-
- inline int32_t get_int() const { assert(_type == INT); return _val._int; }
- inline bool get_bool() const { assert(_type == BOOL); return _val._bool; }
- inline const char* get_string() const { assert(_type == STRING); return _val._string; }
-
- private:
- OptionType _type;
- union {
- bool _bool;
- int32_t _int;
- char* _string;
- } _val;
- };
-
- Configuration& add(const std::string& name,
- char letter,
- const std::string& desc,
- OptionType type,
- const Value& value);
-
- void print_usage(const std::string& program, std::ostream& os);
-
- struct CommandLineError : public Exception {
- explicit CommandLineError(const std::string& m) : Exception(m) {}
- };
-
- void parse(int argc, char** argv) throw (CommandLineError);
-
- void print(std::ostream& os, const std::string mime_type="text/plain") const;
-
- const Value& option(const std::string& long_name) const;
- bool set(const std::string& long_name, const Value& value);
-
- const std::list<std::string>& files() const { return _files; }
-
-private:
- struct Option {
- public:
- Option(const std::string& n, char l, const std::string& d,
- const OptionType type, const Value& def)
- : name(n), letter(l), desc(d), type(type), default_value(def), value(def)
- {}
-
- std::string name;
- char letter;
- std::string desc;
- OptionType type;
- Value default_value;
- Value value;
- };
-
- struct OptionNameOrder {
- inline bool operator()(const Option& a, const Option& b) {
- return a.name < b.name;
- }
- };
-
- typedef std::map<std::string, Option> Options;
- typedef std::map<char, std::string> ShortNames;
- typedef std::list<std::string> Files;
-
- int set_value_from_string(Configuration::Option& option, const std::string& value)
- throw (Configuration::CommandLineError);
-
- const std::string _shortdesc;
- const std::string _desc;
- Options _options;
- ShortNames _short_names;
- Files _files;
- size_t _max_name_length;
-};
-
-} // namespace Raul
-
-static inline std::ostream&
-operator<<(std::ostream& os, const Raul::Configuration::Value& value)
-{
- switch (value.type()) {
- case Raul::Configuration::NOTHING: return os << "(nil)";
- case Raul::Configuration::INT: return os << value.get_int();
- case Raul::Configuration::BOOL: return os << (value.get_bool() ? "true" : "false");
- case Raul::Configuration::STRING: return os << value.get_string();
- }
- return os;
-}
-
-#endif // RAUL_CONFIGURATION_HPP
-
diff --git a/src/Configuration.cpp b/src/Configuration.cpp
deleted file mode 100644
index 151be45..0000000
--- a/src/Configuration.cpp
+++ /dev/null
@@ -1,178 +0,0 @@
-/*
- This file is part of Raul.
- Copyright 2007-2012 David Robillard <http://drobilla.net>
-
- Raul is free software: you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation, either version 3 of the License, or any later version.
-
- Raul is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Raul. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#include <algorithm>
-#include <string>
-
-#include "raul/Configuration.hpp"
-
-using std::endl;
-using std::string;
-
-namespace Raul {
-
-/** 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)
-{
- 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 (letter != '\0') {
- _short_names.insert(make_pair(letter, name));
- }
- return *this;
-}
-
-void
-Configuration::print_usage(const std::string& program, std::ostream& os)
-{
- os << "Usage: " << program << " [OPTION]..." << endl;
- os << _shortdesc << endl << endl;
- os << _desc << endl << endl;
- os << "Options:" << endl;
- for (Options::iterator o = _options.begin(); o != _options.end(); ++o) {
- Option& option = o->second;
- os << " ";
- if (option.letter != '\0')
- os << "-" << option.letter << ", ";
- else
- os << " ";
- os.width(_max_name_length + 4);
- os << std::left << (string("--") + o->first);
- os << option.desc << endl;
- }
-}
-
-int
-Configuration::set_value_from_string(Configuration::Option& option,
- const std::string& value)
- throw (Configuration::CommandLineError)
-{
- int intval = 0;
- char* endptr = NULL;
- switch (option.type) {
- case INT:
- intval = static_cast<int>(strtol(value.c_str(), &endptr, 10));
- if (endptr && *endptr == '\0') {
- option.value = Value(intval);
- } else {
- throw CommandLineError("option `" + option.name
- + "' has non-integer value `" + value + "'");
- }
- break;
- case STRING:
- option.value = Value(value.c_str());
- assert(option.value.type() == STRING);
- break;
- default:
- throw CommandLineError(string("bad option type `--") + option.name + "'");
- }
- return EXIT_SUCCESS;
-}
-
-/** Parse command line arguments. */
-void
-Configuration::parse(int argc, char** argv) throw (Configuration::CommandLineError)
-{
- for (int i = 1; i < argc; ++i) {
- if (argv[i][0] != '-' || !strcmp(argv[i], "-")) {
- _files.push_back(argv[i]);
- } else if (argv[i][1] == '-') {
- const string name = string(argv[i]).substr(2);
- Options::iterator o = _options.find(name);
- if (o == _options.end()) {
- throw CommandLineError(string("unrecognized option `--") + name + "'");
- }
- if (o->second.type == BOOL) {
- o->second.value = Value(true);
- } else {
- if (++i >= argc)
- throw CommandLineError("missing value for `--" + name + "'");
- set_value_from_string(o->second, argv[i]);
- }
- } else {
- const size_t len = strlen(argv[i]);
- for (size_t j = 1; j < len; ++j) {
- char letter = argv[i][j];
- ShortNames::iterator n = _short_names.find(letter);
- if (n == _short_names.end())
- throw CommandLineError(string("unrecognized option `-") + letter + "'");
- Options::iterator o = _options.find(n->second);
- if (j < len - 1) {
- if (o->second.type != BOOL)
- throw CommandLineError(string("missing value for `-") + letter + "'");
- o->second.value = Value(true);
- } else {
- if (o->second.type == BOOL) {
- o->second.value = Value(true);
- } else {
- if (++i >= argc)
- throw CommandLineError(string("missing value for `-") + letter + "'");
- set_value_from_string(o->second, argv[i]);
- }
- }
- }
- }
- }
-}
-
-void
-Configuration::print(std::ostream& os, const std::string mime_type) const
-{
- for (Options::const_iterator o = _options.begin(); o != _options.end(); ++o) {
- const Option& option = o->second;
- os << o->first << " = " << option.value << endl;
- }
-}
-
-const Raul::Configuration::Value&
-Configuration::option(const std::string& long_name) const
-{
- static const Value nil;
- Options::const_iterator o = _options.find(long_name);
- if (o == _options.end()) {
- return nil;
- } else {
- return o->second.value;
- }
-}
-
-bool
-Configuration::set(const std::string& long_name, const Value& value)
-{
- Options::iterator o = _options.find(long_name);
- if (o != _options.end()) {
- o->second.value = value;
- return true;
- }
- return false;
-}
-
-} // namespace Raul
-
diff --git a/wscript b/wscript
index bcce16c..78cab4c 100644
--- a/wscript
+++ b/wscript
@@ -97,7 +97,6 @@ def build(bld):
'GLIB GTHREAD', subst_dict=dict)
lib_source = '''
- src/Configuration.cpp
src/Thread.cpp
'''