diff --git a/src/options.cpp b/src/options.cpp index 9eb6f94..30d6773 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -23,7 +23,10 @@ #include #include +#include +#include #include +#include namespace epubgrep { @@ -53,6 +56,11 @@ po::variables_map parse_options(int argc, char *argv[]) // clang-format on po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); + + std::ifstream configfile(get_config_path()); + po::store(po::parse_config_file(configfile, desc, true), vm); + configfile.close(); + po::notify(vm); if (vm.count("help") != 0) @@ -75,4 +83,34 @@ po::variables_map parse_options(int argc, char *argv[]) return vm; } +std::string get_config_path() +{ + auto get_env = [](std::string const &name) + { + const char *env = std::getenv(name.c_str()); + if (env != nullptr) + { + return env; + } + + return ""; + }; + + std::string path{get_env("XDG_CONFIG_HOME")}; + if (path.empty()) + { + path = get_env("HOME"); + if (!path.empty()) + { + path += "/.config"; + } + } + if (!path.empty()) + { + path += "/"; + } + + return path += "epubgrep.conf"; +} + } // namespace epubgrep diff --git a/src/options.hpp b/src/options.hpp index aea8f2f..8f6d197 100644 --- a/src/options.hpp +++ b/src/options.hpp @@ -19,6 +19,8 @@ #include +#include + namespace epubgrep { @@ -27,6 +29,15 @@ namespace po = boost::program_options; //! Parse options and return them. po::variables_map parse_options(int argc, char *argv[]); +/*! + * @brief Returns the path of the config file. + * + * Tries these paths: ${XDG_CONFIG_HOME}/epubgrep.conf + * ${HOME}/.config/epubgrep.conf + * epubgrep.conf + */ +std::string get_config_path(); + } // namespace epubgrep #endif // EPUBGREP_OPTIONS_HPP