Use an optional for ConfigValueStr

This commit is contained in:
Chris Robinson 2019-06-30 16:15:15 -07:00
parent db026454f2
commit 3658dafdcb
9 changed files with 86 additions and 85 deletions

View File

@ -1024,8 +1024,9 @@ void alc_initconfig(void)
#ifdef HAVE_NEON
capfilter |= CPU_CAP_NEON;
#endif
if(ConfigValueStr(nullptr, nullptr, "disable-cpu-exts", &str))
if(auto cpuopt = ConfigValueStr(nullptr, nullptr, "disable-cpu-exts"))
{
str = cpuopt->c_str();
if(strcasecmp(str, "all") == 0)
capfilter = 0;
else
@ -1094,15 +1095,20 @@ void alc_initconfig(void)
if(ConfigValueFloat(nullptr, "reverb", "boost", &valf))
ReverbBoost *= std::pow(10.0f, valf / 20.0f);
const char *devs{getenv("ALSOFT_DRIVERS")};
if((devs && devs[0]) || ConfigValueStr(nullptr, nullptr, "drivers", &devs))
auto devopt = ConfigValueStr(nullptr, nullptr, "drivers");
if(const char *devs{getenv("ALSOFT_DRIVERS")})
{
if(devs[0])
devopt = al::optional<std::string>{al::in_place, devs};
}
if(devopt)
{
auto backendlist_cur = std::begin(BackendList);
bool endlist{true};
const char *next = devs;
const char *next{devopt->c_str()};
do {
devs = next;
const char *devs{next};
while(isspace(devs[0]))
devs++;
next = strchr(devs, ',');
@ -1182,9 +1188,9 @@ void alc_initconfig(void)
if(!CaptureFactory)
WARN("No capture backend available!\n");
if(ConfigValueStr(nullptr, nullptr, "excludefx", &str))
if(auto exclopt = ConfigValueStr(nullptr, nullptr, "excludefx"))
{
const char *next = str;
const char *next{exclopt->c_str()};
do {
str = next;
next = strchr(str, ',');
@ -1203,9 +1209,10 @@ void alc_initconfig(void)
}
InitEffect(&DefaultEffect);
str = getenv("ALSOFT_DEFAULT_REVERB");
if((str && str[0]) || ConfigValueStr(nullptr, nullptr, "default-reverb", &str))
LoadReverbPreset(str, &DefaultEffect);
auto defrevopt = ConfigValueStr(nullptr, nullptr, "default-reverb");
if((str=getenv("ALSOFT_DEFAULT_REVERB")) && str[0])
defrevopt = al::optional<std::string>{al::in_place, str};
if(defrevopt) LoadReverbPreset(defrevopt->c_str(), &DefaultEffect);
}
#define DO_INITCONFIG() std::call_once(alc_config_once, [](){alc_initconfig();})
@ -1919,9 +1926,9 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
device->HrtfStatus = ALC_HRTF_DISABLED_SOFT;
if(device->Type != Loopback)
{
const char *hrtf;
if(ConfigValueStr(device->DeviceName.c_str(), nullptr, "hrtf", &hrtf))
if(auto hrtfopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "hrtf"))
{
const char *hrtf{hrtfopt->c_str()};
if(strcasecmp(hrtf, "true") == 0)
hrtf_userreq = Hrtf_Enable;
else if(strcasecmp(hrtf, "false") == 0)
@ -3743,8 +3750,7 @@ START_API_FUNC
}
deviceName = device->DeviceName.c_str();
const ALCchar *fmt{};
if(ConfigValueStr(deviceName, nullptr, "channels", &fmt))
if(auto chanopt = ConfigValueStr(deviceName, nullptr, "channels"))
{
static constexpr struct ChannelMap {
const char name[16];
@ -3763,6 +3769,7 @@ START_API_FUNC
{ "ambi3", DevFmtAmbi3D, 3 },
};
const ALCchar *fmt{chanopt->c_str()};
auto iter = std::find_if(std::begin(chanlist), std::end(chanlist),
[fmt](const ChannelMap &entry) -> bool
{ return strcasecmp(entry.name, fmt) == 0; }
@ -3776,7 +3783,7 @@ START_API_FUNC
device->Flags.set<ChannelsRequest>();
}
}
if(ConfigValueStr(deviceName, nullptr, "sample-type", &fmt))
if(auto typeopt = ConfigValueStr(deviceName, nullptr, "sample-type"))
{
static constexpr struct TypeMap {
const char name[16];
@ -3791,6 +3798,7 @@ START_API_FUNC
{ "float32", DevFmtFloat },
};
const ALCchar *fmt{typeopt->c_str()};
auto iter = std::find_if(std::begin(typelist), std::end(typelist),
[fmt](const TypeMap &entry) -> bool
{ return strcasecmp(entry.name, fmt) == 0; }
@ -3842,8 +3850,9 @@ START_API_FUNC
device->NumStereoSources = 1;
device->NumMonoSources = device->SourcesMax - device->NumStereoSources;
if(ConfigValueStr(deviceName, nullptr, "ambi-format", &fmt))
if(auto ambiopt = ConfigValueStr(deviceName, nullptr, "ambi-format"))
{
const ALCchar *fmt{ambiopt->c_str()};
if(strcasecmp(fmt, "fuma") == 0)
{
if(device->mAmbiOrder > 3)

View File

@ -492,13 +492,12 @@ int ConfigValueExists(const char *devName, const char *blockName, const char *ke
return val[0] != 0;
}
int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret)
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 0;
if(!val[0]) return al::nullopt;
*ret = val;
return 1;
return al::optional<std::string>{al::in_place, val};
}
al::optional<int> ConfigValueInt(const char *devName, const char *blockName, const char *keyName)

View File

@ -1,6 +1,8 @@
#ifndef ALCONFIG_H
#define ALCONFIG_H
#include <string>
#include "aloptional.h"
void ReadALConfig();
@ -9,7 +11,7 @@ int ConfigValueExists(const char *devName, const char *blockName, const char *ke
const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def);
int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def);
int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret);
al::optional<std::string> ConfigValueStr(const char *devName, const char *blockName, const char *keyName);
al::optional<int> ConfigValueInt(const char *devName, const char *blockName, const char *keyName);
al::optional<unsigned int> ConfigValueUInt(const char *devName, const char *blockName, const char *keyName);
int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret);

View File

@ -207,11 +207,6 @@ ALSA_FUNCS(MAKE_FUNC);
struct DevMap {
std::string name;
std::string device_name;
template<typename StrT0, typename StrT1>
DevMap(StrT0&& name_, StrT1&& devname_)
: name{std::forward<StrT0>(name_)}, device_name{std::forward<StrT1>(devname_)}
{ }
};
al::vector<DevMap> PlaybackDevices;
@ -233,10 +228,9 @@ al::vector<DevMap> probe_devices(snd_pcm_stream_t stream)
snd_pcm_info_t *pcminfo;
snd_pcm_info_malloc(&pcminfo);
devlist.emplace_back(alsaDevice,
devlist.emplace_back(DevMap{alsaDevice,
GetConfigValue(nullptr, "alsa", (stream==SND_PCM_STREAM_PLAYBACK) ? "device" : "capture",
"default")
);
"default")});
if(stream == SND_PCM_STREAM_PLAYBACK)
{
@ -254,15 +248,15 @@ al::vector<DevMap> probe_devices(snd_pcm_stream_t stream)
}
const char *oldsep{sep++};
devlist.emplace_back(std::string(customdevs, oldsep),
next ? std::string(sep, next++) : std::string(sep));
devlist.emplace_back(DevMap{std::string(customdevs, oldsep),
next ? std::string(sep, next++) : std::string(sep)});
const auto &entry = devlist.back();
TRACE("Got device \"%s\", \"%s\"\n", entry.name.c_str(), entry.device_name.c_str());
}
}
const char *main_prefix{"plughw:"};
ConfigValueStr(nullptr, "alsa", prefix_name(stream), &main_prefix);
const std::string main_prefix{
ConfigValueStr(nullptr, "alsa", prefix_name(stream)).value_or("plughw:")};
int card{-1};
int err{snd_card_next(&card)};
@ -288,9 +282,8 @@ al::vector<DevMap> probe_devices(snd_pcm_stream_t stream)
name = prefix_name(stream);
name += '-';
name += cardid;
const char *card_prefix{main_prefix};
ConfigValueStr(nullptr, "alsa", name.c_str(), &card_prefix);
const std::string card_prefix{
ConfigValueStr(nullptr, "alsa", name.c_str()).value_or(main_prefix)};
int dev{-1};
while(1)
@ -315,8 +308,8 @@ al::vector<DevMap> probe_devices(snd_pcm_stream_t stream)
name += cardid;
name += '-';
name += std::to_string(dev);
const char *device_prefix{card_prefix};
ConfigValueStr(nullptr, "alsa", name.c_str(), &device_prefix);
const std::string device_prefix{
ConfigValueStr(nullptr, "alsa", name.c_str()).value_or(card_prefix)};
/* "CardName, PcmName (CARD=cardid,DEV=dev)" */
name = cardname;
@ -335,7 +328,7 @@ al::vector<DevMap> probe_devices(snd_pcm_stream_t stream)
device += ",DEV=";
device += std::to_string(dev);
devlist.emplace_back(std::move(name), std::move(device));
devlist.emplace_back(DevMap{std::move(name), std::move(device)});
const auto &entry = devlist.back();
TRACE("Got device \"%s\", \"%s\"\n", entry.name.c_str(), entry.device_name.c_str());
}

View File

@ -80,17 +80,12 @@
namespace {
constexpr char DefaultName[] = "OSS Default";
const char *DefaultPlayback{"/dev/dsp"};
const char *DefaultCapture{"/dev/dsp"};
std::string DefaultPlayback{"/dev/dsp"};
std::string DefaultCapture{"/dev/dsp"};
struct DevMap {
std::string name;
std::string device_name;
template<typename StrT0, typename StrT1>
DevMap(StrT0&& name_, StrT1&& devname_)
: name{std::forward<StrT0>(name_)}, device_name{std::forward<StrT1>(devname_)}
{ }
};
bool checkName(const al::vector<DevMap> &list, const std::string &name)
@ -111,7 +106,7 @@ al::vector<DevMap> CaptureDevices;
#define DSP_CAP_INPUT 0x00010000
void ALCossListPopulate(al::vector<DevMap> *devlist, int type)
{
devlist->emplace_back(DefaultName, (type==DSP_CAP_INPUT) ? DefaultCapture : DefaultPlayback);
devlist->emplace_back(DevMap{DefaultName, (type==DSP_CAP_INPUT) ? DefaultCapture : DefaultPlayback});
}
#else
@ -156,7 +151,7 @@ void ALCossListAppend(al::vector<DevMap> *list, const char *handle, size_t hlen,
newname += std::to_string(++count);
}
list->emplace_back(std::move(newname), std::move(devname));
list->emplace_back(DevMap{std::move(newname), std::move(devname)});
const DevMap &entry = list->back();
TRACE("Got device \"%s\", \"%s\"\n", entry.name.c_str(), entry.device_name.c_str());
@ -212,7 +207,7 @@ done:
close(fd);
fd = -1;
const char *defdev{(type_flag==DSP_CAP_INPUT) ? DefaultCapture : DefaultPlayback};
const char *defdev{((type_flag==DSP_CAP_INPUT) ? DefaultCapture : DefaultPlayback).c_str()};
auto iter = std::find_if(devlist->cbegin(), devlist->cend(),
[defdev](const DevMap &entry) -> bool
{ return entry.device_name == defdev; }
@ -331,7 +326,7 @@ int OSSPlayback::mixerProc()
ALCenum OSSPlayback::open(const ALCchar *name)
{
const char *devname{DefaultPlayback};
const char *devname{DefaultPlayback.c_str()};
if(!name)
name = DefaultName;
else
@ -548,7 +543,7 @@ int OSScapture::recordProc()
ALCenum OSScapture::open(const ALCchar *name)
{
const char *devname{DefaultCapture};
const char *devname{DefaultCapture.c_str()};
if(!name)
name = DefaultName;
else
@ -700,8 +695,10 @@ BackendFactory &OSSBackendFactory::getFactory()
bool OSSBackendFactory::init()
{
ConfigValueStr(nullptr, "oss", "device", &DefaultPlayback);
ConfigValueStr(nullptr, "oss", "capture", &DefaultCapture);
if(auto devopt = ConfigValueStr(nullptr, "oss", "device"))
DefaultPlayback = std::move(*devopt);
if(auto capopt = ConfigValueStr(nullptr, "oss", "capture"))
DefaultCapture = std::move(*capopt);
return true;
}

View File

@ -52,7 +52,7 @@ namespace {
constexpr ALCchar solaris_device[] = "Solaris Default";
const char *solaris_driver = "/dev/audio";
std::string solaris_driver{"/dev/audio"};
struct SolarisBackend final : public BackendBase {
@ -149,10 +149,10 @@ ALCenum SolarisBackend::open(const ALCchar *name)
else if(strcmp(name, solaris_device) != 0)
return ALC_INVALID_VALUE;
mFd = ::open(solaris_driver, O_WRONLY);
mFd = ::open(solaris_driver.c_str(), O_WRONLY);
if(mFd == -1)
{
ERR("Could not open %s: %s\n", solaris_driver, strerror(errno));
ERR("Could not open %s: %s\n", solaris_driver.c_str(), strerror(errno));
return ALC_INVALID_VALUE;
}
@ -267,7 +267,8 @@ BackendFactory &SolarisBackendFactory::getFactory()
bool SolarisBackendFactory::init()
{
ConfigValueStr(nullptr, "solaris", "device", &solaris_driver);
if(auto devopt = ConfigValueStr(nullptr, "solaris", "device"))
solaris_driver = std::move(*devopt);
return true;
}
@ -282,7 +283,7 @@ void SolarisBackendFactory::probe(DevProbe type, std::string *outnames)
{
#ifdef HAVE_STAT
struct stat buf;
if(stat(solaris_driver, &buf) == 0)
if(stat(solaris_driver.c_str(), &buf) == 0)
#endif
outnames->append(solaris_device, sizeof(solaris_device));
}

View File

@ -1214,9 +1214,9 @@ al::vector<EnumeratedHrtf> EnumerateHrtf(const char *devname)
al::vector<EnumeratedHrtf> list;
bool usedefaults{true};
const char *pathlist{""};
if(ConfigValueStr(devname, nullptr, "hrtf-paths", &pathlist))
if(auto pathopt = ConfigValueStr(devname, nullptr, "hrtf-paths"))
{
const char *pathlist{pathopt->c_str()};
while(pathlist && *pathlist)
{
const char *next, *end;
@ -1262,20 +1262,22 @@ al::vector<EnumeratedHrtf> EnumerateHrtf(const char *devname)
AddBuiltInEntry(list, "Built-In 48000hz", IDR_DEFAULT_48000_MHR);
}
const char *defaulthrtf{""};
if(!list.empty() && ConfigValueStr(devname, nullptr, "default-hrtf", &defaulthrtf))
if(!list.empty())
{
auto iter = std::find_if(list.begin(), list.end(),
[defaulthrtf](const EnumeratedHrtf &entry) -> bool
{ return entry.name == defaulthrtf; }
);
if(iter == list.end())
WARN("Failed to find default HRTF \"%s\"\n", defaulthrtf);
else if(iter != list.begin())
if(auto defhrtfopt = ConfigValueStr(devname, nullptr, "default-hrtf"))
{
EnumeratedHrtf entry{*iter};
list.erase(iter);
list.insert(list.begin(), entry);
auto iter = std::find_if(list.begin(), list.end(),
[&defhrtfopt](const EnumeratedHrtf &entry) -> bool
{ return entry.name == *defhrtfopt; }
);
if(iter == list.end())
WARN("Failed to find default HRTF \"%s\"\n", defhrtfopt->c_str());
else if(iter != list.begin())
{
EnumeratedHrtf entry{std::move(*iter)};
list.erase(iter);
list.insert(list.begin(), std::move(entry));
}
}
}

View File

@ -155,10 +155,9 @@ ResamplerFunc SelectResampler(Resampler resampler)
void aluInitMixer()
{
const char *str;
if(ConfigValueStr(nullptr, nullptr, "resampler", &str))
if(auto resopt = ConfigValueStr(nullptr, nullptr, "resampler"))
{
const char *str{resopt->c_str()};
if(strcasecmp(str, "point") == 0 || strcasecmp(str, "none") == 0)
ResamplerDefault = PointResampler;
else if(strcasecmp(str, "linear") == 0)

View File

@ -553,9 +553,9 @@ void InitHrtfPanning(ALCdevice *device)
*/
device->mRenderMode = HrtfRender;
ALsizei ambi_order{1};
const char *mode;
if(ConfigValueStr(device->DeviceName.c_str(), nullptr, "hrtf-mode", &mode))
if(auto modeopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "hrtf-mode"))
{
const char *mode{modeopt->c_str()};
if(strcasecmp(mode, "basic") == 0)
{
ERR("HRTF mode \"%s\" deprecated, substituting \"%s\"\n", mode, "ambi2");
@ -783,11 +783,10 @@ void aluInitRenderer(ALCdevice *device, ALint hrtf_id, HrtfRequestMode hrtf_appr
AmbDecConf conf{};
if(layout)
{
const char *fname;
if(ConfigValueStr(devname, "decoder", layout, &fname))
if(auto decopt = ConfigValueStr(devname, "decoder", layout))
{
if(!conf.load(fname))
ERR("Failed to load layout file %s\n", fname);
if(!conf.load(decopt->c_str()))
ERR("Failed to load layout file %s\n", decopt->c_str());
else if(conf.Speakers.size() > MAX_OUTPUT_CHANNELS)
ERR("Unsupported speaker count %zu (max %d)\n", conf.Speakers.size(),
MAX_OUTPUT_CHANNELS);
@ -814,9 +813,9 @@ void aluInitRenderer(ALCdevice *device, ALint hrtf_id, HrtfRequestMode hrtf_appr
bool headphones{device->IsHeadphones != AL_FALSE};
if(device->Type != Loopback)
{
const char *mode;
if(ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-mode", &mode))
if(auto modeopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-mode"))
{
const char *mode{modeopt->c_str()};
if(strcasecmp(mode, "headphones") == 0)
headphones = true;
else if(strcasecmp(mode, "speakers") == 0)
@ -916,9 +915,9 @@ no_hrtf:
}
}
const char *mode;
if(ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-encoding", &mode))
if(auto encopt = ConfigValueStr(device->DeviceName.c_str(), nullptr, "stereo-encoding"))
{
const char *mode{encopt->c_str()};
if(strcasecmp(mode, "uhj") == 0)
device->mRenderMode = NormalRender;
else if(strcasecmp(mode, "panpot") != 0)