Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSodaSea committed Dec 25, 2016
0 parents commit 9e35e28
Show file tree
Hide file tree
Showing 15 changed files with 2,021 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build/
/include/Cats/Corecat/
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.1)

project(Textcat)

set(CMAKE_CXX_STANDARD 11)

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -Ofast")
endif()

include_directories("include")

set(EXAMPLE_XML
SAXReader
SAXWriter
DOMReader
DOMWriter)

foreach(example ${EXAMPLE_XML})
add_executable(XML_${example} example/XML/${example}/${example}.cpp)
endforeach()
23 changes: 23 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# License

MIT License

Copyright (c) 2016 The Cats Project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Textcat: Text data formats library

Textcat is a part of *The Cats Project*. It is designed to be a collection of parsers and serializers for various text data formats.

Currently supported format:

+ [XML](/doc/XML.md)


## Licence

[MIT License](/LICENSE.md)


## Requirement

+ C++11
+ [Corecat][Corecat]


[Corecat]: https://github.com/SuperSodaSea/Corecat
11 changes: 11 additions & 0 deletions data/test1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>

<!--This is a comment-->

<?test pi?>

<list>
<text id="1"><![CDATA[Hello, world!]]></text>
<text id="2">The quick brown fox jumps over the lazy dog.</text>
<text id="3">I can eat glass, it doesn&apos;t hurt me.</text>
</list>
51 changes: 51 additions & 0 deletions doc/XML.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Textcat::XML

Textcat::XML contains the XML parser and serializer.


## About XML

XML stands for "Extensible Markup Language".

Specification for XML:

* [Extensible Markup Language (XML) 1.0](https://www.w3.org/TR/xml/)
* [Extensible Markup Language (XML) 1.1](https://www.w3.org/TR/xml11/)

Specification for DOM:

* [Document Object Model Level 1](https://www.w3.org/TR/REC-DOM-Level-1/)

## Features

* **Easy to use**. Textcat::XML provides both SAX and DOM style API.
* **High-performance**. Textcat::XML learned from RapidXml and RapidJSON, which are probably the fastest choices for XML and JSON. Under the same condition, it is sometimes even faster than RapidXml.
* **Header-only**. Textcat::XML is lightweight, and only require [Corecat][Corecat], which is the core of *The Cats Project* and is also header-only.


## Start in a minute

```cpp
#include <iostream>

#include "Cats/Textcat/XML.hpp"

using namespace Cats::Textcat;

int main() {

std::ios::sync_with_stdio(false);

char data[] = R"(<list><person name="SuperSodaSea" gender="male" age="16"/></list>)";

XML::Document document;
document.parse<XML::Parser::Flag::Default>(data);
std::cout << document << std::endl;

return 0;

}
```
[Corecat]: https://github.com/SuperSodaSea/Corecat
77 changes: 77 additions & 0 deletions example/XML/DOMReader/DOMReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
*
* MIT License
*
* Copyright (c) 2016 The Cats Project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#include <exception>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <vector>

#include "Cats/Textcat/XML.hpp"

using namespace Cats::Textcat;

void readFile(const char* name, std::vector<char>& data) {

std::size_t size;
std::ifstream is(name, std::ios::binary);
if(!is) throw std::runtime_error("can't read file");
is.seekg(0, std::ios::end);
size = is.tellg();
is.seekg(0);
data.resize(size + 1);
is.read(data.data(), size);
data[size] = 0;

}

int main(int argc, char** argv) {

std::ios::sync_with_stdio(false);

if(argc < 2) {

std::cout << "error: file name needed" << std::endl;
return 1;

}
try {

std::vector<char> data;
readFile(argv[1], data);
XML::Document document;
document.parse<XML::Parser::Flag::Default>(data.data());
std::cout << document << std::endl;

} catch(std::exception& e) {

std::cout << "exception: " << e.what() << std::endl;

}

return 0;

}
47 changes: 47 additions & 0 deletions example/XML/DOMWriter/DOMWriter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
*
* MIT License
*
* Copyright (c) 2016 The Cats Project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#include <iostream>

#include "Cats/Textcat/XML.hpp"

using namespace Cats::Textcat;

int main() {

std::ios::sync_with_stdio(false);

XML::Document document;
XML::Element list("list"); document.appendChild(list);
XML::Element person("person"); list.appendChild(person);
XML::Attribute name("name", "SuperSodaSea"); person.appendAttribute(name);
XML::Attribute gender("gender", "male"); person.appendAttribute(gender);
XML::Attribute age("age", "16"); person.appendAttribute(age);
std::cout << document << std::endl;

return 0;

}
134 changes: 134 additions & 0 deletions example/XML/SAXReader/SAXReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
*
* MIT License
*
* Copyright (c) 2016 The Cats Project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#include <fstream>
#include <iostream>
#include <stdexcept>
#include <vector>

#include "Cats/Textcat/XML.hpp"

using namespace Cats::Textcat;

class Handler : public XML::HandlerBase {

private:

int level;

private:

void indent() { for(int i = 0; i < level; ++i) std::cout << " "; }

public:

void startDocument() { level = 0; std::cout << "startDocument()\n"; }
void endDocument() { std::cout << "endDocument()\n"; }
void startElement(const char* name, size_t /*nameLength*/) {

indent(); std::cout << "startElement(\"" << name << "\")\n"; ++level;

}
void endElement(const char* name, size_t /*nameLength*/) {

--level; indent(); std::cout << "endElement(\"" << name << "\")\n";

}
void endAttributes() {

indent(); std::cout << "endAttributes()\n";

}
void doctype() { indent(); std::cout << "doctype()\n"; }
void attribute(const char* name, size_t /*nameLength*/, const char* value, size_t /*valueLength*/) {

indent(); std::cout << "attribute(\"" << name << "\", \"" << value << "\")\n";

}
void text(const char* value, size_t /*valueLength*/) {

indent(); std::cout << "text(\"" << value << "\")\n";

}
void cdata(const char* value, size_t /*valueLength*/) {

indent(); std::cout << "cdata(\"" << value << "\")\n";

}
void comment(const char* value, size_t /*valueLength*/) {

indent(); std::cout << "comment(\"" << value << "\")\n";

}
void processingInstruction(const char* name, size_t /*nameLength*/, const char* value, size_t /*valueLength*/) {

indent(); std::cout << "processingInstruction(\"" << name << "\", \"" << value << "\")\n";

}

};

void readFile(const char* name, std::vector<char>& data) {

std::size_t size;
std::ifstream is(name, std::ios::binary);
if(!is) throw std::runtime_error("can't read file");
is.seekg(0, std::ios::end);
size = is.tellg();
is.seekg(0);
data.resize(size + 1);
is.read(data.data(), size);
data[size] = 0;

}

int main(int argc, char** argv) {

std::ios::sync_with_stdio(false);

if(argc < 2) {

std::cout << "error: file name needed" << std::endl;
return 1;

}
try {

std::vector<char> data;
readFile(argv[1], data);
XML::Parser parser;
Handler handler;
parser.parse<XML::Parser::Flag::Default>(data.data(), handler);

} catch(std::exception& e) {

std::cout << "exception: " << e.what() << std::endl;

}

return 0;

}
Loading

0 comments on commit 9e35e28

Please sign in to comment.