refactor drive letter
This commit is contained in:
@@ -431,25 +431,8 @@ void ui_server::handle_put_mount_location(const httplib::Request &req,
|
|||||||
|
|
||||||
void ui_server::handle_get_available_locations(httplib::Response &res) {
|
void ui_server::handle_get_available_locations(httplib::Response &res) {
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
constexpr std::array<std::string_view, 26U> letters{
|
res.set_content(nlohmann::json(utils::get_available_drive_letters()).dump(),
|
||||||
"a:", "b:", "c:", "d:", "e:", "f:", "g:", "h:", "i:",
|
"application/json");
|
||||||
"j:", "k:", "l:", "m:", "n:", "o:", "p:", "q:", "r:",
|
|
||||||
"s:", "t:", "u:", "v:", "w:", "x:", "y:", "z:",
|
|
||||||
};
|
|
||||||
|
|
||||||
auto available = std::accumulate(
|
|
||||||
letters.begin(), letters.end(), std::vector<std::string_view>(),
|
|
||||||
[](auto &&vec, auto &&letter) -> std::vector<std::string_view> {
|
|
||||||
if (utils::file::directory{utils::path::combine(letter, {"\\"})}
|
|
||||||
.exists()) {
|
|
||||||
return vec;
|
|
||||||
}
|
|
||||||
|
|
||||||
vec.emplace_back(letter);
|
|
||||||
return vec;
|
|
||||||
});
|
|
||||||
|
|
||||||
res.set_content(nlohmann::json(available).dump(), "application/json");
|
|
||||||
#else // !defined(_WIN32)
|
#else // !defined(_WIN32)
|
||||||
res.set_content(nlohmann::json(std::vector<std::string_view>()).dump(),
|
res.set_content(nlohmann::json(std::vector<std::string_view>()).dump(),
|
||||||
"application/json");
|
"application/json");
|
||||||
|
@@ -332,7 +332,9 @@ protected:
|
|||||||
mount_location2 = mount_location;
|
mount_location2 = mount_location;
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
mount_location = utils::string::to_lower(std::string{"V:"});
|
auto letter = utils::get_available_drive_letter('d');
|
||||||
|
ASSERT_TRUE(letter.has_value());
|
||||||
|
mount_location = utils::string::to_lower(std::string{letter.value()});
|
||||||
#else // !defined(_WIN32)
|
#else // !defined(_WIN32)
|
||||||
mount_location = utils::path::combine(test_dir, {"mount"});
|
mount_location = utils::path::combine(test_dir, {"mount"});
|
||||||
ASSERT_TRUE(utils::file::directory(mount_location).create_directory());
|
ASSERT_TRUE(utils::file::directory(mount_location).create_directory());
|
||||||
|
@@ -30,6 +30,12 @@ void create_console();
|
|||||||
|
|
||||||
void free_console();
|
void free_console();
|
||||||
|
|
||||||
|
[[nodiscard]] auto get_available_drive_letter(char first = 'a')
|
||||||
|
-> std::optional<std::string_view>;
|
||||||
|
|
||||||
|
[[nodiscard]] auto get_available_drive_letters(char first = 'a')
|
||||||
|
-> std::vector<std::string_view>;
|
||||||
|
|
||||||
[[nodiscard]] auto get_local_app_data_directory() -> const std::string &;
|
[[nodiscard]] auto get_local_app_data_directory() -> const std::string &;
|
||||||
|
|
||||||
[[nodiscard]] auto get_last_error_code() -> DWORD;
|
[[nodiscard]] auto get_last_error_code() -> DWORD;
|
||||||
|
@@ -29,6 +29,14 @@
|
|||||||
#include "utils/path.hpp"
|
#include "utils/path.hpp"
|
||||||
#include "utils/string.hpp"
|
#include "utils/string.hpp"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
constexpr std::array<std::string_view, 26U> drive_letters{
|
||||||
|
"a:", "b:", "c:", "d:", "e:", "f:", "g:", "h:", "i:",
|
||||||
|
"j:", "k:", "l:", "m:", "n:", "o:", "p:", "q:", "r:",
|
||||||
|
"s:", "t:", "u:", "v:", "w:", "x:", "y:", "z:",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
namespace repertory::utils {
|
namespace repertory::utils {
|
||||||
void create_console() {
|
void create_console() {
|
||||||
if (::AllocConsole() == 0) {
|
if (::AllocConsole() == 0) {
|
||||||
@@ -61,6 +69,47 @@ void create_console() {
|
|||||||
|
|
||||||
void free_console() { ::FreeConsole(); }
|
void free_console() { ::FreeConsole(); }
|
||||||
|
|
||||||
|
auto get_available_drive_letter(char first) -> std::optional<std::string_view> {
|
||||||
|
const auto *begin = std::ranges::find_if(
|
||||||
|
drive_letters, [first](auto &&val) { return val.at(0U) == first; });
|
||||||
|
if (begin == drive_letters.end()) {
|
||||||
|
begin = drive_letters.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto available =
|
||||||
|
std::ranges::find_if(begin, drive_letters.end(), [](auto &&val) -> bool {
|
||||||
|
return not utils::file::directory{utils::path::combine(val, {"\\"})}
|
||||||
|
.exists();
|
||||||
|
});
|
||||||
|
if (available == drive_letters.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *available;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto get_available_drive_letters(char first) -> std::vector<std::string_view> {
|
||||||
|
const auto *begin =
|
||||||
|
std::ranges::find_if(drive_letters, [first](auto &&val) -> bool {
|
||||||
|
return val.at(0U) == first;
|
||||||
|
});
|
||||||
|
if (begin == drive_letters.end()) {
|
||||||
|
begin = drive_letters.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::accumulate(
|
||||||
|
begin, drive_letters.end(), std::vector<std::string_view>(),
|
||||||
|
[](auto &&vec, auto &&letter) -> auto {
|
||||||
|
if (utils::file::directory{utils::path::combine(letter, {"\\"})}
|
||||||
|
.exists()) {
|
||||||
|
return vec;
|
||||||
|
}
|
||||||
|
|
||||||
|
vec.emplace_back(letter);
|
||||||
|
return vec;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
auto get_last_error_code() -> DWORD { return ::GetLastError(); }
|
auto get_last_error_code() -> DWORD { return ::GetLastError(); }
|
||||||
|
|
||||||
auto get_local_app_data_directory() -> const std::string & {
|
auto get_local_app_data_directory() -> const std::string & {
|
||||||
|
Reference in New Issue
Block a user