lfilesystem  0.0.1
C++ filesystem library
lfilesystem_CFile.h
Go to the documentation of this file.
1 /*
2  * ======================================================================================
3  * __ ____ __ __ ____ ___
4  * ( ) (_ _)( \/ )( ___)/ __)
5  * )(__ _)(_ ) ( )__) \__ \
6  * (____)(____)(_/\/\_)(____)(___/
7  *
8  * This file is part of the Limes open source library and is licensed under the terms of the GNU Public License.
9  *
10  * Commercial licenses are available; contact the maintainers at ben.the.vining@gmail.com to inquire for details.
11  *
12  * ======================================================================================
13  */
14 
15 #pragma once
16 
17 #include <cstdio>
18 #include "lfilesystem/lfilesystem_Export.h"
20 
27 namespace limes::files
28 {
29 
30 class File;
31 
50 class LFILE_EXPORT CFile final
51 {
52 public:
54  enum class Mode
55  {
56  Read,
57  Write,
58  Append,
59  ReadExtended,
60  WriteExtended,
61  AppendExtended
62  };
63 
66 
68  CFile() = default;
69 
74  explicit CFile (std::FILE* fileHandle) noexcept;
75 
80  explicit CFile (const Path& filepath, Mode mode) noexcept;
81 
83  CFile (CFile&& other) noexcept;
84 
86 
90  ~CFile() noexcept;
91 
93  CFile& operator= (CFile&& other) noexcept;
94 
95  CFile (const CFile&) = delete;
96  CFile& operator=(const CFile&) = delete;
97 
100 
103  [[nodiscard]] std::FILE* get() const noexcept;
104 
106  std::FILE* operator->() const noexcept;
107 
111  std::FILE& operator*() const;
112 
116  [[nodiscard]] Path getPath() const;
117 
119  operator std::FILE*() const noexcept;
121 
127  void close() noexcept;
128 
134  bool open (const Path& filepath, Mode mode) noexcept;
135 
137  [[nodiscard]] bool isOpen() const noexcept;
138 
140  explicit operator bool() const noexcept;
141 
146  [[nodiscard]] File getFile() const;
147 
156  [[nodiscard]] static CFile createTempFile();
157 
158 private:
159  std::FILE* ptr { nullptr };
160 };
161 
162 } // namespace limes::files
This class is a wrapper around a C-style FILE* that takes care of freeing the file when the object is...
CFile(CFile &&other) noexcept
Move constructor.
CFile(const Path &filepath, Mode mode) noexcept
Creates a CFile referencing the given filepath, in the given mode.
CFile()=default
Creates a default CFile that holds a nullptr.
Mode
Represents possible modes a file can be opened in.
~CFile() noexcept
Destructor.
CFile(std::FILE *fileHandle) noexcept
Creates a CFile that takes ownership of the passed FILE* .
This class represents a file on the filesystem.
std::filesystem::path Path
Convenience typedef for filesystem paths.
This file defines the FilesystemEntry class.
Filesystem utilities.