[DEV] add safe store API

This commit is contained in:
Edouard DUPIN 2016-05-26 21:00:04 +02:00
parent b37e93ca37
commit 3ac477a1df
2 changed files with 27 additions and 7 deletions

View File

@ -6,6 +6,7 @@
#include <ejson/Document.h> #include <ejson/Document.h>
#include <ejson/debug.h> #include <ejson/debug.h>
#include <ejson/internal/Document.h> #include <ejson/internal/Document.h>
#include <etk/os/FSNode.h>
ejson::Document::Document(ememory::SharedPtr<ejson::internal::Value> _internalValue) : ejson::Document::Document(ememory::SharedPtr<ejson::internal::Value> _internalValue) :
ejson::Object(_internalValue) { ejson::Object(_internalValue) {
@ -65,6 +66,18 @@ bool ejson::Document::store(const std::string& _file) {
return static_cast<ejson::internal::Document*>(m_data.get())->store(_file); return static_cast<ejson::internal::Document*>(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<ejson::internal::Document*>(m_data.get())->store(_file+".tmp");
if (done == false) {
return false;
}
return etk::FSNodeMove(_file+".tmp", _file);
}
void ejson::Document::setDisplayError(bool _value){ void ejson::Document::setDisplayError(bool _value){
if (m_data == nullptr) { if (m_data == nullptr) {
EJSON_ERROR("Can not setDisplayError (nullptr) ..."); EJSON_ERROR("Can not setDisplayError (nullptr) ...");

View File

@ -39,33 +39,40 @@ namespace ejson {
*/ */
ejson::Document& operator= (const ejson::Document& _obj); 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 * @param[in] _data Data to parse
* @return false : An error occured * @return false : An error occured
* @return true : Parsing is OK * @return true : Parsing is OK
*/ */
bool parse(const std::string& _data); bool parse(const std::string& _data);
/** /**
* @brief generate a string that contain the created XML * @brief generate a string that contain the created Json
* @param[out] _data Data where the xml is stored * @param[out] _data Data where the Json is stored
* @return false : An error occured * @return false : An error occured
* @return true : Parsing is OK * @return true : Parsing is OK
*/ */
bool generate(std::string& _data); bool generate(std::string& _data);
/** /**
* @brief Load the file that might contain the xml * @brief Load the file that might contain the Json
* @param[in] _file Filename of the xml (compatible with etk FSNode naming) * @param[in] _file Filename of the Json (compatible with etk FSNode naming)
* @return false : An error occured * @return false : An error occured
* @return true : Parsing is OK * @return true : Parsing is OK
*/ */
bool load(const std::string& _file); bool load(const std::string& _file);
/** /**
* @brief Store the Xml in the file * @brief Store the Json in the file
* @param[in] _file Filename of the xml (compatible with etk FSNode naming) * @param[in] _file Filename of the Json (compatible with etk FSNode naming)
* @return false : An error occured * @return false : An error occured
* @return true : Parsing is OK * @return true : Parsing is OK
*/ */
bool store(const std::string& _file); 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. * @brief Set the display of the error when detected.
* @param[in] _value true: display error, false not display error (get it at end) * @param[in] _value true: display error, false not display error (get it at end)