-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangeDirectory.cpp
More file actions
160 lines (117 loc) · 2.45 KB
/
changeDirectory.cpp
File metadata and controls
160 lines (117 loc) · 2.45 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
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<iterator>
#include<cstddef>
#include<regex>
using namespace std;
vector<string>split(string str)
{
vector<string>tokens;
string token;
for_each(str.begin(),str.end(),[&](char c) {
if(c != '/')
token += c;
else
{
if(token.length())
tokens.push_back(token);
token.clear();
}
});
if(token.length())
tokens.push_back(token);
return tokens;
}
string changeDir(string currentDir,string targetDir)
{
vector<string>dest = split(targetDir);
string s = "/";
string st(1,targetDir[0]);
string target;
regex re("[a-z]*+(\\.\\.\\.)+[a-z]*");
smatch match1;
if(regex_search(targetDir,match1,re) && match1.size() > 1)
{
cout <<"invalid Path"<<endl;
return NULL;
}
if(regex_search(currentDir,match1,re) && match1.size() > 1)
{
cout <<"invalid current Path"<<endl;
return NULL;
}
regex re1("[a-z]+(\\.\\.)+[a-z]*");
smatch match2;
if(regex_search(targetDir,match2,re1) && match2.size() > 1)
{
cout <<"invalid Path"<<endl;
return NULL;
}
if(regex_search(currentDir,match2,re1) && match2.size() > 1)
{
cout <<"invalid current Path"<<endl;
return NULL;
}
regex re2("(\\.\\/)+");
smatch match3;
if(regex_search(targetDir,match3,re2) && match3.size() > 1)
{
return currentDir;
}
//case like .
if((targetDir.size() == 1) && (targetDir=="."))
return currentDir;
size_t in = targetDir.find("..");
//check for cases like ..klm
if(in!= string::npos){
string nextchar = targetDir.substr(in+2,1);
if((!nextchar.empty()) && (nextchar.compare("/")))
{
cout<<"Error in operations "<<endl;
return NULL;
}
}
//Go through destination
for(auto str:dest)
{
//check if found root directory as first in target
if(!st.compare("/"))
{
target = target + "/" +str;
}
else
if(!str.compare(".."))
{
size_t index = currentDir.find_last_of("/\\");
currentDir = currentDir.substr(0,index);
//in case we reached to root just display root e.g cases like ../../../..
if(index == 0)
currentDir = "/";
}
else
{
currentDir = currentDir + "/" + str;
}
}
if(!st.compare("/"))
return target;
else
return currentDir;
}
int main(int argc,char *argv[])
{
string currentDir,targetDir;
if(argc < 3)
{
cout << argv[0]<<"<current Directory>" << "<new path>"<<endl;
return 0;
}
currentDir=argv[1];
targetDir=argv[2];
cout << "current Directory: "<< currentDir<<endl;
cout << "target Directory: " << targetDir<<endl;
cout << "after operations: " <<endl;
cout <<changeDir(currentDir,targetDir);
}