You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.0 KiB
C++
39 lines
1.0 KiB
C++
// SPDX-FileCopyrightText: 2026 Reion Wong <reionwong@gmail.com>
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
#include "cutefish/files/file_path.h"
|
|
|
|
#include <cerrno>
|
|
|
|
#include "cutefish/files/error.h"
|
|
|
|
namespace cutefish::files {
|
|
|
|
FilePath::FilePath(std::string bytes)
|
|
: m_bytes(std::move(bytes))
|
|
{
|
|
if (m_bytes.empty() || m_bytes.find('\0') != std::string::npos)
|
|
throw Failure(EINVAL, {});
|
|
}
|
|
|
|
FilePath FilePath::parent() const
|
|
{
|
|
std::string path = m_bytes;
|
|
while (path.size() > 1 && path.back() == '/')
|
|
path.pop_back();
|
|
|
|
const size_t slash = path.rfind('/');
|
|
return FilePath(slash == std::string::npos ? std::string(".") : slash == 0 ? std::string("/") : path.substr(0, slash));
|
|
}
|
|
|
|
FilePath FilePath::child(const std::string& name) const
|
|
{
|
|
if (name.empty() || name == "." || name == ".." || name.find('/') != std::string::npos
|
|
|| name.find('\0') != std::string::npos)
|
|
throw Failure(EINVAL, {});
|
|
|
|
return FilePath(m_bytes + (m_bytes.back() == '/' ? "" : "/") + name);
|
|
}
|
|
|
|
} // namespace cutefish::files
|