标签:
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
"/../"
?"/"
.‘/‘
together, such as "/home//foo/"
."/home/foo"
.1 string simplifyPath(string path) 2 { 3 vector<string> dirs; 4 int start = 0, end = 0; 5 6 while (end < path.size()) 7 { 8 while (start < path.size() && path[start] == ‘/‘) 9 start++; 10 end = start; 11 while (end < path.size() && path[end] != ‘/‘) 12 end++; 13 string str = path.substr(start, end - start); 14 if (str == "..") 15 { 16 if (!dirs.empty()) 17 dirs.pop_back(); 18 } 19 else if (str != "." && str != "") 20 { 21 dirs.push_back(str); 22 } 23 start = end; 24 } 25 26 path = ""; 27 for (int i = 0; i < dirs.size(); i++) 28 path += "/" + dirs[i]; 29 30 return (path == "") ? "/" : path; 31 }
标签:
原文地址:http://www.cnblogs.com/ym65536/p/4304513.html