apio
b4a6e4d56d
All checks were successful
continuous-integration/drone/push Build is passing
This avoids creating a PathParser to use them, which allocates memory that won't be used.
53 lines
1.1 KiB
C++
53 lines
1.1 KiB
C++
#pragma once
|
|
#include <luna/CString.h>
|
|
#include <luna/Heap.h>
|
|
#include <luna/String.h>
|
|
|
|
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;
|
|
|
|
static Result<String> join(StringView path1, StringView path2);
|
|
|
|
static Result<String> realpath(StringView path);
|
|
|
|
static bool is_absolute(StringView path)
|
|
{
|
|
return *path.chars() == '/';
|
|
}
|
|
|
|
~PathParser();
|
|
|
|
bool is_absolute() const
|
|
{
|
|
return is_absolute(StringView { m_original });
|
|
}
|
|
|
|
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 };
|
|
char* m_strtok_saved_state { nullptr };
|
|
};
|