Banuba SDK
Loading...
Searching...
No Matches
filesystem.hpp
1
2#pragma once
3
4#include <bnb/utils/defs.hpp>
5
6#include <filesystem>
7
8// we can't pollute global namespace in header
9namespace bnb
10{
11 namespace fs = std::filesystem;
12 using error_code = std::error_code;
13 using file_time_type = std::filesystem::file_time_type;
14
15 inline fs::path replace_extension(fs::path p, const std::string& ext)
16 {
17 return p.replace_extension(std::string(".") + ext);
18 }
19
20 std::string path_to_utf8(const fs::path& p);
21 fs::path utf8_to_path(const std::string& s);
22
23 inline std::string remove_trailing_separator(std::string strpath)
24 {
25 if (!strpath.empty() && (fs::path::preferred_separator == strpath[strpath.size() - 1])) {
26 strpath.erase(strpath.size() - 1);
27 }
28 return strpath;
29 }
30
31 template<typename... T>
32 bool match_extesions(const fs::path& path, T&&... extensions)
33 {
34 auto ext = bnb::path_to_utf8(path.extension());
35 std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c) { return std::tolower(c); });
36 bool match = false;
37 ([&]() {
38 if (match) {
39 return;
40 }
41 std::string ext_to_lower(extensions);
42 std::transform(ext_to_lower.begin(), ext_to_lower.end(), ext_to_lower.begin(), [](unsigned char c) { return std::tolower(c); });
43 match = ext == ext_to_lower;
44 }(),
45 ...);
46
47 return match;
48 }
49} // namespace bnb