[DEV] rework file interface (part 2)

This commit is contained in:
Edouard DUPIN 2018-09-02 03:43:32 +02:00
parent d302967081
commit 5a1160fb89
21 changed files with 436 additions and 30 deletions

View File

@ -165,7 +165,7 @@ etk::Path::Path(fileSystem::Type _type, const etk::String& _value) {
} }
etk::String etk::Path::get() const { etk::String etk::Path::getString() const {
} }

View File

@ -39,7 +39,7 @@ namespace etk {
* @brief Get the absolute path * @brief Get the absolute path
* @return string like /home/userXXX/aaa/bbb/*** or /c/userXXX/aaa/bbb/*** * @return string like /home/userXXX/aaa/bbb/*** or /c/userXXX/aaa/bbb/***
*/ */
etk::String get() const; etk::String getString() const;
/** /**
* @brief Get the relative path. * @brief Get the relative path.
* @return string like ../../aaa/bbb/*** * @return string like ../../aaa/bbb/***

0
etk/fileSystem/Uri.cpp Normal file
View File

0
etk/fileSystem/Uri.hpp Normal file
View File

0
etk/fileSystem/UriIo.cpp Normal file
View File

0
etk/fileSystem/UriIo.hpp Normal file
View File

View File

View File

View File

@ -145,40 +145,14 @@ etk::String etk::fileSystem::getSystemString(const etk::Path& _path) {
return _path.getNative(); return _path.getNative();
} }
etk::String etk::fileSystem::getMimeType(const etk::Path& _path) { etk::String etk::fileSystem::getMimeType(const etk::Path& _path) {
} }
etk::Path etk::fileSystem::getTemporaryPath() { etk::Path etk::fileSystem::getTemporaryPath() {
} }
static bool initHomeFolder() {
static bool isInit = false;
if (isInit == false) {
TK_DBG_MODE("Real Ini Home folder:");
char * basicPath = getenv("HOME");
if (basicPath == null) {
TK_WARNING("ERROR while trying to get the path of the home folder");
#if defined(__TARGET_OS__Windows)
baseFolderHome = "c:/";
#elif defined(__TARGET_OS__Android)
baseFolderHome = "/sdcard";
#else
baseFolderHome = "~";
#endif
} else {
baseFolderHome = basicPath;
}
TK_DBG_MODE(" home=:" << baseFolderHome);
isInit = true;
}
return isInit;
}
etk::String etk::fileSystem::getHomePathString() { etk::String etk::fileSystem::getHomePathString() {
static bool isInit = false; static bool isInit = false;
static etk::String data = ""; static etk::String data = "";
@ -200,8 +174,9 @@ etk::String etk::fileSystem::getHomePathString() {
} }
return data; return data;
} }
etk::Path etk::fileSystem::getHomePath() { etk::Path etk::fileSystem::getHomePath() {
return etk::Path(etk::fileSystem::Type::Home, ""); return etk::Path(etk::fileSystem::getHomePathString());
} }
etk::Path etk::fileSystem::getExecutionPath() { etk::Path etk::fileSystem::getExecutionPath() {

View File

@ -63,13 +63,16 @@ namespace etk {
* @param[in] _path Path to remove. * @param[in] _path Path to remove.
*/ */
void removeFile(const etk::Path& _path); void removeFile(const etk::Path& _path);
/** /**
* @brief Check if the path exist * @brief Check if the path exist
* @param[in] _path1 Path source. * @param[in] _path1 Path source.
* @param[in] _path2 Path destination. * @param[in] _path2 Path destination.
*/ */
bool exit(const etk::Path& _path); bool exit(const etk::Path& _path);
/**
* @brief Get the File size
* @return the requested size
*/
uint64_t fileSize(const etk::Path& _path); uint64_t fileSize(const etk::Path& _path);
bool isDirectory(const etk::Path& _path); bool isDirectory(const etk::Path& _path);

146
etk/io/File.cpp Normal file
View File

@ -0,0 +1,146 @@
/**
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL-2 (see license file)
*/
#pragma once
#include <etk/types.hpp>
#include <etk/io/Interface.hpp>
etk::io::File::File() {
// nothing to do.
}
etk::io::File::File(const etk::Path& _path):
m_path(_path) {
}
bool etk::io::File::open(etk::io::OpenMode _mode) {
if (m_pointer != null) {
TK_CRITICAL("File Already open : " << *this);
return true;
}
TK_VERBOSE(" Read file : " << m_path);
switch (_mode) {
case etk::io::OpenMode::Read:
m_pointer = fopen(m_path.c_str(),"rb");
break;
case etk::io::OpenMode::Write:
m_pointer = fopen(m_path.c_str(),"wb");
break;
case etk::io::OpenMode::Append:
m_pointer = fopen(m_path.c_str(),"ab");
break;
}
if(m_pointer == null) {
TK_ERROR("Can not open the file " << m_path );
return false;
}
return true;
}
bool etk::io::File::isOpen() {
return m_pointer != null;
}
bool etk::io::File::close() {
if (m_pointer == null) {
TK_CRITICAL("File Already closed : " << *this);
return false;
}
fclose(m_pointer);
m_pointer = null;
return true;
}
uint64_t etk::io::File::size() {
return etk::fileSystem::fileSize(m_path);
}
char* etk::io::File::gets(char* _elementLine, int64_t _maxData) {
return fgets(_elementLine, _maxData, m_pointer);
}
char etk::io::File::get() {
char data='\0';
if (read(&data, 1, 1)!=1) {
return '\0';
}
return data;
}
bool etk::io::File::gets(etk::String& _output) {
_output.clear();
char tmp = get();
while ( tmp != '\0'
&& tmp != '\n') {
_output += tmp;
tmp = get();
}
if (tmp == '\0') {
return false;
}
return true;
}
bool etk::io::File::put(char _input) {
if (fileWrite(&_input, 1, 1) == 1) {
return true;
}
return false;
}
bool etk::io::File::puts(const etk::String& _input) {
if (fileWrite((void*)_input.c_str(), 1, _input.size()) == (int64_t)_input.size()) {
return true;
}
return false;
}
bool etk::io::File::seek(uint64_t _offset, enum etk::io::SeekMode _origin) {
int originFS = 0;
switch(_origin) {
case etk::seekNode_end:
originFS = SEEK_END;
break;
case etk::seekNode_current:
originFS = SEEK_CUR;
break;
default:
originFS = 0;
break;
}
fseek(m_pointer, _offset, originFS);
if(ferror(m_pointer)) {
return false;
}
return true;
}
void etk::io::File::flush() {
if (m_pointer != null) {
fflush(m_pointer);
}
}
void etk::io::File::tell() {
if (m_pointer != null) {
ftell(m_pointer);
}
}
int64_t etk::io::File::read(void* _data, int64_t _blockSize, int64_t _nbBlock) {
if (m_pointer == null) {
TK_ERROR("Can not read in a file that is not open : " << *this);
return 0;
}
return fread(_data, _blockSize, _nbBlock, m_pointer);
}
int64_t etk::io::File::write(const void* _data, int64_t _blockSize, int64_t _nbBlock) {
if (m_pointer == null) {
TK_ERROR("Can not write in a file that is not open : " << *this);
return 0;
}
return fwrite(_data, _blockSize, _nbBlock, m_pointer);
}

41
etk/io/File.hpp Normal file
View File

@ -0,0 +1,41 @@
/**
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL-2 (see license file)
*/
#pragma once
#include <etk/types.hpp>
#include <etk/io/Interface.hpp>
namespace etk {
namespace io {
/**
* @brief System file interface.
*/
class File: public etk::io::Interface {
private:
etk::Path m_path; //!< Path to access in this interface
FILE * m_PointerFile = null; //!< Generic file accesss.
public:
File();
File(const etk::Path& _path);
ETK_CONSTRUCTOR_COPY_DELETE(File);
ETK_CONSTRUCTOR_MOVE_DEFAULT(File);
public:
bool open() override;
bool isOpen() override;
bool close() override;
uint64_t size() override;
char* gets(char* _elementLine, int64_t _maxData) override;
char get() override;
bool gets(etk::String& _output) override;
bool put(char _input) override;
bool puts(const etk::String& _input) override;
bool seek(uint64_t _offset, enum etk::io::SeekMode _origin) override;
void flush() override;
int64_t read(void* _data, int64_t _blockSize, int64_t _nbBlock) override;
int64_t write(const void* _data, int64_t _blockSize, int64_t _nbBlock) override;
};
}
}

0
etk/io/Interface.cpp Normal file
View File

146
etk/io/Interface.hpp Normal file
View File

@ -0,0 +1,146 @@
/**
* @author Edouard DUPIN
* @copyright 2018, Edouard DUPIN, all right reserved
* @license MPL-2 (see license file)
*/
#pragma once
#include <etk/types.hpp>
#include <etk/io/SeekMode.hpp>
#include <etk/io/OpenMode.hpp>
namespace etk {
namespace io {
/**
* @brief string class ...
*/
class Interface {
public:
/**
* @brief Open the file in Read mode
* @param[in] _mode Mode to open the IO
* @return true : action done
* @return false : action not done
*/
virtual bool open(etk::io::OpenMode _mode) = 0;
/**
* @brief Check if the current file is Open
* @return true : File is open in write or in read
* @return false : File is NOT open in write or in read
*/
virtual bool isOpen() = 0;
/**
* @brief Close the curent file
* @return true : action done
* @return false : action not done
*/
virtual bool close() = 0;
/**
* @brief Get the File size
* @return the requested size
*/
virtual uint64_t size() = 0;
/**
* @brief Get the pointer on the start line and the next line (or null)
* @param[in,out] _elementLine Pointer to an array of chars where the string read is copied.
* @param[in] _maxData Maximum number of characters to be copied into str (including the terminating null-character).
* @return the pointer on the end of the cuurent line.
*/
virtual char* gets(char* _elementLine, int64_t _maxData) = 0;
/**
* @brief Get a unique data in the file
* @return the next element in the file.
*/
virtual char get() = 0;
/**
* @brief Get a compleate line in a text file
* @param[out] _output the next element in the file.
* @return true The file is not ended.
* @return false The file is ended.
*/
virtual bool gets(etk::String& _output) = 0;
/**
* @brief Write data on the file
* @param[in] _input data to write.
* @return true Write done corectly.
* @return false ErrorOn write.
*/
virtual bool put(char _input) = 0;
/**
* @brief Write data on the file
* @param[in] _input data to write.
* @return true Write done corectly.
* @return false ErrorOn write.
*/
virtual bool puts(const etk::String& _input) = 0;
/**
* @brief Move in the file Position
* @param[in] _offset Offset to apply at the file
* @param[in] _origin Origin of the position
* @return true : action done
* @return false : action not done
*/
virtual bool seek(uint64_t _offset, enum etk::io::SeekMode _origin) = 0;
/**
* @brief Get the position in the file.
* @return the requested position.
*/
virtual int64_t tell() = 0;
/**
* @brief Flush the current file
*/
virtual void flush() = 0;
/**
* @brief Read data from the file
* @param[in,out] _data Pointer on the buffer that might be set the data
* @param[in] _blockSize Size of one block of data
* @param[in] _nbBlock Number of block needed
* @return Number of element read (in block number)
*/
virtual int64_t read(void* _data, int64_t _blockSize, int64_t _nbBlock) = 0;
/**
* @brief Write data on the file
* @param[in] _data Pointer on the buffer that might be set on the file
* @param[in] _blockSize Size of one block of data
* @param[in] _nbBlock Number of block needed
* @return Number of element written (in block number)
*/
virtual int64_t write(const void* _data, int64_t _blockSize, int64_t _nbBlock) = 0;
/**
* @brief Read all element in a file and set it in a generic vector
* @return the read vector
*/
template<typename T> etk::Vector<T> readAll() {
etk::Vector<T> value;
value.resize(size());
read(&value[0], sizeof(T), size()/sizeof(T));
return value;
}
/**
* @brief Read all element in a file and set it in a generic etk::String
* @return the read string
*/
etk::String readAllString() {
etk::String value;
value.resize(size());
read(&value[0], sizeof(char), size()/sizeof(char));
return value;
}
/**
* @brief Write all the vector in a file
* @param[in] _value Data to write in the File
*/
template<typename T>
void writeAll(const etk::Vector<T>& _value) {
write(static_cast<const void*>(&(_value[0])), sizeof(T), _value.size()/sizeof(T));
}
/**
* @brief Write all the vector in a file
* @param[in] _value String data to write in the File
*/
void fileWriteAll(const etk::String& _value) {
write(static_cast<const void*>(&(_value[0])), sizeof(char), _value.size()/sizeof(char));
}
};
}
}

23
etk/io/OpenMode.cpp Normal file
View File

@ -0,0 +1,23 @@
/**
* @author Edouard DUPIN
* @copyright 2018, Edouard DUPIN, all right reserved
* @license MPL-2 (see license file)
*/
#include <etk/io/OpenMode.hpp>
etk::Stream& etk::operator <<(etk::Stream &_os, const enum etk::io::OpenMode &_obj) {
switch (_obj) {
case etk::io::OpenMode::Read:
_os << "etk::OpenMode::Read";
break;
case etk::io::OpenMode::Write:
_os << "etk::OpenMode::Write";
break;
case etk::io::OpenMode::Append:
_os << "etk::OpenMode::Append";
break;
}
return _os;
}

23
etk/io/OpenMode.hpp Normal file
View File

@ -0,0 +1,23 @@
/** @file
* @author Edouard DUPIN
* @copyright 2018, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#pragma once
#include <etk/types.hpp>
namespace etk {
namespace io {
/**
* @brief Seek mode availlable (just to wrap it ...)
*/
enum class OpenMode {
Read, //!< request File open in read
Write, //!< request File open in write
Append, //!< request File open in append
};
//! @not_in_doc
etk::Stream& operator <<(etk::Stream &_os, const enum etk::OpenMode &_obj);
}
}

23
etk/io/SeekMode.cpp Normal file
View File

@ -0,0 +1,23 @@
/**
* @author Edouard DUPIN
* @copyright 2018, Edouard DUPIN, all right reserved
* @license MPL-2 (see license file)
*/
#include <etk/io/SeekMode.hpp>
etk::Stream& etk::operator <<(etk::Stream &_os, const enum etk::io::SeekMode &_obj) {
switch (_obj) {
case etk::io::SeekMode::Start:
_os << "etk::SeekMode::Start";
break;
case etk::io::SeekMode::End:
_os << "etk::SeekMode::End";
break;
case etk::io::SeekMode::Current:
_os << "etk::SeekMode::Current";
break;
}
return _os;
}

23
etk/io/SeekMode.hpp Normal file
View File

@ -0,0 +1,23 @@
/** @file
* @author Edouard DUPIN
* @copyright 2018, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#pragma once
#include <etk/types.hpp>
namespace etk {
namespace io {
/**
* @brief Seek mode availlable (just to wrap it ...)
*/
enum class SeekNode {
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
};
//! @not_in_doc
etk::Stream& operator <<(etk::Stream &_os, const enum etk::io::SeekMode &_obj);
}
}

0
etk/io/ZipFile.cpp Normal file
View File

0
etk/io/ZipFile.hpp Normal file
View File

View File

@ -38,6 +38,9 @@ def configure(target, my_module):
'etk/fileSystem/Path.cpp', 'etk/fileSystem/Path.cpp',
'etk/fileSystem/Permissions.cpp', 'etk/fileSystem/Permissions.cpp',
'etk/fileSystem/Type.cpp', 'etk/fileSystem/Type.cpp',
'etk/io/OpenMode.cpp',
'etk/io/SeekMode.cpp',
'etk/io/File.cpp',
'etk/theme/theme.cpp', 'etk/theme/theme.cpp',
'etk/math/Matrix2x2.cpp', 'etk/math/Matrix2x2.cpp',
'etk/math/Matrix2x3.cpp', 'etk/math/Matrix2x3.cpp',