CppCommon  1.0.4.1
C++ Common Library
file.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_FILESYSTEM_FILE_H
10 #define CPPCOMMON_FILESYSTEM_FILE_H
11 
12 #include "common/reader.h"
13 #include "common/writer.h"
14 #include "filesystem/path.h"
15 
16 #include <memory>
17 #include <vector>
18 
19 namespace CppCommon {
20 
22 
27 class File : public Path, public Reader, public Writer
28 {
29 public:
35  static const size_t DEFAULT_BUFFER;
36 
38  File();
40 
43  File(const Path& path);
44  File(const File& file);
45  File(File&& file) noexcept;
46  virtual ~File();
47 
48  File& operator=(const Path& path)
49  { Assign(path); return *this; }
50  File& operator=(const File& file);
51  File& operator=(File&& file) noexcept;
52 
54  explicit operator bool() const noexcept { return IsFileOpened(); }
55 
57  uint64_t offset() const;
59  uint64_t size() const;
60 
62  bool IsFileExists() const;
64  bool IsFileEmpty() const
65  { return (size() == 0); }
67  bool IsFileOpened() const;
69  bool IsFileReadOpened() const;
71  bool IsFileWriteOpened() const;
72 
74 
86 
97  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);
99 
107  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);
108 
110 
118  size_t Read(void* buffer, size_t size) override;
119 
120  using Reader::ReadAllBytes;
121  using Reader::ReadAllText;
122  using Reader::ReadAllLines;
123 
125 
133  size_t Write(const void* buffer, size_t size) override;
134 
135  using Writer::Write;
136 
138 
144  void Seek(uint64_t offset);
145 
147 
156  void Resize(uint64_t size);
157 
159 
164  void Flush() override;
165 
167 
171  void Close();
172 
174 
178  static std::vector<uint8_t> ReadAllBytes(const Path& path);
180 
184  static std::string ReadAllText(const Path& path);
186 
190  static std::vector<std::string> ReadAllLines(const Path& path);
191 
193 
199  static size_t WriteAllBytes(const Path& path, const void* buffer, size_t size);
201 
206  static size_t WriteAllText(const Path& path, const std::string& text);
208 
213  static size_t WriteAllLines(const Path& path, const std::vector<std::string>& lines);
214 
216 
219  static void WriteEmpty(const Path& path);
220 
222  void swap(File& file) noexcept;
223  friend void swap(File& file1, File& file2) noexcept;
224 
225 private:
226  class Impl;
227 
228  Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
229  const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
230 
231  static const size_t StorageSize = 128;
232  static const size_t StorageAlign = 8;
233  alignas(StorageAlign) std::byte _storage[StorageSize];
234 };
235 
238 } // namespace CppCommon
239 
240 #include "file.inl"
241 
242 #endif // CPPCOMMON_FILESYSTEM_FILE_H
Filesystem file.
Definition: file.h:28
void Close()
Close the file.
Definition: file.cpp:765
size_t Read(void *buffer, size_t size) override
Read a bytes buffer from the opened file.
Definition: file.cpp:759
static const Flags< FilePermissions > DEFAULT_PERMISSIONS
Default file permissions (IRUSR | IWUSR | IRGRP | IROTH)
Definition: file.h:33
void Resize(uint64_t size)
Resize the current file.
Definition: file.cpp:763
static size_t WriteAllLines(const Path &path, const std::vector< std::string > &lines)
Write text lines into the given file.
Definition: file.cpp:812
static size_t WriteAllText(const Path &path, const std::string &text)
Write a text string into the given file.
Definition: file.cpp:803
File()
Initialize file with an empty path.
Definition: file.cpp:656
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.
Definition: file.cpp:757
uint64_t size() const
Get the current file size.
Definition: file.cpp:713
bool IsFileReadOpened() const
Is the file opened for reading?
Definition: file.cpp:752
virtual ~File()
Definition: file.cpp:694
virtual size_t Write(const void *buffer, size_t size)=0
Write a byte buffer base method.
bool IsFileEmpty() const
Is the file empty?
Definition: file.h:64
bool IsFileOpened() const
Is the file opened?
Definition: file.cpp:751
static const size_t DEFAULT_BUFFER
Default file buffer size (8192)
Definition: file.h:35
void Flush() override
Flush the file.
Definition: file.cpp:764
bool IsFileWriteOpened() const
Is the file opened for writing?
Definition: file.cpp:753
File & operator=(const Path &path)
Definition: file.h:48
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.
Definition: file.cpp:755
bool IsFileExists() const
Is the file exists?
Definition: file.cpp:715
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.
Definition: file.cpp:756
std::vector< std::string > ReadAllLines()
Read all text lines.
Definition: reader.cpp:38
void Seek(uint64_t offset)
Seek into the opened file.
Definition: file.cpp:762
static const Flags< FileAttributes > DEFAULT_ATTRIBUTES
Default file attributes (Normal)
Definition: file.h:31
std::vector< uint8_t > ReadAllBytes()
Read all bytes.
Definition: reader.cpp:15
std::string ReadAllText()
Read all text.
Definition: reader.cpp:32
void swap(File &file) noexcept
Swap two instances.
Definition: file.cpp:828
static size_t WriteAllBytes(const Path &path, const void *buffer, size_t size)
Write a bytes buffer into the given file.
Definition: file.cpp:794
static void WriteEmpty(const Path &path)
Write an empty file.
Definition: file.cpp:821
uint64_t offset() const
Get the current read/write offset of the opened file.
Definition: file.cpp:712
Filesystem path.
Definition: path.h:90
Path & Assign(const Path &path)
Assign the given path to the current one.
Definition: path.inl:20
Flags< FileAttributes > attributes() const
Get the path file attributes.
Definition: path.cpp:472
Flags< FilePermissions > permissions() const
Get the path file permissions.
Definition: path.cpp:499
Reader interface.
Definition: reader.h:24
std::vector< std::string > ReadAllLines()
Read all text lines.
Definition: reader.cpp:38
std::vector< uint8_t > ReadAllBytes()
Read all bytes.
Definition: reader.cpp:15
std::string ReadAllText()
Read all text.
Definition: reader.cpp:32
Writer interface.
Definition: writer.h:23
virtual size_t Write(const void *buffer, size_t size)=0
Write a byte buffer base method.
Filesystem file inline implementation.
C++ Common project definitions.
Definition: token_bucket.h:15
Filesystem path definition.
Reader interface definition.
Writer interface definition.