Skip to content

Commit

Permalink
feat(XMLFile.hpp): implement Buffer for XMLFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamska1008 committed Aug 31, 2024
1 parent 17fd64c commit b4ab35a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 53 deletions.
29 changes: 15 additions & 14 deletions include/myxml/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <string_view>
#include <optional>
#include <tuple>

namespace myxml
{
Expand All @@ -12,14 +13,19 @@ namespace myxml
*/
class Buffer
{
private:
std::size_t offset = 0;

virtual std::tuple<const char *, std::size_t> base() const = 0;

public:
virtual ~Buffer() = default;
virtual std::optional<char> Peek() const = 0;
virtual std::optional<std::string_view> PeekN(int) const = 0;
virtual std::optional<char> AfterN(int) const = 0;
virtual std::optional<std::string_view> AfterNM(int, int) const = 0;
virtual std::optional<char> Take() = 0;
virtual std::optional<std::string_view> TakeN(int) = 0;
virtual std::optional<char> Peek() const;
virtual std::optional<std::string_view> PeekN(int) const;
virtual std::optional<char> AfterN(int) const;
virtual std::optional<std::string_view> AfterNM(int, int) const;
virtual std::optional<char> Take();
virtual std::optional<std::string_view> TakeN(int);
};

class StringBuffer : public Buffer
Expand All @@ -28,18 +34,13 @@ namespace myxml
using stringVariant = std::variant<std::string, std::string_view>;

stringVariant inner;
std::size_t offset;
std::string_view getView() const;

/** Implement Buffer */
virtual std::tuple<const char *, std::size_t> base() const override;

public:
StringBuffer(std::string_view);
StringBuffer(std::string &&);

virtual std::optional<char> Peek() const override;
virtual std::optional<std::string_view> PeekN(int) const override;
virtual std::optional<char> AfterN(int) const override;
virtual std::optional<std::string_view> AfterNM(int, int) const override;
virtual std::optional<char> Take() override;
virtual std::optional<std::string_view> TakeN(int) override;
};
}
13 changes: 11 additions & 2 deletions include/myxml/xmlfile.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
#pragma once
#include <memory>
#include <string_view>
#include <optional>
#include "myxml/buffer.hpp"

namespace myxml
{
class XMLFile
class XMLFile : public Buffer
{
private:
XMLFile();

/* Mmap related*/
int fd;
void *mappedRegion;
std::size_t fileSize;
char *inner;

std::size_t offset;

/* Implement Buffer*/
virtual std::tuple<const char *, std::size_t> base() const;

public:
static XMLFile Open(std::string_view fpath);
Expand Down
74 changes: 40 additions & 34 deletions src/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,84 @@

namespace myxml
{
StringBuffer::StringBuffer(std::string_view inner)
: inner(inner), offset(0)
std::optional<char> Buffer::Peek() const
{
}

StringBuffer::StringBuffer(std::string &&inner)
: inner(inner), offset(0)
{
}

std::optional<char> StringBuffer::Peek() const
{
auto view = this->getView();
if (offset >= view.length())
auto [ptr, len] = this->base();
if (this->offset >= len)
{
return std::nullopt;
}
return view[this->offset];
return ptr[this->offset];
}

std::optional<std::string_view> StringBuffer::PeekN(int n) const
std::optional<std::string_view> Buffer::PeekN(int n) const
{
auto view = this->getView();
if (offset + n > view.length())
auto [ptr, len] = this->base();
if (this->offset >= len)
{
return std::nullopt;
}
return view.substr(this->offset, n);
return std::string_view(ptr + this->offset, n);
}

std::optional<char> StringBuffer::AfterN(int n) const
std::optional<char> Buffer::AfterN(int n) const
{
auto view = this->getView();
if (offset + n > view.length())
auto [ptr, len] = this->base();
if (this->offset + n > len)
{
return std::nullopt;
}
return view[this->offset + n];
return ptr[this->offset + n];
}

std::optional<std::string_view> StringBuffer::AfterNM(int n, int m) const
std::optional<std::string_view> Buffer::AfterNM(int n, int m) const
{
auto view = this->getView();
if (offset + n + m > view.length())
auto [ptr, len] = this->base();
if (this->offset + n + m > len)
{
return std::nullopt;
}
return view.substr(offset + n, m);
return std::string_view(ptr + this->offset + n, m);
}

std::optional<char> StringBuffer::Take()
std::optional<char> Buffer::Take()
{
auto view = this->getView();
if (offset >= view.length())
auto [ptr, len] = this->base();
if (this->offset >= len)
{
return std::nullopt;
}
return view[this->offset++];
return ptr[this->offset++];
}

std::optional<std::string_view> StringBuffer::TakeN(int n)
std::optional<std::string_view> Buffer::TakeN(int n)
{
auto view = this->getView();
if (offset + n >= view.length())
auto [ptr, len] = this->base();
if (offset + n >= len)
{
return std::nullopt;
}
std::string_view it = view.substr(offset, n);
std::string_view it(ptr + this->offset, n);
offset += n;
return it;
}

StringBuffer::StringBuffer(std::string_view inner)
: inner(inner)
{
}

StringBuffer::StringBuffer(std::string &&inner)
: inner(inner)
{
}

std::tuple<const char *, std::size_t> StringBuffer::base() const
{
auto view = this->getView();
return {view.data(), view.length()};
}

std::string_view StringBuffer::getView() const
{
if (std::holds_alternative<std::string>(this->inner))
Expand Down
20 changes: 17 additions & 3 deletions src/xmlfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

namespace myxml
{
XMLFile::XMLFile()
: offset(0)
{
}

XMLFile XMLFile::Open(std::string_view fpath)
{
XMLFile xfile;
Expand All @@ -23,15 +28,24 @@ namespace myxml
{
throw IOError();
}
std::size_t fileSize = fileInfo.st_size;
xfile.mappedRegion = mmap(nullptr, fileSize, PROT_READ, MAP_PRIVATE, xfile.fd, 0);
if (xfile.mappedRegion == MAP_FAILED)
xfile.fileSize = fileInfo.st_size;
void *mappedRegion = mmap(nullptr, xfile.fileSize, PROT_READ, MAP_PRIVATE, xfile.fd, 0);
if (mappedRegion == MAP_FAILED)
{
throw IOError();
}
xfile.inner = static_cast<char *>(mappedRegion);
return xfile;
}

XMLFile::~XMLFile()
{
close(this->fd);
munmap(static_cast<void *>(this->inner), this->fileSize);
}

std::tuple<const char *, std::size_t> XMLFile::base() const
{
return {this->inner, this->fileSize};
}
}

0 comments on commit b4ab35a

Please sign in to comment.