-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·187 lines (176 loc) · 6.19 KB
/
main.cpp
File metadata and controls
executable file
·187 lines (176 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <filesystem>
#include <string>
#include <vector>
#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 pictureType[] = {".jpg","jpeg","png"};
namespace fs = std::filesystem;
std::string home = getenv("HOME");
std::string settingsPath = home+"/.config/fsorter/settings.toml";
std::string sortingPath = fs::current_path();
//createn of the typeAndPath object
class typeAndPaths
{
public:
std::string type;
std::string path;
std::vector<std::string> extensions;
};
//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()
{
std::ofstream settings(settingsPath);
settings << "[Picture]" << std::endl;
settings << "path = " << "\"" << home << "/Pictures/\"" << std::endl;
settings << "extensions = [\".jpg\",\".jpeg\",\".png\"]" << 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();
std::cout << "settings.toml made in " << settingsPath << std::endl;
exit(0);
}
//Reads all the lines in settings.toml and saves them in a vector line by line.
std::vector<typeAndPaths> readSettings(std::vector<std::string> ignoreList)
{
std::vector<typeAndPaths> paths;
typeAndPaths Paths;
if(!fs::exists(settingsPath))
{
fs::create_directory(home + "/.config/fsorter/");
}
std::ifstream settingsFile(settingsPath);
std::vector<std::string> listOfPaths;
std::string setting;
// creates a default settins file
if(!settingsFile)
{
writeSettins();
}
if(settingsFile)
{
settingsFile.close();
toml::table tbl;
try
{
tbl = toml::parse_file(settingsPath);
}
catch (const toml::parse_error& err)
{
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)
{
//std::cout << ignoreList.size() << std::endl;
bool allow = true;
for (int e = 0; e < ignoreList.size(); e++)
{
if (ignoreList[e] == std::string(it->first))
{
allow = false;
break;
}
}
if (allow == true)
{
Paths.type = it->first;
Paths.path = tbl[it->first]["path"].ref<std::string>();
std::vector<std::string> extensions;
for(int i = 0 ; i < tbl[it->first]["extensions"].as_array()->size(); i++)
{
std::string ext = tbl[it->first]["extensions"][i].ref<std::string>();
extensions.push_back(ext);
}
Paths.extensions = extensions;
paths.push_back(Paths);
}
}
}
settingsFile.close();
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
//if the user says yes the directory will be created.
void checkSettingsPaths(std::vector<typeAndPaths> Paths)
{
for (int i = 0; i < Paths.size(); i++)
{
if(!fs::exists(Paths[i].path))
{
bool valid = false;
while (!valid) {
char answer;
std::cout << "Would you like to create (y/n) : " << Paths[i].path << "? \t";
std::cin >> answer;
std::cout << '\n';
if(tolower(answer) == 'y')
{
fs::create_directory(Paths[i].path);
std::cout << "Directory " << Paths[i].path << " was created.\n";
valid = true;
}
else if (tolower(answer) == 'n')
{
std::cout << "Directory" << Paths[i].path <<" was not created!\n";
valid = true;
}
else
{
std::cout << "Please provide a valid answer!" << std::endl;
}
}
}
}
}
//iterates thrue all the files in the given path looks at there respected extention and then moves them to the location found in the Paths.path (typeAndPath) object
//that is related to the extention that is being sorted..
void sortPath(std::string path, std::vector<typeAndPaths> Paths)
{
for(auto const& file: fs::directory_iterator{path})
{
if(file.path().has_extension())
{
for (int i = 0; i < Paths.size(); i++)
{
for (int x = 0; x < Paths[i].extensions.size(); x++)
{
if(Paths[i].extensions[x] == file.path().extension())
{
std::cout << file.path().string() << std::endl;
fs::rename(file.path().string(), Paths[i].path + file.path().filename().string());
}
}
}
}
}
}
int main(int argc, char *argv[])
{
int i = 0;
std::vector<std::string> ignoreList;
for (i = 0; i < argc; i++)
{
if (std::string(argv[i]) == "-i")
{
ignoreList.push_back(argv[i+1]);
}
}
std::vector<typeAndPaths> TypesAndPaths = readSettings(ignoreList);
checkSettingsPaths(TypesAndPaths);
sortPath(sortingPath, TypesAndPaths);
return 0;
}