#pragma once #include #include #include #include #include #include #include #include namespace filepath { std::tuple getFileInfo(const std::filesystem::directory_entry& entry) { const auto file_status (std::filesystem::status(entry)); return {entry.path(), file_status}; } bool endsWith(const std::string& string, const std::string& ending) { if (ending.size() > string.size()) return false; return std::equal(ending.rbegin(), ending.rend(), string.rbegin()); } 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()}; // TO DO : I KNOW THIS PART IS BAD! I have never worked with ::filesystem before, // So... it is a folder i will rewrite it when the prject works and is done // 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)]}; } std::vector split(const std::string &s, char delim) { std::vector result; std::stringstream ss (s); std::string item; while (getline(ss, item, delim)) result.push_back(item); return result; } }