-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinuxVer.h
More file actions
179 lines (148 loc) · 5.54 KB
/
Copy pathLinuxVer.h
File metadata and controls
179 lines (148 loc) · 5.54 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
#pragma once
#ifndef LINUXVER_H
#define LINUXVER_H
#ifdef __unix
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <dirent.h>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <typeinfo>
#include <algorithm>
bool createFolder(std::string outputFolder){
//std::cout << "outputFolder: " << outputFolder << std::endl;
DIR* tempDir = opendir(outputFolder.c_str());
if(tempDir){
closedir (tempDir);
return true;
}
else{
const int dir_err = mkdir(outputFolder.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (-1 == dir_err)
{
printf("Error creating directory!!!N\n");
return false;
}
return true;
}
}
void defaultFolderSetup(std::string path)
{
std::string pdfStr = path + "/pdfFolder";
std::string picStr = path + "/picFolder";
std::string vidStr = path + "/vidFolder";
std::string zipStr = path + "/zipFolder";
std::string textStr = path + "/textFolder";
std::cout << pdfStr << std::endl;
try {
createFolder(pdfStr);
createFolder(picStr);
createFolder(vidStr);
createFolder(zipStr);
createFolder(textStr);
}
catch (...) {
std::cerr << "Something broke in Pre-Set Folder Creation Function (checkPathExists)" << std::endl;
}
}
void addToFolder(std::string fileNVar, std::string pathDirVar, std::string srcpath , std::string folderName){
std::string destpath = pathDirVar + folderName + "/" + fileNVar;
//std::cout << " \n\n destPath :: " << destpath << std::endl;
//std::cout << " \n\n srcpath :: " << srcpath << std::endl;
if (rename (srcpath.c_str(), destpath.c_str())) {
// something went wrong
if (errno == EXDEV) {
// copy data and meta data
}
else { perror("rename"); exit(EXIT_FAILURE); };
}
else { // the rename succeeded
}
}
void lastResort(std::string fileName, const std::string srcpath, const std::string pathDir) {
size_t found = fileName.find_last_of(".");
std::string fileType = fileName.substr(found + 1);
std::string folderName = "/" + fileType + "Folder";
std::string strLoc = pathDir + folderName + "/" + fileName;
std::string strFold = pathDir + folderName;
//std::cout << strLoc << " and " << strFold << std::endl;
if(createFolder(strFold)){
addToFolder(fileName, pathDir, srcpath, folderName);
}
}
void checkTypeFunc(std::string fileName, const std::string pathDir, const std::string fullPath){
std::cout << " Filename: " << fileName << "\n pathDir: " << pathDir << "\n fullPath: " << fullPath << std::endl;
transform(fileName.begin(), fileName.end(), fileName.begin(), ::tolower);
if (fileName.find("pdf") != std::string::npos && fileName != "pdffolder") {
std::string folderName = "/pdfFolder/";
addToFolder(fileName, pathDir, fullPath, folderName);
}
else if (fileName.find("png") != std::string::npos || fileName.find("jpg") != std::string::npos || fileName.find("tiff") != std::string::npos || fileName.find("jpeg") != std::string::npos) {
std::string folderName = "/picFolder/";
addToFolder(fileName, pathDir, fullPath, folderName);
}
else if (fileName.find("mp4") != std::string::npos || fileName.find("mov") != std::string::npos || fileName.find("avi") != std::string::npos || fileName.find(".ts") != std::string::npos) {
std::string folderName = "/vidFolder/";
addToFolder(fileName, pathDir, fullPath, folderName);
}
else if ((fileName.find("zip") != std::string::npos || fileName.find("rar") != std::string::npos) && fileName != "zipfolder") {
std::string folderName = "/zipFolder/";
addToFolder(fileName, pathDir, fullPath, folderName);
}
else if ((fileName.find(".txt") != std::string::npos || fileName.find(".doc") != std::string::npos || fileName.find(".readme") != std::string::npos
|| fileName.find(".text") != std::string::npos) && fileName != "textfolder") {
std::string folderName = "/textFolder/";
addToFolder(fileName, pathDir, fullPath, folderName);
}
else {
lastResort(fileName, fullPath, pathDir);
//if (!strstr(fileName.c_str(), "folder")) {cout << fileName << endl;}
}
}
/*void checkType(DIR * testVar){
std::cout << typeid(testVar).name() << '\n';
}*/
int mainLinuxFunc(std::string PLAT_FORM, vector<string> &allLocs)
{
/*std::cout << "Works (Linux)" << std::endl;
struct passwd *pw = getpwuid(getuid());
std::string homedir = pw->pw_dir;
homedir = homedir + "/Downloads";
std::cout << homedir << std::endl;*/
DIR *dir;
//checkType(dir);
for (auto homedir : allLocs) {
defaultFolderSetup(homedir);
struct dirent *entity;
if ((dir = opendir(homedir.c_str())) != NULL) {
/* print all the files and directories within directory */
while ((entity = readdir(dir)) != NULL) {
//std::cout << entity->d_name << std::endl;
DIR* tempDir = opendir((homedir + "/" + entity->d_name).c_str());
if (tempDir) {
//std::cout << "Folder: " << entity->d_name << std::endl;
closedir(tempDir);
}
else {
//std::cout << entity->d_name << std::endl;
std::string fullPathVar = homedir + "/" + entity->d_name;
checkTypeFunc(entity->d_name, homedir, fullPathVar);
}
}
closedir(dir);
}
else {
/* could not open directory */
perror("");
return EXIT_FAILURE;
}
}
return 0;
}
#endif
#endif