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

@@ -30,23 +30,51 @@
namespace Nz
{
/*!
* \class Nz::File
* \brief Core class that represents a file
*/
/*!
* \brief Constructs a File object by default
*/
File::File() :
m_impl(nullptr)
{
}
/*!
* \brief Constructs a File object with a file path
*
* \param filePath Path to the file
*/
File::File(const String& filePath) :
File()
{
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()
{
Open(filePath, openMode);
}
/*!
* \brief Constructs a File object by move semantic
*
* \param file File to move into this
*/
File::File(File&& file) noexcept :
Stream(std::move(file)),
m_filePath(std::move(file.m_filePath)),
@@ -55,16 +83,33 @@ namespace Nz
file.m_impl = nullptr;
}
/*!
* \brief Destructs the object and calls Close
*
* \see Close
*/
File::~File()
{
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)
{
return Copy(m_filePath, newFilePath);
}
/*!
* \brief Closes the file
*/
void File::Close()
{
NazaraLock(m_mutex)
@@ -79,6 +124,11 @@ namespace Nz
}
}
/*!
* \brief Deletes the file
* \return true if delete is successful
*/
bool File::Delete()
{
NazaraLock(m_mutex)
@@ -88,6 +138,13 @@ namespace Nz
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
{
NazaraLock(m_mutex)
@@ -95,7 +152,7 @@ namespace Nz
#if NAZARA_CORE_SAFE
if (!IsOpen())
{
NazaraError("File not opened");
NazaraError("File not open");
return false;
}
#endif
@@ -103,11 +160,25 @@ namespace Nz
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
{
return EndOfFile();
}
/*!
* \brief Checks whether the file exists
* \return true if file exists
*/
bool File::Exists() const
{
NazaraLock(m_mutex)
@@ -118,6 +189,11 @@ namespace Nz
return Exists(m_filePath);
}
/*!
* \brief Gets the creation time of the file
* \return Information about the creation time
*/
time_t File::GetCreationTime() const
{
NazaraLock(m_mutex)
@@ -125,15 +201,27 @@ namespace Nz
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
{
NazaraLock(m_mutex)
NazaraAssert(IsOpen(), "File is not opened");
NazaraAssert(IsOpen(), "File is not open");
return m_impl->GetCursorPos();
}
/*!
* \brief Gets the directory of the file
* \return Directory of the file
*/
String File::GetDirectory() const
{
NazaraLock(m_mutex)
@@ -141,6 +229,11 @@ namespace Nz
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
{
NazaraLock(m_mutex)
@@ -148,6 +241,11 @@ namespace Nz
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
{
NazaraLock(m_mutex)
@@ -155,6 +253,11 @@ namespace Nz
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
{
NazaraLock(m_mutex)
@@ -162,6 +265,11 @@ namespace Nz
return GetLastWriteTime(m_filePath);
}
/*!
* \brief Gets the path of the file
* \return Path of the file
*/
String File::GetPath() const
{
NazaraLock(m_mutex)
@@ -169,6 +277,11 @@ namespace Nz
return m_filePath;
}
/*!
* \brief Gets the size of the file
* \return Size of the file
*/
UInt64 File::GetSize() const
{
NazaraLock(m_mutex)
@@ -176,6 +289,11 @@ namespace Nz
return GetSize(m_filePath);
}
/*!
* \brief Checks whether the file is open
* \return true if open
*/
bool File::IsOpen() const
{
NazaraLock(m_mutex)
@@ -183,22 +301,14 @@ namespace Nz
return m_impl != nullptr;
}
bool File::Rename(const String& newFilePath)
{
NazaraLock(m_mutex)
bool opened = IsOpen();
Close();
bool success = Rename(m_filePath, newFilePath);
if (success)
m_filePath = NormalizePath(newFilePath);
if (opened)
Open();
return success;
}
/*!
* \brief Opens the file with flags
* \return true if opening is successful
*
* \param openMode Flag for file
*
* \remark Produces a NazaraError if OS error to open a file
*/
bool File::Open(unsigned int openMode)
{
@@ -215,7 +325,7 @@ namespace Nz
std::unique_ptr<FileImpl> impl(new FileImpl(this));
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());
return false;
}
@@ -231,6 +341,16 @@ namespace Nz
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)
{
NazaraLock(m_mutex)
@@ -241,24 +361,72 @@ namespace Nz
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)
{
NazaraLock(m_mutex)
NazaraAssert(IsOpen(), "File is not opened");
NazaraAssert(IsOpen(), "File is not open");
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)
{
NazaraLock(m_mutex)
NazaraAssert(IsOpen(), "File is not opened");
NazaraAssert(IsOpen(), "File is not open");
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)
{
NazaraLock(m_mutex)
@@ -285,6 +453,13 @@ namespace Nz
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)
{
SetFile(filePath);
@@ -292,6 +467,13 @@ namespace Nz
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
{
NazaraLock(m_mutex)
@@ -302,9 +484,18 @@ namespace Nz
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)
{
// 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);
if (path.IsEmpty())
return String();
@@ -319,7 +510,7 @@ namespace Nz
base = "\\\\";
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 end = path.SubString(1, -1);
@@ -351,14 +542,14 @@ namespace Nz
if (path.Split(sep, NAZARA_DIRECTORY_SEPARATOR) <= 1)
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)
{
if (sep[i] == '.')
sep.erase(sep.begin() + 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--);
@@ -377,6 +568,14 @@ namespace Nz
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)
{
if (sourcePath.IsEmpty() || targetPath.IsEmpty())
@@ -385,6 +584,13 @@ namespace Nz
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)
{
if (filePath.IsEmpty())
@@ -393,6 +599,13 @@ namespace Nz
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)
{
if (filePath.IsEmpty())
@@ -401,6 +614,13 @@ namespace Nz
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)
{
if (filePath.IsEmpty())
@@ -409,10 +629,23 @@ namespace Nz
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)
{
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)
{
@@ -422,6 +655,13 @@ namespace Nz
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)
{
if (filePath.IsEmpty())
@@ -430,6 +670,13 @@ namespace Nz
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)
{
if (filePath.IsEmpty())
@@ -438,6 +685,13 @@ namespace Nz
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)
{
String path(filePath.Trimmed());
@@ -451,7 +705,7 @@ namespace Nz
return true;
else if (path.Match("\\\\*")) // Ex: \\Laptop
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;
else
return false;
@@ -462,6 +716,13 @@ namespace Nz
#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 path = NormalizeSeparators(filePath.Trimmed());
@@ -475,6 +736,13 @@ namespace Nz
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 path(filePath);
@@ -490,6 +758,14 @@ namespace Nz
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)
{
if (sourcePath.IsEmpty() || targetPath.IsEmpty())
@@ -498,6 +774,12 @@ namespace Nz
return FileImpl::Rename(NormalizePath(sourcePath), NormalizePath(targetPath));
}
/*!
* \brief Flushes the stream
*
* \remark Produces a NazaraAssert if file is not open
*/
void File::FlushStream()
{
NazaraLock(m_mutex)
@@ -507,11 +789,21 @@ namespace Nz
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)
{
NazaraLock(m_mutex)
NazaraAssert(IsOpen(), "File is not opened");
NazaraAssert(IsOpen(), "File is not open");
if (size == 0)
return 0;
@@ -520,7 +812,7 @@ namespace Nz
return m_impl->Read(buffer, size);
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();
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)
{
NazaraLock(m_mutex)
NazaraAssert(IsOpen(), "File is not opened");
NazaraAssert(IsOpen(), "File is not open");
if (size == 0)
return 0;
@@ -543,9 +846,22 @@ namespace Nz
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)
{
NazaraAssert(hash, "Invalid hash");
File file(originalFile.GetPath());
if (!file.Open(OpenMode_ReadOnly))
{