Skip to content

Commit

Permalink
core: fix some bugs in fs_canonical
Browse files Browse the repository at this point in the history
* when given a relative path the function would fail to add correct path
  separation
* MAVSDK claims to support different path separators, but / was
  hardcoded in some cases.
* along with that, the path separator was assumed to be a character in
  some cases, but the datatype is string, so this assumption is incorrect

this commit fixes all of these things
  • Loading branch information
CodeTriangle committed Jun 27, 2022
1 parent e619fbb commit 0fd5908
Showing 1 changed file with 30 additions and 34 deletions.
64 changes: 30 additions & 34 deletions src/mavsdk/core/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,53 +62,49 @@ std::string fs_filename(const std::string& path)

std::string fs_canonical(const std::string& path)
{
std::stack<std::string> st;
std::string dir;
std::string res{};

if (path.rfind(path_separator, 0) != 0) {
char buffer[PATH_MAX];
if (getcwd(buffer, sizeof(buffer)) != nullptr) {
res = std::string(buffer);
}
} else {
res = path_separator;
}
std::deque<std::string> st;
std::string res;

int len = path.length();

for (int i = 0; i < len; i++) {
dir.clear();
while (path[i] == '/') {
i++;
}
while (i < len && path[i] != '/') {
dir.push_back(path[i]);
i++;
int index = 0;
while (index < len) {
int next_index = path.find(path_separator, index);
if (next_index == -1) {
next_index = path.size();
}

std::string dir = path.substr(index, next_index - index);

if (dir.compare("..") == 0 && !st.empty()) {
st.pop();
} else if (dir.compare(".") == 0) {
continue;
} else if (dir.length() != 0) {
st.push(dir);
st.pop_back();
} else if (dir.length() != 0 && dir.compare(".") != 0) {
st.push_back(dir);
}

index = next_index + path_separator.size();
}

std::stack<std::string> st1;
while (!st.empty()) {
st1.push(st.top());
st.pop();
if (path.rfind(path_separator, path_separator.size() - 1) != 0) {
char buffer[PATH_MAX];
if (getcwd(buffer, sizeof(buffer)) != nullptr) {
res.append(buffer);
if (!st.empty()) {
res.append(path_separator);
}
}
} else {
res.append(path_separator);
}

while (!st1.empty()) {
std::string temp = st1.top();
if (st1.size() != 1) {
res.append(temp + "/");
while (!st.empty()) {
std::string temp = st.front();
if (st.size() != 1) {
res.append(temp + path_separator);
} else {
res.append(temp);
}
st1.pop();
st.pop_front();
}

return res;
Expand Down

0 comments on commit 0fd5908

Please sign in to comment.