[DEV] update new insterface IO of etk

This commit is contained in:
Edouard DUPIN 2018-09-17 21:33:07 +02:00
parent 6af072cdfe
commit f7a78efba4
5 changed files with 88 additions and 56 deletions

View File

@ -6,7 +6,8 @@
#include <ejson/Document.hpp>
#include <ejson/debug.hpp>
#include <ejson/internal/Document.hpp>
#include <etk/os/FSNode.hpp>
#include <etk/path/fileSystem.hpp>
#include <etk/uri/uri.hpp>
ejson::Document::Document(ememory::SharedPtr<ejson::internal::Value> _internalValue) :
ejson::Object(_internalValue) {
@ -50,32 +51,65 @@ bool ejson::Document::generate(etk::String& _data) {
return static_cast<ejson::internal::Document*>(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<ejson::internal::Document*>(m_data.get())->load(_file);
return static_cast<ejson::internal::Document*>(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<ejson::internal::Document*>(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<ejson::internal::Document*>(m_data.get())->store(_file);
return static_cast<ejson::internal::Document*>(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<ejson::internal::Document*>(m_data.get())->store(_file+".tmp");
return static_cast<ejson::internal::Document*>(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<ejson::internal::Document*>(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<ejson::internal::Document*>(m_data.get())->store(_uri);
}
etk::Uri uriTmp = _uri;
uriTmp.setPath(uriTmp.getPath() + ".tmp");
bool done = static_cast<ejson::internal::Document*>(m_data.get())->store(uriTmp);
if (done == false) {
return false;
}
return etk::uri::move(uriTmp, _uri);
}
void ejson::Document::setDisplayError(bool _value){

View File

@ -11,6 +11,7 @@
#include <ejson/String.hpp>
#include <ejson/Array.hpp>
#include <ejson/Object.hpp>
#include <etk/uri/Uri.hpp>
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)

View File

@ -6,7 +6,6 @@
#include <ejson/internal/Document.hpp>
#include <ejson/debug.hpp>
#include <etk/os/FSNode.hpp>
#include <ejson/internal/Object.hpp>
#include <ejson/internal/Array.hpp>
@ -14,6 +13,8 @@
#include <ejson/internal/Null.hpp>
#include <ejson/internal/Number.hpp>
#include <ejson/internal/Boolean.hpp>
#include <etk/path/fileSystem.hpp>
#include <etk/uri/uri.hpp>
ememory::SharedPtr<ejson::internal::Document> ejson::internal::Document::create() {
return ememory::SharedPtr<ejson::internal::Document>(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<char> 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;
}

View File

@ -11,6 +11,7 @@
#include <ejson/internal/String.hpp>
#include <ejson/internal/Array.hpp>
#include <ejson/internal/Object.hpp>
#include <etk/uri/Uri.hpp>
namespace ejson {
namespace internal {
@ -30,33 +31,33 @@ namespace ejson {
static ememory::SharedPtr<Document> 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

View File

@ -11,7 +11,6 @@
#include <test-debug/debug.hpp>
#include <ejson/ejson.hpp>
#include <etest/etest.hpp>
#include <etk/os/FSNode.hpp>
int main(int argc, const char *argv[]) {