-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__utils__.h
144 lines (122 loc) Β· 3.49 KB
/
__utils__.h
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
#include <cstring>
#include <string>
#include <algorithm>
#include <filesystem> // for ostringstream
#include <stdlib.h>
using namespace std;
// defines
#define umap_s_s unordered_map<string,string>
#define ALL_PERM 0777
#define MY_READ_PERM 0
#define OTHER_READ_PERM 6
#define MY_WRITE_PERM 1
#define OTHER_WRITE_PERM 7
#define GRP_READ_PERM 3
string path_ka_jugaad_kar(string curr_dir, string gaepathig){
char* nongaepath = new char[PATH_MAX];
if(gaepathig[0] == '/'){
realpath(gaepathig.c_str(), nongaepath);
string nongaepath_s = string(nongaepath);
free(nongaepath);
return nongaepath_s;
}
else if (gaepathig[0] == '~'){
string home_path = string(getenv("HOME"));
if(gaepathig == "~"){
return home_path;
}
else{
string home_appended_path = home_path + "/" + gaepathig.substr(1);
realpath(home_appended_path.c_str(), nongaepath);
string nongaepath_s = string(nongaepath);
free(nongaepath);
return nongaepath_s;
}
}
string lilgaepath = curr_dir + "/" + gaepathig;
realpath(lilgaepath.c_str(), nongaepath);
string nongaepath_s = string(nongaepath);
delete[] nongaepath;
return string(nongaepath_s);
}
string trim_newline(string str){
str.erase(std::remove(str.begin(), str.end(), '\n'), str.cend());
str.erase(std::remove(str.begin(), str.end(), '\r'), str.cend());
return str;
}
bool if_permission_granted(umap_s_s& content){
if(content["perm"][OTHER_READ_PERM]=='r')
return true;
else if(getenv("USERNAME")==content["uown"] && content["perm"][MY_READ_PERM]=='r')
return true;
else
return false;
}
void nuke_stack_contents(stack<string>& stk){
while(!stk.empty())
stk.pop();
}
int _min(int a, int b){
return (a<b)?a:b;
}
int _max(int a, int b){
return (a>b)?a:b;
}
string get_prev_directory_from_path(string path){
int slash_index;
int path_ind = 0;
int path_len = path.length();
if (count(path.begin(), path.end(), '/') <= 1) return "/";
else{
reverse(path.begin(), path.end());
for(int path_ind=0; path_ind<path_len; path_ind++){
if( path[path_ind] == '/'){
slash_index = path_ind;
break;
}
}
}
string parent_dir = path.substr(slash_index+1);
reverse(parent_dir.begin(), parent_dir.end());
return parent_dir;
}
string get_filename_from_path(string path){
int file_dir_length = get_prev_directory_from_path(path).length();
return path.substr(file_dir_length);
}
string get_contentname_from_path(string path){
int file_dir_length = get_prev_directory_from_path(path).length();
return path.substr(file_dir_length);
}
string get_content_icon(string content_type){
if(content_type == "file")
return "π";
else if (content_type == "dir")
return "π";
else if (content_type == "slink")
return "π";
else
return "π©";
}
bool cmp_content_name(umap_s_s c1, umap_s_s c2){
return c1["name"] < c2["name"];
}
void cls(){
cout << "\033[2J\033[1;1H";
}
char* s_to_carr(const string& s) {
char* carr = new char[s.length()+1];
strcpy(carr, s.c_str());
return carr;
}
string convert_to_string(double num) {
ostringstream convert;
convert << num;
return convert.str();
}
double round_off(double n) {
double d = n * 10.0;
int i = d + 0.5;
d = (float)i / 10.0;
return d;
}