[DEV] update new insterface IO of etk

This commit is contained in:
Edouard DUPIN 2018-09-17 21:35:27 +02:00
parent 7ae4ea7215
commit be5702b029
5 changed files with 55 additions and 46 deletions

View File

@ -50,20 +50,36 @@ bool exml::Document::generate(etk::String& _data) {
return static_cast<exml::internal::Document*>(m_data.get())->generate(_data); return static_cast<exml::internal::Document*>(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) { if (m_data == null) {
EXML_DEBUG("Can not load (null) ..."); EXML_DEBUG("Can not load (null) ...");
return false; return false;
} }
return static_cast<exml::internal::Document*>(m_data.get())->load(_file); return static_cast<exml::internal::Document*>(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<exml::internal::Document*>(m_data.get())->load(_uri);
}
bool exml::Document::store(const etk::Path& _path) {
if (m_data == null) { if (m_data == null) {
EXML_DEBUG("Can not store (null) ..."); EXML_DEBUG("Can not store (null) ...");
return false; return false;
} }
return static_cast<exml::internal::Document*>(m_data.get())->store(_file); return static_cast<exml::internal::Document*>(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<exml::internal::Document*>(m_data.get())->store(_uri);
} }
void exml::Document::display() { void exml::Document::display() {

View File

@ -7,6 +7,7 @@
#include <exml/Element.hpp> #include <exml/Element.hpp>
#include <etk/Vector.hpp> #include <etk/Vector.hpp>
#include <etk/uri/uri.hpp>
namespace exml { namespace exml {
/** /**
@ -61,18 +62,22 @@ namespace exml {
bool generate(etk::String& _data); bool generate(etk::String& _data);
/** /**
* @brief Load the file that might contain the xml * @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 false : An error occured
* @return true : Parsing is OK * @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 * @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 false : An error occured
* @return true : Parsing is OK * @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 * @brief Display the Document on console
*/ */

View File

@ -6,7 +6,7 @@
#include <exml/internal/Document.hpp> #include <exml/internal/Document.hpp>
#include <exml/debug.hpp> #include <exml/debug.hpp>
#include <etk/os/FSNode.hpp> #include <etk/uri/provider/provider.hpp>
ememory::SharedPtr<exml::internal::Document> exml::internal::Document::create() { ememory::SharedPtr<exml::internal::Document> exml::internal::Document::create() {
return ememory::SharedPtr<exml::internal::Document>(ETK_NEW(exml::internal::Document)); return ememory::SharedPtr<exml::internal::Document>(ETK_NEW(exml::internal::Document));
@ -46,57 +46,46 @@ bool exml::internal::Document::generate(etk::String& _data) {
return iGenerate(_data,0); 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 : // Start loading the XML :
EXML_VERBOSE("open file (xml) \"" << _file << "\""); EXML_VERBOSE("open file (xml) " << _uri);
clear(); clear();
etk::FSNode tmpFile(_file); auto fileIo = etk::uri::get(_uri);
if (tmpFile.exist() == false) { if (fileIo == null) {
EXML_ERROR("File Does not exist : " << _file); EXML_ERROR("File Does not exist : " << _uri);
return false; return false;
} }
int64_t fileSize = tmpFile.fileSize(); if (fileIo->open(etk::io::OpenMode::Read) == false) {
if (fileSize == 0) { EXML_ERROR("Can not open (r) the file : " << _uri);
EXML_ERROR("This file is empty : " << _file);
return false; return false;
} }
if (tmpFile.fileOpenRead() == false) { // load data from the file:
EXML_ERROR("Can not open (r) the file : " << _file); etk::String tmpDataUnicode = fileIo->readAllString();
return false;
}
// allocate data
etk::Vector<char> fileBuffer;
fileBuffer.resize(fileSize+5, 0);
// load data from the file :
tmpFile.fileRead(&fileBuffer[0], 1, fileSize);
// close the file: // close the file:
tmpFile.fileClose(); fileIo->close();
// parse the data:
// convert in UTF8 :
etk::String tmpDataUnicode(&fileBuffer[0]);
// parse the data :
bool ret = parse(tmpDataUnicode); bool ret = parse(tmpDataUnicode);
//Display(); //Display();
return ret; return ret;
} }
bool exml::internal::Document::store(const etk::String& _file) { bool exml::internal::Document::store(const etk::Uri& _uri) {
etk::String createData; etk::String createData;
if (generate(createData) == false) { if (generate(createData) == false) {
EXML_ERROR("Error while creating the XML : " << _file); EXML_ERROR("Error while creating the XML: " << _uri);
return false; return false;
} }
etk::FSNode tmpFile(_file); auto fileIo = etk::uri::get(_uri);
if (tmpFile.fileOpenWrite() == false) { if (fileIo == null) {
EXML_ERROR("Can not open (w) the file : " << _file); EXML_ERROR("Can not create the uri: " << _uri);
return false; return false;
} }
if (tmpFile.fileWrite((char*)createData.c_str(), sizeof(char), createData.size()) != (int64_t)createData.size()) { if (fileIo->open(etk::io::OpenMode::Write) == false) {
EXML_ERROR("Error while writing output XML file : " << _file); EXML_ERROR("Can not open (r) the file : " << _uri);
tmpFile.fileClose();
return false; return false;
} }
tmpFile.fileClose(); fileIo->fileWriteAll(createData);
fileIo->close();
return true; return true;
} }

View File

@ -7,6 +7,7 @@
#include <exml/internal/Element.hpp> #include <exml/internal/Element.hpp>
#include <etk/Vector.hpp> #include <etk/Vector.hpp>
#include <etk/uri/uri.hpp>
namespace exml { namespace exml {
namespace internal { namespace internal {
@ -59,18 +60,18 @@ namespace exml {
bool generate(etk::String& _data); bool generate(etk::String& _data);
/** /**
* @brief Load the file that might contain the xml * @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 false : An error occured
* @return true : Parsing is OK * @return true : Parsing is OK
*/ */
bool load(const etk::String& _file); bool load(const etk::Uri& _uri);
/** /**
* @brief Store the Xml in the file * @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 false : An error occured
* @return true : Parsing is OK * @return true : Parsing is OK
*/ */
bool store(const etk::String& _file); bool store(const etk::Uri& _uri);
/** /**
* @brief Display the Document on console * @brief Display the Document on console
*/ */

View File

@ -6,7 +6,6 @@
#include <test-debug/debug.hpp> #include <test-debug/debug.hpp>
#include <etest/etest.hpp> #include <etest/etest.hpp>
#include <etk/os/FSNode.hpp>
#include <etk/etk.hpp> #include <etk/etk.hpp>
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
@ -22,6 +21,5 @@ int main(int argc, const char *argv[]) {
exit(0); exit(0);
} }
} }
etk::initDefaultFolder("exml_test");
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
} }