Merge pull request #8 from Kiritow/pre-merge

Weekly Update
This commit is contained in:
Kirigaya Kazuto 2017-05-22 15:10:19 +08:00 committed by GitHub
commit c024cd8467
7 changed files with 932 additions and 100 deletions

View File

@ -51,6 +51,56 @@ namespace MiniEngine
}
}
/// FIXME: return SDL_WindowFlags or Uint32 ?
Uint32 getSDLWindowFlagsFromWindowType(WindowType type)
{
switch(type)
{
case WindowType::FullScreen:
return SDL_WINDOW_FULLSCREEN;
case WindowType::OpenGL:
return SDL_WINDOW_OPENGL;
case WindowType::Shown:
return SDL_WINDOW_SHOWN;
case WindowType::Hidden:
return SDL_WINDOW_HIDDEN;
case WindowType::Borderless:
return SDL_WINDOW_BORDERLESS;
case WindowType::Resizable:
return SDL_WINDOW_RESIZABLE;
case WindowType::Minimized:
return SDL_WINDOW_MINIMIZED;
case WindowType::Maximized:
return SDL_WINDOW_MAXIMIZED;
case WindowType::InputGrabbed:
return SDL_WINDOW_INPUT_GRABBED;
case WindowType::InputFocus:
return SDL_WINDOW_INPUT_FOCUS;
case WindowType::MouseFocus:
return SDL_WINDOW_MOUSE_FOCUS;
case WindowType::FullScreenDesktop:
return SDL_WINDOW_FULLSCREEN_DESKTOP;
case WindowType::Foreign:
return SDL_WINDOW_FOREIGN;
case WindowType::AllowHighDPI:
return SDL_WINDOW_ALLOW_HIGHDPI;
case WindowType::MouseCapture:
return SDL_WINDOW_MOUSE_CAPTURE;
case WindowType::AlwaysOnTop:
return SDL_WINDOW_ALWAYS_ON_TOP;
case WindowType::SkipTaskBar:
return SDL_WINDOW_SKIP_TASKBAR;
case WindowType::Utility:
return SDL_WINDOW_UTILITY;
case WindowType::ToolTip:
return SDL_WINDOW_TOOLTIP;
case WindowType::PopUpMenu:
return SDL_WINDOW_POPUP_MENU;
default:
return 0;/// Return 0 on default.
}
}
SystemCursorType getCursorTypeFromSDLSystemCursor(SDL_SystemCursor id)
{
switch(id)
@ -116,6 +166,52 @@ namespace MiniEngine
return SDL_SYSTEM_CURSOR_ARROW;
}
}
int getTTFFontStyleFromFontStyle(Font::Style style)
{
switch(style)
{
case Font::Style::Bold:
return TTF_STYLE_BOLD;
case Font::Style::Italic:
return TTF_STYLE_ITALIC;
case Font::Style::Normal:
return TTF_STYLE_NORMAL;
case Font::Style::StrikeThrough:
return TTF_STYLE_STRIKETHROUGH;
case Font::Style::UnderLine:
return TTF_STYLE_UNDERLINE;
default:
return TTF_STYLE_NORMAL;
}
}
std::vector<Font::Style> getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style)
{
std::vector<Font::Style> vec;
if(Mixed_TTF_Font_Style&TTF_STYLE_BOLD)
{
vec.push_back(Font::Style::Bold);
}
if(Mixed_TTF_Font_Style&TTF_STYLE_ITALIC)
{
vec.push_back(Font::Style::Italic);
}
if(Mixed_TTF_Font_Style&TTF_STYLE_STRIKETHROUGH)
{
vec.push_back(Font::Style::StrikeThrough);
}
if(Mixed_TTF_Font_Style&TTF_STYLE_UNDERLINE)
{
vec.push_back(Font::Style::UnderLine);
}
if(vec.empty())
{
vec.push_back(Font::Style::Normal);
}
return vec;
}
}/// End of namespace _internal
Rect::Rect(int X, int Y, int W, int H)
@ -311,6 +407,11 @@ namespace MiniEngine
_set(SDL_RWFromMem(mem,size));
}
void RWOP::release()
{
_clear();
}
void Surface::_set(SDL_Surface* p)//private
{
_surf.reset(p,SDL_FreeSurface);
@ -502,6 +603,11 @@ namespace MiniEngine
return surf;
}
void Surface::release()
{
_clear();
}
void Texture::_set(SDL_Texture* p)//private
{
_text.reset(p,SDL_DestroyTexture);
@ -610,6 +716,11 @@ namespace MiniEngine
rect.x = rect.y = 0;
}
void Texture::release()
{
_clear();
}
void Renderer::_set(SDL_Renderer* p)
{
_rnd.reset(p,SDL_DestroyRenderer);
@ -844,6 +955,16 @@ namespace MiniEngine
return t;
}
bool Renderer::isReady()
{
return (_get() != nullptr);
}
void Renderer::release()
{
_clear();
}
//private
void Cursor::_set(SDL_Cursor* p)
{
@ -862,6 +983,11 @@ namespace MiniEngine
return _cur.get();
}
void Cursor::_clear()
{
_cur.reset();
}
//static
Cursor Cursor::CreateCursor(Surface surf,Point hotspot)
{
@ -907,16 +1033,16 @@ namespace MiniEngine
SDL_ShowCursor(Settings?SDL_ENABLE:SDL_DISABLE);
}
void Cursor::release()
{
_clear();
}
void Cursor::activate()
{
SDL_SetCursor(_get());
}
bool Renderer::isReady()
{
return (_get() != nullptr);
}
void Window::_set(SDL_Window* p)
{
_wnd.reset(p,SDL_DestroyWindow);
@ -932,9 +1058,18 @@ namespace MiniEngine
return _wnd.get();
}
Window::Window(std::string Title, int Width, int Height, std::initializer_list<RendererType> RendererFlags) throw(ErrorViewer)
Window::Window(std::string Title, int Width, int Height,
std::initializer_list<RendererType> RendererFlags,
std::initializer_list<WindowType> WindowFlags , int WindowPositionX, int WindowPositionY) throw(ErrorViewer)
{
SDL_Window* temp = SDL_CreateWindow(Title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_WINDOW_SHOWN);
/// Calculate Window Flags
Uint32 windowFlag=0;
for(auto v:WindowFlags)
{
windowFlag|=_internal::getSDLWindowFlagsFromWindowType(v);
}
SDL_Window* temp = SDL_CreateWindow(Title.c_str(), WindowPositionX, WindowPositionY, Width, Height, windowFlag);
if (temp == NULL)
{
ErrorViewer e;
@ -1060,6 +1195,12 @@ namespace MiniEngine
return s;
}
void Window::release()
{
_clear();
}
// private
Uint32 Window::_render_caster(RendererType Type)
{
switch(Type)
@ -1078,6 +1219,7 @@ namespace MiniEngine
return 0;
}
// private
void Window::_setRenderer_Real(Uint32 flags)
{
winrnd._rnd.reset(SDL_CreateRenderer(_get(), -1, flags), SDL_DestroyRenderer);
@ -1139,6 +1281,68 @@ namespace MiniEngine
return (_get() != nullptr);
}
bool Font::isNormal()
{
return !(TTF_GetFontStyle(_get()));
}
bool Font::isBold()
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_BOLD );
}
bool Font::isItalic()
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_ITALIC );
}
bool Font::isUnderLine()
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_UNDERLINE );
}
bool Font::isStrikeThrough()
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_STRIKETHROUGH );
}
void Font::setNormal()
{
_real_setFontStyle(TTF_STYLE_NORMAL);
}
void Font::setBold(bool enable)
{
if( enable!=isBold() )
{
_real_setFontStyle( TTF_GetFontStyle(_get()) | (enable?TTF_STYLE_BOLD:!TTF_STYLE_BOLD) );
}
}
void Font::setItalic(bool enable)
{
if( enable!=isItalic() )
{
_real_setFontStyle( TTF_GetFontStyle(_get()) | (enable?TTF_STYLE_ITALIC:!TTF_STYLE_ITALIC) );
}
}
void Font::setUnderLine(bool enable)
{
if( enable!=isUnderLine() )
{
_real_setFontStyle( TTF_GetFontStyle(_get()) | (enable?TTF_STYLE_UNDERLINE:!TTF_STYLE_UNDERLINE) );
}
}
void Font::setStrikeThrough(bool enable)
{
if( enable!=isStrikeThrough() )
{
_real_setFontStyle( TTF_GetFontStyle(_get()) | (enable?TTF_STYLE_STRIKETHROUGH:!TTF_STYLE_STRIKETHROUGH) );
}
}
void Font::_real_setFontStyle(int Style)
{
TTF_SetFontStyle(_get(),Style);
@ -1146,22 +1350,35 @@ namespace MiniEngine
int Font::_style_caster(Style style)
{
switch(style)
{
case Style::Bold:
return TTF_STYLE_BOLD;
case Style::Italic:
return TTF_STYLE_ITALIC;
case Style::Normal:
return TTF_STYLE_NORMAL;
case Style::StrikeThrough:
return TTF_STYLE_STRIKETHROUGH;
case Style::UnderLine:
return TTF_STYLE_UNDERLINE;
}
return _internal::getTTFFontStyleFromFontStyle(style);
}
/// If an error occurs, return 0 instead of -1.
return 0;
std::vector<Font::Style> Font::getFontStyles()
{
int styles=TTF_GetFontStyle(_get());
return _internal::getFontStyleVecFromMixedTTFFontStyle(styles);
}
Rect Font::sizeText(const std::string& Text) throw (ErrorViewer)
{
int w=0,h=0;
if(TTF_SizeText(_get(),Text.c_str(),&w,&h)!=0)
{
/// Something might be wrong
throw ErrorViewer();
}
return Rect(0,0,w,h);
}
Rect Font::sizeUTF8(const std::string& Text) throw (ErrorViewer)
{
int w=0,h=0;
if(TTF_SizeUTF8(_get(),Text.c_str(),&w,&h)!=0)
{
/// Something might be wrong
throw ErrorViewer();
}
return Rect(0,0,w,h);
}
/// rendering surfaces...
@ -1262,6 +1479,11 @@ namespace MiniEngine
return rnd.render(renderUTF8Solid(Text,fg));
}
void Font::release()
{
_clear();
}
void LogSystem::d(const char* fmt,...)
{
va_list ap;
@ -1310,6 +1532,21 @@ namespace MiniEngine
va_end(ap);
}
void* SharedLibrary::_get()
{
return _obj.get();
}
void SharedLibrary::_set(void* ptr)
{
_obj.reset(ptr,SDL_UnloadObject);
}
void SharedLibrary::_clear()
{
_obj.reset();
}
SharedLibrary::SharedLibrary()
{
_obj=nullptr;
@ -1323,40 +1560,44 @@ namespace MiniEngine
SharedLibrary::~SharedLibrary()
{
if(_obj)
{
unload();
}
}
int SharedLibrary::load(const std::string& Filename)
{
if(_obj) return -1;
if(_get()!=nullptr) return -1; /// Loaded
else
{
_obj=SDL_LoadObject(Filename.c_str());
if(_obj) return 0;
else return -2;
void* ptr=SDL_LoadObject(Filename.c_str());
if(ptr)
{
_set(ptr);
return 0; /// Success
}
else return -2; /// Failed to load
}
}
int SharedLibrary::unload()
{
if(_obj)
if(_get()!=nullptr)
{
SDL_UnloadObject(_obj);
_obj=nullptr;
return 0;
_clear();
return 0; /// Success to unload
}
else return -1;
else return -1; /// Not Loaded.
}
void* SharedLibrary::get(const std::string& FunctionName)
{
if(!_obj) return nullptr;
else return SDL_LoadFunction(_obj,FunctionName.c_str());
if(_get()==nullptr) return nullptr;
else return SDL_LoadFunction(_get(),FunctionName.c_str());
}
void SharedLibrary::release()
{
_clear();
}
int SDLSystem::SDLInit()
{
@ -1787,6 +2028,7 @@ namespace MiniEngine
{
pimpl=new impl;
pimpl->status=false;
std::ifstream ifs(StringFile);
if(!ifs) return;
rapidxml::file<> strFile(ifs);
@ -1819,9 +2061,11 @@ namespace MiniEngine
std::string StringEngine::getString(std::string Tag)
{
if(!ready()) return "";
char* context=pimpl->root->first_node(Tag.c_str())->value();
if(context==nullptr) return "";
if(!ready()) return "(StringEngine::STRING_NOT_FOUND)";
rapidxml::xml_node<>* pnode=pimpl->root->first_node(Tag.c_str());
if(pnode==nullptr) return "(StringEngine::STRING_NOT_FOUND)";
char* context=pnode->value();
if(context==nullptr) return "";/// Empty String.
else return std::string(context);
}

View File

@ -1,29 +1,9 @@
#pragma once
#ifdef _MSC_VER
/// Visual Studio (VC++ Compiler)
#include <SDL.h>
#undef main
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
/// VC++ does not implied C++ exception. Use this to ignore compile warning on this.
#pragma warning (disable:4290)
#else
/// CodeBlocks (MinGW Compiler)
#include <SDL2/SDL.h>
#undef main
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#endif
#include "MiniEngine_Config.h"
#include <string>
#include <memory>
#include <functional>
#define _DECL_DEPRECATED [[deprecated]]
#include <vector>
namespace MiniEngine
{
@ -100,6 +80,8 @@ namespace MiniEngine
RWOP(void* mem,int size);
RWOP()=default;
~RWOP()=default;
void release();
private:
std::shared_ptr<SDL_RWops> _op;
SDL_RWops* _get();
@ -146,6 +128,7 @@ namespace MiniEngine
static Surface createSurface(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask) throw(ErrorViewer);
void release();
protected:
Surface() = default;
private:
@ -181,6 +164,7 @@ namespace MiniEngine
RGBA getRGBA();
void setRGBA(RGBA pack);
void release();
protected:
/// updateInfo() must be called after Texture is changed.
void updateInfo();
@ -232,6 +216,8 @@ namespace MiniEngine
Texture createTexture(int Width, int Height) throw(ErrorViewer);
bool isReady();
void release();
private:
std::shared_ptr<SDL_Renderer> _rnd;
void _set(SDL_Renderer*);
@ -262,6 +248,8 @@ namespace MiniEngine
static bool isShow();
void activate();
void release();
private:
std::shared_ptr<SDL_Cursor> _cur;
void _set(SDL_Cursor*);
@ -272,10 +260,23 @@ namespace MiniEngine
enum class MessageBoxType { Error, Warning, Information };
enum class WindowType
{
FullScreen, OpenGL, Shown, Hidden,
Borderless, Resizable, Minimized, Maximized,
InputGrabbed, InputFocus, MouseFocus,
FullScreenDesktop, Foreign, AllowHighDPI,
MouseCapture, AlwaysOnTop, SkipTaskBar,
Utility, ToolTip, PopUpMenu
};
class Window
{
public:
Window(std::string Title, int Width, int Height, std::initializer_list<RendererType> RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture }) throw(ErrorViewer);
Window(std::string Title, int Width, int Height,
std::initializer_list<RendererType> RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture },
std::initializer_list<WindowType> WindowFlags = {WindowType::Shown} ,
int WindowPositionX=SDL_WINDOWPOS_CENTERED, int WindowPositionY=SDL_WINDOWPOS_CENTERED) throw(ErrorViewer);
Renderer getRenderer() const;
void setRenderer(RendererType Type)
@ -321,8 +322,9 @@ namespace MiniEngine
void maximize();
void restore();
_DECL_DEPRECATED Surface getSurface();
void release();
protected:
template<typename... Args>
void _setRenderer(RendererType Type,Args&&... args)
@ -359,19 +361,35 @@ namespace MiniEngine
int use(std::string FontFileName, int size);
bool isReady();
bool isNormal();
bool isBold();
bool isItalic();
bool isUnderLine();
bool isStrikeThrough();
void setNormal();
void setBold(bool);
void setItalic(bool);
void setUnderLine(bool);
void setStrikeThrough(bool);
template<typename... Args>
void setFontStyle(Style style,Args&&... args)
{
_internal_fontcalc=0;
_setFontStyle(style,std::forward(args...));
int fontcalc=0;
_setFontStyle(fontcalc,style,args...);
}
void setFontStyle(Style style)
{
_real_setFontStyle(_style_caster(style));
int fontcalc=0;
_setFontStyle(fontcalc,style);
}
std::tuple<Style> getFontStyles();
std::vector<Style> getFontStyles();
Rect sizeText(const std::string& Text) throw (ErrorViewer);
Rect sizeUTF8(const std::string& Text) throw (ErrorViewer);
Surface renderText(std::string Text, RGBA fg);
Surface renderTextWrapped(std::string Text, RGBA fg, int WrapLength);
@ -392,23 +410,24 @@ namespace MiniEngine
Texture renderUTF8Wrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength);
Texture renderUTF8Shaded(Renderer rnd, std::string Text, RGBA fg, RGBA bg);
Texture renderUTF8Solid(Renderer rnd, std::string Text, RGBA fg);
void release();
protected:
template<typename... Args>
void _setFontStyle(Style style,Args&&... args)
void _setFontStyle(int& fontcalc,Style style,Args&&... args)
{
_internal_fontcalc|=_style_caster(style);
_setFontStyle(args...);
fontcalc|=_style_caster(style);
_setFontStyle(fontcalc,args...);
}
void _setFontStyle(Style style)
void _setFontStyle(int& fontcalc,Style style)
{
_internal_fontcalc|=_style_caster(style);
_real_setFontStyle(_internal_fontcalc);
fontcalc|=_style_caster(style);
_real_setFontStyle(fontcalc);
}
private:
void _real_setFontStyle(int);
int _style_caster(Style);
int _internal_fontcalc;
std::shared_ptr<TTF_Font> _font;
void _set(TTF_Font*);
@ -438,8 +457,12 @@ namespace MiniEngine
int load(const std::string& Filename);
int unload();
void* get(const std::string& FunctionName);
void release();
private:
void* _obj;
void* _get();
void _set(void*);
void _clear();
std::shared_ptr<void> _obj;
};
class SDLSystem
@ -491,7 +514,7 @@ namespace MiniEngine
template<typename VoidCallable,typename... Args>
Timer(Uint32 interval,VoidCallable&& vcallable,Args&&... args) : Timer()
{
auto realCall=[&](Uint32 ims)->Uint32{vcallable(ims,args...);return interval;};
auto realCall=[&,vcallable](Uint32 ims)->Uint32{vcallable(ims,args...);return ims;};
auto pfunc=new std::function<Uint32(Uint32 interval)>(realCall);
_real_timer_call(_global_timer_executor,interval,pfunc);
}
@ -500,7 +523,7 @@ namespace MiniEngine
template<typename Callable,typename... Args>
Timer(Callable&& callable,Uint32 interval,Args&&... args) : Timer()
{
auto realCall=[&](Uint32 ims)->Uint32{return callable(ims,args...);};
auto realCall=[&,callable](Uint32 ims)->Uint32{return callable(ims,args...);};
auto pfunc=new std::function<Uint32(Uint32 interval)>(realCall);
_real_timer_call(_global_timer_executor,interval,pfunc);
}

19
MiniEngine_Config.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#ifdef _MSC_VER
/// Visual Studio (VC++ Compiler)
/// VC++ does not implied C++ exception. Use this to ignore compile warning on this.
#pragma warning (disable:4290)
#define _COMPILER_LABLE 1
#else
/// CodeBlocks (MinGW Compiler)
#define _COMPILER_LABLE 2
#endif /// End of #ifdef _MSC_VER
#include <SDL2/SDL.h>
#undef main
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#define _DECL_DEPRECATED [[deprecated]]

View File

@ -1,7 +1,8 @@
#pragma once
#include "MiniEngine.h"
#include "MiniEngine_Config.h"
#include <map>
#include <list>
#include <functional>
typedef SDL_Event Event;
typedef decltype(Event::type) _SDLEventType_;

382
MiniEngine_Xml.cpp Normal file
View File

@ -0,0 +1,382 @@
#include "MiniEngine_Xml.h"
#include "rapidxml/rapidxml_print.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include <fstream>
namespace MiniEngine
{
namespace XML
{
void Attribute::_set(XAttr* pattr)
{
_pattr=pattr;
}
XAttr* Attribute::_get() const
{
return _pattr;
}
void Attribute::_clear()
{
_pattr=nullptr;
}
void Attribute::_setdoc(Document* pDoc)
{
_pdoc=pDoc;
}
Attribute::Attribute()
{
_pattr=nullptr;
_pdoc=nullptr;
}
Attribute::Attribute(XAttr* pAttr)
{
_pattr=pAttr;
_pdoc=nullptr;
}
std::string Attribute::getName() const
{
return std::string(getNameRaw());
}
std::string Attribute::getValue() const
{
return std::string(getValueRaw());
}
char* Attribute::getNameRaw() const
{
return _pattr->name();
}
char* Attribute::getValueRaw() const
{
return _pattr->value();
}
bool Attribute::hasPrevAttr() const
{
return _pattr->previous_attribute()!=nullptr;
}
bool Attribute::hasNextAttr() const
{
return _pattr->next_attribute()!=nullptr;
}
Attribute Attribute::getPrevAttr() const
{
return Attribute(_pattr->previous_attribute());
}
Attribute Attribute::getNextAttr() const
{
return Attribute(_pattr->next_attribute());
}
Node::Node()
{
_pnode=nullptr;
_pdoc=nullptr;
}
Node::Node(XNode* expNode)
{
_pnode=expNode;
_pdoc=nullptr;
}
void Node::_set(XNode* node)
{
_pnode=node;
}
XNode* Node::_get() const
{
return _pnode;
}
void Node::_clear()
{
_pnode=nullptr;
}
void Node::_setdoc(Document* pDoc)
{
_pdoc=pDoc;
}
std::string Node::getName() const
{
return std::string(getNameRaw());
}
std::string Node::getValue() const
{
return std::string(getValueRaw());
}
char* Node::getNameRaw() const
{
return _pnode->name();
}
char* Node::getValueRaw() const
{
return _pnode->value();
}
Node& Node::push_front(const Node& node)
{
_pnode->prepend_node(node._pnode);
return *this;
}
Node& Node::push_back(const Node& node)
{
_pnode->append_node(node._pnode);
return *this;
}
Node& Node::insert(const Node& where, const Node& val)
{
_pnode->insert_node(where._pnode,val._pnode);
return *this;
}
Node& Node::remove_first_node()
{
_pnode->remove_first_node();
return *this;
}
Node& Node::remove_last_node()
{
_pnode->remove_last_node();
return *this;
}
Node& Node::remove_node(const Node& todelete)
{
_pnode->remove_node(todelete._pnode);
return *this;
}
Node& Node::remove_all_node()
{
_pnode->remove_all_nodes();
return *this;
}
Node& Node::push_front(const Attribute& attr)
{
_pnode->prepend_attribute(attr._get());
return *this;
}
Node& Node::push_back(const Attribute& attr)
{
_pnode->append_attribute(attr._get());
return *this;
}
Node& Node::insert(const Attribute& where, const Attribute& val)
{
_pnode->insert_attribute(where._get(),val._get());
return *this;
}
Node& Node::remove_first_attr()
{
_pnode->remove_first_attribute();
return *this;
}
Node& Node::remove_last_attr()
{
_pnode->remove_last_attribute();
return *this;
}
Node& Node::remove_attr(const Attribute& todelete)
{
_pnode->remove_attribute(todelete._get());
return *this;
}
Node& Node::remove_all_attr()
{
_pnode->remove_all_attributes();
return *this;
}
bool Node::operator==(const Node& node)
{
return _pnode==node._pnode && _pdoc==node._pdoc;
}
bool Node::hasPrevNode() const
{
return _pnode->previous_sibling()!=nullptr;
}
bool Node::hasNextNode() const
{
return _pnode->next_sibling()!=nullptr;
}
bool Node::hasParentNode() const
{
return _pnode->parent()!=nullptr;
}
Node Node::getPrevNode() const
{
return Node(_pnode->previous_sibling());
}
Node Node::getNextNode() const
{
return Node(_pnode->next_sibling());
}
Node Node::getPrevNode(const std::string& name) const
{
return Node(_pnode->previous_sibling(name.c_str()));
}
Node Node::getNextNode(const std::string& name) const
{
return Node(_pnode->next_sibling(name.c_str()));
}
Node Node::getParentNode() const
{
return Node(_pnode->parent());
}
bool Node::valid()
{
return _pnode!=nullptr && _pdoc!=nullptr;
}
Node Node::getChild() const
{
return _pnode->first_node();
}
Node Node::getChild(const std::string& nodename) const
{
return _pnode->first_node(nodename.c_str());
}
Document::Document()
{
_is_ready=false;
}
Document::Document(const std::string& filename)
{
if(loadFrom(filename,false)!=0)
{
_is_ready=false;
}
else
{
_is_ready=true;
}
}
int Document::loadFrom(const std::string& filename, bool clearCurrent)
{
std::ifstream ifs(filename);
if(!ifs)
{
/// File Read Error.
return -1;
}
rapidxml::file<> infilereader(ifs);
if(clearCurrent)
{
_doc.clear();
}
_doc.parse<0>(infilereader.data());
return 0;
}
int Document::saveTo(const std::string& filename)
{
std::string ans;
rapidxml::print(std::back_inserter(ans),_doc,0);
std::ofstream ofs(filename);
if(!ofs) return -1;
ofs<<ans;
return 0;
}
bool Document::ready()
{
return _is_ready;
}
Node Document::newNode(const std::string& name,const std::string& value)
{
Node node;
node._set(_doc.allocate_node(rapidxml::node_type::node_element,
_allocate_string(name),_allocate_string(value)));
node._setdoc(this);
return node;
}
Attribute Document::newAttr(const std::string& name,const std::string& value)
{
Attribute attr;
attr._set(_doc.allocate_attribute(_allocate_string(name),_allocate_string(value)));
attr._setdoc(this);
return attr;
}
Node Document::cloneNode(const Node& node)
{
return Node(_doc.clone_node(node._get()));
}
void Document::clear()
{
return _doc.clear();
}
//protected
char* Document::_allocate_string(const std::string& str)
{
return _doc.allocate_string(str.c_str(),str.size());
}
//protected
char* Document::_allocate_string(const char* source,int sz)
{
return _doc.allocate_string(source,sz);
}
}/// End of namespace MiniEngine::XML
}/// End of namespace MiniEngine

129
MiniEngine_Xml.h Normal file
View File

@ -0,0 +1,129 @@
#pragma once
#include "rapidxml/rapidxml.hpp"
#include <string>
namespace MiniEngine
{
namespace XML
{
typedef rapidxml::xml_node<> XNode;
typedef rapidxml::xml_attribute<> XAttr;
typedef rapidxml::xml_document<> XDoc;
/// Fwd Decl
class Document;
class Attribute
{
public:
void _set(XAttr*);
XAttr* _get() const;
void _clear();
void _setdoc(Document*);
Attribute();
Attribute(XAttr*);
std::string getName() const;
std::string getValue() const;
char* getNameRaw() const;
char* getValueRaw() const;
bool hasPrevAttr() const;
bool hasNextAttr() const;
Attribute getPrevAttr() const;
Attribute getNextAttr() const;
Attribute getPrevAttr(const std::string& name) const;
Attribute getNextAttr(const std::string& name) const;
private:
XAttr* _pattr;
Document* _pdoc;
};
class Node
{
public:
void _set(XNode*);
XNode* _get() const;
void _clear();
void _setdoc(Document*);
Node();
Node(XNode*);
std::string getName() const;
std::string getValue() const;
char* getNameRaw() const;
char* getValueRaw() const;
Node& push_front(const Node&);
Node& push_back(const Node&);
Node& insert(const Node& where,const Node& val);
Node& remove_first_node();
Node& remove_last_node();
Node& remove_node(const Node& todelete);
Node& remove_all_node();
Node& push_front(const Attribute&);
Node& push_back(const Attribute&);
Node& insert(const Attribute& where,const Attribute& val);
Node& remove_first_attr();
Node& remove_last_attr();
Node& remove_attr(const Attribute& todelete);
Node& remove_all_attr();
bool operator == (const Node& node);
bool hasPrevNode() const;
bool hasNextNode() const;
bool hasParentNode() const;
Node getPrevNode() const;
Node getNextNode() const;
Node getParentNode() const;
Node getPrevNode(const std::string& name) const;
Node getNextNode(const std::string& name) const;
Node getChild() const;
Node getChild(const std::string& nodename) const;
bool valid();
private:
XNode* _pnode;
Document* _pdoc;
};
class Document
{
public:
Document();
Document(const std::string& filename);
int loadFrom(const std::string& filename,bool clearCurrent=true);
int saveTo(const std::string& filename);
bool ready();
Node newNode(const std::string& name,const std::string& value);
Attribute newAttr(const std::string& name,const std::string& value);
Node cloneNode(const Node&);
void clear();
protected:
char* _allocate_string(const std::string& str);
char* _allocate_string(const char* pstr,int sz);
private:
XDoc _doc;
bool _is_ready;
};
}/// End of namespace MiniEngine::XML
}/// End of namespace MiniEngine

View File

@ -28,10 +28,10 @@ namespace rapidxml
//! \cond internal
namespace internal
{
///////////////////////////////////////////////////////////////////////////
// Internal character operations
// Copy characters from given range to given output iterator
template<class OutIt, class Ch>
inline OutIt copy_chars(const Ch *begin, const Ch *end, OutIt out)
@ -40,7 +40,7 @@ namespace rapidxml
*out++ = *begin++;
return out;
}
// Copy characters from given range to given output iterator and expand
// characters into references (&lt; &gt; &apos; &quot; &amp;)
template<class OutIt, class Ch>
@ -59,17 +59,17 @@ namespace rapidxml
case Ch('<'):
*out++ = Ch('&'); *out++ = Ch('l'); *out++ = Ch('t'); *out++ = Ch(';');
break;
case Ch('>'):
case Ch('>'):
*out++ = Ch('&'); *out++ = Ch('g'); *out++ = Ch('t'); *out++ = Ch(';');
break;
case Ch('\''):
case Ch('\''):
*out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('p'); *out++ = Ch('o'); *out++ = Ch('s'); *out++ = Ch(';');
break;
case Ch('"'):
case Ch('"'):
*out++ = Ch('&'); *out++ = Ch('q'); *out++ = Ch('u'); *out++ = Ch('o'); *out++ = Ch('t'); *out++ = Ch(';');
break;
case Ch('&'):
*out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('m'); *out++ = Ch('p'); *out++ = Ch(';');
case Ch('&'):
*out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('m'); *out++ = Ch('p'); *out++ = Ch(';');
break;
default:
*out++ = *begin; // No expansion, copy character
@ -101,7 +101,41 @@ namespace rapidxml
///////////////////////////////////////////////////////////////////////////
// Internal printing operations
/// Forward Declaration : Fix Compile Bug in MinGW
template<class OutIt, class Ch>
inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_children(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_attributes(OutIt out, const xml_node<Ch> *node, int flags);
template<class OutIt, class Ch>
inline OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_cdata_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_declaration_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_comment_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_doctype_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_pi_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
// Print node
template<class OutIt, class Ch>
inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
@ -119,12 +153,12 @@ namespace rapidxml
case node_element:
out = print_element_node(out, node, flags, indent);
break;
// Data
case node_data:
out = print_data_node(out, node, flags, indent);
break;
// CDATA
case node_cdata:
out = print_cdata_node(out, node, flags, indent);
@ -139,7 +173,7 @@ namespace rapidxml
case node_comment:
out = print_comment_node(out, node, flags, indent);
break;
// Doctype
case node_doctype:
out = print_doctype_node(out, node, flags, indent);
@ -155,7 +189,7 @@ namespace rapidxml
assert(0);
break;
}
// If indenting not disabled, add line break after node
if (!(flags & print_no_indenting))
*out = Ch('\n'), ++out;
@ -163,8 +197,8 @@ namespace rapidxml
// Return modified iterator
return out;
}
// Print children of the node
// Print children of the node
template<class OutIt, class Ch>
inline OutIt print_children(OutIt out, const xml_node<Ch> *node, int flags, int indent)
{
@ -249,7 +283,7 @@ namespace rapidxml
*out = Ch('<'), ++out;
out = copy_chars(node->name(), node->name() + node->name_size(), out);
out = print_attributes(out, node, flags);
// If node is childless
if (node->value_size() == 0 && !node->first_node())
{
@ -308,11 +342,11 @@ namespace rapidxml
// Print attributes
out = print_attributes(out, node, flags);
// Print declaration end
*out = Ch('?'), ++out;
*out = Ch('>'), ++out;
return out;
}
@ -384,7 +418,7 @@ namespace rapidxml
//! \param node Node to be printed. Pass xml_document to print entire document.
//! \param flags Flags controlling how XML is printed.
//! \return Output iterator pointing to position immediately after last character of printed text.
template<class OutIt, class Ch>
template<class OutIt, class Ch>
inline OutIt print(OutIt out, const xml_node<Ch> &node, int flags = 0)
{
return internal::print_node(out, &node, flags, 0);
@ -397,7 +431,7 @@ namespace rapidxml
//! \param node Node to be printed. Pass xml_document to print entire document.
//! \param flags Flags controlling how XML is printed.
//! \return Output stream.
template<class Ch>
template<class Ch>
inline std::basic_ostream<Ch> &print(std::basic_ostream<Ch> &out, const xml_node<Ch> &node, int flags = 0)
{
print(std::ostream_iterator<Ch>(out), node, flags);
@ -408,7 +442,7 @@ namespace rapidxml
//! \param out Output stream to print to.
//! \param node Node to be printed.
//! \return Output stream.
template<class Ch>
template<class Ch>
inline std::basic_ostream<Ch> &operator <<(std::basic_ostream<Ch> &out, const xml_node<Ch> &node)
{
return print(out, node);