Luna/libluna/include/luna/PathParser.h

54 lines
1.2 KiB
C
Raw Normal View History

#pragma once
#include <luna/CString.h>
#include <luna/Heap.h>
#include <luna/String.h>
2023-05-20 14:39:18 +00:00
static inline bool is_not_delim(char c)
{
return c && c != '/';
}
class PathParser
{
public:
static Result<PathParser> create(const char* path);
PathParser(PathParser&& other);
PathParser(const PathParser&) = delete;
2023-04-11 20:15:21 +00:00
static Result<String> join(StringView path1, StringView path2);
static Result<String> join_paths(StringView path1, StringView path2);
2023-04-11 20:15:21 +00:00
static Result<String> realpath(StringView path);
static bool is_absolute(StringView path)
{
return *path.chars() == '/';
}
~PathParser();
bool is_absolute() const
{
2023-04-11 20:15:21 +00:00
return is_absolute(StringView { m_original });
}
2023-05-20 14:39:18 +00:00
bool has_next() const
{
return m_already_called_next ? (bool)m_strtok_saved_state : is_not_delim(*m_copy);
}
static Result<String> basename(StringView path);
static Result<String> dirname(StringView path);
Option<const char*> next();
private:
PathParser(const char* original, char* copy);
const char* m_original { nullptr };
char* m_copy { nullptr };
bool m_already_called_next { false };
2023-05-20 14:39:18 +00:00
char* m_strtok_saved_state { nullptr };
};