Files

299 lines
8.2 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/File.h
*
* @brief Header file for file class.
*/
#if (!defined ACTIV_BASE_FILE_H)
#define ACTIV_BASE_FILE_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/ActivBase/IFile.h"
//! Activ namespace.
namespace Activ
{
/**
* @brief File class.
*/
class File ACTIV_FINAL : public IFile
{
ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(File)
public:
static const FileType FILE_TYPE = FILE_TYPE_NORMAL; ///< The file type.
//! Default constructor.
ACTIV_BASE_API File();
/**
* @brief Move constructor.
*
* @param rhs transfer ownership of the underlying file managed by @a rhs to this.
*/
File(ACTIV_BOOST_RV_REF(File) rhs) :
IFile(ACTIV_BOOST_MOVE_BASE(IFile, rhs))
{
// Inline here for G++.
}
/**
* @brief Move assignment operator.
*
* Transfer ownership of a file from another IFile object to this.
*
* If this object has a file open, it is closed. The underlying file associated with rhs is transferred to this object,
* and no file is then associated with rhs.
*
* @param rhs the file to transfer to this object.
*
* @return *this
*/
File &operator=(ACTIV_BOOST_RV_REF(File) rhs)
{
// Inline here for G++.
if (this != &rhs)
IFile::operator=(ACTIV_BOOST_MOVE_BASE(IFile, rhs));
return *this;
}
using IFile::GetSize;
using IFile::GetLastWriteDateTime;
using IFile::GetCreationDateTime;
using IFile::GetLastAccessDateTime;
/**
* @brief Copy a file.
*
* @param sourceFileName the name of the existing file.
* @param targetFileName the name of the new file.
* @param replaceExisting whether an existing file should be overwritten.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_FAILURE
* @retval ...
*/
ACTIV_BASE_API static StatusCode Copy(const String &sourceFileName, const String &targetFileName, bool replaceExisting = false);
/**
* @brief Remove a file.
*
* @param fileName the file to remove.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_BASE_API static StatusCode Remove(const String &fileName);
/**
* @brief Rename a file.
*
* @param sourceFileName the name of the existing file.
* @param targetFileName the name of the new file.
* @param replaceExisting whether an existing file should be overwritten.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_BASE_API static StatusCode Rename(const String &sourceFileName, const String &targetFileName,
bool replaceExisting = false);
/**
* @brief Get the size of a file.
*
* @param fileName the file name.
* @param size the size of the file.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_NOT_OPEN
* @retval STATUS_CODE_FAILURE
* @retval ...
*/
ACTIV_BASE_API static StatusCode GetSize(const String &fileName, uint64_t &size);
/**
* @brief Get the last write date time of a file.
*
* @param fileName the file name.
* @param tm reference to a tm structure to receive the UTC date / time.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_NOT_OPEN
* @retval ...
*/
ACTIV_BASE_API static StatusCode GetLastWriteDateTime(const String &fileName, std::tm &tm);
/**
* @brief Get the creation date time of a file.
*
* @param fileName the file name.
* @param tm reference to a tm structure to receive the UTC date / time.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_NOT_OPEN
* @retval ...
*/
ACTIV_BASE_API static StatusCode GetCreationDateTime(const String &fileName, std::tm &tm);
/**
* @brief Get the last access date time of a file.
*
* @param fileName the file name.
* @param tm reference to a tm structure to receive the UTC date / time.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_NOT_OPEN
* @retval ...
*/
ACTIV_BASE_API static StatusCode GetLastAccessDateTime(const String &fileName, std::tm &tm);
/**
* @brief Does a file exist?
*
* @param fileName the file to find.
*
* @return whether the file exists.
*/
ACTIV_BASE_API static bool IsFound(const String &fileName);
/**
* @brief Is a path a directory?
*
* @param pathName path to check.
*
* @return boolean.
*/
ACTIV_BASE_API static bool IsDirectory(const String &pathName);
/**
* @brief Is a path absolute?
*
* @param pathName path to check.
*
* @return boolean.
*/
ACTIV_BASE_API static bool IsAbsolutePath(const String &pathName);
//! Get the end of line characters for this platform.
ACTIV_BASE_API static const String &GetEndOfLine();
//! Get directory separator character for this platform.
ACTIV_BASE_API static char GetDirectorySeparator();
/**
* @brief Get temporary directory path.
*
* Note the returned path may or may not contain a trailing directory separator,
* so use File::MakePath() to construct the path of a file in the temporary directory.
*
* @return Temporary directory path.
*/
ACTIV_BASE_API static String GetTemporaryPath();
/**
* @brief Create a path from directory and file name.
*
* @param directory the directory.
* @param fileName the file name.
*
* @return Full pathname.
*/
ACTIV_BASE_API static String MakePath(const String &directory, const String &fileName);
/**
* @brief Create an absolute path from a relative one.
*
* @param path The relative path.
*
* @return Absolute pathname with no trailing directory separator, unless a root directory.
*/
ACTIV_BASE_API static String MakeAbsolutePath(const String &path);
/**
* @brief Get the directory and filename from a path.
*
* SplitPath() will not append a trailing directory separator to @a directory unless it is a root directory. Some examples:
* \verbatim
path directory fileName
file file
c: c:
c:\ c:\
\ \
\file \ file
.\file . file
..\..\ ..\..
c:\dir1\dir2\file c:\dir1\dir2 file
dir1\dir2\file dir1\dir2 file
/ /
/file / file
./file . file
../../ ../..
/dir1/dir2/file /dir1/dir2 file
dir1/dir2/file dir1/dir2 file
~ ~
~/file ~ file
~/dir/ ~/dir
~/dir/file ~/dir file
~user ~user
~user/file ~user file
~user/dir/ ~user/dir
~user/dir/file ~user/dir file
\endverbatim
* SplitPath() does not do any checking that the trailing path component is in fact a file.
* For instance, the following is possible:
* \verbatim
/dir1/dir2/dir3 /dir1/dir2 dir3
\endverbatim
* @param path the path to split.
* @param directory to receive the directory component of @a path.
* @param fileName to receive the file name component of @a path.
*/
ACTIV_BASE_API static void SplitPath(const String &path, String &directory, String &fileName);
/**
* @brief Expand ~ in a path.
*
* @param path the path to expand.
*
* @return The path with a leading ~ expanded, if possible.
*/
ACTIV_BASE_API static String ExpandPath(const String &path);
/**
* @brief Search the current path for a file.
*
* NB the current directory is always included in the search.
*
* @param fileName the file (without any directory component) to search for.
* @param fullyQualifiedFileName reference to a string to receive the path + filename of the found file.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_NOT_FOUND
* @retval STATUS_CODE_FAILURE
* @retval ...
*/
ACTIV_BASE_API static StatusCode SearchPath(const String &fileName, String &fullyQualifiedFileName);
/**
* @brief Is the provided path name just a file name?
*
* @param pathName path to check.
*
* @return true if \a pathName is just a file name.
*/
ACTIV_BASE_API static bool IsFileNameOnly(const String &pathName);
protected:
ACTIV_BASE_API virtual StatusCode Decode(void *pBuffer, const size_t nBytesToRead, size_t &nBytesRead) ACTIV_OVERRIDE;
ACTIV_BASE_API virtual StatusCode Encode(const void *pBuffer, const size_t nBytesToWrite, size_t &nBytesWritten) ACTIV_OVERRIDE;
};
} // namespace Activ
#endif // (!defined ACTIV_BASE_FILE_H)