Handle empty directory and invalid image extension

This commit is contained in:
Vyn 2024-06-04 12:56:46 +02:00
parent fce4fad1fb
commit 90ac4001e7
3 changed files with 104 additions and 73 deletions

View file

@ -2,14 +2,20 @@
#include <algorithm> #include <algorithm>
#include <filesystem> #include <filesystem>
#include <iostream> #include <iostream>
#include <ostream>
#include <stdexcept>
Wallpapers::Wallpapers(const std::string& directoryPath) : directoryPath(directoryPath) { Wallpapers::Wallpapers(const std::string& directoryPath) : directoryPath(directoryPath) {
for (const auto& entry : std::filesystem::directory_iterator(directoryPath)) { for (const auto& entry : std::filesystem::directory_iterator(directoryPath)) {
if (!isValidPath(entry.path())) {
continue;
}
wallpapersPath.push_back(entry.path()); wallpapersPath.push_back(entry.path());
} }
for (const std::string& wallpaperPath : wallpapersPath) { if (wallpapersPath.size() == 0) {
std::cout << wallpaperPath << std::endl; throw std::runtime_error("Directory '" + directoryPath + "' doesn't contain any valid wallapers\n\
Allowed extension are .png, .jpg, .jpeg");
} }
e1 = std::default_random_engine(r()); e1 = std::default_random_engine(r());
uniformDist = std::uniform_int_distribution<int>(0, wallpapersPath.size()); uniformDist = std::uniform_int_distribution<int>(0, wallpapersPath.size());
@ -22,7 +28,21 @@ void Wallpapers::shuffle() {
} }
const std::string& Wallpapers::next() { const std::string& Wallpapers::next() {
if (wallpapersPathQueue.size() == 0) {
throw std::runtime_error("No valid wallpapers found when calling Wallpapers::next method");
}
const std::string& wallpaperPath = wallpapersPathQueue[currentWallpaperIndex]; const std::string& wallpaperPath = wallpapersPathQueue[currentWallpaperIndex];
currentWallpaperIndex = (currentWallpaperIndex + 1) % wallpapersPathQueue.size(); currentWallpaperIndex = (currentWallpaperIndex + 1) % wallpapersPathQueue.size();
return wallpaperPath; return wallpaperPath;
} }
bool Wallpapers::isValidPath(const std::string& path) const {
static std::array validExtensions {"jpg", "jpeg", "png"};
for (const auto& extension : validExtensions) {
if (path.ends_with(extension)) {
return true;
}
}
return false;
}

View file

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <array>
#include <random> #include <random>
#include <string> #include <string>
#include <vector> #include <vector>
@ -14,6 +15,8 @@ class Wallpapers {
private: private:
bool isValidPath(const std::string& path) const;
std::string directoryPath; std::string directoryPath;
std::vector<std::string> wallpapersPath; std::vector<std::string> wallpapersPath;
std::vector<std::string> wallpapersPathQueue; std::vector<std::string> wallpapersPathQueue;

View file

@ -1,12 +1,16 @@
#include "CliArguments.h" #include "CliArguments.h"
#include "Wallpapers.h" #include "Wallpapers.h"
#include "SwaybgProcess.h" #include "SwaybgProcess.h"
#include <exception>
#include <iostream> #include <iostream>
#include <ostream> #include <ostream>
#include <string> #include <string>
int main(int argc, char** argv, char** envp) { int main(int argc, char** argv, char** envp) {
try {
CliArguments::CliArguments arguments{ CliArguments::CliArguments arguments{
argc, argv, argc, argv,
{ {
@ -81,4 +85,8 @@ int main(int argc, char** argv, char** envp) {
swayBgProcess.kill(); swayBgProcess.kill();
return 0; return 0;
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
} }