2010-05-21 03:13:36 +02:00
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
/// @file option.h
|
|
|
|
/// @brief Public interface for option values.
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
2011-07-15 06:04:34 +02:00
|
|
|
#pragma once
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
#include <iosfwd>
|
2011-07-16 05:36:42 +02:00
|
|
|
#include <map>
|
2013-06-10 15:58:13 +02:00
|
|
|
#include <memory>
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs_fwd.h>
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2010-06-11 04:25:07 +02:00
|
|
|
namespace json {
|
|
|
|
class UnknownElement;
|
2011-10-18 00:00:38 +02:00
|
|
|
typedef std::map<std::string, UnknownElement> Object;
|
2010-06-11 04:25:07 +02:00
|
|
|
}
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
namespace agi {
|
2011-07-15 06:04:34 +02:00
|
|
|
class OptionValue;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2014-03-31 18:54:20 +02:00
|
|
|
using OptionValueMap = std::map<std::string, std::unique_ptr<OptionValue>>;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
class Options {
|
2011-01-04 05:24:40 +01:00
|
|
|
public:
|
|
|
|
/// Options class settings.
|
|
|
|
enum OptionSetting {
|
|
|
|
NONE = 0x000, ///< Do nothing (default)
|
2011-12-28 22:27:06 +01:00
|
|
|
FLUSH_SKIP = 0x001 ///< Skip writing the config file to disk
|
2011-01-04 05:24:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2010-05-21 03:13:36 +02:00
|
|
|
/// Internal OptionValueMap
|
|
|
|
OptionValueMap values;
|
|
|
|
|
|
|
|
/// User config (file that will be written to disk)
|
2013-01-04 16:01:50 +01:00
|
|
|
const agi::fs::path config_file;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-01-04 05:24:40 +01:00
|
|
|
/// Settings.
|
|
|
|
const OptionSetting setting;
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
/// @brief Load a config file into the Options object.
|
|
|
|
/// @param config Config to load.
|
2011-12-22 22:12:15 +01:00
|
|
|
/// @param ignore_errors Log invalid entires in the option file and continue rather than throwing an exception
|
|
|
|
void LoadConfig(std::istream& stream, bool ignore_errors = false);
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// @brief Constructor
|
|
|
|
/// @param file User config that will be loaded from and written back to.
|
|
|
|
/// @param default_config Default configuration.
|
2014-03-21 22:02:49 +01:00
|
|
|
Options(agi::fs::path const& file, std::pair<const char *, size_t> default_config, const OptionSetting setting = NONE);
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2014-03-31 19:16:59 +02:00
|
|
|
template<size_t N>
|
|
|
|
Options(agi::fs::path const& file, const char (&default_config)[N], const OptionSetting setting = NONE)
|
2014-05-25 16:41:21 +02:00
|
|
|
: Options(file, {default_config, N - 1}, setting) { }
|
2014-03-31 19:16:59 +02:00
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
/// Destructor
|
|
|
|
~Options();
|
|
|
|
|
|
|
|
/// @brief Get an option by name.
|
|
|
|
/// @param name Option to get.
|
|
|
|
/// Get an option value object by name throw an internal exception if the option is not found.
|
|
|
|
OptionValue* Get(const std::string &name);
|
|
|
|
|
|
|
|
/// @brief Next configuration file to load.
|
|
|
|
/// @param[in] src Stream to load from.
|
2011-12-22 22:11:29 +01:00
|
|
|
/// Load next config which will supersede any values from previous configs
|
2010-05-21 03:13:36 +02:00
|
|
|
/// can be called as many times as required, but only after ConfigDefault() and
|
|
|
|
/// before ConfigUser()
|
|
|
|
void ConfigNext(std::istream &stream);
|
|
|
|
|
|
|
|
/// @brief Set user config file.
|
2011-12-22 22:11:29 +01:00
|
|
|
/// Set the user configuration file and read options from it, closes all
|
|
|
|
/// possible config file loading and sets the file to write to.
|
2010-05-21 03:13:36 +02:00
|
|
|
void ConfigUser();
|
|
|
|
|
2011-12-22 22:11:29 +01:00
|
|
|
/// Write the user configuration to disk, throws an exception if something goes wrong.
|
2012-12-16 22:18:18 +01:00
|
|
|
void Flush() const;
|
2010-05-21 03:13:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace agi
|