From 232fb530db8c23e2afe3cdc0a9ffc1a1741eb2ac Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 18 Sep 2018 22:48:14 +0200 Subject: [PATCH] [DEBUG] add missing uri --- etk/uri/uri.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++++++++ etk/uri/uri.hpp | 58 +++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 etk/uri/uri.cpp create mode 100644 etk/uri/uri.hpp diff --git a/etk/uri/uri.cpp b/etk/uri/uri.cpp new file mode 100644 index 0000000..c67b820 --- /dev/null +++ b/etk/uri/uri.cpp @@ -0,0 +1,104 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2018, Edouard DUPIN, all right reserved + * @license MPL v2.0 (see license file) + */ +#include +#include +#include +#include +#include +#include + +namespace etk { + namespace uri { + namespace provider { + etk::Map>& getProviders(); + } + } +} + +bool etk::uri::exist(const etk::Uri& _uri) { + etk::String scheme = _uri.getScheme(); + if (scheme.empty() == true) { + scheme = "RAW"; + } + if (etk::uri::provider::getProviders().exist(scheme) == false) { + return false; + } + return etk::uri::provider::getProviders()[scheme]->exist(_uri); +} + +etk::Vector etk::uri::list(const etk::Uri& _uri) { + etk::String scheme = _uri.getScheme(); + if (scheme.empty() == true) { + scheme = "RAW"; + } + if (etk::uri::provider::getProviders().exist(scheme) == false) { + return etk::Vector(); + } + return etk::uri::provider::getProviders()[scheme]->list(_uri); +} + +ememory::SharedPtr etk::uri::get(const etk::Uri& _uri) { + etk::String scheme = _uri.getScheme(); + if (scheme.empty() == true) { + scheme = "RAW"; + } + if (etk::uri::provider::getProviders().exist(scheme) == false) { + TK_ERROR("lklklk " << scheme << " " << _uri << " " << etk::uri::provider::getProviders().getKeys()); + return null; + } + if (_uri.getPath().getString().size() == 0) { + TK_WARNING("uri:" << _uri << " Have NO path"); + } + return etk::uri::provider::getProviders()[scheme]->create(_uri); +} + +bool etk::uri::canMove(const etk::Uri& _uri) { + etk::String scheme = _uri.getScheme(); + if (scheme.empty() == true) { + scheme = "RAW"; + } + if (etk::uri::provider::getProviders().exist(scheme) == false) { + return false; + } + return etk::uri::provider::getProviders()[scheme]->canMove(); +} + +bool etk::uri::move(const etk::Uri& _uriSource, const etk::Uri& _uriDestination) { + etk::String scheme = _uriSource.getScheme(); + if (scheme.empty() == true) { + scheme = "RAW"; + } + etk::String scheme2 = _uriDestination.getScheme(); + if (scheme2.empty() == true) { + scheme2 = "RAW"; + } + if (scheme != scheme2) { + TK_ERROR("Can not move 2 uri between 2 model of resource... " << _uriSource << " => " << _uriDestination); + return false; + } + if (etk::uri::provider::getProviders().exist(scheme) == false) { + return false; + } + if (etk::uri::provider::getProviders()[scheme]->canMove() == false) { + return false; + } + return etk::uri::provider::getProviders()[scheme]->move(_uriSource, _uriDestination); +} + +bool etk::uri::writeAll(const etk::Uri& _uri, const etk::String& _data) { + auto fileIo = etk::uri::get(_uri); + if (fileIo == null) { + TK_ERROR("Can not create the uri: " << _uri); + return false; + } + if (fileIo->open(etk::io::OpenMode::Write) == false) { + TK_ERROR("Can not open (w) the file : " << _uri); + return false; + } + fileIo->fileWriteAll(_data); + fileIo->close(); + return true; +} \ No newline at end of file diff --git a/etk/uri/uri.hpp b/etk/uri/uri.hpp new file mode 100644 index 0000000..5a5b714 --- /dev/null +++ b/etk/uri/uri.hpp @@ -0,0 +1,58 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2018, Edouard DUPIN, all right reserved + * @license MPL v2.0 (see license file) + */ +#pragma once + +#include + +#include +#include +#include +#include +#include +#include + +namespace etk { + namespace uri { + /** + * @brief Check if an URI really exist. + * @param[in] _uri Uri to check. + * @return true if exist, false ortherwise + */ + bool exist(const etk::Uri& _uri); + /** + * @brief Get the list of sub-element in the Uri + * @param[in] _uri Uri requested as parent. + * @return list of sub-uri + */ + etk::Vector list(const etk::Uri& _uri); + /** + * @brief Get an IO interface with a specific URI + * @param[in] _uri Data interface requested + * @return The interface requested. + */ + ememory::SharedPtr get(const etk::Uri& _uri); + /** + * @brief Check if an URI Can be moved. + * @param[in] _uri Uri to check. + * @return true if it is possible, false ortherwise + */ + bool canMove(const etk::Uri& _uri); + /** + * @brief Move an element for a source to a destination. + * @param[in] _uriSource Source Uri. + * @param[in] _uriDestination Destination Uri. + * @return true if moved, false ortherwise + */ + bool move(const etk::Uri& _uriSource, const etk::Uri& _uriDestination); + /** + * @brief Write all a string in a uri (if possible) + * @param[in] _uri Uri destination. + * @param[in] _data Data to write. + * @return true All data are write, false otherwise. + */ + bool writeAll(const etk::Uri& _uri, const etk::String& _data); + } +}