CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
CppCommon::File Class Reference

Filesystem file. More...

#include <file.h>

Inheritance diagram for CppCommon::File:
CppCommon::Path CppCommon::Reader CppCommon::Writer

Public Member Functions

 File ()
 Initialize file with an empty path. More...
 
 File (const Path &path)
 Initialize file with a given path. More...
 
 File (const File &file)
 
 File (File &&file) noexcept
 
virtual ~File ()
 
Fileoperator= (const Path &path)
 
Fileoperator= (const File &file)
 
Fileoperator= (File &&file) noexcept
 
 operator bool () const noexcept
 Check if the file opened. More...
 
uint64_t offset () const
 Get the current read/write offset of the opened file. More...
 
uint64_t size () const
 Get the current file size. More...
 
bool IsFileExists () const
 Is the file exists? More...
 
bool IsFileEmpty () const
 Is the file empty? More...
 
bool IsFileOpened () const
 Is the file opened? More...
 
bool IsFileReadOpened () const
 Is the file opened for reading? More...
 
bool IsFileWriteOpened () const
 Is the file opened for writing? More...
 
void Create (bool read, bool write, const Flags< FileAttributes > &attributes=File::DEFAULT_ATTRIBUTES, const Flags< FilePermissions > &permissions=File::DEFAULT_PERMISSIONS, size_t buffer=File::DEFAULT_BUFFER)
 Create a new file. More...
 
void Open (bool read, bool write, bool truncate=false, const Flags< FileAttributes > &attributes=File::DEFAULT_ATTRIBUTES, const Flags< FilePermissions > &permissions=File::DEFAULT_PERMISSIONS, size_t buffer=File::DEFAULT_BUFFER)
 Open an existing file. More...
 
void OpenOrCreate (bool read, bool write, bool truncate=false, const Flags< FileAttributes > &attributes=File::DEFAULT_ATTRIBUTES, const Flags< FilePermissions > &permissions=File::DEFAULT_PERMISSIONS, size_t buffer=File::DEFAULT_BUFFER)
 Open or create file. More...
 
size_t Read (void *buffer, size_t size) override
 Read a bytes buffer from the opened file. More...
 
size_t Write (const void *buffer, size_t size) override
 Write a byte buffer into the opened file. More...
 
void Seek (uint64_t offset)
 Seek into the opened file. More...
 
void Resize (uint64_t size)
 Resize the current file. More...
 
void Flush () override
 Flush the file. More...
 
void Close ()
 Close the file. More...
 
void swap (File &file) noexcept
 Swap two instances. More...
 
std::vector< uint8_t > ReadAllBytes ()
 Read all bytes. More...
 
std::string ReadAllText ()
 Read all text. More...
 
std::vector< std::string > ReadAllLines ()
 Read all text lines. More...
 
virtual size_t Write (const void *buffer, size_t size)=0
 Write a byte buffer base method. More...
 
size_t Write (const std::string &text)
 Write a text string. More...
 
size_t Write (const std::vector< std::string > &lines)
 Write text lines. More...
 
- Public Member Functions inherited from CppCommon::Path
 Path ()
 Initialize path with an empty value. More...
 
 Path (const char *path)
 Initialize path with a given C-string value. More...
 
 Path (const std::string &path)
 Initialize path with a given string value. More...
 
 Path (const wchar_t *path)
 Initialize path with a given wide C-string value. More...
 
 Path (const std::wstring &path)
 Initialize path with a given wide string value. More...
 
 Path (const Path &)=default
 
 Path (Path &&)=default
 
 ~Path ()=default
 
Pathoperator= (const Path &)=default
 
Pathoperator= (Path &&)=default
 
 operator bool () const noexcept
 Check if the path is not empty. More...
 
Pathoperator/= (const Path &path)
 
Pathoperator+= (const Path &path)
 
const std::string & string () const noexcept
 Get the path value as UTF-8 string. More...
 
std::wstring wstring () const
 Get the path value as a wide string. More...
 
Path root () const
 Decompose root path from the current path. More...
 
Path relative () const
 Decompose relative path from the current path. More...
 
Path parent () const
 Decompose parent path from the current path. More...
 
Path filename () const
 Decompose filename from the current path. More...
 
Path stem () const
 Decompose stem from the current path. More...
 
Path extension () const
 Decompose extension from the current path. More...
 
Path absolute () const
 Transform the current path to the real path on a filesystem. More...
 
Path canonical () const
 Transform the current path and replace all '.' and '..' properly. More...
 
Path validate (char placeholder='_') const
 Transform the current path and replace all deprecated characters with a given placeholder (default is '_') More...
 
FileType type () const
 Get the path file type. More...
 
Flags< FileAttributesattributes () const
 Get the path file attributes. More...
 
Flags< FilePermissionspermissions () const
 Get the path file permissions. More...
 
UtcTimestamp created () const
 Get the path created UTC timestamp. More...
 
UtcTimestamp modified () const
 Get the path modified UTC timestamp. More...
 
size_t hardlinks () const
 Get the path count of hardlinks. More...
 
SpaceInfo space () const
 Get the path space information. More...
 
bool empty () const noexcept
 Is the path empty? More...
 
bool HasRoot () const
 Has root path? More...
 
bool HasRelative () const
 Has relative path? More...
 
bool HasParent () const
 Has parent path? More...
 
bool HasFilename () const
 Has filename? More...
 
bool HasStem () const
 Has stem? More...
 
bool HasExtension () const
 Has extension? More...
 
bool IsAbsolute () const
 Is absolute path? More...
 
bool IsRelative () const
 Is relative path? More...
 
bool IsExists () const
 Is the path exists? More...
 
bool IsRegularFile () const
 Is the path points to regular file? More...
 
bool IsDirectory () const
 Is the path points to directory? More...
 
bool IsSymlink () const
 Is the path points to symbolic link? More...
 
bool IsOther () const
 Is the path points to special file (block, character, FIFO, socket)? More...
 
bool IsEquivalent (const Path &path) const
 Is the current path is equivalent to the given one (points to the same node on a filesystem)? More...
 
void Clear () noexcept
 Clear the path content. More...
 
PathAssign (const Path &path)
 Assign the given path to the current one. More...
 
PathAppend (const Path &path)
 Append the given path to the current one. More...
 
PathConcat (const Path &path)
 Concatenate the given path to the current one. More...
 
PathMakePreferred ()
 Convert all path separators to system ones ('\' for Windows or '/' for Unix) More...
 
PathReplaceFilename (const Path &filename)
 Replace the current path filename with a given one. More...
 
PathReplaceExtension (const Path &extension)
 Replace the current path extension with a given one. More...
 
PathRemoveFilename ()
 Remove the current path filename. More...
 
PathRemoveExtension ()
 Remove the current path extension. More...
 
PathRemoveTrailingSeparators ()
 Remove all trailing separators form the current path. More...
 
void swap (Path &path) noexcept
 Swap two instances. More...
 
- Public Member Functions inherited from CppCommon::Reader
 Reader () noexcept=default
 
 Reader (const Reader &) noexcept=default
 
 Reader (Reader &&) noexcept=default
 
virtual ~Reader () noexcept=default
 
Readeroperator= (const Reader &) noexcept=default
 
Readeroperator= (Reader &&) noexcept=default
 
std::vector< uint8_t > ReadAllBytes ()
 Read all bytes. More...
 
std::string ReadAllText ()
 Read all text. More...
 
std::vector< std::string > ReadAllLines ()
 Read all text lines. More...
 
- Public Member Functions inherited from CppCommon::Writer
 Writer () noexcept=default
 
 Writer (const Writer &) noexcept=default
 
 Writer (Writer &&) noexcept=default
 
virtual ~Writer () noexcept=default
 
Writeroperator= (const Writer &) noexcept=default
 
Writeroperator= (Writer &&) noexcept=default
 
size_t Write (const std::string &text)
 Write a text string. More...
 
size_t Write (const std::vector< std::string > &lines)
 Write text lines. More...
 

Static Public Member Functions

static std::vector< uint8_t > ReadAllBytes (const Path &path)
 Read all bytes from the given file. More...
 
static std::string ReadAllText (const Path &path)
 Read all text from the given file. More...
 
static std::vector< std::string > ReadAllLines (const Path &path)
 Read all text lines from the given file. More...
 
static size_t WriteAllBytes (const Path &path, const void *buffer, size_t size)
 Write a bytes buffer into the given file. More...
 
static size_t WriteAllText (const Path &path, const std::string &text)
 Write a text string into the given file. More...
 
static size_t WriteAllLines (const Path &path, const std::vector< std::string > &lines)
 Write text lines into the given file. More...
 
static void WriteEmpty (const Path &path)
 Write an empty file. More...
 
- Static Public Member Functions inherited from CppCommon::Path
static bool deprecated (char character) noexcept
 Is the given character is deprecated by the filesystem? More...
 
static bool deprecated (wchar_t character) noexcept
 Is the given wide character is deprecated by the filesystem? More...
 
static std::string deprecated ()
 Get filesystem deprecated characters ('\', '/', '?', '', '*', ':', '|', '"', '<', '>') More...
 
static char separator () noexcept
 Get the system path separator character ('\' for Windows or '/' for Unix) More...
 
static Path initial ()
 Get the initial path of the process. More...
 
static Path current ()
 Get the current path of the process. More...
 
static Path executable ()
 Get the executable path of the process. More...
 
static Path home ()
 Get the home path of the process. More...
 
static Path temp ()
 Get the temporary path of the process. More...
 
static Path unique ()
 Get the unique filename in UUID format "00000000-0000-0000-0000-000000000000". More...
 
static Path Copy (const Path &src, const Path &dst, bool overwrite=false)
 Copy the given source path to destination path (file, empty directory, symlink, etc) More...
 
static Path CopyIf (const Path &src, const Path &dst, const std::string &pattern="", bool overwrite=false)
 Copy all matched files from the the given source path to destination path (files, directories, symlinks, etc) More...
 
static Path CopyAll (const Path &src, const Path &dst, bool overwrite=false)
 Recursively copy the given source path to destination path (files, directories, symlinks, etc) More...
 
static Path Rename (const Path &src, const Path &dst)
 Rename the given source path to destination path (file, empty directory, symlink, etc) More...
 
static Path Remove (const Path &path)
 Remove the given path (file, empty directory, symlink, etc) from the filesystem. More...
 
static Path RemoveAll (const Path &path)
 Recursively remove the given path (file, empty directory, symlink, etc) from the filesystem. More...
 
static Path RemoveIf (const Path &path, const std::string &pattern="")
 Recursively remove the given path matched to the given pattern (file, empty directory, symlink, etc) from the filesystem. More...
 
static void SetAttributes (const Path &path, const Flags< FileAttributes > &attributes)
 Set file attributes for the given path. More...
 
static void SetPermissions (const Path &path, const Flags< FilePermissions > &permissions)
 Set file permissions for the given path. More...
 
static void SetCreated (const Path &path, const UtcTimestamp &timestamp)
 Set created UTC timestamp for the given path. More...
 
static void SetModified (const Path &path, const UtcTimestamp &timestamp)
 Set modified UTC timestamp for the given path. More...
 
static void SetCurrent (const Path &path)
 Set the given path of the process as a current one. More...
 
static void Touch (const Path &path)
 Touch the given path and set its modified UTC timestamp to the current value. More...
 

Static Public Attributes

static const Flags< FileAttributesDEFAULT_ATTRIBUTES = FileAttributes::NORMAL
 Default file attributes (Normal) More...
 
static const Flags< FilePermissionsDEFAULT_PERMISSIONS = FilePermissions::IRUSR | FilePermissions::IWUSR | FilePermissions::IRGRP | FilePermissions::IROTH
 Default file permissions (IRUSR | IWUSR | IRGRP | IROTH) More...
 
static const size_t DEFAULT_BUFFER = 8192
 Default file buffer size (8192) More...
 

Friends

void swap (File &file1, File &file2) noexcept
 

Additional Inherited Members

- Protected Attributes inherited from CppCommon::Path
std::string _path
 Path string. More...
 

Detailed Description

Filesystem file.

Filesystem file wraps file management operations (create, open, read, write, flush, close).

Not thread-safe.

Examples
filesystem_file.cpp.

Definition at line 27 of file file.h.

Constructor & Destructor Documentation

◆ File() [1/4]

CppCommon::File::File ( )

Initialize file with an empty path.

Definition at line 656 of file file.cpp.

◆ File() [2/4]

CppCommon::File::File ( const Path path)

Initialize file with a given path.

Parameters
path- File path

Definition at line 667 of file file.cpp.

◆ File() [3/4]

CppCommon::File::File ( const File file)

Definition at line 678 of file file.cpp.

◆ File() [4/4]

CppCommon::File::File ( File &&  file)
noexcept

Definition at line 689 of file file.cpp.

◆ ~File()

CppCommon::File::~File ( )
virtual

Definition at line 694 of file file.cpp.

Member Function Documentation

◆ Close()

void CppCommon::File::Close ( )

Close the file.

If the file is not opened for writing the method will raise a filesystem exception!

Examples
filesystem_file.cpp.

Definition at line 765 of file file.cpp.

◆ Create()

void CppCommon::File::Create ( bool  read,
bool  write,
const Flags< FileAttributes > &  attributes = File::DEFAULT_ATTRIBUTES,
const Flags< FilePermissions > &  permissions = File::DEFAULT_PERMISSIONS,
size_t  buffer = File::DEFAULT_BUFFER 
)

Create a new file.

If the file with the same name is already exist the method will raise a filesystem exception!

Parameters
read- Read mode
write- Write mode
attributes- File attributes (default is File::DEFAULT_ATTRIBUTES)
permissions- File permissions (default is File::DEFAULT_PERMISSIONS)
buffer- File buffer size (default is File::DEFAULT_BUFFER)
Examples
filesystem_file.cpp.

Definition at line 755 of file file.cpp.

◆ Flush()

void CppCommon::File::Flush ( )
overridevirtual

Flush the file.

Flush any unwritten data of the opened file to the physical file on a disk. If the file is not opened for writing the method will raise a filesystem exception!

Reimplemented from CppCommon::Writer.

Examples
filesystem_file.cpp.

Definition at line 764 of file file.cpp.

◆ IsFileEmpty()

bool CppCommon::File::IsFileEmpty ( ) const
inline

Is the file empty?

Definition at line 64 of file file.h.

◆ IsFileExists()

bool CppCommon::File::IsFileExists ( ) const

Is the file exists?

Definition at line 715 of file file.cpp.

◆ IsFileOpened()

bool CppCommon::File::IsFileOpened ( ) const

Is the file opened?

Definition at line 751 of file file.cpp.

◆ IsFileReadOpened()

bool CppCommon::File::IsFileReadOpened ( ) const

Is the file opened for reading?

Definition at line 752 of file file.cpp.

◆ IsFileWriteOpened()

bool CppCommon::File::IsFileWriteOpened ( ) const

Is the file opened for writing?

Definition at line 753 of file file.cpp.

◆ offset()

uint64_t CppCommon::File::offset ( ) const

Get the current read/write offset of the opened file.

Definition at line 712 of file file.cpp.

◆ Open()

void CppCommon::File::Open ( bool  read,
bool  write,
bool  truncate = false,
const Flags< FileAttributes > &  attributes = File::DEFAULT_ATTRIBUTES,
const Flags< FilePermissions > &  permissions = File::DEFAULT_PERMISSIONS,
size_t  buffer = File::DEFAULT_BUFFER 
)

Open an existing file.

If the file with the same name is not exist the method will raise a filesystem exception!

Parameters
read- Read mode
write- Write mode
truncate- Truncate file (default is false)
attributes- File attributes (default is File::DEFAULT_ATTRIBUTES)
permissions- File permissions (default is File::DEFAULT_PERMISSIONS)
buffer- File buffer size (default is File::DEFAULT_BUFFER)

Definition at line 756 of file file.cpp.

◆ OpenOrCreate()

void CppCommon::File::OpenOrCreate ( bool  read,
bool  write,
bool  truncate = false,
const Flags< FileAttributes > &  attributes = File::DEFAULT_ATTRIBUTES,
const Flags< FilePermissions > &  permissions = File::DEFAULT_PERMISSIONS,
size_t  buffer = File::DEFAULT_BUFFER 
)

Open or create file.

Parameters
read- Read mode
write- Write mode
truncate- Truncate file (default is false)
attributes- File attributes (default is File::DEFAULT_ATTRIBUTES)
permissions- File permissions (default is File::DEFAULT_PERMISSIONS)
buffer- File buffer size (default is File::DEFAULT_BUFFER)

Definition at line 757 of file file.cpp.

◆ operator bool()

CppCommon::File::operator bool ( ) const
inlineexplicitnoexcept

Check if the file opened.

Definition at line 54 of file file.h.

◆ operator=() [1/3]

File & CppCommon::File::operator= ( const File file)

Definition at line 700 of file file.cpp.

◆ operator=() [2/3]

File& CppCommon::File::operator= ( const Path path)
inline

Definition at line 48 of file file.h.

◆ operator=() [3/3]

File & CppCommon::File::operator= ( File &&  file)
noexcept

Definition at line 706 of file file.cpp.

◆ Read()

size_t CppCommon::File::Read ( void *  buffer,
size_t  size 
)
overridevirtual

Read a bytes buffer from the opened file.

If the file is not opened for reading the method will raise a filesystem exception!

Parameters
buffer- Buffer to read
size- Buffer size
Returns
Count of read bytes

Implements CppCommon::Reader.

Definition at line 759 of file file.cpp.

◆ ReadAllBytes() [1/2]

std::vector< uint8_t > CppCommon::Reader::ReadAllBytes

Read all bytes.

Returns
Bytes buffer

Definition at line 46 of file reader.cpp.

◆ ReadAllBytes() [2/2]

std::vector< uint8_t > CppCommon::File::ReadAllBytes ( const Path path)
static

Read all bytes from the given file.

Parameters
path- File path
Returns
Bytes buffer

Definition at line 767 of file file.cpp.

◆ ReadAllLines() [1/2]

std::vector< std::string > CppCommon::Reader::ReadAllLines

Read all text lines.

Returns
Text lines

Definition at line 56 of file reader.cpp.

◆ ReadAllLines() [2/2]

std::vector< std::string > CppCommon::File::ReadAllLines ( const Path path)
static

Read all text lines from the given file.

Parameters
path- File path
Returns
Text lines

Definition at line 785 of file file.cpp.

◆ ReadAllText() [1/2]

std::string CppCommon::Reader::ReadAllText

Read all text.

Returns
Text string
Examples
filesystem_file.cpp.

Definition at line 51 of file reader.cpp.

◆ ReadAllText() [2/2]

std::string CppCommon::File::ReadAllText ( const Path path)
static

Read all text from the given file.

Parameters
path- File path
Returns
Text string

Definition at line 776 of file file.cpp.

◆ Resize()

void CppCommon::File::Resize ( uint64_t  size)

Resize the current file.

If the current file size is greater than required size then the file will be truncated with data loss. If the current file size is less than required size then the file will be extend.

Works for opened and not opened files!

Parameters
size- File size

Definition at line 763 of file file.cpp.

◆ Seek()

void CppCommon::File::Seek ( uint64_t  offset)

Seek into the opened file.

If the file is not opened for writing the method will raise a filesystem exception!

Parameters
offset- Seek offset

Definition at line 762 of file file.cpp.

◆ size()

uint64_t CppCommon::File::size ( ) const

Get the current file size.

Examples
filesystem_file.cpp.

Definition at line 713 of file file.cpp.

◆ swap()

void CppCommon::File::swap ( File file)
noexcept

Swap two instances.

Definition at line 828 of file file.cpp.

◆ Write() [1/4]

size_t CppCommon::Writer::Write

Write a text string.

Parameters
text- Text string
Returns
Count of written characters

Definition at line 46 of file writer.cpp.

◆ Write() [2/4]

size_t CppCommon::Writer::Write

Write text lines.

Parameters
lines- Text lines
Returns
Count of written lines

Definition at line 52 of file writer.cpp.

◆ Write() [3/4]

size_t CppCommon::File::Write ( const void *  buffer,
size_t  size 
)
overridevirtual

Write a byte buffer into the opened file.

If the file is not opened for writing the method will raise a filesystem exception!

Parameters
buffer- Buffer to write
size- Buffer size
Returns
Count of written bytes

Implements CppCommon::Writer.

Examples
filesystem_file.cpp.

Definition at line 760 of file file.cpp.

◆ Write() [4/4]

virtual size_t CppCommon::Writer::Write

Write a byte buffer base method.

Parameters
buffer- Buffer to write
size- Buffer size
Returns
Count of written bytes

◆ WriteAllBytes()

size_t CppCommon::File::WriteAllBytes ( const Path path,
const void *  buffer,
size_t  size 
)
static

Write a bytes buffer into the given file.

Parameters
path- File path
buffer- Buffer to write
size- Buffer size
Returns
Count of written bytes

Definition at line 794 of file file.cpp.

◆ WriteAllLines()

size_t CppCommon::File::WriteAllLines ( const Path path,
const std::vector< std::string > &  lines 
)
static

Write text lines into the given file.

Parameters
path- File path
lines- Text lines
Returns
Count of written lines

Definition at line 812 of file file.cpp.

◆ WriteAllText()

size_t CppCommon::File::WriteAllText ( const Path path,
const std::string &  text 
)
static

Write a text string into the given file.

Parameters
path- File path
text- Text string
Returns
Count of written characters

Definition at line 803 of file file.cpp.

◆ WriteEmpty()

void CppCommon::File::WriteEmpty ( const Path path)
static

Write an empty file.

Parameters
path- File path

Definition at line 821 of file file.cpp.

Friends And Related Function Documentation

◆ swap

void swap ( File file1,
File file2 
)
friend

Definition at line 11 of file file.inl.

Member Data Documentation

◆ DEFAULT_ATTRIBUTES

const Flags< FileAttributes > CppCommon::File::DEFAULT_ATTRIBUTES = FileAttributes::NORMAL
static

Default file attributes (Normal)

Definition at line 31 of file file.h.

◆ DEFAULT_BUFFER

const size_t CppCommon::File::DEFAULT_BUFFER = 8192
static

Default file buffer size (8192)

Definition at line 35 of file file.h.

◆ DEFAULT_PERMISSIONS

Default file permissions (IRUSR | IWUSR | IRGRP | IROTH)

Definition at line 33 of file file.h.


The documentation for this class was generated from the following files: