Skip to content

Commit

Permalink
Merge pull request #32 from Adamska1008/31-refactor-project-naming-co…
Browse files Browse the repository at this point in the history
…nvention

31 refactor project naming convention
  • Loading branch information
Adamska1008 authored Sep 5, 2024
2 parents ae7af4a + a5fbd53 commit 1314117
Show file tree
Hide file tree
Showing 34 changed files with 1,054 additions and 850 deletions.
1 change: 1 addition & 0 deletions Testing/Temporary/CTestCostData.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
3 changes: 3 additions & 0 deletions Testing/Temporary/LastTest.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Start testing: Sep 05 15:55 CST
----------------------------------------------------------
End testing: Sep 05 15:55 CST
91 changes: 91 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Examples

## Document

### Simple

```C++
using namespace myxml;
// `std::string xml`
document doc = document::parse(xml);
// or
doc = document::load(path);
// get root elem
std::optional<element> elem = doc.root().first_elem();
// or directly query elem in root
elem = doc.first_elem();
// or query by name
elem = doc.first_elem("elem");
```

### Element

#### Basic

```C++
using namespace myxml;
element elem("root");
fmt::println(root->name());
root->set_name("far");
```
#### Attributes
```C++
using namespace myxml;
// create an element with name 'root'
element elem("root");
// or get an element from parsing
element elem = element::parse(xml);
// or do it by custom string literal
element elem = "<root/>"_elem;
// query attributes, it returns an string reference
fmt::println(elem["hello"]);
// modify attribute, will create new attribute if not found one
elem["hello"] = "world!";
// if key not exist, query it will create an empty attribute
elem["hello"]; // == elem["hello"] = "";
```

#### Children

```C++
using namespace myxml;
// element root;
element child = root.first_elem();
// or first element with name "child"
element child = root.first_elem("child");
// elements == vector<element>
std::vector<element> children = root.elems();
// or elements with same name
std::vector<element> children = root.elems("child");
```

### Text

```C++
using namespace myxml;
// create from string
text txt = "Hello";
// or from query
text txt = root.first_text();
// print raw
fmt::println(txt);
// print trimmed text, it returns std::string and will not modify it
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);
```
42 changes: 21 additions & 21 deletions include/myxml/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,47 @@
namespace myxml
{
/**
* ADT. Used by Parser.
* ADT. Used by parser.
* Implement by StringBuffer and XMLFile
*/
class Buffer
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;
// update line and column
void updateLocation(char);
void update_loc(char);
// update line and column
void updateLocation(std::string_view);
void update_loc(std::string_view);

public:
virtual ~Buffer() = default;
std::optional<char> Peek() const;
std::optional<std::string_view> PeekN(int) const;
std::optional<char> AfterN(int) const;
std::optional<std::string_view> AfterNM(int, int) const;
std::optional<char> Take();
std::optional<std::string_view> TakeN(int);
virtual ~buffer() = default;
std::optional<char> peek() const;
std::optional<std::string_view> peek_n(int) const;
std::optional<char> after_n(int) const;
std::optional<std::string_view> after_n_m(int, int) const;
std::optional<char> take();
std::optional<std::string_view> take_n(int);
// @returns {line, column}
std::tuple<std::size_t, std::size_t> CurrentLocation();
std::tuple<std::size_t, std::size_t> cur_loc();
};

class StringBuffer : public Buffer
class string_buffer : public buffer
{
private:
std::variant<std::string, std::string_view> inner;
std::string_view getView() const;
std::variant<std::string, std::string_view> _inner;
std::string_view view() const;

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

public:
StringBuffer(std::string_view);
StringBuffer(std::string &&);
StringBuffer(const char *);
string_buffer(std::string_view);
string_buffer(std::string &&);
string_buffer(const char *);
};
}
35 changes: 28 additions & 7 deletions include/myxml/cdata.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
#pragma once
#include "myxml/node.hpp"
#include "myxml/printable.hpp"

namespace myxml
{
class CData : public Node
class cdata_impl;

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

cdata(std::shared_ptr<cdata_impl>);

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

virtual void entity_encoding(bool) override {}
virtual void platform_specific_newline(bool) override {}
virtual void print(std::ostream &) const override;
};

class cdata_impl : public node
{
public:
explicit CData(std::string);
std::string _inner;

cdata_impl() {};
explicit cdata_impl(std::string_view);
explicit cdata_impl(std::string &&);
virtual ~cdata_impl() = default;

virtual ~CData() = default;
virtual std::string ExportRaw() const override;
virtual std::string ExportFormatted(int indentLevel = 0, int indentSize = 4) const override;
virtual void SetEntityEncoding(bool) override;
virtual void entity_encoding(bool) override {}
virtual void platform_specific_newline(bool) override {}
virtual void print(std::ostream &) const override;
};
}
50 changes: 25 additions & 25 deletions include/myxml/document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,61 @@
// Declaration and Documant are both NOT Node
namespace myxml
{
struct Declaration : public Exportable
struct declaration : public printable
{
std::string version;
std::optional<std::string> encoding;
std::optional<std::string> standalone;

// return `std::nullopt` if declartion is in bad format
// TODO: use exception to distinguish each of bad format
static std::optional<Declaration> BuildFromAttrs(std::map<std::string, std::string> attrs);
static std::optional<declaration> from_attrs(std::map<std::string, std::string> attrs);

/* Exportable */
virtual std::string ExportRaw() const;
virtual std::string ExportFormatted(int indentLevel = 0, int indentSize = 4) const;
virtual void entity_encoding(bool) {};
virtual void platform_specific_newline(bool) {};
virtual void print(std::ostream &os) const {}
};

class Document : public Exportable
class document : public printable
{
private:
Declaration declaration;
std::shared_ptr<Element> root;
declaration _decl;
element _root;

public:
/* Manipulate */
void SetDeclaration(const Declaration &);
void SetRoot(std::shared_ptr<Element> root);
void set_declaration(const declaration &);
void set_root(std::shared_ptr<element_impl> root);

/* Query */
const Declaration &GetDeclartion() const;
Declaration &GetDeclartion();
const std::shared_ptr<Element> &GetRoot() const;
std::shared_ptr<Element> GetRoot();
std::shared_ptr<Element> Elem(std::string_view);
std::shared_ptr<Element> FirstElem();
std::shared_ptr<Text> FirstText();
declaration &get_declaration();
element &root();
element first_elem(std::string_view);
element first_elem();
text first_text();

/** Load */
static Document Parse(std::string_view);
static Document ParseFile(std::string fileName);
static document parse(std::string_view);
static document load(std::string fileName);

/* Exportable */
virtual std::string ExportRaw() const;
virtual std::string ExportFormatted(int indentLevel = 0, int indentSize = 4) const;
virtual void SetEntityEncoding(bool);
virtual void SetPlatformSpecificNewline(bool);
/* Implement printable */
virtual void print(std::ostream &os) const override;
virtual void entity_encoding(bool) override;
virtual void platform_specific_newline(bool) override;
};

namespace util
{
bool isValidXmlVersion(std::string_view);
bool isValidXmlEncoding(std::string_view);
bool isValidXmlStandalone(std::string_view);
bool is_valid_xml_version(std::string_view);
bool is_valid_xml_encoding(std::string_view);
bool is_valid_xml_standalone(std::string_view);
}

namespace literals
{
Document operator""_doc(const char *, std::size_t);
document operator""_doc(const char *, std::size_t);
}
}
Loading

0 comments on commit 1314117

Please sign in to comment.