|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 Hannes Haberl |
| 3 | + * |
| 4 | + * This file is part of GLMViz. |
| 5 | + * |
| 6 | + * GLMViz is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * GLMViz is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with GLMViz. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "xdg.hpp" |
| 21 | + |
| 22 | +#include <pwd.h> |
| 23 | +#include <unistd.h> |
| 24 | +#include <fstream> |
| 25 | +#include <sstream> |
| 26 | +#include <cstdlib> |
| 27 | + |
| 28 | +namespace xdg{ |
| 29 | + std::string config_home(){ |
| 30 | + std::string config_home; |
| 31 | + const char* cxdg_cfg_home = std::getenv("XDG_CONFIG_HOME"); |
| 32 | + if(cxdg_cfg_home != nullptr){ |
| 33 | + config_home = cxdg_cfg_home; |
| 34 | + } |
| 35 | + |
| 36 | + return config_home; |
| 37 | + } |
| 38 | + |
| 39 | + std::string default_config_home(){ |
| 40 | + std::string config_home; |
| 41 | + // get default config directory |
| 42 | + struct passwd* pw = ::getpwuid(::getuid()); |
| 43 | + config_home = pw->pw_dir; |
| 44 | + config_home += "/.config"; |
| 45 | + |
| 46 | + return config_home; |
| 47 | + } |
| 48 | + |
| 49 | + std::vector<std::string> config_dirs(){ |
| 50 | + std::string config_dirs; |
| 51 | + const char* cxdg_cfg_dirs = std::getenv("XDG_CONFIG_DIRS"); |
| 52 | + if(cxdg_cfg_dirs != nullptr){ |
| 53 | + config_dirs = cxdg_cfg_dirs; |
| 54 | + } |
| 55 | + |
| 56 | + std::vector<std::string> dirs; |
| 57 | + std::istringstream ss(config_dirs); |
| 58 | + std::string dir; |
| 59 | + |
| 60 | + while(std::getline(ss, dir, ':')){ |
| 61 | + dirs.push_back(dir); |
| 62 | + } |
| 63 | + |
| 64 | + return dirs; |
| 65 | + } |
| 66 | + |
| 67 | + std::string default_config_dir(){ return "/etc/xdg"; } |
| 68 | + |
| 69 | + bool verify_path(const std::string& path){ |
| 70 | + std::ifstream file(path); |
| 71 | + return file.good(); |
| 72 | + } |
| 73 | + |
| 74 | + std::string find_config(const std::string& path){ |
| 75 | + std::string config; |
| 76 | + // check XDG_CONFIG_HOME |
| 77 | + config = config_home() + path; |
| 78 | + if(verify_path(config)) return config; |
| 79 | + |
| 80 | + // use default value for XDG_CONFIG_HOME |
| 81 | + config = default_config_home() + path; |
| 82 | + if(verify_path(config)) return config; |
| 83 | + |
| 84 | + // check XDG_CONFIG_DIRS |
| 85 | + for(std::string& dir : config_dirs()){ |
| 86 | + config = dir + path; |
| 87 | + if(verify_path(config)) return config; |
| 88 | + } |
| 89 | + |
| 90 | + // check XDG_CONFIG_DIRS default value |
| 91 | + config = default_config_dir() + path; |
| 92 | + if(verify_path(config)) return config; |
| 93 | + |
| 94 | + // return empty string if no config file was found |
| 95 | + return ""; |
| 96 | + } |
| 97 | +} |
0 commit comments