2023-03-10 21:17:03 +00:00
|
|
|
#pragma once
|
|
|
|
#include <luna/CString.h>
|
|
|
|
#include <luna/Heap.h>
|
2023-03-29 15:28:22 +00:00
|
|
|
#include <luna/String.h>
|
2023-03-10 21:17:03 +00:00
|
|
|
|
2023-05-20 14:39:18 +00:00
|
|
|
static inline bool is_not_delim(char c)
|
|
|
|
{
|
|
|
|
return c && c != '/';
|
|
|
|
}
|
|
|
|
|
2023-03-10 21:17:03 +00:00
|
|
|
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);
|
2023-07-02 14:38:12 +00:00
|
|
|
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() == '/';
|
|
|
|
}
|
|
|
|
|
2023-03-10 21:17:03 +00:00
|
|
|
~PathParser();
|
|
|
|
|
|
|
|
bool is_absolute() const
|
|
|
|
{
|
2023-04-11 20:15:21 +00:00
|
|
|
return is_absolute(StringView { m_original });
|
2023-03-10 21:17:03 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-06-19 09:21:58 +00:00
|
|
|
static Result<String> basename(StringView path);
|
|
|
|
static Result<String> dirname(StringView path);
|
2023-03-11 00:13:44 +00:00
|
|
|
|
2023-03-10 21:17:03 +00:00
|
|
|
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 };
|
2023-03-10 21:17:03 +00:00
|
|
|
};
|