2016-05-02 21:15:43 -07:00
|
|
|
// Directory utilities. This library contains functions for locating configuration directories, for
|
|
|
|
|
// testing if a command with a given name can be found in the PATH, and various other path-related
|
|
|
|
|
// issues.
|
2016-05-18 22:30:21 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
|
2019-10-13 15:50:48 -07:00
|
|
|
#include "path.h"
|
|
|
|
|
|
2019-05-05 12:09:25 +02:00
|
|
|
#include <cstring>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <string>
|
2022-08-20 23:14:48 -07:00
|
|
|
#include <utility>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <vector>
|
2006-10-19 21:50:23 +10:00
|
|
|
|
|
|
|
|
#include "common.h"
|
2016-05-02 21:15:43 -07:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2020-07-29 16:37:23 -07:00
|
|
|
#include "wcstringutil.h"
|
2016-05-02 21:15:43 -07:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2006-10-19 21:50:23 +10:00
|
|
|
|
2020-01-15 13:16:43 -08:00
|
|
|
void append_path_component(wcstring &path, const wcstring &component) {
|
|
|
|
|
if (path.empty() || component.empty()) {
|
|
|
|
|
path.append(component);
|
|
|
|
|
} else {
|
|
|
|
|
size_t path_len = path.size();
|
|
|
|
|
bool path_slash = path.at(path_len - 1) == L'/';
|
|
|
|
|
bool comp_slash = component.at(0) == L'/';
|
|
|
|
|
if (!path_slash && !comp_slash) {
|
|
|
|
|
// Need a slash
|
|
|
|
|
path.push_back(L'/');
|
|
|
|
|
} else if (path_slash && comp_slash) {
|
|
|
|
|
// Too many slashes.
|
|
|
|
|
path.erase(path_len - 1, 1);
|
|
|
|
|
}
|
|
|
|
|
path.append(component);
|
|
|
|
|
}
|
|
|
|
|
}
|