Luna/libluna/src/PathParser.cpp

34 lines
690 B
C++
Raw Normal View History

#include <luna/PathParser.h>
Result<PathParser> PathParser::create(const char* path)
{
char* copy = strdup(path);
if (!copy) return err(ENOMEM);
return PathParser { path, copy };
}
PathParser::PathParser(const char* original, char* copy) : m_original(original), m_copy(copy)
{
}
PathParser::PathParser(PathParser&& other) : m_original(other.m_original), m_copy(other.m_copy)
{
other.m_copy = nullptr;
}
PathParser::~PathParser()
{
if (m_copy) free_impl(m_copy);
}
Option<const char*> PathParser::next()
{
char* result = strtok(m_already_called_next ? nullptr : m_copy, "/");
m_already_called_next = true;
if (!result) return {};
return result;
}