Skip to content

Commit

Permalink
Improve compatibility with C++20's u8string()
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Nov 22, 2024
1 parent b27ef50 commit a2085cc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
21 changes: 12 additions & 9 deletions alc/alconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ void ReadALConfig()
path = fs::path{buffer};
path /= L"alsoft.ini";

TRACE("Loading config %s...\n", path.u8string().c_str());
TRACE("Loading config %s...\n",
reinterpret_cast<const char*>(path.u8string().c_str()));
if(std::ifstream f{path}; f.is_open())
LoadConfigFromFile(f);
}
Expand All @@ -364,15 +365,15 @@ void ReadALConfig()
if(!path.empty())
{
path /= L"alsoft.ini";
TRACE("Loading config %s...\n", path.u8string().c_str());
TRACE("Loading config %s...\n", reinterpret_cast<const char*>(path.u8string().c_str()));
if(std::ifstream f{path}; f.is_open())
LoadConfigFromFile(f);
}

if(auto confpath = al::getenv(L"ALSOFT_CONF"))
{
path = *confpath;
TRACE("Loading config %s...\n", path.u8string().c_str());
TRACE("Loading config %s...\n", reinterpret_cast<const char*>(path.u8string().c_str()));
if(std::ifstream f{path}; f.is_open())
LoadConfigFromFile(f);
}
Expand All @@ -385,7 +386,7 @@ void ReadALConfig()
namespace fs = std::filesystem;
fs::path path{"/etc/openal/alsoft.conf"};

TRACE("Loading config %s...\n", path.u8string().c_str());
TRACE("Loading config %s...\n", reinterpret_cast<const char*>(path.u8string().c_str()));
if(std::ifstream f{path}; f.is_open())
LoadConfigFromFile(f);

Expand All @@ -410,12 +411,14 @@ void ReadALConfig()
}

if(!path.is_absolute())
WARN("Ignoring XDG config dir: %s\n", path.u8string().c_str());
WARN("Ignoring XDG config dir: %s\n",
reinterpret_cast<const char*>(path.u8string().c_str()));
else
{
path /= "alsoft.conf";

TRACE("Loading config %s...\n", path.u8string().c_str());
TRACE("Loading config %s...\n",
reinterpret_cast<const char*>(path.u8string().c_str()));
if(std::ifstream f{path}; f.is_open())
LoadConfigFromFile(f);
}
Expand All @@ -442,7 +445,7 @@ void ReadALConfig()
path = *homedir;
path /= ".alsoftrc";

TRACE("Loading config %s...\n", path.u8string().c_str());
TRACE("Loading config %s...\n", reinterpret_cast<const char*>(path.u8string().c_str()));
if(std::ifstream f{path}; f.is_open())
LoadConfigFromFile(f);
}
Expand All @@ -463,7 +466,7 @@ void ReadALConfig()
}
if(!path.empty())
{
TRACE("Loading config %s...\n", path.u8string().c_str());
TRACE("Loading config %s...\n", reinterpret_cast<const char*>(path.u8string().c_str()));
if(std::ifstream f{path}; f.is_open())
LoadConfigFromFile(f);
}
Expand All @@ -473,7 +476,7 @@ void ReadALConfig()
{
path /= "alsoft.conf";

TRACE("Loading config %s...\n", path.u8string().c_str());
TRACE("Loading config %s...\n", reinterpret_cast<const char*>(path.u8string().c_str()));
if(std::ifstream f{path}; f.is_open())
LoadConfigFromFile(f);
}
Expand Down
15 changes: 15 additions & 0 deletions common/alstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ int case_compare(const std::string_view str0, const std::string_view str1) noexc
[[nodiscard]]
int case_compare(const std::wstring_view str0, const std::wstring_view str1) noexcept;

/* C++20 changes path::u8string() to return a string using a new/distinct
* char8_t type for UTF-8 strings. However, support for this with standard
* string functions is totally inadequate, and we already hold UTF-8 with plain
* char strings. So this function is used to reinterpret a char8_t string as a
* char string_view.
*/
#if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201907L
inline auto u8_as_char(const std::u8string_view str) -> std::string_view
#else
inline auto u8_as_char(const std::string_view str) -> std::string_view
#endif
{
return std::string_view{reinterpret_cast<const char*>(str.data()), str.size()};
}

} // namespace al

#endif /* AL_STRING_H */
14 changes: 9 additions & 5 deletions core/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <mutex>
#include <optional>
#include <string>
#include <string_view>
#include <system_error>

#include "almalloc.h"
Expand Down Expand Up @@ -44,16 +45,19 @@ void DirectorySearch(const std::filesystem::path &path, const std::string_view e
if(!fs::exists(fpath))
return;

TRACE("Searching %s for *%.*s\n", fpath.u8string().c_str(), al::sizei(ext), ext.data());
TRACE("Searching %s for *%.*s\n", reinterpret_cast<const char*>(fpath.u8string().c_str()),
al::sizei(ext), ext.data());
for(auto&& dirent : fs::directory_iterator{fpath})
{
auto&& entrypath = dirent.path();
if(!entrypath.has_extension())
continue;

if(fs::status(entrypath).type() == fs::file_type::regular
&& al::case_compare(entrypath.extension().u8string(), ext) == 0)
results->emplace_back(entrypath.u8string());
if(fs::status(entrypath).type() != fs::file_type::regular)
continue;
const auto u8ext = entrypath.extension().u8string();
if(al::case_compare(al::u8_as_char(u8ext), ext) == 0)
results->emplace_back(al::u8_as_char(entrypath.u8string()));
}
}
catch(std::exception& e) {
Expand Down Expand Up @@ -276,7 +280,7 @@ const PathNamePair &GetProcBinary()
continue;
if(auto path = std::filesystem::read_symlink(name); !path.empty())
{
pathname = path.u8string();
pathname = al::u8_as_char(path.u8string());
break;
}
}
Expand Down

0 comments on commit a2085cc

Please sign in to comment.