mirror of
https://github.com/mrfluffy-dev/fsorter.git
synced 2026-01-17 02:50:33 +00:00
added toml support
This commit is contained in:
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.17)
|
|||||||
project(fsorter)
|
project(fsorter)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
include(FetchContent)
|
||||||
|
|
||||||
add_executable(fsorter main.cpp)
|
add_executable(fsorter main.cpp)
|
||||||
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
|
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
|
||||||
|
|||||||
12504
libs/toml.hpp
Normal file
12504
libs/toml.hpp
Normal file
File diff suppressed because it is too large
Load Diff
98
main.cpp
98
main.cpp
@@ -3,13 +3,18 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
// making use of https://marzer.github.io/tomlplusplus/ to handle toml files
|
||||||
|
#include "libs/toml.hpp"
|
||||||
|
using namespace std::string_view_literals;
|
||||||
|
|
||||||
std::string musicTypes[] = {".mp3",".wav"};
|
std::string musicTypes[] = {".mp3",".wav"};
|
||||||
std::string pictureType[] = {".jpg","jpeg","png"};
|
std::string pictureType[] = {".jpg","jpeg","png"};
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
std::string home = getenv("HOME");
|
std::string home = getenv("HOME");
|
||||||
std::string settingsPath = home+"/.config/fsorter/settings.conf";
|
std::string settingsPath = home+"/.config/fsorter/settings.toml";
|
||||||
std::string sortingPath = fs::current_path();
|
std::string sortingPath = fs::current_path();
|
||||||
|
|
||||||
|
|
||||||
//createn of the typeAndPath object
|
//createn of the typeAndPath object
|
||||||
class typeAndPaths{
|
class typeAndPaths{
|
||||||
public:
|
public:
|
||||||
@@ -18,20 +23,30 @@ class typeAndPaths{
|
|||||||
std::vector<std::string> extensions;
|
std::vector<std::string> extensions;
|
||||||
};
|
};
|
||||||
|
|
||||||
//this is just a basic function that will create a basic settings.conf file if it does not exist (it is called by readSettings in the case settings.conf does not exist)
|
//this is just a basic function that will create a basic settings.toml file if it does not exist (it is called by readSettings in the case settings.conf does not exist)
|
||||||
void writeSettins()
|
void writeSettins()
|
||||||
{
|
{
|
||||||
std::ofstream settings(settingsPath);
|
std::ofstream settings(settingsPath);
|
||||||
settings << "Picture=\n"+ home +"/Pictures/\n.jpg,.jpeg,.png," << std::endl;
|
settings << "[Picture]" << std::endl;
|
||||||
settings << "Music=\n"+home+"/Music/\n.mp3,.wav," << std::endl;
|
settings << "path = " << "\"" << home << "/Pictures/\"" << std::endl;
|
||||||
settings << "Video=\n"+home+"/Videos/\n.mp4," << std::endl;
|
settings << "extensions = [\".jpg\",\".jpeg\",\".png\"]" << std::endl;
|
||||||
settings << "Arcive=\n"+home+"/Documents/Compressed/\n.zip,.rar,.7z," << std::endl;
|
settings << "[Music]" << std::endl;
|
||||||
|
settings << "path = " << "\"" << home << "/Music/\"" << std::endl;
|
||||||
|
settings << "extensions = [\".mp3\",\".wav\"]" << std::endl;
|
||||||
|
settings << "[Video]" << std::endl;
|
||||||
|
settings << "path = " << "\"" << home << "/Videos/\"" << std::endl;
|
||||||
|
settings << "extensions = [\".mp4\"]" << std::endl;
|
||||||
|
settings << "[Arcive]" << std::endl;
|
||||||
|
settings << "path = " << "\"" << home << "/Documents/Compressed/\"" << std::endl;
|
||||||
|
settings << "extensions = [\".zip\",\".rar\",\".7z\"]";
|
||||||
settings.close();
|
settings.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Reads all the lines in settings.conf and saves them in a vector line by line.
|
//Reads all the lines in settings.toml and saves them in a vector line by line.
|
||||||
std::vector<std::string> readSettings()
|
std::vector<typeAndPaths> readSettings()
|
||||||
{
|
{
|
||||||
|
std::vector<typeAndPaths> paths;
|
||||||
|
typeAndPaths Paths;
|
||||||
if(!fs::exists(settingsPath))
|
if(!fs::exists(settingsPath))
|
||||||
{
|
{
|
||||||
fs::create_directory(home + "/.config/fsorter/");
|
fs::create_directory(home + "/.config/fsorter/");
|
||||||
@@ -46,52 +61,40 @@ std::vector<std::string> readSettings()
|
|||||||
}
|
}
|
||||||
if(settingsFile)
|
if(settingsFile)
|
||||||
{
|
{
|
||||||
while(getline(settingsFile, setting))
|
|
||||||
{
|
|
||||||
listOfPaths.push_back(setting);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
settingsFile.close();
|
settingsFile.close();
|
||||||
return listOfPaths;
|
toml::table tbl;
|
||||||
}
|
try
|
||||||
|
|
||||||
//declarePaths will take a vector of all the text written in settings.conf and devide them in to there respected atrebutes in Object typeAndPath
|
|
||||||
std::vector<typeAndPaths> declarePaths(std::vector<std::string> listOfPaths)
|
|
||||||
{
|
|
||||||
typeAndPaths Paths;
|
|
||||||
std::vector<typeAndPaths> paths;
|
|
||||||
for (int i = 0; i < listOfPaths.size(); i+=3)
|
|
||||||
{
|
{
|
||||||
listOfPaths[i].pop_back();
|
tbl = toml::parse_file(settingsPath);
|
||||||
Paths.type = listOfPaths[i];
|
}
|
||||||
Paths.path = listOfPaths[i+1];
|
catch (const toml::parse_error& err)
|
||||||
std::string stringextensions = listOfPaths[i+2];
|
{
|
||||||
|
std::cerr
|
||||||
|
<< "Error parsing file '" << *err.source().path
|
||||||
|
<< "':\n" << err.description()
|
||||||
|
<< "\n (" << err.source().begin << ")\n";
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
for(auto it = tbl.begin(); it != tbl.end(); ++it)
|
||||||
|
{
|
||||||
|
Paths.type = it->first;
|
||||||
|
Paths.path = tbl[it->first]["path"].ref<std::string>();
|
||||||
std::vector<std::string> extensions;
|
std::vector<std::string> extensions;
|
||||||
std::string tempWord;
|
for(int i = 0 ; i < tbl[it->first]["extensions"].as_array()->size(); i++)
|
||||||
int counter = 0;
|
|
||||||
i = 0;
|
|
||||||
while (i < stringextensions.size()) {
|
|
||||||
char temp = stringextensions[i];
|
|
||||||
if(temp != ',')
|
|
||||||
{
|
{
|
||||||
|
std::string ext = tbl[it->first]["extensions"][i].ref<std::string>();
|
||||||
tempWord.push_back(temp);
|
extensions.push_back(ext);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
extensions.push_back(tempWord);
|
|
||||||
tempWord = "";
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
Paths.extensions = extensions;
|
Paths.extensions = extensions;
|
||||||
paths.push_back(Paths);
|
paths.push_back(Paths);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
settingsFile.close();
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//this will check if the Folder that is mentiond in the object Paths.path (typeAndPath) exists and if not will prompt the user if they want to create the directory or not
|
//this will check if the Folder that is mentiond in the object Paths.path (typeAndPath) exists and if not will prompt the user if they want to create the directory or not
|
||||||
//if the user says yes the directory will be created.
|
//if the user says yes the directory will be created.
|
||||||
void checkSettingsPaths(std::vector<typeAndPaths> Paths)
|
void checkSettingsPaths(std::vector<typeAndPaths> Paths)
|
||||||
@@ -150,11 +153,8 @@ void sortPath(std::string path, std::vector<typeAndPaths> Paths)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::vector<std::string> listOfPaths = readSettings();
|
std::vector<typeAndPaths> TypesAndPaths = readSettings();
|
||||||
std::vector<typeAndPaths> Paths = declarePaths(listOfPaths);
|
checkSettingsPaths(TypesAndPaths);
|
||||||
checkSettingsPaths(Paths);
|
sortPath(sortingPath, TypesAndPaths);
|
||||||
sortPath(sortingPath, Paths);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user