2020-12-13 13:45:52 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <numeric>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
2020-12-18 15:38:41 -05:00
|
|
|
#include <set>
|
2020-12-13 13:45:52 -05:00
|
|
|
#include <filesystem>
|
2020-12-18 15:38:41 -05:00
|
|
|
#include <iostream>
|
2020-12-13 13:45:52 -05:00
|
|
|
|
2020-12-18 15:12:28 -05:00
|
|
|
namespace filepath
|
2020-12-13 13:45:52 -05:00
|
|
|
{
|
2020-12-20 08:49:24 -05:00
|
|
|
std::tuple<std::filesystem::path, std::filesystem::file_status> getFileInfo(const std::filesystem::directory_entry& entry)
|
2020-12-18 15:12:28 -05:00
|
|
|
{
|
|
|
|
const auto file_status (std::filesystem::status(entry));
|
|
|
|
return {entry.path(), file_status};
|
|
|
|
}
|
|
|
|
|
2020-12-20 08:49:24 -05:00
|
|
|
bool endsWith(const std::string& string, const std::string& ending)
|
2020-12-18 15:12:28 -05:00
|
|
|
{
|
|
|
|
if (ending.size() > string.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return std::equal(ending.rbegin(), ending.rend(), string.rbegin());
|
|
|
|
}
|
2020-12-18 15:38:41 -05:00
|
|
|
|
2021-04-24 16:46:31 -04:00
|
|
|
std::string randomChoice(const std::vector<std::string>& poll)
|
2020-12-18 15:38:41 -05:00
|
|
|
{
|
2021-04-24 16:46:31 -04:00
|
|
|
std::cout << "Loading random image!\n";
|
|
|
|
const auto range = poll.size() - 1;
|
|
|
|
return poll.at(rand() & range);
|
|
|
|
}
|
2020-12-18 15:38:41 -05:00
|
|
|
|
2021-04-24 16:46:31 -04:00
|
|
|
std::vector<std::tuple<std::filesystem::path, std::filesystem::file_status>> extractFilesFrom(std::filesystem::path&& path)
|
|
|
|
{
|
2020-12-18 15:38:41 -05:00
|
|
|
std::vector<std::tuple<std::filesystem::path, std::filesystem::file_status>> dir_items;
|
|
|
|
std::transform(std::filesystem::directory_iterator(path), {}, std::back_inserter(dir_items), filepath::getFileInfo);
|
2021-04-24 16:46:31 -04:00
|
|
|
return dir_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isImage(const std::filesystem::path& filepath)
|
|
|
|
{
|
|
|
|
static std::set<std::string> allowed_ext = {".bmp", ".jpg", "*.jpeg", ".png"};
|
|
|
|
const std::string path_string = filepath.string();
|
|
|
|
|
|
|
|
return std::filesystem::is_regular_file(filepath) &&
|
|
|
|
std::any_of(allowed_ext.begin(), allowed_ext.end(),
|
|
|
|
[&](const std::string& e)
|
|
|
|
{
|
|
|
|
return filepath::endsWith(path_string, e);
|
|
|
|
});
|
|
|
|
}
|
2020-12-18 15:38:41 -05:00
|
|
|
|
2021-04-24 16:46:31 -04:00
|
|
|
std::tuple<int, std::vector<std::string>> parseFolder(std::filesystem::path&& path)
|
|
|
|
{
|
|
|
|
auto directory_items = extractFilesFrom(std::move(path));
|
2020-12-18 15:38:41 -05:00
|
|
|
|
2021-04-24 16:46:31 -04:00
|
|
|
std::vector<std::string> image_pathes;
|
|
|
|
for (const auto &[local_path, status] : directory_items)
|
2020-12-18 15:38:41 -05:00
|
|
|
{
|
2021-04-24 16:46:31 -04:00
|
|
|
if (isImage(local_path))
|
|
|
|
image_pathes.emplace_back(local_path.string());
|
2020-12-18 15:38:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 16:46:31 -04:00
|
|
|
if (image_pathes.empty())
|
|
|
|
return {EXIT_FAILURE, {}};
|
|
|
|
|
|
|
|
const std::tuple<int, std::vector<std::string>> empty_folder = {EXIT_FAILURE, {}};
|
|
|
|
const std::tuple<int, std::vector<std::string>> found_images = {EXIT_SUCCESS, image_pathes};
|
|
|
|
|
|
|
|
return image_pathes.empty()
|
|
|
|
? empty_folder
|
|
|
|
: found_images;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<int, std::vector<std::string>> parsePath(const std::string &argv)
|
|
|
|
{
|
|
|
|
std::filesystem::path path(argv);
|
|
|
|
if (!std::filesystem::exists(path))
|
2020-12-18 15:38:41 -05:00
|
|
|
{
|
2021-04-24 16:46:31 -04:00
|
|
|
std::cout << "Path " << path << " does not exist.\n";
|
2020-12-18 15:38:41 -05:00
|
|
|
return {EXIT_FAILURE, {}};
|
|
|
|
}
|
|
|
|
|
2021-04-24 16:46:31 -04:00
|
|
|
if (std::filesystem::is_regular_file(path))
|
|
|
|
{
|
|
|
|
return {EXIT_SUCCESS, {path.string()}};
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseFolder(std::move(path));
|
2020-12-18 15:38:41 -05:00
|
|
|
}
|
2020-12-20 08:49:24 -05:00
|
|
|
|
|
|
|
std::vector<std::string> split(const std::string &s, char delim)
|
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
std::stringstream ss (s);
|
|
|
|
std::string item;
|
|
|
|
|
|
|
|
while (getline(ss, item, delim))
|
|
|
|
result.push_back(item);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2020-12-13 13:45:52 -05:00
|
|
|
}
|