diff --git a/alc/alc.cpp b/alc/alc.cpp index 6fc96dddc3..b9c71ef96a 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -2082,8 +2082,6 @@ ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum para static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span values) { - size_t i; - if(values.empty()) { alcSetError(device, ALC_INVALID_VALUE); @@ -2144,11 +2142,9 @@ static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span values[0] = MaxCaptureAttributes; return 1; case ALC_ALL_ATTRIBUTES: - i = 0; - if(values.size() < MaxCaptureAttributes) - alcSetError(device, ALC_INVALID_VALUE); - else + if(values.size() >= MaxCaptureAttributes) { + size_t i{0}; values[i++] = ALC_MAJOR_VERSION; values[i++] = alcMajorVersion; values[i++] = ALC_MINOR_VERSION; @@ -2159,8 +2155,10 @@ static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span values[i++] = device->Connected.load(std::memory_order_relaxed); values[i++] = 0; assert(i == MaxCaptureAttributes); + return i; } - return i; + alcSetError(device, ALC_INVALID_VALUE); + return 0; case ALC_MAJOR_VERSION: values[0] = alcMajorVersion; @@ -2184,7 +2182,7 @@ static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span } /* render device */ - auto NumAttrsForDevice = [](ALCdevice *aldev) noexcept + auto NumAttrsForDevice = [](const ALCdevice *aldev) noexcept -> uint8_t { if(aldev->Type == DeviceType::Loopback && aldev->FmtChans == DevFmtAmbi3D) return 37; @@ -2197,11 +2195,9 @@ static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span return 1; case ALC_ALL_ATTRIBUTES: - i = 0; - if(values.size() < static_cast(NumAttrsForDevice(device))) - alcSetError(device, ALC_INVALID_VALUE); - else + if(values.size() >= NumAttrsForDevice(device)) { + size_t i{0}; values[i++] = ALC_MAJOR_VERSION; values[i++] = alcMajorVersion; values[i++] = ALC_MINOR_VERSION; @@ -2267,8 +2263,11 @@ static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span values[i++] = static_cast(device->getOutputMode1()); values[i++] = 0; + assert(i == NumAttrsForDevice(device)); + return i; } - return i; + alcSetError(device, ALC_INVALID_VALUE); + return 0; case ALC_MAJOR_VERSION: values[0] = alcMajorVersion; @@ -2503,7 +2502,7 @@ ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, values[i++] = clock.Latency.count(); values[i++] = ALC_OUTPUT_MODE_SOFT; - values[i++] = static_cast(device->getOutputMode1()); + values[i++] = al::to_underlying(device->getOutputMode1()); values[i++] = 0; } diff --git a/alc/backends/alsa.cpp b/alc/backends/alsa.cpp index a31cac0b3a..e3ff0544f5 100644 --- a/alc/backends/alsa.cpp +++ b/alc/backends/alsa.cpp @@ -262,14 +262,52 @@ const std::string_view prefix_name(snd_pcm_stream_t stream) return "capture-prefix"sv; } +struct SndCtlCardInfo { + snd_ctl_card_info_t *mInfo{}; + + SndCtlCardInfo() { snd_ctl_card_info_malloc(&mInfo); } + ~SndCtlCardInfo() { if(mInfo) snd_ctl_card_info_free(mInfo); } + SndCtlCardInfo(const SndCtlCardInfo&) = delete; + SndCtlCardInfo& operator=(const SndCtlCardInfo&) = delete; + + [[nodiscard]] + operator snd_ctl_card_info_t*() const noexcept { return mInfo; } +}; + +struct SndPcmInfo { + snd_pcm_info_t *mInfo{}; + + SndPcmInfo() { snd_pcm_info_malloc(&mInfo); } + ~SndPcmInfo() { if(mInfo) snd_pcm_info_free(mInfo); } + SndPcmInfo(const SndPcmInfo&) = delete; + SndPcmInfo& operator=(const SndPcmInfo&) = delete; + + [[nodiscard]] + operator snd_pcm_info_t*() const noexcept { return mInfo; } +}; + +struct SndCtl { + snd_ctl_t *mHandle{}; + + SndCtl() = default; + ~SndCtl() { if(mHandle) snd_ctl_close(mHandle); } + SndCtl(const SndCtl&) = delete; + SndCtl& operator=(const SndCtl&) = delete; + + [[nodiscard]] + auto open(const char *name, int mode) { return snd_ctl_open(&mHandle, name, mode); } + + [[nodiscard]] + operator snd_ctl_t*() const noexcept { return mHandle; } +}; + + std::vector probe_devices(snd_pcm_stream_t stream) { std::vector devlist; - snd_ctl_card_info_t *info; - snd_ctl_card_info_malloc(&info); - snd_pcm_info_t *pcminfo; - snd_pcm_info_malloc(&pcminfo); + SndCtlCardInfo info; + SndPcmInfo pcminfo; auto defname = ConfigValueStr({}, "alsa"sv, (stream == SND_PCM_STREAM_PLAYBACK) ? "device"sv : "capture"sv); @@ -278,28 +316,27 @@ std::vector probe_devices(snd_pcm_stream_t stream) if(auto customdevs = ConfigValueStr({}, "alsa"sv, (stream == SND_PCM_STREAM_PLAYBACK) ? "custom-devices"sv : "custom-captures"sv)) { - size_t nextpos{customdevs->find_first_not_of(';')}; - size_t curpos; - while((curpos=nextpos) < customdevs->length()) + size_t curpos{customdevs->find_first_not_of(';')}; + while(curpos < customdevs->length()) { - nextpos = customdevs->find_first_of(';', curpos+1); - - size_t seppos{customdevs->find_first_of('=', curpos)}; + size_t nextpos{customdevs->find(';', curpos+1)}; + const size_t seppos{customdevs->find('=', curpos)}; if(seppos == curpos || seppos >= nextpos) { - std::string spec{customdevs->substr(curpos, nextpos-curpos)}; + const std::string spec{customdevs->substr(curpos, nextpos-curpos)}; ERR("Invalid ALSA device specification \"%s\"\n", spec.c_str()); } else { - devlist.emplace_back(customdevs->substr(curpos, seppos-curpos), - customdevs->substr(seppos+1, nextpos-seppos-1)); - const auto &entry = devlist.back(); + const std::string_view strview{*customdevs}; + const auto &entry = devlist.emplace_back(strview.substr(curpos, seppos-curpos), + strview.substr(seppos+1, nextpos-seppos-1)); TRACE("Got device \"%s\", \"%s\"\n", entry.name.c_str(), entry.device_name.c_str()); } if(nextpos < customdevs->length()) nextpos = customdevs->find_first_not_of(';', nextpos+1); + curpos = nextpos; } } @@ -312,8 +349,8 @@ std::vector probe_devices(snd_pcm_stream_t stream) { std::string name{"hw:" + std::to_string(card)}; - snd_ctl_t *handle; - err = snd_ctl_open(&handle, name.c_str(), 0); + SndCtl handle; + err = handle.open(name.c_str(), 0); if(err < 0) { ERR("control open (hw:%d): %s\n", card, snd_strerror(err)); @@ -323,7 +360,6 @@ std::vector probe_devices(snd_pcm_stream_t stream) if(err < 0) { ERR("control hardware info (hw:%d): %s\n", card, snd_strerror(err)); - snd_ctl_close(handle); continue; } @@ -378,18 +414,13 @@ std::vector probe_devices(snd_pcm_stream_t stream) device += ",DEV="; device += std::to_string(dev); - devlist.emplace_back(std::move(name), std::move(device)); - const auto &entry = devlist.back(); + const auto &entry = devlist.emplace_back(std::move(name), std::move(device)); TRACE("Got device \"%s\", \"%s\"\n", entry.name.c_str(), entry.device_name.c_str()); } - snd_ctl_close(handle); } if(err < 0) ERR("snd_card_next failed: %s\n", snd_strerror(err)); - snd_pcm_info_free(pcminfo); - snd_ctl_card_info_free(info); - return devlist; } @@ -398,7 +429,6 @@ int verify_state(snd_pcm_t *handle) { snd_pcm_state_t state{snd_pcm_state(handle)}; - int err; switch(state) { case SND_PCM_STATE_OPEN: @@ -411,12 +441,12 @@ int verify_state(snd_pcm_t *handle) break; case SND_PCM_STATE_XRUN: - err=snd_pcm_recover(handle, -EPIPE, 1); - if(err < 0) return err; + if(int err{snd_pcm_recover(handle, -EPIPE, 1)}; err < 0) + return err; break; case SND_PCM_STATE_SUSPENDED: - err = snd_pcm_recover(handle, -ESTRPIPE, 1); - if(err < 0) return err; + if(int err{snd_pcm_recover(handle, -ESTRPIPE, 1)}; err < 0) + return err; break; case SND_PCM_STATE_DISCONNECTED: diff --git a/alc/backends/jack.cpp b/alc/backends/jack.cpp index 6bf6293b3e..3c1f109670 100644 --- a/alc/backends/jack.cpp +++ b/alc/backends/jack.cpp @@ -457,7 +457,7 @@ void JackPlayback::open(std::string_view name) const PathNamePair &binname = GetProcBinary(); const char *client_name{binname.fname.empty() ? "alsoft" : binname.fname.c_str()}; - jack_status_t status; + jack_status_t status{}; mClient = jack_client_open(client_name, ClientOptions, &status, nullptr); if(mClient == nullptr) throw al::backend_exception{al::backend_error::DeviceError, @@ -675,7 +675,7 @@ bool JackBackendFactory::init() void (*old_error_cb)(const char*){&jack_error_callback ? jack_error_callback : nullptr}; jack_set_error_function(jack_msg_handler); - jack_status_t status; + jack_status_t status{}; jack_client_t *client{jack_client_open(client_name, ClientOptions, &status, nullptr)}; jack_set_error_function(old_error_cb); if(!client) @@ -704,7 +704,7 @@ std::string JackBackendFactory::probe(BackendType type) const PathNamePair &binname = GetProcBinary(); const char *client_name{binname.fname.empty() ? "alsoft" : binname.fname.c_str()}; - jack_status_t status; + jack_status_t status{}; switch(type) { case BackendType::Playback: diff --git a/alc/backends/pipewire.cpp b/alc/backends/pipewire.cpp index b86197312b..70dbf65289 100644 --- a/alc/backends/pipewire.cpp +++ b/alc/backends/pipewire.cpp @@ -335,7 +335,7 @@ using Pod_t = typename PodInfo::Type; template al::span> get_array_span(const spa_pod *pod) { - uint32_t nvals; + uint32_t nvals{}; if(void *v{spa_pod_get_array(pod, &nvals)}) { if(get_array_value_type(pod) == T) diff --git a/alc/backends/pulseaudio.cpp b/alc/backends/pulseaudio.cpp index 8b2d256558..f37ce4afd2 100644 --- a/alc/backends/pulseaudio.cpp +++ b/alc/backends/pulseaudio.cpp @@ -513,18 +513,17 @@ void MainloopUniqueLock::connectContext() int err{pa_context_connect(mutex()->mContext, nullptr, pulse_ctx_flags, nullptr)}; if(err >= 0) { - pa_context_state_t state; - while((state=pa_context_get_state(mutex()->mContext)) != PA_CONTEXT_READY) + wait([&err,this]() { + pa_context_state_t state{pa_context_get_state(mutex()->mContext)}; if(!PA_CONTEXT_IS_GOOD(state)) { err = pa_context_errno(mutex()->mContext); - if(err > 0) err = -err; - break; + if(err > 0) err = -err; + return true; } - - wait(); - } + return state == PA_CONTEXT_READY; + }); } pa_context_set_state_callback(mutex()->mContext, nullptr, nullptr); @@ -559,9 +558,9 @@ pa_stream *MainloopUniqueLock::connectStream(const char *device_name, pa_stream_ stream_id, pa_strerror(err)}; } - pa_stream_state_t state; - while((state=pa_stream_get_state(stream)) != PA_STREAM_READY) + wait([&err,stream,stream_id,this]() { + pa_stream_state_t state{pa_stream_get_state(stream)}; if(!PA_STREAM_IS_GOOD(state)) { err = pa_context_errno(mutex()->mContext); @@ -569,9 +568,9 @@ pa_stream *MainloopUniqueLock::connectStream(const char *device_name, pa_stream_ throw al::backend_exception{al::backend_error::DeviceError, "%s did not get ready (%s)", stream_id, pa_strerror(err)}; } + return state == PA_STREAM_READY; + }); - wait(); - } pa_stream_set_state_callback(stream, nullptr, nullptr); return stream; @@ -1031,8 +1030,8 @@ void PulsePlayback::stop() ClockLatency PulsePlayback::getClockLatency() { ClockLatency ret; - pa_usec_t latency; - int neg, err; + pa_usec_t latency{}; + int neg{}, err{}; { MainloopUniqueLock plock{mMainloop}; @@ -1302,8 +1301,8 @@ void PulseCapture::captureSamples(std::byte *buffer, uint samples) break; } - const void *capbuf; - size_t caplen; + const void *capbuf{}; + size_t caplen{}; if(pa_stream_peek(mStream, &capbuf, &caplen) < 0) UNLIKELY { mDevice->handleDisconnect("Failed retrieving capture samples: %s", @@ -1359,8 +1358,8 @@ uint PulseCapture::availableSamples() ClockLatency PulseCapture::getClockLatency() { ClockLatency ret; - pa_usec_t latency; - int neg, err; + pa_usec_t latency{}; + int neg{}, err{}; { MainloopUniqueLock plock{mMainloop}; diff --git a/common/polyphase_resampler.cpp b/common/polyphase_resampler.cpp index 9c569b3a5a..ee9c707223 100644 --- a/common/polyphase_resampler.cpp +++ b/common/polyphase_resampler.cpp @@ -150,17 +150,9 @@ void PPhaseResampler::init(const uint srcRate, const uint dstRate) * ends before the nyquist (0.5). Both are scaled by the downsampling * factor. */ - double cutoff, width; - if(mP > mQ) - { - cutoff = 0.475 / mP; - width = 0.05 / mP; - } - else - { - cutoff = 0.475 / mQ; - width = 0.05 / mQ; - } + const auto [cutoff, width] = (mP > mQ) ? std::make_tuple(0.475 / mP, 0.05 / mP) + : std::make_tuple(0.475 / mQ, 0.05 / mQ); + // A rejection of -180 dB is used for the stop band. Round up when // calculating the left offset to avoid increasing the transition width. const uint l{(CalcKaiserOrder(180.0, width)+1) / 2}; diff --git a/common/ringbuffer.cpp b/common/ringbuffer.cpp index 2636bfb419..ffbc5a4491 100644 --- a/common/ringbuffer.cpp +++ b/common/ringbuffer.cpp @@ -29,6 +29,7 @@ #include #include "almalloc.h" +#include "alnumeric.h" RingBufferPtr RingBuffer::Create(std::size_t sz, std::size_t elem_sz, int limit_writes) @@ -74,18 +75,9 @@ std::size_t RingBuffer::read(void *dest, std::size_t cnt) noexcept const std::size_t to_read{std::min(cnt, free_cnt)}; std::size_t read_ptr{mReadPtr.load(std::memory_order_relaxed) & mSizeMask}; - std::size_t n1, n2; const std::size_t cnt2{read_ptr + to_read}; - if(cnt2 > mSizeMask+1) - { - n1 = mSizeMask+1 - read_ptr; - n2 = cnt2 & mSizeMask; - } - else - { - n1 = to_read; - n2 = 0; - } + const auto [n1, n2] = (cnt2 <= mSizeMask+1) ? std::make_tuple(to_read, 0_uz) + : std::make_tuple(mSizeMask+1 - read_ptr, cnt2&mSizeMask); auto outiter = std::copy_n(mBuffer.begin() + read_ptr*mElemSize, n1*mElemSize, static_cast(dest)); @@ -107,18 +99,9 @@ std::size_t RingBuffer::peek(void *dest, std::size_t cnt) const noexcept const std::size_t to_read{std::min(cnt, free_cnt)}; std::size_t read_ptr{mReadPtr.load(std::memory_order_relaxed) & mSizeMask}; - std::size_t n1, n2; const std::size_t cnt2{read_ptr + to_read}; - if(cnt2 > mSizeMask+1) - { - n1 = mSizeMask+1 - read_ptr; - n2 = cnt2 & mSizeMask; - } - else - { - n1 = to_read; - n2 = 0; - } + const auto [n1, n2] = (cnt2 <= mSizeMask+1) ? std::make_tuple(to_read, 0_uz) + : std::make_tuple(mSizeMask+1 - read_ptr, cnt2&mSizeMask); auto outiter = std::copy_n(mBuffer.begin() + read_ptr*mElemSize, n1*mElemSize, static_cast(dest)); @@ -135,18 +118,9 @@ std::size_t RingBuffer::write(const void *src, std::size_t cnt) noexcept const std::size_t to_write{std::min(cnt, free_cnt)}; std::size_t write_ptr{mWritePtr.load(std::memory_order_relaxed) & mSizeMask}; - std::size_t n1, n2; const std::size_t cnt2{write_ptr + to_write}; - if(cnt2 > mSizeMask+1) - { - n1 = mSizeMask+1 - write_ptr; - n2 = cnt2 & mSizeMask; - } - else - { - n1 = to_write; - n2 = 0; - } + const auto [n1, n2] = (cnt2 <= mSizeMask+1) ? std::make_tuple(to_write, 0_uz) + : std::make_tuple(mSizeMask+1 - write_ptr, cnt2&mSizeMask); auto srcbytes = static_cast(src); std::copy_n(srcbytes, n1*mElemSize, mBuffer.begin() + write_ptr*mElemSize); diff --git a/core/device.h b/core/device.h index dfaf60f414..44c39c5746 100644 --- a/core/device.h +++ b/core/device.h @@ -325,9 +325,8 @@ struct DeviceBase { /** Waits for the mixer to not be mixing or updating the clock. */ [[nodiscard]] auto waitForMix() const noexcept -> uint { - uint refcount; - while((refcount=mMixCount.load(std::memory_order_acquire))&1) { - } + uint refcount{mMixCount.load(std::memory_order_acquire)}; + while((refcount&1)) refcount = mMixCount.load(std::memory_order_acquire); return refcount; } diff --git a/core/filters/nfc.cpp b/core/filters/nfc.cpp index 95b84e2cef..96dcb250dd 100644 --- a/core/filters/nfc.cpp +++ b/core/filters/nfc.cpp @@ -59,13 +59,11 @@ constexpr std::array B{ NfcFilter1 NfcFilterCreate1(const float w0, const float w1) noexcept { NfcFilter1 nfc{}; - float b_00, g_0; - float r; /* Calculate bass-cut coefficients. */ - r = 0.5f * w1; - b_00 = B[1][0] * r; - g_0 = 1.0f + b_00; + float r{0.5f * w1}; + float b_00{B[1][0] * r}; + float g_0{1.0f + b_00}; nfc.base_gain = 1.0f / g_0; nfc.a1 = 2.0f * b_00 / g_0; @@ -95,14 +93,12 @@ void NfcFilterAdjust1(NfcFilter1 *nfc, const float w0) noexcept NfcFilter2 NfcFilterCreate2(const float w0, const float w1) noexcept { NfcFilter2 nfc{}; - float b_10, b_11, g_1; - float r; /* Calculate bass-cut coefficients. */ - r = 0.5f * w1; - b_10 = B[2][0] * r; - b_11 = B[2][1] * r * r; - g_1 = 1.0f + b_10 + b_11; + float r{0.5f * w1}; + float b_10{B[2][0] * r}; + float b_11{B[2][1] * r * r}; + float g_1{1.0f + b_10 + b_11}; nfc.base_gain = 1.0f / g_1; nfc.a1 = (2.0f*b_10 + 4.0f*b_11) / g_1; @@ -137,17 +133,14 @@ void NfcFilterAdjust2(NfcFilter2 *nfc, const float w0) noexcept NfcFilter3 NfcFilterCreate3(const float w0, const float w1) noexcept { NfcFilter3 nfc{}; - float b_10, b_11, g_1; - float b_00, g_0; - float r; /* Calculate bass-cut coefficients. */ - r = 0.5f * w1; - b_10 = B[3][0] * r; - b_11 = B[3][1] * r * r; - b_00 = B[3][2] * r; - g_1 = 1.0f + b_10 + b_11; - g_0 = 1.0f + b_00; + float r{0.5f * w1}; + float b_10{B[3][0] * r}; + float b_11{B[3][1] * r * r}; + float b_00{B[3][2] * r}; + float g_1{1.0f + b_10 + b_11}; + float g_0{1.0f + b_00}; nfc.base_gain = 1.0f / (g_1 * g_0); nfc.a1 = (2.0f*b_10 + 4.0f*b_11) / g_1; @@ -189,18 +182,15 @@ void NfcFilterAdjust3(NfcFilter3 *nfc, const float w0) noexcept NfcFilter4 NfcFilterCreate4(const float w0, const float w1) noexcept { NfcFilter4 nfc{}; - float b_10, b_11, g_1; - float b_00, b_01, g_0; - float r; /* Calculate bass-cut coefficients. */ - r = 0.5f * w1; - b_10 = B[4][0] * r; - b_11 = B[4][1] * r * r; - b_00 = B[4][2] * r; - b_01 = B[4][3] * r * r; - g_1 = 1.0f + b_10 + b_11; - g_0 = 1.0f + b_00 + b_01; + float r{0.5f * w1}; + float b_10{B[4][0] * r}; + float b_11{B[4][1] * r * r}; + float b_00{B[4][2] * r}; + float b_01{B[4][3] * r * r}; + float g_1{1.0f + b_10 + b_11}; + float g_0{1.0f + b_00 + b_01}; nfc.base_gain = 1.0f / (g_1 * g_0); nfc.a1 = (2.0f*b_10 + 4.0f*b_11) / g_1; diff --git a/core/hrtf.cpp b/core/hrtf.cpp index 5bac92d3d1..eb4e41a93c 100644 --- a/core/hrtf.cpp +++ b/core/hrtf.cpp @@ -118,34 +118,32 @@ class databuf final : public std::streambuf { if((mode&std::ios_base::out) || !(mode&std::ios_base::in)) return traits_type::eof(); - char_type *cur; switch(whence) { - case std::ios_base::beg: - if(offset < 0 || offset > egptr()-eback()) - return traits_type::eof(); - cur = eback() + offset; - break; - - case std::ios_base::cur: - if((offset >= 0 && offset > egptr()-gptr()) || - (offset < 0 && -offset > gptr()-eback())) - return traits_type::eof(); - cur = gptr() + offset; - break; + case std::ios_base::beg: + if(offset < 0 || offset > egptr()-eback()) + return traits_type::eof(); + setg(eback(), eback()+offset, egptr()); + break; - case std::ios_base::end: - if(offset > 0 || -offset > egptr()-eback()) - return traits_type::eof(); - cur = egptr() + offset; - break; + case std::ios_base::cur: + if((offset >= 0 && offset > egptr()-gptr()) || + (offset < 0 && -offset > gptr()-eback())) + return traits_type::eof(); + setg(eback(), gptr()+offset, egptr()); + break; - default: + case std::ios_base::end: + if(offset > 0 || -offset > egptr()-eback()) return traits_type::eof(); + setg(eback(), egptr()+offset, egptr()); + break; + + default: + return traits_type::eof(); } - setg(eback(), cur, egptr()); - return cur - eback(); + return gptr() - eback(); } pos_type seekpos(pos_type pos, std::ios_base::openmode mode) override @@ -157,7 +155,7 @@ class databuf final : public std::streambuf { if(pos < 0 || pos > egptr()-eback()) return traits_type::eof(); - setg(eback(), eback() + static_cast(pos), egptr()); + setg(eback(), eback()+static_cast(pos), egptr()); return pos; } @@ -1245,35 +1243,32 @@ std::vector EnumerateHrtf(std::optional pathopt) bool usedefaults{true}; if(pathopt) { - const char *pathlist{pathopt->c_str()}; - while(pathlist && *pathlist) + std::string_view pathlist{*pathopt}; + while(!pathlist.empty()) { - const char *next, *end; - - while(isspace(*pathlist) || *pathlist == ',') - pathlist++; - if(*pathlist == '\0') - continue; + while(!pathlist.empty() && (std::isspace(pathlist.front()) || pathlist.front() == ',')) + pathlist.remove_prefix(1); + if(pathlist.empty()) + break; - next = strchr(pathlist, ','); - if(next) - end = next++; + auto endpos = std::min(pathlist.find(','), pathlist.size()); + auto entry = pathlist.substr(0, endpos); + if(endpos < pathlist.size()) + pathlist.remove_prefix(++endpos); else { - end = pathlist + strlen(pathlist); + pathlist.remove_prefix(endpos); usedefaults = false; } - while(end != pathlist && isspace(*(end-1))) - --end; - if(end != pathlist) + while(!entry.empty() && std::isspace(entry.back())) + entry.remove_suffix(1); + if(!entry.empty()) { - const std::string pname{pathlist, end}; + const std::string pname{entry}; for(const auto &fname : SearchDataFiles(".mhr", pname.c_str())) AddFileEntry(fname); } - - pathlist = next; } }