Documentation for Documentation & File

Former-commit-id: 8a69e6dca76fba4a23b36c563b52c4ccbbec7309
This commit is contained in:
Gawaboumga 2016-02-21 14:37:37 +01:00
parent a82c33c314
commit 5a5f25a950
3 changed files with 523 additions and 40 deletions

View File

@ -8,11 +8,27 @@
namespace Nz namespace Nz
{ {
/*!
* \brief Computes the hash for the file
* \return ByteArray represing the result of the hash of the file
*
* \param hash Hash to execute
* \param filePath Path for the file
*/
inline ByteArray File::ComputeHash(HashType hash, const String& filePath) inline ByteArray File::ComputeHash(HashType hash, const String& filePath)
{ {
return ComputeHash(AbstractHash::Get(hash).get(), filePath); return ComputeHash(AbstractHash::Get(hash).get(), filePath);
} }
/*!
* \brief Computes the hash for the file
* \return ByteArray represing the result of the hash of the file
*
* \param hash Hash to execute
* \param filePath Path for the file
*/
inline ByteArray File::ComputeHash(AbstractHash* hash, const String& filePath) inline ByteArray File::ComputeHash(AbstractHash* hash, const String& filePath)
{ {
return Nz::ComputeHash(hash, File(filePath)); return Nz::ComputeHash(hash, File(filePath));

View File

@ -34,12 +34,27 @@ namespace Nz
thread_local std::string currentPath(DirectoryImpl::GetCurrent()); thread_local std::string currentPath(DirectoryImpl::GetCurrent());
} }
/*!
* \class Nz::Directory
* \brief Core class that represents a directory
*/
/*!
* \brief Constructs a Directory object by default
*/
Directory::Directory() : Directory::Directory() :
m_pattern('*'), m_pattern('*'),
m_impl(nullptr) m_impl(nullptr)
{ {
} }
/*!
* \brief Constructs a Directory object with a path
*
* \param dirPath Path to the directory
*/
Directory::Directory(const String& dirPath) : Directory::Directory(const String& dirPath) :
m_dirPath(dirPath), m_dirPath(dirPath),
m_pattern('*'), m_pattern('*'),
@ -47,11 +62,21 @@ namespace Nz
{ {
} }
/*!
* \brief Destructs the object and calls Close
*
* \see Close
*/
Directory::~Directory() Directory::~Directory()
{ {
Close(); Close();
} }
/*!
* \brief Closes the directory
*/
void Directory::Close() void Directory::Close()
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
@ -64,16 +89,26 @@ namespace Nz
} }
} }
/*!
* \brief Checks whether the directory exists
* \return true if directory exists
*/
bool Directory::Exists() const bool Directory::Exists() const
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
if (IsOpen()) if (IsOpen())
return true; // Le fichier est ouvert, donc il existe return true; // If directory is open, then it exists
else else
return Exists(m_dirPath); return Exists(m_dirPath);
} }
/*!
* \brief Gets the path of the directory
* \return Path of the directory
*/
String Directory::GetPath() const String Directory::GetPath() const
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
@ -81,6 +116,11 @@ namespace Nz
return m_dirPath; return m_dirPath;
} }
/*!
* \brief Gets the pattern for the path of the directory
* \return Pattern for the path of the directory
*/
String Directory::GetPattern() const String Directory::GetPattern() const
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
@ -88,12 +128,19 @@ namespace Nz
return m_pattern; return m_pattern;
} }
/*!
* \brief Gets the result name of the directory
* \return Resulting name
*
* \remark Produces a NazaraError if directory is not open with NAZARA_CORE_SAFE defined
*/
String Directory::GetResultName() const String Directory::GetResultName() const
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
#if NAZARA_CORE_SAFE #if NAZARA_CORE_SAFE
if (!m_impl) if (!IsOpen())
{ {
NazaraError("Directory not opened"); NazaraError("Directory not opened");
return String(); return String();
@ -103,12 +150,19 @@ namespace Nz
return m_impl->GetResultName(); return m_impl->GetResultName();
} }
/*!
* \brief Gets the result path of the directory
* \return Resulting path
*
* \remark Produces a NazaraError if directory is not open with NAZARA_CORE_SAFE defined
*/
String Directory::GetResultPath() const String Directory::GetResultPath() const
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
#if NAZARA_CORE_SAFE #if NAZARA_CORE_SAFE
if (!m_impl) if (!IsOpen())
{ {
NazaraError("Directory not opened"); NazaraError("Directory not opened");
return String(); return String();
@ -118,12 +172,19 @@ namespace Nz
return m_dirPath + NAZARA_DIRECTORY_SEPARATOR + m_impl->GetResultName(); return m_dirPath + NAZARA_DIRECTORY_SEPARATOR + m_impl->GetResultName();
} }
/*!
* \brief Gets the resulting size of the directory
* \return Size of the directory
*
* \remark Produces a NazaraError if directory is not open with NAZARA_CORE_SAFE defined
*/
UInt64 Directory::GetResultSize() const UInt64 Directory::GetResultSize() const
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
#if NAZARA_CORE_SAFE #if NAZARA_CORE_SAFE
if (!m_impl) if (!IsOpen())
{ {
NazaraError("Directory not opened"); NazaraError("Directory not opened");
return 0; return 0;
@ -133,6 +194,11 @@ namespace Nz
return m_impl->GetResultSize(); return m_impl->GetResultSize();
} }
/*!
* \brief Checks whether the directory is open
* \return true if open
*/
bool Directory::IsOpen() const bool Directory::IsOpen() const
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
@ -140,12 +206,19 @@ namespace Nz
return m_impl != nullptr; return m_impl != nullptr;
} }
/*!
* \brief Checks whether the directory is result
* \return true if result
*
* \remark Produces a NazaraError if directory is not open with NAZARA_CORE_SAFE defined
*/
bool Directory::IsResultDirectory() const bool Directory::IsResultDirectory() const
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
#if NAZARA_CORE_SAFE #if NAZARA_CORE_SAFE
if (!m_impl) if (!IsOpen())
{ {
NazaraError("Directory not opened"); NazaraError("Directory not opened");
return false; return false;
@ -155,12 +228,21 @@ namespace Nz
return m_impl->IsResultDirectory(); return m_impl->IsResultDirectory();
} }
/*!
* \brief Sets the next result in the directory
* \return true if directory has a next result
*
* \param skipDots Skips the dots in the path
*
* \remark Produces a NazaraError if directory is not open with NAZARA_CORE_SAFE defined
*/
bool Directory::NextResult(bool skipDots) bool Directory::NextResult(bool skipDots)
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
#if NAZARA_CORE_SAFE #if NAZARA_CORE_SAFE
if (!m_impl) if (!IsOpen())
{ {
NazaraError("Directory not opened"); NazaraError("Directory not opened");
return false; return false;
@ -185,6 +267,11 @@ namespace Nz
return true; return true;
} }
/*!
* \brief Opens the directory
* \return true if opening is successful
*/
bool Directory::Open() bool Directory::Open()
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
@ -206,6 +293,12 @@ namespace Nz
return true; return true;
} }
/*!
* \brief Sets the path of the directory
*
* \param dirPath Path of the directory
*/
void Directory::SetPath(const String& dirPath) void Directory::SetPath(const String& dirPath)
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
@ -215,6 +308,12 @@ namespace Nz
m_dirPath = File::AbsolutePath(dirPath); m_dirPath = File::AbsolutePath(dirPath);
} }
/*!
* \brief Sets the pattern of the directory
*
* \param dirPath Pattern of the directory
*/
void Directory::SetPattern(const String& pattern) void Directory::SetPattern(const String& pattern)
{ {
NazaraLock(m_mutex); NazaraLock(m_mutex);
@ -222,6 +321,18 @@ namespace Nz
m_pattern = pattern; m_pattern = pattern;
} }
/*!
* \brief Copies the first directory to a new directory path
* \return true if copy is successful
*
* \param sourcePath Path of the original directory
* \param targetPath Path of the copied directory
*
* \remark Produces a NazaraError if could not create destination directory
* \remark Produces a NazaraError if could not open origin directory
* \remark Produces a NazaraError if could not copy a file
*/
bool Directory::Copy(const String& sourcePath, const String& destPath) bool Directory::Copy(const String& sourcePath, const String& destPath)
{ {
if (sourcePath.IsEmpty() || destPath.IsEmpty()) if (sourcePath.IsEmpty() || destPath.IsEmpty())
@ -262,6 +373,14 @@ namespace Nz
return true; return true;
} }
/*!
* \brief Creates a directory from a path
* \return true if creation is successful
*
* \param dirPath Path of the directory
* \param recursive Creates subdirectories
*/
bool Directory::Create(const String& dirPath, bool recursive) bool Directory::Create(const String& dirPath, bool recursive)
{ {
if (dirPath.IsEmpty()) if (dirPath.IsEmpty())
@ -275,7 +394,7 @@ namespace Nz
return false; return false;
#ifdef NAZARA_PLATFORM_WINDOWS #ifdef NAZARA_PLATFORM_WINDOWS
// Contrairement au disque (Ex: "C:"), le chemin réseau n'est pas considéré comme un dossier (Ex: "\\Laptop") // Unlike to disk (Ex: "C:"), the netwrok path is not considered as a directory (Ex: "\\Laptop")
if (path.Match("\\\\*")) if (path.Match("\\\\*"))
{ {
foundPos = path.Find('\\', 2); foundPos = path.Find('\\', 2);
@ -309,6 +428,13 @@ namespace Nz
return DirectoryImpl::Create(File::NormalizePath(dirPath)); return DirectoryImpl::Create(File::NormalizePath(dirPath));
} }
/*!
* \brief Checks whether the directory exists
* \return true if directory exists
*
* \param dirPath Path of the directory
*/
bool Directory::Exists(const String& dirPath) bool Directory::Exists(const String& dirPath)
{ {
if (dirPath.IsEmpty()) if (dirPath.IsEmpty())
@ -317,14 +443,24 @@ namespace Nz
return DirectoryImpl::Exists(File::NormalizePath(dirPath)); return DirectoryImpl::Exists(File::NormalizePath(dirPath));
} }
/*!
* \brief Gets the current path of this directory
* \return Current path
*/
String Directory::GetCurrent() String Directory::GetCurrent()
{ {
return currentPath; return currentPath;
} }
/*!
* \brief Gets this current file relative to the engine
* \return Path to this file
*/
const char* Directory::GetCurrentFileRelativeToEngine(const char* currentFile) const char* Directory::GetCurrentFileRelativeToEngine(const char* currentFile)
{ {
///FIXME: Est-ce que cette méthode est au bon endroit ? ///FIXME: Is this method in the right place ?
const char* ptr = std::strstr(currentFile, "NazaraEngine/"); const char* ptr = std::strstr(currentFile, "NazaraEngine/");
if (!ptr) if (!ptr)
ptr = std::strstr(currentFile, "NazaraEngine\\"); ptr = std::strstr(currentFile, "NazaraEngine\\");
@ -335,6 +471,14 @@ namespace Nz
return ptr; return ptr;
} }
/*!
* \brief Removes the directory
* \return true if remove is successful
*
* \param dirPath Path of the directory
* \param emptyDirectory Remove recursively
*/
bool Directory::Remove(const String& dirPath, bool emptyDirectory) bool Directory::Remove(const String& dirPath, bool emptyDirectory)
{ {
if (dirPath.IsEmpty()) if (dirPath.IsEmpty())
@ -344,7 +488,7 @@ namespace Nz
{ {
Directory dir(dirPath); Directory dir(dirPath);
if (!dir.Open()) if (!dir.Open())
return DirectoryImpl::Remove(dirPath); // Si on n'arrive pas à ouvrir le dossier, on tente de le supprimer return DirectoryImpl::Remove(dirPath); // If we can't open the directory, we try to delete it
while (dir.NextResult(true)) while (dir.NextResult(true))
{ {
@ -366,6 +510,13 @@ namespace Nz
return DirectoryImpl::Remove(File::NormalizePath(dirPath)); return DirectoryImpl::Remove(File::NormalizePath(dirPath));
} }
/*!
* \brief Sets the current directory
* \return true if directory path exists
*
* \param dirPath Path of the directory
*/
bool Directory::SetCurrent(const String& dirPath) bool Directory::SetCurrent(const String& dirPath)
{ {
String path = File::AbsolutePath(dirPath); String path = File::AbsolutePath(dirPath);

View File

@ -30,23 +30,51 @@
namespace Nz namespace Nz
{ {
/*!
* \class Nz::File
* \brief Core class that represents a file
*/
/*!
* \brief Constructs a File object by default
*/
File::File() : File::File() :
m_impl(nullptr) m_impl(nullptr)
{ {
} }
/*!
* \brief Constructs a File object with a file path
*
* \param filePath Path to the file
*/
File::File(const String& filePath) : File::File(const String& filePath) :
File() File()
{ {
SetFile(filePath); SetFile(filePath);
} }
/*!
* \brief Constructs a File object with a file path and flags
*
* \param filePath Path to the file
* \param openMode Flag of the file
*/
File::File(const String& filePath, UInt32 openMode) : File::File(const String& filePath, UInt32 openMode) :
File() File()
{ {
Open(filePath, openMode); Open(filePath, openMode);
} }
/*!
* \brief Constructs a File object by move semantic
*
* \param file File to move into this
*/
File::File(File&& file) noexcept : File::File(File&& file) noexcept :
Stream(std::move(file)), Stream(std::move(file)),
m_filePath(std::move(file.m_filePath)), m_filePath(std::move(file.m_filePath)),
@ -55,16 +83,33 @@ namespace Nz
file.m_impl = nullptr; file.m_impl = nullptr;
} }
/*!
* \brief Destructs the object and calls Close
*
* \see Close
*/
File::~File() File::~File()
{ {
Close(); Close();
} }
/*!
* \brief Copies this file to a new file path
* \return true if copy is successful
*
* \param newFilePath Path of the new file
*/
bool File::Copy(const String& newFilePath) bool File::Copy(const String& newFilePath)
{ {
return Copy(m_filePath, newFilePath); return Copy(m_filePath, newFilePath);
} }
/*!
* \brief Closes the file
*/
void File::Close() void File::Close()
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -79,6 +124,11 @@ namespace Nz
} }
} }
/*!
* \brief Deletes the file
* \return true if delete is successful
*/
bool File::Delete() bool File::Delete()
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -88,6 +138,13 @@ namespace Nz
return Delete(m_filePath); return Delete(m_filePath);
} }
/*!
* \brief Checks whether the file has reached the end
* \return true if cursor is at the end of the file
*
* \remark Produces a NazaraError if file is not open with NAZARA_CORE_SAFE defined
*/
bool File::EndOfFile() const bool File::EndOfFile() const
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -95,7 +152,7 @@ namespace Nz
#if NAZARA_CORE_SAFE #if NAZARA_CORE_SAFE
if (!IsOpen()) if (!IsOpen())
{ {
NazaraError("File not opened"); NazaraError("File not open");
return false; return false;
} }
#endif #endif
@ -103,11 +160,25 @@ namespace Nz
return m_impl->EndOfFile(); return m_impl->EndOfFile();
} }
/*!
* \brief Checks whether the file has reached the end of the stream
* \return true if cursor is at the end of the file
*
* \remark Produces a NazaraError if file is not open with NAZARA_CORE_SAFE defined
*
* \see EndOfFile
*/
bool File::EndOfStream() const bool File::EndOfStream() const
{ {
return EndOfFile(); return EndOfFile();
} }
/*!
* \brief Checks whether the file exists
* \return true if file exists
*/
bool File::Exists() const bool File::Exists() const
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -118,6 +189,11 @@ namespace Nz
return Exists(m_filePath); return Exists(m_filePath);
} }
/*!
* \brief Gets the creation time of the file
* \return Information about the creation time
*/
time_t File::GetCreationTime() const time_t File::GetCreationTime() const
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -125,15 +201,27 @@ namespace Nz
return GetCreationTime(m_filePath); return GetCreationTime(m_filePath);
} }
/*!
* \brief Gets the position of the cursor in the file
* \return Position of the cursor
*
* \remark Produces a NazaraAssert if file is not open
*/
UInt64 File::GetCursorPos() const UInt64 File::GetCursorPos() const
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
NazaraAssert(IsOpen(), "File is not opened"); NazaraAssert(IsOpen(), "File is not open");
return m_impl->GetCursorPos(); return m_impl->GetCursorPos();
} }
/*!
* \brief Gets the directory of the file
* \return Directory of the file
*/
String File::GetDirectory() const String File::GetDirectory() const
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -141,6 +229,11 @@ namespace Nz
return m_filePath.SubStringTo(NAZARA_DIRECTORY_SEPARATOR, -1, true, true); return m_filePath.SubStringTo(NAZARA_DIRECTORY_SEPARATOR, -1, true, true);
} }
/*!
* \brief Gets the name of the file
* \return Name of the file
*/
String File::GetFileName() const String File::GetFileName() const
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -148,6 +241,11 @@ namespace Nz
return m_filePath.SubStringFrom(NAZARA_DIRECTORY_SEPARATOR, -1, true); return m_filePath.SubStringFrom(NAZARA_DIRECTORY_SEPARATOR, -1, true);
} }
/*!
* \brief Gets the last time the file was accessed
* \return Information about the last access time
*/
time_t File::GetLastAccessTime() const time_t File::GetLastAccessTime() const
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -155,6 +253,11 @@ namespace Nz
return GetLastAccessTime(m_filePath); return GetLastAccessTime(m_filePath);
} }
/*!
* \brief Gets the last time the file was written
* \return Information about the last writing time
*/
time_t File::GetLastWriteTime() const time_t File::GetLastWriteTime() const
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -162,6 +265,11 @@ namespace Nz
return GetLastWriteTime(m_filePath); return GetLastWriteTime(m_filePath);
} }
/*!
* \brief Gets the path of the file
* \return Path of the file
*/
String File::GetPath() const String File::GetPath() const
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -169,6 +277,11 @@ namespace Nz
return m_filePath; return m_filePath;
} }
/*!
* \brief Gets the size of the file
* \return Size of the file
*/
UInt64 File::GetSize() const UInt64 File::GetSize() const
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -176,6 +289,11 @@ namespace Nz
return GetSize(m_filePath); return GetSize(m_filePath);
} }
/*!
* \brief Checks whether the file is open
* \return true if open
*/
bool File::IsOpen() const bool File::IsOpen() const
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -183,22 +301,14 @@ namespace Nz
return m_impl != nullptr; return m_impl != nullptr;
} }
bool File::Rename(const String& newFilePath) /*!
{ * \brief Opens the file with flags
NazaraLock(m_mutex) * \return true if opening is successful
*
bool opened = IsOpen(); * \param openMode Flag for file
Close(); *
* \remark Produces a NazaraError if OS error to open a file
bool success = Rename(m_filePath, newFilePath); */
if (success)
m_filePath = NormalizePath(newFilePath);
if (opened)
Open();
return success;
}
bool File::Open(unsigned int openMode) bool File::Open(unsigned int openMode)
{ {
@ -215,7 +325,7 @@ namespace Nz
std::unique_ptr<FileImpl> impl(new FileImpl(this)); std::unique_ptr<FileImpl> impl(new FileImpl(this));
if (!impl->Open(m_filePath, openMode)) if (!impl->Open(m_filePath, openMode))
{ {
ErrorFlags flags(ErrorFlag_Silent); // Silencieux par défaut ErrorFlags flags(ErrorFlag_Silent); // Silent by default
NazaraError("Failed to open \"" + m_filePath + "\": " + Error::GetLastSystemError()); NazaraError("Failed to open \"" + m_filePath + "\": " + Error::GetLastSystemError());
return false; return false;
} }
@ -231,6 +341,16 @@ namespace Nz
return true; return true;
} }
/*!
* \brief Opens the file with file path and flags
* \return true if opening is successful
*
* \param filePath Path to the file
* \param openMode Flag for file
*
* \remark Produces a NazaraError if OS error to open a file
*/
bool File::Open(const String& filePath, unsigned int openMode) bool File::Open(const String& filePath, unsigned int openMode)
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -241,24 +361,72 @@ namespace Nz
return Open(openMode); return Open(openMode);
} }
/*!
* \brief Renames the file with a new name
* \return true if rename is successful
*/
bool File::Rename(const String& newFilePath)
{
NazaraLock(m_mutex)
bool open = IsOpen();
Close();
bool success = Rename(m_filePath, newFilePath);
if (success)
m_filePath = NormalizePath(newFilePath);
if (open)
Open();
return success;
}
/*!
* \brief Sets the position of the cursor
* \return true if cursor is successfully positioned
*
* \param pos Position of the cursor
* \param offset Offset according to the cursor position
*
* \remark Produces a NazaraAssert if file is not open
*/
bool File::SetCursorPos(CursorPosition pos, Int64 offset) bool File::SetCursorPos(CursorPosition pos, Int64 offset)
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
NazaraAssert(IsOpen(), "File is not opened"); NazaraAssert(IsOpen(), "File is not open");
return m_impl->SetCursorPos(pos, offset); return m_impl->SetCursorPos(pos, offset);
} }
/*!
* \brief Sets the position of the cursor
* \return true if cursor is successfully positioned
*
* \param offset Offset according to the cursor begin position
*
* \remark Produces a NazaraAssert if file is not open
*/
bool File::SetCursorPos(UInt64 offset) bool File::SetCursorPos(UInt64 offset)
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
NazaraAssert(IsOpen(), "File is not opened"); NazaraAssert(IsOpen(), "File is not open");
return m_impl->SetCursorPos(CursorPosition_AtBegin, offset); return m_impl->SetCursorPos(CursorPosition_AtBegin, offset);
} }
/*!
* \brief Sets the file path
* \return true if file opening is successful
*
* \remark Produces a NazaraError if file path can not be open
*/
bool File::SetFile(const String& filePath) bool File::SetFile(const String& filePath)
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -285,6 +453,13 @@ namespace Nz
return true; return true;
} }
/*!
* \brief Sets the file path
* \return A reference to this
*
* \remark Produces a NazaraError if file path can not be open
*/
File& File::operator=(const String& filePath) File& File::operator=(const String& filePath)
{ {
SetFile(filePath); SetFile(filePath);
@ -292,6 +467,13 @@ namespace Nz
return *this; return *this;
} }
/*!
* \brief Moves the other file into this
* \return A reference to this
*
* \param file File to move in this
*/
File& File::operator=(File&& file) noexcept File& File::operator=(File&& file) noexcept
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -302,9 +484,18 @@ namespace Nz
return *this; return *this;
} }
/*!
* \brief Gets the absolute path of the file
* \return Absolute path of the file
*
* \param filePath Path of the file
*
* \remark Produces a NazaraError if filePath is weird with NAZARA_PLATFORM_WINDOWS defined
*/
String File::AbsolutePath(const String& filePath) String File::AbsolutePath(const String& filePath)
{ {
// Je n'utilise pas les fonctions de l'OS car elles ne fonctionnent que pour un chemin existant // We don't use OS functions because they only work for existing path
String path = NormalizePath(filePath); String path = NormalizePath(filePath);
if (path.IsEmpty()) if (path.IsEmpty())
return String(); return String();
@ -319,7 +510,7 @@ namespace Nz
base = "\\\\"; base = "\\\\";
start = 2; start = 2;
} }
else if (path.StartsWith('\\')) // Spécial : '\' fait référence au disque racine else if (path.StartsWith('\\')) // Special : '\' refering to root
{ {
String drive = Directory::GetCurrent().SubStringTo('\\'); String drive = Directory::GetCurrent().SubStringTo('\\');
String end = path.SubString(1, -1); String end = path.SubString(1, -1);
@ -351,14 +542,14 @@ namespace Nz
if (path.Split(sep, NAZARA_DIRECTORY_SEPARATOR) <= 1) if (path.Split(sep, NAZARA_DIRECTORY_SEPARATOR) <= 1)
return path; return path;
// Nous avons un chemin absolu, mais il nous faut un peu le nettoyer // We have the absolute path, but we need to clean it up
for (unsigned int i = 0; i < sep.size(); ++i) for (unsigned int i = 0; i < sep.size(); ++i)
{ {
if (sep[i] == '.') if (sep[i] == '.')
sep.erase(sep.begin() + i--); sep.erase(sep.begin() + i--);
else if (sep[i] == "..") else if (sep[i] == "..")
{ {
if (i > start) // Si nous ne sommes pas dans la partie protégée if (i > start) // If we are not in the protected area
sep.erase(sep.begin() + i--); sep.erase(sep.begin() + i--);
sep.erase(sep.begin() + i--); sep.erase(sep.begin() + i--);
@ -377,6 +568,14 @@ namespace Nz
return stream; return stream;
} }
/*!
* \brief Copies the first file to a new file path
* \return true if copy is successful
*
* \param sourcePath Path of the original file
* \param targetPath Path of the copied file
*/
bool File::Copy(const String& sourcePath, const String& targetPath) bool File::Copy(const String& sourcePath, const String& targetPath)
{ {
if (sourcePath.IsEmpty() || targetPath.IsEmpty()) if (sourcePath.IsEmpty() || targetPath.IsEmpty())
@ -385,6 +584,13 @@ namespace Nz
return FileImpl::Copy(NormalizePath(sourcePath), NormalizePath(targetPath)); return FileImpl::Copy(NormalizePath(sourcePath), NormalizePath(targetPath));
} }
/*!
* \brief Deletes the file
* \return true if delete is successful
*
* \param filePath Path of the file
*/
bool File::Delete(const String& filePath) bool File::Delete(const String& filePath)
{ {
if (filePath.IsEmpty()) if (filePath.IsEmpty())
@ -393,6 +599,13 @@ namespace Nz
return FileImpl::Delete(NormalizePath(filePath)); return FileImpl::Delete(NormalizePath(filePath));
} }
/*!
* \brief Checks whether the file exists
* \return true if file exists
*
* \param filePath Path of the file
*/
bool File::Exists(const String& filePath) bool File::Exists(const String& filePath)
{ {
if (filePath.IsEmpty()) if (filePath.IsEmpty())
@ -401,6 +614,13 @@ namespace Nz
return FileImpl::Exists(NormalizePath(filePath)); return FileImpl::Exists(NormalizePath(filePath));
} }
/*!
* \brief Gets the creation time of the file
* \return Information about the creation time
*
* \param filePath Path of the file
*/
time_t File::GetCreationTime(const String& filePath) time_t File::GetCreationTime(const String& filePath)
{ {
if (filePath.IsEmpty()) if (filePath.IsEmpty())
@ -409,10 +629,23 @@ namespace Nz
return FileImpl::GetCreationTime(NormalizePath(filePath)); return FileImpl::GetCreationTime(NormalizePath(filePath));
} }
/*!
* \brief Gets the directory of the file
* \return Directory of the file
*
* \param filePath Path of the file
*/
String File::GetDirectory(const String& filePath) String File::GetDirectory(const String& filePath)
{ {
return filePath.SubStringTo(NAZARA_DIRECTORY_SEPARATOR, -1, true, true); return filePath.SubStringTo(NAZARA_DIRECTORY_SEPARATOR, -1, true, true);
} }
/*!
* \brief Gets the last time the file was accessed
* \return Information about the last access time
*
* \param filePath Path of the file
*/
time_t File::GetLastAccessTime(const String& filePath) time_t File::GetLastAccessTime(const String& filePath)
{ {
@ -422,6 +655,13 @@ namespace Nz
return FileImpl::GetLastAccessTime(NormalizePath(filePath)); return FileImpl::GetLastAccessTime(NormalizePath(filePath));
} }
/*!
* \brief Gets the last time the file was written
* \return Information about the last writing time
*
* \param filePath Path of the file
*/
time_t File::GetLastWriteTime(const String& filePath) time_t File::GetLastWriteTime(const String& filePath)
{ {
if (filePath.IsEmpty()) if (filePath.IsEmpty())
@ -430,6 +670,13 @@ namespace Nz
return FileImpl::GetLastWriteTime(NormalizePath(filePath)); return FileImpl::GetLastWriteTime(NormalizePath(filePath));
} }
/*!
* \brief Gets the size of the file
* \return Size of the file
*
* \param filePath Path of the file
*/
UInt64 File::GetSize(const String& filePath) UInt64 File::GetSize(const String& filePath)
{ {
if (filePath.IsEmpty()) if (filePath.IsEmpty())
@ -438,6 +685,13 @@ namespace Nz
return FileImpl::GetSize(NormalizePath(filePath)); return FileImpl::GetSize(NormalizePath(filePath));
} }
/*!
* \brief Checks whether the file path is absolute
* \return true if path is absolute
*
* \param filePath Path to test
*/
bool File::IsAbsolute(const String& filePath) bool File::IsAbsolute(const String& filePath)
{ {
String path(filePath.Trimmed()); String path(filePath.Trimmed());
@ -451,7 +705,7 @@ namespace Nz
return true; return true;
else if (path.Match("\\\\*")) // Ex: \\Laptop else if (path.Match("\\\\*")) // Ex: \\Laptop
return true; return true;
else if (path.StartsWith('\\')) // Spécial : '\' fait référence au disque racine else if (path.StartsWith('\\')) // Special : '\' refering to the root
return true; return true;
else else
return false; return false;
@ -462,6 +716,13 @@ namespace Nz
#endif #endif
} }
/*!
* \brief Normalizes the file path
* \return Path normalized (replacing '/' with '\\' on Windows, ...)
*
* \param filePath Path to normalize
*/
String File::NormalizePath(const String& filePath) String File::NormalizePath(const String& filePath)
{ {
String path = NormalizeSeparators(filePath.Trimmed()); String path = NormalizeSeparators(filePath.Trimmed());
@ -475,6 +736,13 @@ namespace Nz
return path; return path;
} }
/*!
* \brief Normalizes the path separator
* \return Path normalized (replacing '/' with '\\' on Windows, ...)
*
* \param filePath Path to normalize
*/
String File::NormalizeSeparators(const String& filePath) String File::NormalizeSeparators(const String& filePath)
{ {
String path(filePath); String path(filePath);
@ -490,6 +758,14 @@ namespace Nz
return path; return path;
} }
/*!
* \brief Renames the file with a new name
* \return true if rename is successful
*
* \param sourcePath Path of the original file
* \param targetPath Path of the renamed file
*/
bool File::Rename(const String& sourcePath, const String& targetPath) bool File::Rename(const String& sourcePath, const String& targetPath)
{ {
if (sourcePath.IsEmpty() || targetPath.IsEmpty()) if (sourcePath.IsEmpty() || targetPath.IsEmpty())
@ -498,6 +774,12 @@ namespace Nz
return FileImpl::Rename(NormalizePath(sourcePath), NormalizePath(targetPath)); return FileImpl::Rename(NormalizePath(sourcePath), NormalizePath(targetPath));
} }
/*!
* \brief Flushes the stream
*
* \remark Produces a NazaraAssert if file is not open
*/
void File::FlushStream() void File::FlushStream()
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -507,11 +789,21 @@ namespace Nz
m_impl->Flush(); m_impl->Flush();
} }
/*!
* \brief Reads blocks
* \return Number of blocks read
*
* \param buffer Preallocated buffer to contain information read
* \param size Size of the read and thus of the buffer
*
* \remark Produces a NazaraAssert if file is not open
*/
std::size_t File::ReadBlock(void* buffer, std::size_t size) std::size_t File::ReadBlock(void* buffer, std::size_t size)
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
NazaraAssert(IsOpen(), "File is not opened"); NazaraAssert(IsOpen(), "File is not open");
if (size == 0) if (size == 0)
return 0; return 0;
@ -520,7 +812,7 @@ namespace Nz
return m_impl->Read(buffer, size); return m_impl->Read(buffer, size);
else else
{ {
// Si nous ne devons rien lire, nous avançons simplement // If we don't have to read, we move forward
UInt64 currentPos = m_impl->GetCursorPos(); UInt64 currentPos = m_impl->GetCursorPos();
m_impl->SetCursorPos(CursorPosition_AtCurrent, size); m_impl->SetCursorPos(CursorPosition_AtCurrent, size);
@ -529,11 +821,22 @@ namespace Nz
} }
} }
/*!
* \brief Writes blocks
* \return Number of blocks written
*
* \param buffer Preallocated buffer containing information to write
* \param size Size of the writting and thus of the buffer
*
* \remark Produces a NazaraAssert if file is not open
* \remark Produces a NazaraAssert if buffer is nullptr
*/
std::size_t File::WriteBlock(const void* buffer, std::size_t size) std::size_t File::WriteBlock(const void* buffer, std::size_t size)
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
NazaraAssert(IsOpen(), "File is not opened"); NazaraAssert(IsOpen(), "File is not open");
if (size == 0) if (size == 0)
return 0; return 0;
@ -543,9 +846,22 @@ namespace Nz
return m_impl->Write(buffer, size); return m_impl->Write(buffer, size);
} }
/*!
* \brief Appends the file to the hash
* \return true if hash is successful
*
* \param hash Hash to append data of the file
* \param originalFile Path of the file
*
* \remark Produces a NazaraAssert if hash is nullptr
* \remark Produces a NazaraError if file could not be open
* \remark Produces a NazaraError if file could not be read
*/
NAZARA_CORE_API bool HashAppend(AbstractHash* hash, const File& originalFile) NAZARA_CORE_API bool HashAppend(AbstractHash* hash, const File& originalFile)
{ {
NazaraAssert(hash, "Invalid hash");
File file(originalFile.GetPath()); File file(originalFile.GetPath());
if (!file.Open(OpenMode_ReadOnly)) if (!file.Open(OpenMode_ReadOnly))
{ {