Extract function to create a random filename.

This commit is contained in:
Colin LeMahieu 2024-09-16 13:27:52 +01:00
commit d831937f12
No known key found for this signature in database
GPG key ID: 43708520C8DFB938
2 changed files with 8 additions and 2 deletions

View file

@ -47,7 +47,7 @@ std::filesystem::path nano::working_path (nano::networks network)
return result;
}
std::filesystem::path nano::unique_path (nano::networks network)
std::filesystem::path nano::random_filename ()
{
std::random_device rd;
std::mt19937 gen (rd ());
@ -61,8 +61,12 @@ std::filesystem::path nano::unique_path (nano::networks network)
{
random_string += hex_chars[dis (gen)];
}
return std::filesystem::path{ random_string };
}
auto result = working_path (network) / random_string;
std::filesystem::path nano::unique_path (nano::networks network)
{
auto result = working_path (network) / random_filename ();
std::filesystem::create_directories (result);

View file

@ -9,6 +9,8 @@ namespace nano
std::filesystem::path app_path ();
// OS-specific way of finding a path to a home directory.
std::filesystem::path working_path (nano::networks network = nano::network_constants::active_network);
// Construct a random filename
std::filesystem::path random_filename ();
// Get a unique path within the home directory, used for testing.
// Any directories created at this location will be removed when a test finishes.
std::filesystem::path unique_path (nano::networks network = nano::network_constants::active_network);