当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 0

碧海青天/daemon
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Yaml.hpp 15.99 KB
一键复制 编辑 原始数据 按行查看 历史
碧海青天 提交于 2019-05-30 18:03 . no message
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
/*
* MIT License
*
* Copyright(c) 2018 Jimmie Bergmann
*
* 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.
*
*/
/*
YAML documentation:
http://yaml.org/spec/1.0/index.html
https://www.codeproject.com/Articles/28720/YAML-Parser-in-C
*/
#pragma once
#include <exception>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <map>
/**
* @breif Namespace wrapping mini-yaml classes.
*
*/
namespace Yaml
{
/**
* @breif Forward declarations.
*
*/
class Node;
/**
* @breif Helper classes and functions
*
*/
namespace impl
{
/**
* @breif Helper functionality, converting string to any data type.
* Strings are left untouched.
*
*/
template<typename T>
struct StringConverter
{
static T Get(const std::string & data)
{
T type;
std::stringstream ss(data);
ss >> type;
return type;
}
static T Get(const std::string & data, const T & defaultValue)
{
T type;
std::stringstream ss(data);
ss >> type;
if(ss.fail())
{
return defaultValue;
}
return type;
}
};
template<>
struct StringConverter<std::string>
{
static std::string Get(const std::string & data)
{
return data;
}
static std::string Get(const std::string & data, const std::string & defaultValue)
{
if(data.size() == 0)
{
return defaultValue;
}
return data;
}
};
template<>
struct StringConverter<bool>
{
static bool Get(const std::string & data)
{
std::string tmpData = data;
std::transform(tmpData.begin(), tmpData.end(), tmpData.begin(), ::tolower);
if(tmpData == "true" || tmpData == "yes" || tmpData == "1")
{
return true;
}
return false;
}
static bool Get(const std::string & data, const bool & defaultValue)
{
if(data.size() == 0)
{
return defaultValue;
}
return Get(data);
}
};
}
/**
* @breif Exception class.
*
*/
class Exception : public std::runtime_error
{
public:
/**
* @breif Enumeration of exception types.
*
*/
enum eType
{
InternalError, ///< Internal error.
ParsingError, ///< Invalid parsing data.
OperationError ///< User operation error.
};
/**
* @breif Constructor.
*
* @param message Exception message.
* @param type Type of exception.
*
*/
Exception(const std::string & message, const eType type);
/**
* @breif Get type of exception.
*
*/
eType Type() const;
/**
* @breif Get message of exception.
*
*/
const char * Message() const;
private:
eType m_Type; ///< Type of exception.
};
/**
* @breif Internal exception class.
*
* @see Exception
*
*/
class InternalException : public Exception
{
public:
/**
* @breif Constructor.
*
* @param message Exception message.
*
*/
InternalException(const std::string & message);
};
/**
* @breif Parsing exception class.
*
* @see Exception
*
*/
class ParsingException : public Exception
{
public:
/**
* @breif Constructor.
*
* @param message Exception message.
*
*/
ParsingException(const std::string & message);
};
/**
* @breif Operation exception class.
*
* @see Exception
*
*/
class OperationException : public Exception
{
public:
/**
* @breif Constructor.
*
* @param message Exception message.
*
*/
OperationException(const std::string & message);
};
/**
* @breif Iterator class.
*
*/
class Iterator
{
public:
friend class Node;
/**
* @breif Default constructor.
*
*/
Iterator();
/**
* @breif Copy constructor.
*
*/
Iterator(const Iterator & it);
/**
* @breif Assignment operator.
*
*/
Iterator & operator = (const Iterator & it);
/**
* @breif Destructor.
*
*/
~Iterator();
/**
* @breif Get node of iterator.
* First pair item is the key of map value, empty if type is sequence.
*
*/
std::pair<const std::string &, Node &> operator *();
/**
* @breif Post-increment operator.
*
*/
Iterator & operator ++ (int);
/**
* @breif Post-decrement operator.
*
*/
Iterator & operator -- (int);
/**
* @breif Check if iterator is equal to other iterator.
*
*/
bool operator == (const Iterator & it);
/**
* @breif Check if iterator is not equal to other iterator.
*
*/
bool operator != (const Iterator & it);
private:
enum eType
{
None,
SequenceType,
MapType
};
eType m_Type; ///< Type of iterator.
void * m_pImp; ///< Implementation of iterator class.
};
/**
* @breif Constant iterator class.
*
*/
class ConstIterator
{
public:
friend class Node;
/**
* @breif Default constructor.
*
*/
ConstIterator();
/**
* @breif Copy constructor.
*
*/
ConstIterator(const ConstIterator & it);
/**
* @breif Assignment operator.
*
*/
ConstIterator & operator = (const ConstIterator & it);
/**
* @breif Destructor.
*
*/
~ConstIterator();
/**
* @breif Get node of iterator.
* First pair item is the key of map value, empty if type is sequence.
*
*/
std::pair<const std::string &, const Node &> operator *();
/**
* @breif Post-increment operator.
*
*/
ConstIterator & operator ++ (int);
/**
* @breif Post-decrement operator.
*
*/
ConstIterator & operator -- (int);
/**
* @breif Check if iterator is equal to other iterator.
*
*/
bool operator == (const ConstIterator & it);
/**
* @breif Check if iterator is not equal to other iterator.
*
*/
bool operator != (const ConstIterator & it);
private:
enum eType
{
None,
SequenceType,
MapType
};
eType m_Type; ///< Type of iterator.
void * m_pImp; ///< Implementation of constant iterator class.
};
/**
* @breif Node class.
*
*/
class Node
{
public:
friend class Iterator;
/**
* @breif Enumeration of node types.
*
*/
enum eType
{
None,
SequenceType,
MapType,
ScalarType
};
/**
* @breif Default constructor.
*
*/
Node();
/**
* @breif Copy constructor.
*
*/
Node(const Node & node);
/**
* @breif Assignment constructors.
* Converts node to scalar type if needed.
*
*/
Node(const std::string & value);
Node(const char * value);
/**
* @breif Destructor.
*
*/
~Node();
/**
* @breif Functions for checking type of node.
*
*/
eType Type() const;
bool IsNone() const;
bool IsSequence() const;
bool IsMap() const;
bool IsScalar() const;
/**
* @breif Completely clear node.
*
*/
void Clear();
/**
* @breif Get node as given template type.
*
*/
template<typename T>
T As() const
{
// @bhqt, throws an exception when accessing a field that does not exist
if (this->IsNone()) {
throw std::runtime_error("field does not exist");
}
return impl::StringConverter<T>::Get(AsString());
}
/**
* @breif Get node as given template type.
*
*/
template<typename T>
T As(const T & defaultValue) const
{
// @bhqt, throws an exception when accessing a field that does not exist
if (this->IsNone()) {
throw std::runtime_error("field does not exist");
}
return impl::StringConverter<T>::Get(AsString(), defaultValue);
}
/**
* @breif Get size of node.
* Nodes of type None or Scalar will return 0.
*
*/
size_t Size() const;
// Sequence operators
/**
* @breif Insert sequence item at given index.
* Converts node to sequence type if needed.
* Adding new item to end of sequence if index is larger than sequence size.
*
*/
Node & Insert(const size_t index);
/**
* @breif Add new sequence index to back.
* Converts node to sequence type if needed.
*
*/
Node & PushFront();
/**
* @breif Add new sequence index to front.
* Converts node to sequence type if needed.
*
*/
Node & PushBack();
/**
* @breif Get sequence/map item.
* Converts node to sequence/map type if needed.
*
* @param index Sequence index. Returns None type Node if index is unknown.
* @param key Map key. Creates a new node if key is unknown.
*
*/
Node & operator [] (const size_t index);
Node & operator [] (const std::string & key);
/**
* @breif Erase item.
* No action if node is not a sequence or map.
*
*/
void Erase(const size_t index);
void Erase(const std::string & key);
/**
* @breif Assignment operators.
*
*/
Node & operator = (const Node & node);
Node & operator = (const std::string & value);
Node & operator = (const char * value);
/**
* @breif Get start iterator.
*
*/
Iterator Begin();
ConstIterator Begin() const;
/**
* @breif Get end iterator.
*
*/
Iterator End();
ConstIterator End() const;
private:
/**
* @breif Get as string. If type is scalar, else empty.
*
*/
const std::string & AsString() const;
void * m_pImp; ///< Implementation of node class.
};
/**
* @breif Parsing functions.
* Population given root node with deserialized data.
*
* @param root Root node to populate.
* @param filename Path of input file.
* @param stream Input stream.
* @param string String of input data.
* @param buffer Char array of input data.
* @param size Buffer size.
*
* @throw InternalException An internal error occurred.
* @throw ParsingException Invalid input YAML data.
* @throw OperationException If filename or buffer pointer is invalid.
*
*/
void Parse(Node & root, const char * filename);
void Parse(Node & root, std::iostream & stream);
void Parse(Node & root, const std::string & string);
void Parse(Node & root, const char * buffer, const size_t size);
/**
* @breif Serialization configuration structure,
* describing output behavior.
*
*/
struct SerializeConfig
{
/**
* @breif Constructor.
*
* @param spaceIndentation Number of spaces per indentation.
* @param scalarMaxLength Maximum length of scalars. Serialized as folder scalars if exceeded.
* Ignored if equal to 0.
* @param sequenceMapNewline Put maps on a new line if parent node is a sequence.
* @param mapScalarNewline Put scalars on a new line if parent node is a map.
*
*/
SerializeConfig(const size_t spaceIndentation = 2,
const size_t scalarMaxLength = 64,
const bool sequenceMapNewline = false,
const bool mapScalarNewline = false);
size_t SpaceIndentation; ///< Number of spaces per indentation.
size_t ScalarMaxLength; ///< Maximum length of scalars. Serialized as folder scalars if exceeded.
bool SequenceMapNewline; ///< Put maps on a new line if parent node is a sequence.
bool MapScalarNewline; ///< Put scalars on a new line if parent node is a map.
};
/**
* @breif Serialization functions.
*
* @param root Root node to serialize.
* @param filename Path of output file.
* @param stream Output stream.
* @param string String of output data.
* @param config Serialization configurations.
*
* @throw InternalException An internal error occurred.
* @throw OperationException If filename or buffer pointer is invalid.
* If config is invalid.
*
*/
void Serialize(const Node & root, const char * filename, const SerializeConfig & config = {2, 64, false, false});
void Serialize(const Node & root, std::iostream & stream, const SerializeConfig & config = {2, 64, false, false});
void Serialize(const Node & root, std::string & string, const SerializeConfig & config = {2, 64, false, false});
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/gwsbhqt/daemon.git
git@gitee.com:gwsbhqt/daemon.git
gwsbhqt
daemon
daemon
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385