diff --git a/ejson/Document.cpp b/ejson/Document.cpp index 52e8bb1..dd7f6ca 100644 --- a/ejson/Document.cpp +++ b/ejson/Document.cpp @@ -6,6 +6,7 @@ #include #include #include +#include ejson::Document::Document(ememory::SharedPtr _internalValue) : ejson::Object(_internalValue) { @@ -65,6 +66,18 @@ bool ejson::Document::store(const std::string& _file) { return static_cast(m_data.get())->store(_file); } +bool ejson::Document::storeSafe(const std::string& _file) { + if (m_data == nullptr) { + EJSON_ERROR("Can not store (nullptr) ..."); + return false; + } + bool done = static_cast(m_data.get())->store(_file+".tmp"); + if (done == false) { + return false; + } + return etk::FSNodeMove(_file+".tmp", _file); +} + void ejson::Document::setDisplayError(bool _value){ if (m_data == nullptr) { EJSON_ERROR("Can not setDisplayError (nullptr) ..."); diff --git a/ejson/Document.h b/ejson/Document.h index 4dae191..a0ae522 100644 --- a/ejson/Document.h +++ b/ejson/Document.h @@ -39,33 +39,40 @@ namespace ejson { */ ejson::Document& operator= (const ejson::Document& _obj); /** - * @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 std::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(std::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] _file Filename of the Json (compatible with etk FSNode naming) * @return false : An error occured * @return true : Parsing is OK */ bool load(const std::string& _file); /** - * @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] _file Filename of the Json (compatible with etk FSNode naming) * @return false : An error occured * @return true : Parsing is OK */ bool store(const std::string& _file); + /** + * @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) + * @return false : An error occured + * @return true : Parsing is OK + */ + bool storeSafe(const std::string& _file); /** * @brief Set the display of the error when detected. * @param[in] _value true: display error, false not display error (get it at end)