123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- /**
- * OpenAL cross platform audio library
- * Copyright (C) 1999-2007 by authors.
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library 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
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- * Or go to http://www.gnu.org/copyleft/lgpl.html
- */
- #include "config.h"
- #include "alconfig.h"
- #include <cstdlib>
- #include <cctype>
- #include <cstring>
- #ifdef _WIN32
- #include <windows.h>
- #include <shlobj.h>
- #endif
- #ifdef __APPLE__
- #include <CoreFoundation/CoreFoundation.h>
- #endif
- #include <algorithm>
- #include <cstdio>
- #include <string>
- #include <utility>
- #include "alfstream.h"
- #include "alstring.h"
- #include "compat.h"
- #include "core/logging.h"
- #include "strutils.h"
- #include "vector.h"
- namespace {
- struct ConfigEntry {
- std::string key;
- std::string value;
- };
- al::vector<ConfigEntry> ConfOpts;
- std::string &lstrip(std::string &line)
- {
- size_t pos{0};
- while(pos < line.length() && std::isspace(line[pos]))
- ++pos;
- line.erase(0, pos);
- return line;
- }
- bool readline(std::istream &f, std::string &output)
- {
- while(f.good() && f.peek() == '\n')
- f.ignore();
- return std::getline(f, output) && !output.empty();
- }
- std::string expdup(const char *str)
- {
- std::string output;
- std::string envval;
- while(*str != '\0')
- {
- const char *addstr;
- size_t addstrlen;
- if(str[0] != '$')
- {
- const char *next = std::strchr(str, '$');
- addstr = str;
- addstrlen = next ? static_cast<size_t>(next-str) : std::strlen(str);
- str += addstrlen;
- }
- else
- {
- str++;
- if(*str == '$')
- {
- const char *next = std::strchr(str+1, '$');
- addstr = str;
- addstrlen = next ? static_cast<size_t>(next-str) : std::strlen(str);
- str += addstrlen;
- }
- else
- {
- const bool hasbraces{(*str == '{')};
- if(hasbraces) str++;
- const char *envstart = str;
- while(std::isalnum(*str) || *str == '_')
- ++str;
- if(hasbraces && *str != '}')
- continue;
- const std::string envname{envstart, str};
- if(hasbraces) str++;
- envval = al::getenv(envname.c_str()).value_or(std::string{});
- addstr = envval.data();
- addstrlen = envval.length();
- }
- }
- if(addstrlen == 0)
- continue;
- output.append(addstr, addstrlen);
- }
- return output;
- }
- void LoadConfigFromFile(std::istream &f)
- {
- std::string curSection;
- std::string buffer;
- while(readline(f, buffer))
- {
- if(lstrip(buffer).empty())
- continue;
- if(buffer[0] == '[')
- {
- char *line{&buffer[0]};
- char *section = line+1;
- char *endsection;
- endsection = std::strchr(section, ']');
- if(!endsection || section == endsection)
- {
- ERR(" config parse error: bad line \"%s\"\n", line);
- continue;
- }
- if(endsection[1] != 0)
- {
- char *end = endsection+1;
- while(std::isspace(*end))
- ++end;
- if(*end != 0 && *end != '#')
- {
- ERR(" config parse error: bad line \"%s\"\n", line);
- continue;
- }
- }
- *endsection = 0;
- curSection.clear();
- if(al::strcasecmp(section, "general") != 0)
- {
- do {
- char *nextp = std::strchr(section, '%');
- if(!nextp)
- {
- curSection += section;
- break;
- }
- curSection.append(section, nextp);
- section = nextp;
- if(((section[1] >= '0' && section[1] <= '9') ||
- (section[1] >= 'a' && section[1] <= 'f') ||
- (section[1] >= 'A' && section[1] <= 'F')) &&
- ((section[2] >= '0' && section[2] <= '9') ||
- (section[2] >= 'a' && section[2] <= 'f') ||
- (section[2] >= 'A' && section[2] <= 'F')))
- {
- int b{0};
- if(section[1] >= '0' && section[1] <= '9')
- b = (section[1]-'0') << 4;
- else if(section[1] >= 'a' && section[1] <= 'f')
- b = (section[1]-'a'+0xa) << 4;
- else if(section[1] >= 'A' && section[1] <= 'F')
- b = (section[1]-'A'+0x0a) << 4;
- if(section[2] >= '0' && section[2] <= '9')
- b |= (section[2]-'0');
- else if(section[2] >= 'a' && section[2] <= 'f')
- b |= (section[2]-'a'+0xa);
- else if(section[2] >= 'A' && section[2] <= 'F')
- b |= (section[2]-'A'+0x0a);
- curSection += static_cast<char>(b);
- section += 3;
- }
- else if(section[1] == '%')
- {
- curSection += '%';
- section += 2;
- }
- else
- {
- curSection += '%';
- section += 1;
- }
- } while(*section != 0);
- }
- continue;
- }
- auto cmtpos = std::min(buffer.find('#'), buffer.size());
- while(cmtpos > 0 && std::isspace(buffer[cmtpos-1]))
- --cmtpos;
- if(!cmtpos) continue;
- buffer.erase(cmtpos);
- auto sep = buffer.find('=');
- if(sep == std::string::npos)
- {
- ERR(" config parse error: malformed option line: \"%s\"\n", buffer.c_str());
- continue;
- }
- auto keyend = sep++;
- while(keyend > 0 && std::isspace(buffer[keyend-1]))
- --keyend;
- if(!keyend)
- {
- ERR(" config parse error: malformed option line: \"%s\"\n", buffer.c_str());
- continue;
- }
- while(sep < buffer.size() && std::isspace(buffer[sep]))
- sep++;
- std::string fullKey;
- if(!curSection.empty())
- {
- fullKey += curSection;
- fullKey += '/';
- }
- fullKey += buffer.substr(0u, keyend);
- std::string value{(sep < buffer.size()) ? buffer.substr(sep) : std::string{}};
- if(value.size() > 1)
- {
- if((value.front() == '"' && value.back() == '"')
- || (value.front() == '\'' && value.back() == '\''))
- {
- value.pop_back();
- value.erase(value.begin());
- }
- }
- TRACE(" found '%s' = '%s'\n", fullKey.c_str(), value.c_str());
- /* Check if we already have this option set */
- auto find_key = [&fullKey](const ConfigEntry &entry) -> bool
- { return entry.key == fullKey; };
- auto ent = std::find_if(ConfOpts.begin(), ConfOpts.end(), find_key);
- if(ent != ConfOpts.end())
- {
- if(!value.empty())
- ent->value = expdup(value.c_str());
- else
- ConfOpts.erase(ent);
- }
- else if(!value.empty())
- ConfOpts.emplace_back(ConfigEntry{std::move(fullKey), expdup(value.c_str())});
- }
- ConfOpts.shrink_to_fit();
- }
- } // namespace
- #ifdef _WIN32
- void ReadALConfig()
- {
- WCHAR buffer[MAX_PATH];
- if(SHGetSpecialFolderPathW(nullptr, buffer, CSIDL_APPDATA, FALSE) != FALSE)
- {
- std::string filepath{wstr_to_utf8(buffer)};
- filepath += "\\alsoft.ini";
- TRACE("Loading config %s...\n", filepath.c_str());
- al::ifstream f{filepath};
- if(f.is_open())
- LoadConfigFromFile(f);
- }
- std::string ppath{GetProcBinary().path};
- if(!ppath.empty())
- {
- ppath += "\\alsoft.ini";
- TRACE("Loading config %s...\n", ppath.c_str());
- al::ifstream f{ppath};
- if(f.is_open())
- LoadConfigFromFile(f);
- }
- if(auto confpath = al::getenv(L"ALSOFT_CONF"))
- {
- TRACE("Loading config %s...\n", wstr_to_utf8(confpath->c_str()).c_str());
- al::ifstream f{*confpath};
- if(f.is_open())
- LoadConfigFromFile(f);
- }
- }
- #else
- void ReadALConfig()
- {
- const char *str{"/etc/openal/alsoft.conf"};
- TRACE("Loading config %s...\n", str);
- al::ifstream f{str};
- if(f.is_open())
- LoadConfigFromFile(f);
- f.close();
- std::string confpaths{al::getenv("XDG_CONFIG_DIRS").value_or("/etc/xdg")};
- /* Go through the list in reverse, since "the order of base directories
- * denotes their importance; the first directory listed is the most
- * important". Ergo, we need to load the settings from the later dirs
- * first so that the settings in the earlier dirs override them.
- */
- std::string fname;
- while(!confpaths.empty())
- {
- auto next = confpaths.find_last_of(':');
- if(next < confpaths.length())
- {
- fname = confpaths.substr(next+1);
- confpaths.erase(next);
- }
- else
- {
- fname = confpaths;
- confpaths.clear();
- }
- if(fname.empty() || fname.front() != '/')
- WARN("Ignoring XDG config dir: %s\n", fname.c_str());
- else
- {
- if(fname.back() != '/') fname += "/alsoft.conf";
- else fname += "alsoft.conf";
- TRACE("Loading config %s...\n", fname.c_str());
- f = al::ifstream{fname};
- if(f.is_open())
- LoadConfigFromFile(f);
- }
- fname.clear();
- }
- #ifdef __APPLE__
- CFBundleRef mainBundle = CFBundleGetMainBundle();
- if(mainBundle)
- {
- unsigned char fileName[PATH_MAX];
- CFURLRef configURL;
- if((configURL=CFBundleCopyResourceURL(mainBundle, CFSTR(".alsoftrc"), CFSTR(""), nullptr)) &&
- CFURLGetFileSystemRepresentation(configURL, true, fileName, sizeof(fileName)))
- {
- f = al::ifstream{reinterpret_cast<char*>(fileName)};
- if(f.is_open())
- LoadConfigFromFile(f);
- }
- }
- #endif
- if(auto homedir = al::getenv("HOME"))
- {
- fname = *homedir;
- if(fname.back() != '/') fname += "/.alsoftrc";
- else fname += ".alsoftrc";
- TRACE("Loading config %s...\n", fname.c_str());
- f = al::ifstream{fname};
- if(f.is_open())
- LoadConfigFromFile(f);
- }
- if(auto configdir = al::getenv("XDG_CONFIG_HOME"))
- {
- fname = *configdir;
- if(fname.back() != '/') fname += "/alsoft.conf";
- else fname += "alsoft.conf";
- }
- else
- {
- fname.clear();
- if(auto homedir = al::getenv("HOME"))
- {
- fname = *homedir;
- if(fname.back() != '/') fname += "/.config/alsoft.conf";
- else fname += ".config/alsoft.conf";
- }
- }
- if(!fname.empty())
- {
- TRACE("Loading config %s...\n", fname.c_str());
- f = al::ifstream{fname};
- if(f.is_open())
- LoadConfigFromFile(f);
- }
- std::string ppath{GetProcBinary().path};
- if(!ppath.empty())
- {
- if(ppath.back() != '/') ppath += "/alsoft.conf";
- else ppath += "alsoft.conf";
- TRACE("Loading config %s...\n", ppath.c_str());
- f = al::ifstream{ppath};
- if(f.is_open())
- LoadConfigFromFile(f);
- }
- if(auto confname = al::getenv("ALSOFT_CONF"))
- {
- TRACE("Loading config %s...\n", confname->c_str());
- f = al::ifstream{*confname};
- if(f.is_open())
- LoadConfigFromFile(f);
- }
- }
- #endif
- const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def)
- {
- if(!keyName)
- return def;
- std::string key;
- if(blockName && al::strcasecmp(blockName, "general") != 0)
- {
- key = blockName;
- if(devName)
- {
- key += '/';
- key += devName;
- }
- key += '/';
- key += keyName;
- }
- else
- {
- if(devName)
- {
- key = devName;
- key += '/';
- }
- key += keyName;
- }
- auto iter = std::find_if(ConfOpts.cbegin(), ConfOpts.cend(),
- [&key](const ConfigEntry &entry) -> bool
- { return entry.key == key; }
- );
- if(iter != ConfOpts.cend())
- {
- TRACE("Found %s = \"%s\"\n", key.c_str(), iter->value.c_str());
- if(!iter->value.empty())
- return iter->value.c_str();
- return def;
- }
- if(!devName)
- {
- TRACE("Key %s not found\n", key.c_str());
- return def;
- }
- return GetConfigValue(nullptr, blockName, keyName, def);
- }
- int ConfigValueExists(const char *devName, const char *blockName, const char *keyName)
- {
- const char *val = GetConfigValue(devName, blockName, keyName, "");
- return val[0] != 0;
- }
- al::optional<std::string> ConfigValueStr(const char *devName, const char *blockName, const char *keyName)
- {
- const char *val = GetConfigValue(devName, blockName, keyName, "");
- if(!val[0]) return al::nullopt;
- return al::make_optional<std::string>(val);
- }
- al::optional<int> ConfigValueInt(const char *devName, const char *blockName, const char *keyName)
- {
- const char *val = GetConfigValue(devName, blockName, keyName, "");
- if(!val[0]) return al::nullopt;
- return al::make_optional(static_cast<int>(std::strtol(val, nullptr, 0)));
- }
- al::optional<unsigned int> ConfigValueUInt(const char *devName, const char *blockName, const char *keyName)
- {
- const char *val = GetConfigValue(devName, blockName, keyName, "");
- if(!val[0]) return al::nullopt;
- return al::make_optional(static_cast<unsigned int>(std::strtoul(val, nullptr, 0)));
- }
- al::optional<float> ConfigValueFloat(const char *devName, const char *blockName, const char *keyName)
- {
- const char *val = GetConfigValue(devName, blockName, keyName, "");
- if(!val[0]) return al::nullopt;
- return al::make_optional(std::strtof(val, nullptr));
- }
- al::optional<bool> ConfigValueBool(const char *devName, const char *blockName, const char *keyName)
- {
- const char *val = GetConfigValue(devName, blockName, keyName, "");
- if(!val[0]) return al::nullopt;
- return al::make_optional(
- al::strcasecmp(val, "true") == 0 || al::strcasecmp(val, "yes") == 0 ||
- al::strcasecmp(val, "on") == 0 || atoi(val) != 0);
- }
- int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def)
- {
- const char *val = GetConfigValue(devName, blockName, keyName, "");
- if(!val[0]) return def != 0;
- return (al::strcasecmp(val, "true") == 0 || al::strcasecmp(val, "yes") == 0 ||
- al::strcasecmp(val, "on") == 0 || atoi(val) != 0);
- }
|