This source file includes following definitions.
- IsAbsolute
- Part
- Size
- Top
- Append
- Prepend
- Set
- Parent
- Basename
- Join
- Range
- Split
- Normalize
- Join
- Range
- Split
#include "nacl_io/path.h"
#include <stdio.h>
#include <string.h>
#include <string>
#include "sdk_util/string_util.h"
namespace nacl_io {
Path::Path() {}
Path::Path(const Path& path) {
paths_ = path.paths_;
}
Path::Path(const std::string& path) {
Set(path);
}
Path::~Path() {}
bool Path::IsAbsolute() const {
return !paths_.empty() && paths_[0] == "/";
}
const std::string& Path::Part(size_t index) const {
return paths_[index];
}
size_t Path::Size() const {
return paths_.size();
}
bool Path::Top() const {
return (paths_.size() == 0) || (paths_.size() == 1 && paths_[0] == "/");
}
Path& Path::Append(const std::string& path) {
StringArray_t paths = Split(path);
for (size_t index = 0; index < paths.size(); index++) {
if (paths_.size() && index == 0 && paths[0] == "/") continue;
paths_.push_back(paths[index]);
}
paths_ = Normalize(paths_);
return *this;
}
Path& Path::Prepend(const std::string& path) {
StringArray_t paths = Split(path);
for (size_t index = 0; index < paths_.size(); index++) {
if (index == 0 && paths_[0] == "/") continue;
paths.push_back(paths[index]);
}
paths_ = Normalize(paths);
return *this;
}
Path& Path::Set(const std::string& path) {
StringArray_t paths = Split(path);
paths_ = Normalize(paths);
return *this;
}
Path Path::Parent() const {
Path out;
out.paths_ = paths_;
if (out.paths_.size()) out.paths_.pop_back();
return out;
}
std::string Path::Basename() const {
if (paths_.size()) return paths_.back();
return std::string();
}
std::string Path::Join() const {
return Range(paths_, 0, paths_.size());
}
std::string Path::Range(size_t start, size_t end) const {
return Range(paths_, start, end);
}
StringArray_t Path::Split() const {
return paths_;
}
StringArray_t Path::Normalize(const StringArray_t& paths) {
StringArray_t path_out;
for (size_t index = 0; index < paths.size(); index++) {
const std::string &curr = paths[index];
if (curr == "/" && index != 0) continue;
if (curr == ".") continue;
if (curr == "..") {
if (path_out.empty() || path_out.back() == "..") {
path_out.push_back(curr);
continue;
}
if (path_out.back() == "/") {
continue;
}
if (path_out.back() == "/") {
continue;
}
path_out.pop_back();
continue;
}
path_out.push_back(curr);
}
if (path_out.size() == 0) path_out.push_back(".");
return path_out;
}
std::string Path::Join(const StringArray_t& paths) {
return Range(paths, 0, paths.size());
}
std::string Path::Range(const StringArray_t& paths, size_t start, size_t end) {
std::string out_path;
size_t index = start;
if (end > paths.size()) end = paths.size();
if (start == 0 && end > 0 && paths[0] == "/") {
out_path += "/";
index++;
}
for (; index < end; index++) {
out_path += paths[index];
if (index < end - 1)
out_path += "/";
}
return out_path;
}
StringArray_t Path::Split(const std::string& path) {
StringArray_t path_split;
StringArray_t components;
sdk_util::SplitString(path, '/', &path_split);
if (path[0] == '/')
components.push_back("/");
for (StringArray_t::const_iterator it = path_split.begin();
it != path_split.end(); ++it) {
if (!it->empty()) components.push_back(*it);
}
return components;
}
Path& Path::operator =(const Path& p) {
paths_ = p.paths_;
return *this;
}
Path& Path::operator =(const std::string& p) {
return Set(p);
}
}