From 6accec7c57a87aaff2e701f5108ff3f87c858467 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Fri, 18 Dec 2020 23:38:41 +0300 Subject: [PATCH] Move path functions to filepath_util.h --- filepath_util.h | 45 +++++++++++++++++++++++++++++++++++++++++++ main.cpp | 51 +++---------------------------------------------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/filepath_util.h b/filepath_util.h index dd7ef04..87f9ba6 100644 --- a/filepath_util.h +++ b/filepath_util.h @@ -5,7 +5,9 @@ #include #include #include +#include #include +#include namespace filepath { @@ -22,4 +24,47 @@ namespace filepath return std::equal(ending.rbegin(), ending.rend(), string.rbegin()); } + + static std::tuple parsePath(const std::string &argv) + { + std::filesystem::path path(argv); + if (!std::filesystem::exists(path)) + { + std::cout << "Path " << path << " does not exist.\n"; + return {EXIT_FAILURE, {}}; + } + + // Maybe user chose a specific image, not a folder + if (std::filesystem::is_regular_file(path)) + return {EXIT_SUCCESS, path.string()}; + + // So... it is a folder + // Creating a vector of everything in the given directory + std::vector> dir_items; + std::transform(std::filesystem::directory_iterator(path), {}, std::back_inserter(dir_items), filepath::getFileInfo); + + std::set allowed_ext = {".bmp", ".dds", ".jpg", ".png", ".tga", ".psd"}; + + // Now getting images + std::vector dir_image_items; + for (const auto &[local_path, status] : dir_items) + { + const std::string str_path = local_path.string(); + if (std::filesystem::is_regular_file(local_path) && + std::any_of(allowed_ext.begin(), allowed_ext.end(), [&](const std::string& e) { return filepath::endsWith(str_path, e); })) + { + dir_image_items.emplace_back(str_path); + } + } + + if (dir_image_items.empty()) + { + std::cout << "No images found at " << path << "\n--help for more information.\n"; + return {EXIT_FAILURE, {}}; + } + + std::cout << "Loading random image file from " << path << "\n"; + srand(static_cast(time(nullptr))); + return {EXIT_SUCCESS, dir_image_items[rand() & (dir_image_items.size() - 1)]}; + } } diff --git a/main.cpp b/main.cpp index b64615e..e224e35 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,8 @@ #include "application.h" #include "output_util.h" #include "filepath_util.h" -#include #include #include -#include ///////////////////////////////////////////////////////////////////////// @@ -22,49 +20,6 @@ static std::tuple error(const char* msg) return {EXIT_FAILURE, -1, {}}; } -static std::tuple parsePath(const std::string &argv) -{ - std::filesystem::path path(argv); - if (!std::filesystem::exists(path)) - { - std::cout << "Path " << path << " does not exist.\n"; - return {EXIT_FAILURE, {}}; - } - - // Maybe user chose a specific image, not a folder - if (std::filesystem::is_regular_file(path)) - return {EXIT_SUCCESS, path.string()}; - - // So... it is a folder - // Creating a vector of everything in the given directory - std::vector> dir_items; - std::transform(std::filesystem::directory_iterator(path), {}, std::back_inserter(dir_items), filepath::getFileInfo); - - std::set allowed_ext = {".bmp", ".dds", ".jpg", ".png", ".tga", ".psd"}; - - // Now getting images - std::vector dir_image_items; - for (const auto &[local_path, status] : dir_items) - { - const std::string str_path = local_path.string(); - if (std::filesystem::is_regular_file(local_path) && - std::any_of(allowed_ext.begin(), allowed_ext.end(), [&](const std::string& e) { return filepath::endsWith(str_path, e); })) - { - dir_image_items.emplace_back(str_path); - } - } - - if (dir_image_items.empty()) - { - std::cout << "No images found at " << path << "\n--help for more information.\n"; - return {EXIT_FAILURE, {}}; - } - - std::cout << "Loading random image file from " << path << "\n"; - srand(static_cast(time(nullptr))); - return {EXIT_SUCCESS, dir_image_items[rand() & (dir_image_items.size() - 1)]}; -} - static std::tuple parseInput(int argc, char **argv) { int splitting = DEFAULT_SPLITTING; @@ -80,7 +35,7 @@ static std::tuple parseInput(int argc, char **argv) // Just launch the application with default parameters std::cout << "No arguments given. Launching at \"" << DEFAULT_PATH << "\" with splitting " << std::to_string(DEFAULT_SPLITTING) << "\n" << "--help for more information.\n"; - const auto &[ret_code, ret_path] = parsePath(DEFAULT_PATH); + const auto &[ret_code, ret_path] = filepath::parsePath(DEFAULT_PATH); if (ret_code) return error(output::IMG_FAIL_MSG); @@ -95,7 +50,7 @@ static std::tuple parseInput(int argc, char **argv) if (strcmp(argv[1], output::HELP_FLAG) == 0) return error(output::HELP_MSG); - const auto &[ret_code, ret_path] = parsePath(argv[1]); + const auto &[ret_code, ret_path] = filepath::parsePath(argv[1]); if (ret_code) return error(output::IMG_FAIL_MSG); @@ -117,7 +72,7 @@ static std::tuple parseInput(int argc, char **argv) if (splitting < 1) return error(output::SPLITTING_MSG); - const auto &[ret_code, ret_path] = parsePath(argc == MAX_ARGC ? argv[3] : DEFAULT_PATH); + const auto &[ret_code, ret_path] = filepath::parsePath(argc == MAX_ARGC ? argv[3] : DEFAULT_PATH); if (ret_code) return error(output::IMG_FAIL_MSG);