-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
114 lines (92 loc) · 3.16 KB
/
functions.cpp
File metadata and controls
114 lines (92 loc) · 3.16 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
#include "functions.h"
#include "osinterface.h"
#include <string>
/* upravi retezec ve vystupnim parametru aby koncil jednim oddelovacem
* a neobsahoval zdvojene oddelovace
*/
void repairPath(std::string &path){
unsigned int pos;
std::string doubled;
doubled.push_back(OSInterface::dir_sep);
doubled.push_back(OSInterface::dir_sep);
while((pos = path.find(doubled)) != std::string::npos){
path = path.substr(0, pos) + path.substr(pos + 1, path.size());
}
if(path[path.size() - 1] != OSInterface::dir_sep)
path.push_back(OSInterface::dir_sep);
}
/* porovna, jestli retezec expr odpovida vzoru */
bool matchExpression(std::string &expr, std::string &pattern){
int pos = pattern.find('*');
for(int i = 0; i < pos; ++i){
if(expr[i] != pattern[i])
return false;
}
for(unsigned int i = 0; i < pattern.size() - pos - 1; ++i){
if(expr[expr.size() - 1 - i] != pattern[pattern.size() - 1 - i])
return false;
}
return true;
}
/* vrati jmeno souboru ziskane z cesty */
std::string getBasename(std::string &path){
int pos;
if(path.empty()) return "";
if((pos = path.find_last_of(OSInterface::dir_sep)) == -1) return "";
return path.substr(pos + 1, path.size());
}
/* vrati cestu k souboru, bez samotneho jmena */
std::string getPath(std::string &path){
int pos;
if(path.empty()) return "";
if((pos = path.find_last_of(OSInterface::dir_sep)) == -1) return "";
return path.substr(0, pos);
}
/* vrati koncovku souboru */
std::string getExtension(std::string &path){
if(path.empty()) return "";
std::string fn = getBasename(path);
int pos;
if((pos = path.find_last_of('.')) == -1) return "";
return path.substr(pos + 1, path.size());
}
/* rozhodne, zdali se jedna o archiv */
bool isArch(std::string &path){
std::string ext(getExtension(path));
return ((ext == "gz") || (ext == "bz2") || (ext == "zip") || (ext == "rar"));
}
/* jedna se o obrazek */
bool isImg(std::string &path){
std::string ext(getExtension(path));
return ((ext == "png") || (ext == "jpg") || (ext == "gif"));
}
void addSizeInfo(std::stringstream &ss){
std::shared_ptr<Data> data_instance = Data::getInstance();
if(data_instance->size_in == data_instance->B)
ss << " B";
if(data_instance->size_in == data_instance->KB)
ss << " KB";
if(data_instance->size_in == data_instance->MB)
ss << " MB";
if(data_instance->size_in == data_instance->GB)
ss << " GB";
}
/* soubor ma typ, ktery umi otevrit v externim programu */
bool isKnown(std::string &path){
std::string ext(getExtension(path));
std::shared_ptr<Data> data_instance = Data::getInstance();
auto it = data_instance->extern_programmes.find(ext);
if(it != data_instance->extern_programmes.end())
return true;
return false;
}
/* pokud jmeno souboru obsahuje nepovolene znaky, vrati prvni z techto znaku */
char isValidFn(std::string &path){
std::string forbidden(" <>|\\:()&;#?*");
forbidden.push_back(OSInterface::dir_sep);
for(char &a : forbidden){
if(path.find(a) != std::string::npos)
return a;
}
return 0;
}