Skip to content

Commit

Permalink
Clean up some clang-tidy warnings in the router
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed May 3, 2024
1 parent eaf824f commit ef33e89
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 85 deletions.
81 changes: 41 additions & 40 deletions router/alc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
#include "AL/alc.h"

#include "almalloc.h"
#include "alstring.h"
#include "router.h"


namespace {

using namespace std::string_view_literals;

struct FuncExportEntry {
const char *funcName;
void *address;
Expand Down Expand Up @@ -277,15 +278,18 @@ const std::array alcEnumerations{
};
#undef DECL

constexpr ALCchar alcNoError[] = "No Error";
constexpr ALCchar alcErrInvalidDevice[] = "Invalid Device";
constexpr ALCchar alcErrInvalidContext[] = "Invalid Context";
constexpr ALCchar alcErrInvalidEnum[] = "Invalid Enum";
constexpr ALCchar alcErrInvalidValue[] = "Invalid Value";
constexpr ALCchar alcErrOutOfMemory[] = "Out of Memory";
constexpr ALCchar alcExtensionList[] =
"ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
"ALC_EXT_thread_local_context";
[[nodiscard]] constexpr auto GetNoErrorString() noexcept { return "No Error"; }
[[nodiscard]] constexpr auto GetInvalidDeviceString() noexcept { return "Invalid Device"; }
[[nodiscard]] constexpr auto GetInvalidContextString() noexcept { return "Invalid Context"; }
[[nodiscard]] constexpr auto GetInvalidEnumString() noexcept { return "Invalid Enum"; }
[[nodiscard]] constexpr auto GetInvalidValueString() noexcept { return "Invalid Value"; }
[[nodiscard]] constexpr auto GetOutOfMemoryString() noexcept { return "Out of Memory"; }

[[nodiscard]] constexpr auto GetExtensionList() noexcept -> std::string_view
{
return "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
"ALC_EXT_thread_local_context"sv;
}

constexpr ALCint alcMajorVersion = 1;
constexpr ALCint alcMinorVersion = 1;
Expand Down Expand Up @@ -318,7 +322,8 @@ struct EnumeratedList {
}

void AppendDeviceList(const ALCchar *names, ALint idx);
ALint GetDriverIndexForName(const std::string_view name) const;
[[nodiscard]]
auto GetDriverIndexForName(const std::string_view name) const -> ALint;
};
EnumeratedList DevicesList;
EnumeratedList AllDevicesList;
Expand All @@ -329,24 +334,24 @@ void EnumeratedList::AppendDeviceList(const ALCchar* names, ALint idx)
const ALCchar *name_end = names;
if(!name_end) return;

ALCsizei count = 0;
size_t count{0};
while(*name_end)
{
TRACE("Enumerated \"%s\", driver %d\n", name_end, idx);
++count;
name_end += strlen(name_end)+1;
name_end += strlen(name_end)+1; /* NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) */
}
if(names == name_end)
return;

Names.reserve(Names.size() + (name_end - names) + 1);
Names.reserve(Names.size() + static_cast<size_t>(name_end - names) + 1);
Names.insert(Names.cend(), names, name_end);

Indicies.reserve(Indicies.size() + count);
Indicies.insert(Indicies.cend(), count, idx);
}

ALint EnumeratedList::GetDriverIndexForName(const std::string_view name) const
auto EnumeratedList::GetDriverIndexForName(const std::string_view name) const -> ALint
{
auto devnames = Names.cbegin();
auto index = Indicies.cbegin();
Expand All @@ -355,7 +360,7 @@ ALint EnumeratedList::GetDriverIndexForName(const std::string_view name) const
{
if(name == al::to_address(devnames))
return *index;
devnames += strlen(al::to_address(devnames))+1;
devnames += ptrdiff_t(strlen(al::to_address(devnames))+1);
++index;
}
return -1;
Expand Down Expand Up @@ -423,10 +428,8 @@ ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *devicename) noexcep
* quality hint for the wrapper driver. Ignore them since there's no sane
* way to map them.
*/
if(devicename && (devicename[0] == '\0' ||
strcmp(devicename, "DirectSound3D") == 0 ||
strcmp(devicename, "DirectSound") == 0 ||
strcmp(devicename, "MMSYSTEM") == 0))
if(devicename && (*devicename == '\0' || devicename == "DirectSound3D"sv
|| devicename == "DirectSound"sv || devicename == "MMSYSTEM"sv))
devicename = nullptr;
if(devicename)
{
Expand Down Expand Up @@ -629,18 +632,16 @@ ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const A
return ALC_FALSE;
}

const size_t len{strlen(extname)};
const char *ptr{alcExtensionList};
while(ptr && *ptr)
const auto tofind = std::string_view{extname};
const auto extlist = GetExtensionList();
auto matchpos = extlist.find(tofind);
while(matchpos != std::string_view::npos)
{
if(al::strncasecmp(ptr, extname, len) == 0 && (ptr[len] == '\0' || isspace(ptr[len])))
const auto endpos = matchpos + tofind.size();
if((matchpos == 0 || std::isspace(extlist[matchpos-1]))
&& (endpos == extlist.size() || std::isspace(extlist[endpos])))
return ALC_TRUE;
if((ptr=strchr(ptr, ' ')) != nullptr)
{
do {
++ptr;
} while(isspace(*ptr));
}
matchpos = extlist.find(tofind, matchpos+1);
}
return ALC_FALSE;
}
Expand Down Expand Up @@ -694,13 +695,13 @@ ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *device, ALCenum para

switch(param)
{
case ALC_NO_ERROR: return alcNoError;
case ALC_INVALID_ENUM: return alcErrInvalidEnum;
case ALC_INVALID_VALUE: return alcErrInvalidValue;
case ALC_INVALID_DEVICE: return alcErrInvalidDevice;
case ALC_INVALID_CONTEXT: return alcErrInvalidContext;
case ALC_OUT_OF_MEMORY: return alcErrOutOfMemory;
case ALC_EXTENSIONS: return alcExtensionList;
case ALC_NO_ERROR: return GetNoErrorString();
case ALC_INVALID_ENUM: return GetInvalidEnumString();
case ALC_INVALID_VALUE: return GetInvalidValueString();
case ALC_INVALID_DEVICE: return GetInvalidDeviceString();
case ALC_INVALID_CONTEXT: return GetInvalidContextString();
case ALC_OUT_OF_MEMORY: return GetOutOfMemoryString();
case ALC_EXTENSIONS: return GetExtensionList().data();

case ALC_DEVICE_SPECIFIER:
{
Expand Down Expand Up @@ -829,14 +830,14 @@ ALC_API void ALC_APIENTRY alcGetIntegerv(ALCdevice *device, ALCenum param, ALCsi
case ALC_MAJOR_VERSION:
if(size >= 1)
{
values[0] = alcMajorVersion;
*values = alcMajorVersion;
return;
}
/*fall-through*/
case ALC_MINOR_VERSION:
if(size >= 1)
{
values[0] = alcMinorVersion;
*values = alcMinorVersion;
return;
}
LastError.store(ALC_INVALID_VALUE);
Expand Down Expand Up @@ -866,7 +867,7 @@ ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *devicename,
ALCdevice *device{nullptr};
ALint idx{0};

if(devicename && devicename[0] == '\0')
if(devicename && *devicename == '\0')
devicename = nullptr;
if(devicename)
{
Expand Down
54 changes: 27 additions & 27 deletions router/router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "router.h"

#include <algorithm>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <cstring>
Expand All @@ -14,15 +15,15 @@
#include "AL/alc.h"
#include "AL/al.h"

#include "almalloc.h"
#include "alstring.h"
#include "opthelpers.h"
#include "strutils.h"

#include "version.h"


enum LogLevel LogLevel = LogLevel_Error;
FILE *LogFile;
eLogLevel LogLevel{eLogLevel::Error};
gsl::owner<std::FILE*> LogFile;

namespace {

Expand Down Expand Up @@ -170,7 +171,7 @@ void AddModule(HMODULE module, const std::wstring_view name)
LOAD_PROC(alDistanceModel);
if(!err)
{
ALCint alc_ver[2] = { 0, 0 };
std::array<ALCint,2> alc_ver{0, 0};
newdrv.alcGetIntegerv(nullptr, ALC_MAJOR_VERSION, 1, &alc_ver[0]);
newdrv.alcGetIntegerv(nullptr, ALC_MINOR_VERSION, 1, &alc_ver[1]);
if(newdrv.alcGetError(nullptr) == ALC_NO_ERROR)
Expand Down Expand Up @@ -240,22 +241,21 @@ void SearchDrivers(const std::wstring_view path)

WIN32_FIND_DATAW fdata{};
HANDLE srchHdl{FindFirstFileW(srchPath.c_str(), &fdata)};
if(srchHdl != INVALID_HANDLE_VALUE)
{
do {
srchPath = path;
srchPath += L"\\";
srchPath += fdata.cFileName;
TRACE("Found %ls\n", srchPath.c_str());

HMODULE mod{LoadLibraryW(srchPath.c_str())};
if(!mod)
WARN("Could not load %ls\n", srchPath.c_str());
else
AddModule(mod, fdata.cFileName);
} while(FindNextFileW(srchHdl, &fdata));
FindClose(srchHdl);
}
if(srchHdl == INVALID_HANDLE_VALUE) return;

do {
srchPath = path;
srchPath += L"\\";
srchPath += std::data(fdata.cFileName);
TRACE("Found %ls\n", srchPath.c_str());

HMODULE mod{LoadLibraryW(srchPath.c_str())};
if(!mod)
WARN("Could not load %ls\n", srchPath.c_str());
else
AddModule(mod, std::data(fdata.cFileName));
} while(FindNextFileW(srchHdl, &fdata));
FindClose(srchHdl);
}

bool GetLoadedModuleDirectory(const WCHAR *name, std::wstring *moddir)
Expand All @@ -265,7 +265,7 @@ bool GetLoadedModuleDirectory(const WCHAR *name, std::wstring *moddir)
if(name)
{
module = GetModuleHandleW(name);
if(!module) return 0;
if(!module) return false;
}

moddir->assign(256, '\0');
Expand Down Expand Up @@ -384,10 +384,9 @@ BOOL APIENTRY DllMain(HINSTANCE, DWORD reason, void*)
switch(reason)
{
case DLL_PROCESS_ATTACH:
LogFile = stderr;
if(auto logfname = al::getenv("ALROUTER_LOGFILE"))
{
FILE *f = fopen(logfname->c_str(), "w");
gsl::owner<std::FILE*> f{fopen(logfname->c_str(), "w")};
if(f == nullptr)
ERR("Could not open log file: %s\n", logfname->c_str());
else
Expand All @@ -396,13 +395,14 @@ BOOL APIENTRY DllMain(HINSTANCE, DWORD reason, void*)
if(auto loglev = al::getenv("ALROUTER_LOGLEVEL"))
{
char *end = nullptr;
long l = strtol(loglev->c_str(), &end, 0);
long l{strtol(loglev->c_str(), &end, 0)};
if(!end || *end != '\0')
ERR("Invalid log level value: %s\n", loglev->c_str());
else if(l < LogLevel_None || l > LogLevel_Trace)
else if(l < al::to_underlying(eLogLevel::None)
|| l > al::to_underlying(eLogLevel::Trace))
ERR("Log level out of range: %s\n", loglev->c_str());
else
LogLevel = static_cast<enum LogLevel>(l);
LogLevel = static_cast<eLogLevel>(l);
}
TRACE("Initializing router v0.1-%s %s\n", ALSOFT_GIT_COMMIT_HASH, ALSOFT_GIT_BRANCH);
LoadDriverList();
Expand All @@ -417,7 +417,7 @@ BOOL APIENTRY DllMain(HINSTANCE, DWORD reason, void*)
case DLL_PROCESS_DETACH:
DriverList.clear();

if(LogFile && LogFile != stderr)
if(LogFile)
fclose(LogFile);
LogFile = nullptr;

Expand Down
40 changes: 22 additions & 18 deletions router/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
#include <windows.h>
#include <winnt.h>

#include <stdio.h>

#include <atomic>
#include <cstdio>
#include <memory>
#include <mutex>
#include <string>
Expand All @@ -18,6 +17,8 @@
#include "AL/al.h"
#include "AL/alext.h"

#include "almalloc.h"


#define MAKE_ALC_VER(major, minor) (((major)<<8) | (minor))

Expand Down Expand Up @@ -182,34 +183,37 @@ inline DriverIface *GetThreadDriver() noexcept { return ThreadCtxDriver; }
inline void SetThreadDriver(DriverIface *driver) noexcept { ThreadCtxDriver = driver; }


enum LogLevel {
LogLevel_None = 0,
LogLevel_Error = 1,
LogLevel_Warn = 2,
LogLevel_Trace = 3,
enum class eLogLevel {
None = 0,
Error = 1,
Warn = 2,
Trace = 3,
};
extern enum LogLevel LogLevel;
extern FILE *LogFile;
extern eLogLevel LogLevel;
extern gsl::owner<std::FILE*> LogFile;

#define TRACE(...) do { \
if(LogLevel >= LogLevel_Trace) \
if(LogLevel >= eLogLevel::Trace) \
{ \
fprintf(LogFile, "AL Router (II): " __VA_ARGS__); \
fflush(LogFile); \
std::FILE *file{LogFile ? LogFile : stderr}; \
fprintf(file, "AL Router (II): " __VA_ARGS__); \
fflush(file); \
} \
} while(0)
#define WARN(...) do { \
if(LogLevel >= LogLevel_Warn) \
if(LogLevel >= eLogLevel::Warn) \
{ \
fprintf(LogFile, "AL Router (WW): " __VA_ARGS__); \
fflush(LogFile); \
std::FILE *file{LogFile ? LogFile : stderr}; \
fprintf(file, "AL Router (WW): " __VA_ARGS__); \
fflush(file); \
} \
} while(0)
#define ERR(...) do { \
if(LogLevel >= LogLevel_Error) \
if(LogLevel >= eLogLevel::Error) \
{ \
fprintf(LogFile, "AL Router (EE): " __VA_ARGS__); \
fflush(LogFile); \
std::FILE *file{LogFile ? LogFile : stderr}; \
fprintf(file, "AL Router (EE): " __VA_ARGS__); \
fflush(file); \
} \
} while(0)

Expand Down

0 comments on commit ef33e89

Please sign in to comment.