-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.cpp
More file actions
executable file
·159 lines (140 loc) · 3.33 KB
/
utils.cpp
File metadata and controls
executable file
·159 lines (140 loc) · 3.33 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
#include "database.h"
#include "utils.h"
#ifdef APPLE
int clock_gettime(int i, struct timespec* b) {
return 0;
}
char* strdup(const char* str) {
char* newstr = (char*)malloc(strlen(str)+1);
strcpy(newstr, str);
return newstr;
}
#endif
int get_file_size(string file_name){
string tempbase = file_name;
string base=basename(strdup(tempbase.c_str()));
string path=append_path2(base);
struct stat st;
stat(path.c_str(), &st);
return st.st_size;
}
vector<string> split(string istr, string delim) {
int start=0, end;
vector<string> vec;
char *saveptr;
char *token;
char *str = strdup(istr.c_str());
const char *delimiters = delim.c_str();
for(token = strtok_r(str, delimiters, &saveptr);
token != NULL;
token = strtok_r(NULL, delimiters, &saveptr)) {
vec.push_back(string(token));
}
return vec;
}
string join(vector<string> these, string delim) {
string ret = "";
for(int i=0; i<these.size(); i++) {
if(i>0) {
ret+=delim;
}
ret+=these[i];
}
return ret;
}
string subtract(string files1, string files2){
string ret="";
string tok1="";
string tok2="";
stringstream f1(files1);
while(getline(f1,tok1,':')){
stringstream f2(files2);
bool found = false;
while(getline(f2,tok2,':')){
if(strcmp(tok1.c_str(),tok2.c_str())==0){
found = true;
}
}
if(!found) {
ret+=":"+tok1;
}
}
return ret;
}
string intersect(string files1, string files2){
string ret="";
string tok1="";
string tok2="";
stringstream f1(files1);
while(getline(f1,tok1,':')){
stringstream f2(files2);
while(getline(f2,tok2,':')){
if(strcmp(tok1.c_str(),tok2.c_str())==0){
ret+=":"+tok1;
}
}
}
return ret;
}
string trim_right(string source, string t = " \n")
{
string str = source;
return str.erase( str.find_last_not_of(t) + 1);
}
string trim_left( string source, string t = " \n")
{
std::string str = source;
return str.erase(0 , source.find_first_not_of(t) );
}
string trim(string source, string t)
{
string str = source;
return trim_left( trim_right( str , t) , t );
}
int count_string(string tobesplit){
int count=0;
if(strcmp(tobesplit.c_str(),"null")==0){
return 0;
} else {
stringstream ss(tobesplit.c_str());
string token;
while(getline(ss, token, ':')){
if(token.length()>0) {
count++;
}
}
return count;
}
}
char* append_path(const char * newp) {
char msg[100];
sprintf(msg,"in append_path with %s and %s",servers[0].c_str(),newp);
log_msg(msg);
fpath=(char*)malloc(MAX_PATH_LENGTH);
memset(fpath,0,MAX_PATH_LENGTH);
sprintf(&fpath[0],"%s%s",servers[0].c_str(),newp);
//log_msg("returning");
return fpath;
}
char* append_path2(string newp) {
//get file from database
//cout<<"in append_path2"<<endl;
string fid=database_getval("name",newp);
//cout<<"got fid:"<<fid<<endl;
//get server name
string server=database_getval(fid,"server");
//cout<<"got server:"<<server<<endl;
vector<string> places = split(server, ":");
int i=0;
server = "";
for(i=0; i<places.size(); i++) {
//prefer local to cloud
if(places[i]=="cloud" && server=="") {
server = "/tmp";
} else {
server = places[i];
}
}
//append and return c_str
return strdup((server+"/"+newp).c_str());
}