From be5702b0298d9825c171f29dc7e2b5615013b8b2 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Mon, 17 Sep 2018 21:35:27 +0200 Subject: [PATCH] [DEV] update new insterface IO of etk --- exml/Document.cpp | 24 ++++++++++++++--- exml/Document.hpp | 13 +++++++--- exml/internal/Document.cpp | 53 +++++++++++++++----------------------- exml/internal/Document.hpp | 9 ++++--- test/main.cpp | 2 -- 5 files changed, 55 insertions(+), 46 deletions(-) diff --git a/exml/Document.cpp b/exml/Document.cpp index 2c86a58..6ca7567 100644 --- a/exml/Document.cpp +++ b/exml/Document.cpp @@ -50,20 +50,36 @@ bool exml::Document::generate(etk::String& _data) { return static_cast(m_data.get())->generate(_data); } -bool exml::Document::load(const etk::String& _file) { +bool exml::Document::load(const etk::Path& _path) { if (m_data == null) { EXML_DEBUG("Can not load (null) ..."); return false; } - return static_cast(m_data.get())->load(_file); + return static_cast(m_data.get())->load(_path); } -bool exml::Document::store(const etk::String& _file) { +bool exml::Document::load(const etk::Uri& _uri) { + if (m_data == null) { + EXML_DEBUG("Can not load (null) ..."); + return false; + } + return static_cast(m_data.get())->load(_uri); +} + +bool exml::Document::store(const etk::Path& _path) { if (m_data == null) { EXML_DEBUG("Can not store (null) ..."); return false; } - return static_cast(m_data.get())->store(_file); + return static_cast(m_data.get())->store(_path); +} + +bool exml::Document::store(const etk::Uri& _uri) { + if (m_data == null) { + EXML_DEBUG("Can not store (null) ..."); + return false; + } + return static_cast(m_data.get())->store(_uri); } void exml::Document::display() { diff --git a/exml/Document.hpp b/exml/Document.hpp index fb6fdb7..d3e598c 100644 --- a/exml/Document.hpp +++ b/exml/Document.hpp @@ -7,6 +7,7 @@ #include #include +#include namespace exml { /** @@ -61,18 +62,22 @@ namespace exml { bool generate(etk::String& _data); /** * @brief Load the file that might contain the xml - * @param[in] _file Filename of the xml (compatible with etk FSNode naming) + * @param[in] _path/_uri Path/URI of the xml * @return false : An error occured * @return true : Parsing is OK */ - bool load(const etk::String& _file); + bool load(const etk::Path& _path); + /// @previous + bool load(const etk::Uri& _uri); /** * @brief Store the Xml in the file - * @param[in] _file Filename of the xml (compatible with etk FSNode naming) + * @param[in] _path/_uri Path/URI of the xml * @return false : An error occured * @return true : Parsing is OK */ - bool store(const etk::String& _file); + bool store(const etk::Path& _path); + /// @previous + bool store(const etk::Uri& _uri); /** * @brief Display the Document on console */ diff --git a/exml/internal/Document.cpp b/exml/internal/Document.cpp index 73194e5..c5d1a85 100644 --- a/exml/internal/Document.cpp +++ b/exml/internal/Document.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include ememory::SharedPtr exml::internal::Document::create() { return ememory::SharedPtr(ETK_NEW(exml::internal::Document)); @@ -46,57 +46,46 @@ bool exml::internal::Document::generate(etk::String& _data) { return iGenerate(_data,0); } -bool exml::internal::Document::load(const etk::String& _file) { +bool exml::internal::Document::load(const etk::Uri& _uri) { // Start loading the XML : - EXML_VERBOSE("open file (xml) \"" << _file << "\""); + EXML_VERBOSE("open file (xml) " << _uri); clear(); - etk::FSNode tmpFile(_file); - if (tmpFile.exist() == false) { - EXML_ERROR("File Does not exist : " << _file); + auto fileIo = etk::uri::get(_uri); + if (fileIo == null) { + EXML_ERROR("File Does not exist : " << _uri); return false; } - int64_t fileSize = tmpFile.fileSize(); - if (fileSize == 0) { - EXML_ERROR("This file is empty : " << _file); + if (fileIo->open(etk::io::OpenMode::Read) == false) { + EXML_ERROR("Can not open (r) the file : " << _uri); return false; } - if (tmpFile.fileOpenRead() == false) { - EXML_ERROR("Can not open (r) the file : " << _file); - return false; - } - // allocate data - etk::Vector fileBuffer; - fileBuffer.resize(fileSize+5, 0); - // load data from the file : - tmpFile.fileRead(&fileBuffer[0], 1, fileSize); + // load data from the file: + etk::String tmpDataUnicode = fileIo->readAllString(); // close the file: - tmpFile.fileClose(); - - // convert in UTF8 : - etk::String tmpDataUnicode(&fileBuffer[0]); - // parse the data : + fileIo->close(); + // parse the data: bool ret = parse(tmpDataUnicode); //Display(); return ret; } -bool exml::internal::Document::store(const etk::String& _file) { +bool exml::internal::Document::store(const etk::Uri& _uri) { etk::String createData; if (generate(createData) == false) { - EXML_ERROR("Error while creating the XML : " << _file); + EXML_ERROR("Error while creating the XML: " << _uri); return false; } - etk::FSNode tmpFile(_file); - if (tmpFile.fileOpenWrite() == false) { - EXML_ERROR("Can not open (w) the file : " << _file); + auto fileIo = etk::uri::get(_uri); + if (fileIo == null) { + EXML_ERROR("Can not create the uri: " << _uri); return false; } - if (tmpFile.fileWrite((char*)createData.c_str(), sizeof(char), createData.size()) != (int64_t)createData.size()) { - EXML_ERROR("Error while writing output XML file : " << _file); - tmpFile.fileClose(); + if (fileIo->open(etk::io::OpenMode::Write) == false) { + EXML_ERROR("Can not open (r) the file : " << _uri); return false; } - tmpFile.fileClose(); + fileIo->fileWriteAll(createData); + fileIo->close(); return true; } diff --git a/exml/internal/Document.hpp b/exml/internal/Document.hpp index c663f39..c128066 100644 --- a/exml/internal/Document.hpp +++ b/exml/internal/Document.hpp @@ -7,6 +7,7 @@ #include #include +#include namespace exml { namespace internal { @@ -59,18 +60,18 @@ namespace exml { bool generate(etk::String& _data); /** * @brief Load the file that might contain the xml - * @param[in] _file Filename of the xml (compatible with etk FSNode naming) + * @param[in] _uri URI of the xml * @return false : An error occured * @return true : Parsing is OK */ - bool load(const etk::String& _file); + bool load(const etk::Uri& _uri); /** * @brief Store the Xml in the file - * @param[in] _file Filename of the xml (compatible with etk FSNode naming) + * @param[in] _uri URI of the xml * @return false : An error occured * @return true : Parsing is OK */ - bool store(const etk::String& _file); + bool store(const etk::Uri& _uri); /** * @brief Display the Document on console */ diff --git a/test/main.cpp b/test/main.cpp index f20d166..0617ef9 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -6,7 +6,6 @@ #include #include -#include #include int main(int argc, const char *argv[]) { @@ -22,6 +21,5 @@ int main(int argc, const char *argv[]) { exit(0); } } - etk::initDefaultFolder("exml_test"); return RUN_ALL_TESTS(); }