-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_simplify_unix_like_pathnames.cpp
99 lines (80 loc) · 2.02 KB
/
stack_simplify_unix_like_pathnames.cpp
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
/* C++ program to simplify a Unix
styled absolute path of a file */
#include <bits/stdc++.h>
using namespace std;
// function to simplify a Unix - styled
// absolute path
string simplify(string A)
{
// stack to store the file's names.
stack<string> st;
// temporary string which stores the extracted
// directory name or commands("." / "..")
// Eg. "/a/b/../."
// dir will contain "a", "b", "..", ".";
string dir;
// contains resultant simplifies string.
string res;
// every string starts from root directory.
res.append("/");
// stores length of input string.
int len_A = A.length();
for (int i = 0; i < len_A; i++) {
// we will clear the temporary string
// every time to accomodate new directory
// name or command.
dir.clear();
// skip all the multiple '/' Eg. "/////""
while (A[i] == '/')
i++;
// stores directory's name("a", "b" etc.)
// or commands("."/"..") into dir
while (i < len_A && A[i] != '/') {
dir.push_back(A[i]);
i++;
}
// if dir has ".." just pop the topmost
// element if the stack is not empty
// otherwise ignore.
if (dir.compare("..") == 0) {
if (!st.empty())
st.pop();
}
// if dir has "." then simply continue
// with the process.
else if (dir.compare(".") == 0)
continue;
// pushes if it encounters directory's
// name("a", "b").
else if (dir.length() != 0)
st.push(dir);
}
// a temporary stack (st1) which will contain
// the reverse of original stack(st).
stack<string> st1;
while (!st.empty()) {
st1.push(st.top());
st.pop();
}
// the st1 will contain the actual res.
while (!st1.empty()) {
string temp = st1.top();
// if it's the last element no need
// to append "/"
if (st1.size() != 1)
res.append(temp + "/");
else
res.append(temp);
st1.pop();
}
return res;
}
// Driver code.
int main()
{
// absolute path which we have to simplify.
string str = "/a/./b/../../c/";
string res = simplify(str);
cout << res;
return 0;
}