[DEV] normalize API of etk::Path

This commit is contained in:
Edouard DUPIN 2018-09-03 04:29:24 +02:00
parent f6ffdb9b25
commit ea53e1db72
7 changed files with 152 additions and 68 deletions

View File

@ -112,26 +112,40 @@ static etk::String simplifyPath(etk::String _input) {
return _input;
}
static etk::String convertToWindows(etk::String _path) {
_path.replace("/", "\\");
if ( _path.size() > 3
&& _path[0] == '/'
&& _path[2] == '/') {
_path[0] = _path[1];
_path[0] = ':';
}
return _path;
}
static etk::String convertToUnix(etk::String _path) {
_path.replace("\\", "/");
if ( _path.size() > 3
&& _path[1] == ':'
&& _path[2] == '/') {
#ifndef __TARGET_OS__Windows
TK_WARNING("Path name have a windows form: '" << _path << "' c:/ but not a windwos platform");
#endif
if ( _path[0] >= 'A'
&& _path[0] <= 'Z') {
_path[1] = _path[0] + 'a' - 'A';
} else {
_path[1] = _path[0];
}
_path[0] = '/';
}
return _path;
}
static etk::String parsePath(etk::String _path) {
etk::String out = _path;
TK_DBG_MODE("1 : Set Name : '" << out << "'");
// Replace all time to prevent Windows user error when port on Unix
out.replace("\\", "/");
if ( out.size() > 3
&& out[1] == ':'
&& out[2] == '/') {
#ifndef __TARGET_OS__Windows
TK_WARNING("Path name have a windows form: '" << _path << "' c:/ but not a windwos platform");
#endif
if ( out[0] >= 'A'
&& out[0] <= 'Z') {
out[1] = out[0] + 'a' - 'A';
} else {
out[1] = out[0];
}
out[0] = '/';
}
out = convertToUnix(out);
out = simplifyPath(out);
TK_DBG_MODE("3 : parse done : '" << _path << "' ==>\"" << out << "\"");
return out;
@ -147,22 +161,11 @@ etk::String etk::Path::getString() const {
}
etk::String etk::Path::getStringWindows() const {
etk::String out = m_data;
out.replace("/", "\\");
if ( out.size() > 3
&& out[0] == '/'
&& out[2] == '/') {
out[0] = out[1];
out[0] = ':';
}
etk::String out = getString();
out = convertToWindows(out);
return out;
}
etk::String etk::Path::getRelative() const {
// TODO : plouf ...
return "todo";
}
etk::String etk::Path::getNative() const {
#ifdef __TARGET_OS__Windows
return getStringWindows();
@ -171,6 +174,50 @@ etk::String etk::Path::getNative() const {
#endif
}
etk::String etk::Path::getRelative() const {
if (isRelative() == true) {
return m_data;
}
// TODO : plouf ...
return "todo";
}
etk::String etk::Path::getRelativeWindows() const {
etk::String out = getRelative();
out = convertToWindows(out);
return out;
}
etk::String etk::Path::getRelativeNative() const {
#ifdef __TARGET_OS__Windows
return getRelativeWindows();
#else
return getRelative();
#endif
}
etk::String etk::Path::getAbsolute() const {
if (isAbsolute() == true) {
return m_data;
}
// TODO : plouf ...
return "todo";
}
etk::String etk::Path::getAbsoluteWindows() const {
etk::String out = getAbsolute();
out = convertToWindows(out);
return out;
}
etk::String etk::Path::getAbsoluteNative() const {
#ifdef __TARGET_OS__Windows
return getAbsoluteWindows();
#else
return getAbsolute();
#endif
}
bool etk::Path::isRelative() const {
if ( m_data.size() >= 1
&& m_data[0] != '/') {
@ -215,7 +262,7 @@ bool etk::Path::operator!= (const etk::Path &_obj) const {
etk::Path etk::Path::operator/ (const etk::String & _element) const {
etk::Path tmp = *this;
tmp /= _element;
tmp /= etk::Path(_element);
return tmp;
}
@ -226,14 +273,7 @@ etk::Path etk::Path::operator/ (const etk::Path & _path) const {
}
etk::Path& etk::Path::operator/= (const etk::String & _element) {
if (_element.size() == 0) {
return *this;
}
if (_element[0] == '/') {
ETK_THROW_EXCEPTION(etk::exception::InvalidArgument("add path that is absolute"));
}
m_data += '/' + _element;
m_data = simplifyPath(m_data);
*this /= etk::Path(_element);
return *this;
}
@ -250,16 +290,27 @@ etk::Path& etk::Path::operator/= (const etk::Path & _path) {
}
etk::Path etk::Path::operator+ (const etk::String & _element) const {
etk::Path tmp = *this;
tmp += etk::Path(_element);
return tmp;
}
etk::Path etk::Path::operator+ (const etk::Path & _element) const {
etk::Path tmp = *this;
tmp += _element;
return tmp;
}
etk::Path& etk::Path::operator+= (const etk::String & _element) {
if (_element.size() == 0) {
*this += etk::Path(_element);
return *this;
}
etk::Path& etk::Path::operator+= (const etk::Path & _element) {
if (_element.m_data.size() == 0) {
return *this;
}
m_data += _element;
m_data += _element.m_data;
m_data = simplifyPath(m_data);
return *this;
}

View File

@ -30,25 +30,59 @@ namespace etk {
*/
Path(const etk::String& _value);
/**
* @brief Get the absolute path
* @return string like /home/userXXX/aaa/bbb/### or /c/userXXX/aaa/bbb/###
* @brief Get the set path (by the user)
* - /home/userXXX/aaa/bbb/###
* - /c/userXXX/aaa/bbb/###
* - aaa/bbb/###
*/
etk::String getString() const;
/**
* @brief Get the absolute path
* @return string like /home/userXXX/aaa/bbb/### or /c/userXXX/aaa/bbb/###
* @brief Get the set path (by the user) with a windows view
* @return string like:
* - \home\userXXX\aaa\bbb\###
* - c:\userXXX\aaa\bbb\###
* - aaa\bbb\###
*/
etk::String getStringWindows() const;
/**
* @brief Get the set path (by the user) with the local machine preference view
* @return string like:
* - /home/userXXX/aaa/bbb/###
* - c:\userXXX\aaa\bbb\###
* - aaa/bbb/###
* - aaa\bbb\###
*/
etk::String getNative() const;
/**
* @brief Get the relative path.
* @return string like ../../aaa/bbb/###
*/
etk::String getRelative() const;
/**
* @brief Get the absolute path
* @brief Get the relative path.
* @return string like ..\..\aaa\bbb\###
*/
etk::String getRelativeWindows() const;
/**
* @brief Get the absolute path (depend on the system)
* @return string like ../../aaa/bbb/### or ..\..\aaa\bbb\###
*/
etk::String getRelativeNative() const;
/**
* @brief Get the Absolute path.
* @return string like /home/userXXX/aaa/bbb/### or /c/userXXX/aaa/bbb/###
*/
etk::String getAbsolute() const;
/**
* @brief Get the Absolute path.
* @return string like \home\userXXX\aaa\bbb\### or c:\userXXX\aaa\bbb\###
*/
etk::String getAbsoluteWindows() const;
/**
* @brief Get the absolute path (depend on the system)
* @return string like /home/userXXX/aaa/bbb/### or c:\userXXX\aaa\bbb\###
*/
etk::String getNative() const;
etk::String getAbsoluteNative() const;
/**
* @brief Check if the path is relative or not.
* @return true The path is relative.

View File

@ -17,6 +17,12 @@ extern "C" {
#include <sys/stat.h>
#include <errno.h>
}
#include <unistd.h>
#include <stdlib.h>
#include <etk/io/File.hpp>
#include <etk/io/SeekMode.hpp>
namespace etk {
static int32_t mkdir(const char* _path, mode_t _mode) {
struct stat st;
@ -111,7 +117,7 @@ bool etk::fileSystem::remove(const etk::Path& _path) {
}
bool etk::fileSystem::removeDirectory(const etk::Path& _path) {
if( 0 != ::rmdir(_path1.getString().c_str()) ) {
if( 0 != ::rmdir(_path.getString().c_str()) ) {
if (ENOTEMPTY == errno) {
TK_ERROR("The Directory is not empty...");
}
@ -121,7 +127,7 @@ bool etk::fileSystem::removeDirectory(const etk::Path& _path) {
}
bool etk::fileSystem::removeFile(const etk::Path& _path) {
if (0 != unlink(_path1.getString().c_str()) ) {
if (0 != unlink(_path.getString().c_str()) ) {
return false;
}
return true;
@ -140,7 +146,7 @@ bool etk::fileSystem::touch(const etk::Path& _path) {
bool etk::fileSystem::exist(const etk::Path& _path) {
struct stat st;
int32_t status = 0;
if (stat(_path.get().c_str(), &st) != 0) {
if (stat(_path.getString().c_str(), &st) != 0) {
return false;
}
return true;
@ -150,7 +156,7 @@ uint64_t etk::fileSystem::fileSize(const etk::Path& _path) {
// Note : this is a proper methode to get the file size for Big files ... otherwithe the size is limited at 2^31 bytes
// tmpStat Buffer :
struct stat statProperty;
if (stat(_path.get().c_str(), &statProperty) == -1) {
if (stat(_path.getString().c_str(), &statProperty) == -1) {
//Normal case when the file does not exist ... ==> the it was in unknow mode ...
return 0;
}
@ -165,7 +171,7 @@ uint64_t etk::fileSystem::fileSize(const etk::Path& _path) {
bool etk::fileSystem::isDirectory(const etk::Path& _path) {
struct stat st;
int32_t status = 0;
if (stat(_path.get().c_str(), &st) != 0) {
if (stat(_path.getString().c_str(), &st) != 0) {
return false;
} else if (!S_ISDIR(st.st_mode)) {
return false;
@ -176,7 +182,7 @@ bool etk::fileSystem::isDirectory(const etk::Path& _path) {
bool etk::fileSystem::isFile(const etk::Path& _path) {
struct stat st;
int32_t status = 0;
if (stat(_path.get().c_str(), &st) != 0) {
if (stat(_path.getString().c_str(), &st) != 0) {
return false;
} else if (!S_ISREG(st.st_mode)) {
return false;
@ -187,7 +193,7 @@ bool etk::fileSystem::isFile(const etk::Path& _path) {
bool etk::fileSystem::isSymLink(const etk::Path& _path) {
struct stat st;
int32_t status = 0;
if (stat(_path.get().c_str(), &st) != 0) {
if (stat(_path.getString().c_str(), &st) != 0) {
return false;
} else if (!S_ISLNK(st.st_mode)) {
return false;
@ -200,7 +206,7 @@ etk::fileSystem::Permissions etk::fileSystem::getPermission(const etk::Path& _pa
etk::fileSystem::Permissions permissions;
// tmpStat Buffer :
struct stat statProperty;
if (-1 == stat(m_systemFileName.c_str(), &statProperty)) {
if (-1 == stat(_path.getString().c_str(), &statProperty)) {
//Normal case when the file does not exist ... ==> the it was in unknow mode ...
return permissions;
}
@ -213,12 +219,8 @@ etk::String etk::fileSystem::getRelativeString(const etk::Path& _path) {
return _path.getRelative();
}
etk::String etk::fileSystem::getDecoratedString(const etk::Path& _path) {
return _path.getDecorated();
}
etk::String etk::fileSystem::getAbsoluteString(const etk::Path& _path) {
return _path.get();
return _path.getAbsolute();
}
etk::String etk::fileSystem::getSystemString(const etk::Path& _path) {
@ -226,11 +228,11 @@ etk::String etk::fileSystem::getSystemString(const etk::Path& _path) {
}
etk::String etk::fileSystem::getMimeType(const etk::Path& _path) {
return "*";
}
etk::Path etk::fileSystem::getTemporaryPath() {
return etk::Path{};
}
etk::String etk::fileSystem::getHomePathString() {

View File

@ -3,10 +3,9 @@
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL-2 (see license file)
*/
#pragma once
#include <etk/types.hpp>
#include <etk/io/Interface.hpp>
#include <etk/io/File.hpp>
#include <etk/debug.hpp>
etk::io::File::File() {
// nothing to do.

View File

@ -18,6 +18,6 @@ namespace etk {
Append, //!< request File open in append
};
//! @not_in_doc
etk::Stream& operator <<(etk::Stream &_os, const enum etk::OpenMode &_obj);
etk::Stream& operator <<(etk::Stream &_os, const enum etk::io::OpenMode &_obj);
}
}

View File

@ -12,7 +12,7 @@ namespace etk {
/**
* @brief Seek mode availlable (just to wrap it ...)
*/
enum class SeekNode {
enum class SeekMode {
Start, //!< request seek position start at the START of the file
End, //!< request seek position start at the END of the file
Current, //!< request seek position start at the CURRENT position in the file

View File

@ -3,11 +3,9 @@
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL-2 (see license file)
*/
#pragma once
#include <etk/types.hpp>
#include <etk/io/Interface.hpp>
#include <etk/io/ZipFile.hpp>
#include <etk/debug.hpp>
etk::io::ZipFile::ZipFile(ememory::SharedPtr<etk::Archive> _archive):
m_archive(_archive) {