diff --git a/ejson/Document.cpp b/ejson/Document.cpp index 4f7b111..d3c391f 100644 --- a/ejson/Document.cpp +++ b/ejson/Document.cpp @@ -6,7 +6,8 @@ #include #include #include -#include +#include +#include ejson::Document::Document(ememory::SharedPtr _internalValue) : ejson::Object(_internalValue) { @@ -50,32 +51,65 @@ bool ejson::Document::generate(etk::String& _data) { return static_cast(m_data.get())->generate(_data); } -bool ejson::Document::load(const etk::String& _file) { +bool ejson::Document::load(const etk::Path& _path) { if (m_data == null) { EJSON_DEBUG("Can not load (null) ..."); return false; } - return static_cast(m_data.get())->load(_file); + return static_cast(m_data.get())->load(_path); } -bool ejson::Document::store(const etk::String& _file) { +bool ejson::Document::load(const etk::Uri& _uri) { + if (m_data == null) { + EJSON_DEBUG("Can not load (null) ..."); + return false; + } + return static_cast(m_data.get())->load(_uri); +} + +bool ejson::Document::store(const etk::Path& _path) { if (m_data == null) { EJSON_DEBUG("Can not store (null) ..."); return false; } - return static_cast(m_data.get())->store(_file); + return static_cast(m_data.get())->store(_path); } -bool ejson::Document::storeSafe(const etk::String& _file) { +bool ejson::Document::store(const etk::Uri& _uri) { if (m_data == null) { EJSON_DEBUG("Can not store (null) ..."); return false; } - bool done = static_cast(m_data.get())->store(_file+".tmp"); + return static_cast(m_data.get())->store(_uri); +} + +bool ejson::Document::storeSafe(const etk::Path& _path) { + if (m_data == null) { + EJSON_DEBUG("Can not store (null) ..."); + return false; + } + bool done = static_cast(m_data.get())->store(_path+".tmp"); if (done == false) { return false; } - return etk::FSNodeMove(_file+".tmp", _file); + return etk::path::move(_path+".tmp", _path); +} + +bool ejson::Document::storeSafe(const etk::Uri& _uri) { + if (m_data == null) { + EJSON_DEBUG("Can not store (null) ..."); + return false; + } + if (etk::uri::canMove(_uri) == false) { + return static_cast(m_data.get())->store(_uri); + } + etk::Uri uriTmp = _uri; + uriTmp.setPath(uriTmp.getPath() + ".tmp"); + bool done = static_cast(m_data.get())->store(uriTmp); + if (done == false) { + return false; + } + return etk::uri::move(uriTmp, _uri); } void ejson::Document::setDisplayError(bool _value){ diff --git a/ejson/Document.hpp b/ejson/Document.hpp index 2c5977c..53bfe49 100644 --- a/ejson/Document.hpp +++ b/ejson/Document.hpp @@ -11,6 +11,7 @@ #include #include #include +#include namespace ejson { /** @@ -54,25 +55,31 @@ namespace ejson { bool generate(etk::String& _data); /** * @brief Load the file that might contain the Json - * @param[in] _file Filename of the Json (compatible with etk FSNode naming) + * @param[in] _path/_uri Path/URI of the json * @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 Json in the file - * @param[in] _file Filename of the Json (compatible with etk FSNode naming) + * @param[in] _path/_uri Path/URI of the json * @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 Store the Json in the file (safe mode mean that the file is store in a second file xxx.tmp and moved in the file xxx (only one mode to be really safe with filesystem ...) - * @param[in] _file Filename of the Json (compatible with etk FSNode naming) + * @param[in] _path/_uri Path/URI of the json * @return false : An error occured * @return true : Parsing is OK */ - bool storeSafe(const etk::String& _file); + bool storeSafe(const etk::Path& _path); + /// @previous + bool storeSafe(const etk::Uri& _uri); /** * @brief Set the display of the error when detected. * @param[in] _value true: display error, false not display error (get it at end) diff --git a/ejson/internal/Document.cpp b/ejson/internal/Document.cpp index 20fa24d..324f1ad 100644 --- a/ejson/internal/Document.cpp +++ b/ejson/internal/Document.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include @@ -14,6 +13,8 @@ #include #include #include +#include +#include ememory::SharedPtr ejson::internal::Document::create() { return ememory::SharedPtr(ETK_NEW(ejson::internal::Document)); @@ -46,56 +47,46 @@ bool ejson::internal::Document::generate(etk::String& _data) { return iGenerate(_data,0); } -bool ejson::internal::Document::load(const etk::String& _file) { - // Start loading the XML : - EJSON_VERBOSE("open file (xml) \"" << _file << "\""); +bool ejson::internal::Document::load(const etk::Uri& _uri) { + // Start loading the json : + EJSON_VERBOSE("open file (json) " << _uri); clear(); - etk::FSNode tmpFile(_file); - if (false == tmpFile.exist()) { - EJSON_ERROR("File Does not exist : " << _file); + auto fileIo = etk::uri::get(_uri); + if (fileIo == null) { + EJSON_ERROR("File Does not exist : " << _uri); return false; } - int64_t fileSize = tmpFile.fileSize(); - if (0 == fileSize) { - EJSON_ERROR("This file is empty : " << _file); + if (fileIo->open(etk::io::OpenMode::Read) == false) { + EJSON_ERROR("Can not open (r) the file : " << _uri); return false; } - if (false == tmpFile.fileOpenRead()) { - EJSON_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(); - - etk::String tmpDataUnicode(&fileBuffer[0]); + fileIo->close(); // parse the data : bool ret = parse(tmpDataUnicode); //Display(); return ret; } -bool ejson::internal::Document::store(const etk::String& _file) { +bool ejson::internal::Document::store(const etk::Uri& _uri) { etk::String createData; - if (false == generate(createData)) { - EJSON_ERROR("Error while creating the XML : " << _file); + if (generate(createData) == false) { + EJSON_ERROR("Error while creating the JSON : " << _uri); return false; } - etk::FSNode tmpFile(_file); - if (false == tmpFile.fileOpenWrite()) { - EJSON_ERROR("Can not open (w) the file : " << _file); + auto fileIo = etk::uri::get(_uri); + if (fileIo == null) { + EJSON_ERROR("Can not create the uri: " << _uri); return false; } - if (tmpFile.fileWrite((char*)createData.c_str(), sizeof(char), createData.size()) != (int32_t)createData.size()) { - EJSON_ERROR("Error while writing output XML file : " << _file); - tmpFile.fileClose(); + if (fileIo->open(etk::io::OpenMode::Write) == false) { + EJSON_ERROR("Can not open (r) the file : " << _uri); return false; } - tmpFile.fileClose(); + fileIo->fileWriteAll(createData); + fileIo->close(); return true; } diff --git a/ejson/internal/Document.hpp b/ejson/internal/Document.hpp index 0584dca..c2617c2 100644 --- a/ejson/internal/Document.hpp +++ b/ejson/internal/Document.hpp @@ -11,6 +11,7 @@ #include #include #include +#include namespace ejson { namespace internal { @@ -30,33 +31,33 @@ namespace ejson { static ememory::SharedPtr create(); public: /** - * @brief parse a string that contain an XML + * @brief parse a string that contain an json * @param[in] _data Data to parse * @return false : An error occured * @return true : Parsing is OK */ bool parse(const etk::String& _data); /** - * @brief generate a string that contain the created XML - * @param[out] _data Data where the xml is stored + * @brief generate a string that contain the created json + * @param[out] _data Data where the json is stored * @return false : An error occured * @return true : Parsing is OK */ 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) + * @brief Load the file that might contain the json + * @param[in] _uri URI of the json * @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) + * @brief Store the json in the file + * @param[in] _uri URI of the json * @return false : An error occured * @return true : Parsing is OK */ - bool store(const etk::String& _file); + bool store(const etk::Uri& _uri); private: bool m_writeErrorWhenDetexted; //!< Flag to know if we need to display error when they are detected etk::String m_comment; //!< Error comment diff --git a/test/test.cpp b/test/test.cpp index 0e539ca..f3a19c8 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -11,7 +11,6 @@ #include #include #include -#include int main(int argc, const char *argv[]) {