Skip to content

Commit

Permalink
feat(element.hpp): add first_cdata method
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamska1008 committed Sep 5, 2024
1 parent 4555a00 commit 0d0deb3
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 144 deletions.
13 changes: 13 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,16 @@ fmt::println(txt.trimmed());
// yet it will modify. return a new text
fmt::println(txt.trim());
```
### CData
```C++
// similar to text
using namespace myxml;
// create from string
cdata txt = "Hello";
// or from query
cdata txt = root.first_cdata();
// print raw
fmt::println(txt);
```
8 changes: 4 additions & 4 deletions include/myxml/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace myxml
class buffer
{
private:
std::size_t offset = 0;
std::size_t line = 0;
std::size_t column = 0;
std::size_t _offset = 0;
std::size_t _line = 0;
std::size_t _column = 0;

// @returns {pointer to data, data length}
virtual std::tuple<const char *, std::size_t> base() const = 0;
Expand All @@ -40,7 +40,7 @@ namespace myxml
class string_buffer : public buffer
{
private:
std::variant<std::string, std::string_view> inner;
std::variant<std::string, std::string_view> _inner;
std::string_view view() const;

/** Implement Buffer */
Expand Down
3 changes: 2 additions & 1 deletion include/myxml/cdata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace myxml

class cdata : public printable
{
friend class element;
private:
std::shared_ptr<cdata_impl> _impl;

Expand All @@ -25,7 +26,7 @@ namespace myxml
class cdata_impl : public node
{
public:
std::string inner;
std::string _inner;

cdata_impl() {};
explicit cdata_impl(std::string_view);
Expand Down
4 changes: 2 additions & 2 deletions include/myxml/document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace myxml
class document : public printable
{
private:
declaration decl;
element root;
declaration _decl;
element _root;

public:
/* Manipulate */
Expand Down
6 changes: 4 additions & 2 deletions include/myxml/element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <map>

#include "myxml/text.hpp"
#include "myxml/cdata.hpp"
#include "myxml/printable.hpp"

namespace myxml
Expand Down Expand Up @@ -46,6 +47,7 @@ namespace myxml
element first_elem();
element first_elem(std::string_view);
text first_text();
cdata first_cdata();

/* Implement printable */
virtual void print(std::ostream &) const override;
Expand All @@ -56,8 +58,8 @@ namespace myxml
struct element_impl : public composite_node // public std::enable_shared_from_this<Element>, public Node
{
public:
std::string name;
std::map<std::string, std::string, std::less<>> attributes;
std::string _name;
std::map<std::string, std::string, std::less<>> _attributes;

/* Set initializer as private to avoid using Element without share_ptr*/

Expand Down
10 changes: 5 additions & 5 deletions include/myxml/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ namespace myxml
class node : public std::enable_shared_from_this<node>, public printable
{
public:
virtual ~node() = default;
std::shared_ptr<composite_node> _parent;
std::shared_ptr<node> _prev;
std::shared_ptr<node> _next;

template <typename T, typename = std::enable_if_t<std::is_base_of_v<node, T>>>
std::shared_ptr<T> next()
{
Expand All @@ -44,11 +49,6 @@ namespace myxml
return nullptr;
}

virtual ~node() = default;
std::shared_ptr<composite_node> _parent;
std::shared_ptr<node> _prev;
std::shared_ptr<node> _next;

template <typename T, typename = std::enable_if_t<std::is_base_of_v<node, T>>>
std::shared_ptr<T> as()
{
Expand Down
2 changes: 1 addition & 1 deletion include/myxml/printable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace myxml
class printable
{
protected:
print_config config;
print_config _config;

public:
virtual ~printable() = default;
Expand Down
5 changes: 0 additions & 5 deletions include/myxml/text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ namespace myxml

virtual ~text_impl() = default;

// may used in Export
bool IsAllSpace() const;

/* Implment Exportable*/
// virtual std::string ExportRaw() const override;
// virtual std::string ExportFormatted(int indentLevel = 0, int indentSize = 4) const override;
virtual void print(std::ostream &) const override;
};

Expand Down
19 changes: 9 additions & 10 deletions include/myxml/xmlfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,25 @@

namespace myxml
{
class XMLFile : public buffer
class xml_file : public buffer
{
private:
XMLFile();
xml_file();

/* mmap related*/
int fd;
std::size_t fileSize;
char *inner;

std::size_t offset;
int _fd;
std::size_t _size;
char *_mapped;
std::size_t _offset;

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

public:
static std::shared_ptr<XMLFile> Open(std::string_view fpath);
static std::shared_ptr<xml_file> open(std::string_view fpath);
// RAII
~XMLFile();
~xml_file();
// always copy shared_ptr instead of XMLFile
XMLFile(const XMLFile &) = delete;
xml_file(const xml_file &) = delete;
};
}
44 changes: 22 additions & 22 deletions src/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace myxml
{
if (ch == '\n')
{
this->column = 0;
this->line++;
this->_column = 0;
this->_line++;
}
else
{
this->column++;
this->_column++;
}
}

Expand All @@ -26,80 +26,80 @@ namespace myxml
std::optional<char> buffer::peek() const
{
auto [ptr, len] = this->base();
if (this->offset >= len)
if (this->_offset >= len)
{
return std::nullopt;
}
return ptr[this->offset];
return ptr[this->_offset];
}

std::optional<std::string_view> buffer::peek_n(int n) const
{
auto [ptr, len] = this->base();
if (this->offset >= len)
if (this->_offset >= len)
{
return std::nullopt;
}
return std::string_view(ptr + this->offset, n);
return std::string_view(ptr + this->_offset, n);
}

std::optional<char> buffer::after_n(int n) const
{
auto [ptr, len] = this->base();
if (this->offset + n > len)
if (this->_offset + n > len)
{
return std::nullopt;
}
return ptr[this->offset + n];
return ptr[this->_offset + n];
}

std::optional<std::string_view> buffer::after_n_m(int n, int m) const
{
auto [ptr, len] = this->base();
if (this->offset + n + m > len)
if (this->_offset + n + m > len)
{
return std::nullopt;
}
return std::string_view(ptr + this->offset + n, m);
return std::string_view(ptr + this->_offset + n, m);
}

std::optional<char> buffer::take()
{
auto [ptr, len] = this->base();
if (this->offset >= len)
if (this->_offset >= len)
{
return std::nullopt;
}
auto ch = ptr[this->offset++];
auto ch = ptr[this->_offset++];
this->update_loc(ch);
return ch;
}

std::optional<std::string_view> buffer::take_n(int n)
{
auto [ptr, len] = this->base();
if (offset + n >= len)
if (_offset + n >= len)
{
return std::nullopt;
}
std::string_view strv(ptr + this->offset, n);
std::string_view strv(ptr + this->_offset, n);
this->update_loc(strv);
offset += n;
_offset += n;
return strv;
}

std::tuple<std::size_t, std::size_t> buffer::cur_loc()
{
return {this->line, this->column};
return {this->_line, this->_column};
}

string_buffer::string_buffer(std::string_view inner)
: inner(inner)
: _inner(inner)
{
}

string_buffer::string_buffer(std::string &&inner)
: inner(inner)
: _inner(inner)
{
}

Expand All @@ -116,13 +116,13 @@ namespace myxml

std::string_view string_buffer::view() const
{
if (std::holds_alternative<std::string>(this->inner))
if (std::holds_alternative<std::string>(this->_inner))
{
return std::string_view(std::get<std::string>(this->inner));
return std::string_view(std::get<std::string>(this->_inner));
}
else
{
return std::get<std::string_view>(this->inner);
return std::get<std::string_view>(this->_inner);
}
}
}
6 changes: 3 additions & 3 deletions src/cdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ namespace myxml
}

cdata_impl::cdata_impl(std::string_view str)
: inner(str)
: _inner(str)
{
}

cdata_impl::cdata_impl(std::string &&str)
: inner(str)
: _inner(str)
{
}

void cdata_impl::print(std::ostream &os) const
{
os << "<![CDATA[" << this->inner << "]]>\n";
os << "<![CDATA[" << this->_inner << "]]>\n";
}
}
Loading

0 comments on commit 0d0deb3

Please sign in to comment.