lfilesystem  0.0.1
C++ filesystem library
lfilesystem_File.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 <cstddef> // for size_t
18 #include <string> // for string
19 #include <string_view>
20 #include <vector> // for vector
21 #include <functional> // for std::hash
22 #include <optional>
23 #include <fstream>
24 #include <memory>
25 #include <iterator>
26 #include "lfilesystem/lfilesystem_Export.h"
27 #include "lfilesystem/lfilesystem_FilesystemEntry.h" // for FilesystemEntry, Path
29 
36 namespace limes::files
37 {
38 
39 #pragma mark File
40 #pragma region File
41 
61 class LFILE_EXPORT File : public FilesystemEntry
62 {
63 public:
65 
66  File (const File&) = default;
67  File& operator= (const File&) = default;
68 
69  File (File&&) = default;
70  File& operator= (File&&) = default;
71 
74 
82  [[nodiscard]] std::string getFilename (bool includeExtension = false) const;
83 
87  [[nodiscard]] std::string getFileExtension() const;
88 
92  [[nodiscard]] bool hasFileExtension (const std::string_view& extension) const;
93 
95  [[nodiscard]] bool hasFileExtension() const;
96 
102  [[nodiscard]] bool isMacOSBundle() const noexcept;
103 
104  [[nodiscard]] bool isFile() const noexcept final;
105  [[nodiscard]] bool isDirectory() const noexcept final;
106  [[nodiscard]] bool isSymLink() const noexcept final;
107 
109 
112 
128  bool replaceFileExtension (const std::string_view& newFileExtension,
129  bool renameOnDisk = true);
130 
134  File& operator= (const Path& newPath);
135 
139  File& operator= (const std::string_view& newPath);
140 
142 
145 
149  [[nodiscard]] std::string loadAsString() const noexcept;
150 
155  [[nodiscard]] std::vector<std::string> loadAsLines() const;
156 
161  [[nodiscard]] std::unique_ptr<std::ifstream> getInputStream() const;
162 
164 
167 
170  bool overwrite (const char* const data, std::size_t numBytes) const noexcept;
171  bool overwrite (const std::string_view& text) const noexcept;
172 
177  [[nodiscard]] std::unique_ptr<std::ofstream> getOutputStream() const;
178 
180 
183 
191  bool append (const char* const data, std::size_t numBytes) const noexcept;
192  bool append (const std::string_view& text) const noexcept;
194 
197 
200  bool prepend (const char* const data, std::size_t numBytes) const noexcept;
201  bool prepend (const std::string_view& text) const noexcept;
203 
218  bool resize (std::uintmax_t newSizeInBytes, bool allowTruncation = true, bool allowIncreasing = true) const noexcept;
219 
222 
235  std::optional<File> createHardLink (const Path& path) const;
236 
243  [[nodiscard]] std::uintmax_t getHardLinkCount() const noexcept;
244 
246 
278  std::optional<File> duplicate() const noexcept;
279 
285  [[nodiscard]] CFile getCfile (CFile::Mode mode = CFile::Mode::Read) const noexcept;
286 
294  struct LFILE_EXPORT Iterator final
295  {
296  public:
297  using iterator_category = std::forward_iterator_tag;
298  using value_type = std::string;
299  using difference_type = std::ptrdiff_t;
300  using pointer = std::string*;
301  using reference = std::string&;
302 
303  Iterator& operator++();
304  [[nodiscard]] Iterator operator++ (int);
305  bool operator== (Iterator other);
306  bool operator!= (Iterator other);
307 
308  reference operator*() const;
309  pointer operator->() const;
310 
311  explicit Iterator();
312 
313  Iterator (const Iterator&) = default;
314  Iterator& operator= (const Iterator&) = default;
315 
316  Iterator (Iterator&&) = default;
317  Iterator& operator= (Iterator&&) = default;
318 
319  private:
320  using VectorType = std::vector<std::string>;
321 
322  explicit Iterator (VectorType&& v);
323 
324  std::shared_ptr<VectorType> lines { nullptr };
325 
326  VectorType::size_type idx { 0UL };
327 
328  friend class File;
329  };
330 
332  [[nodiscard]] Iterator begin() const;
333 
335  [[nodiscard]] Iterator end() const;
336 
345  [[nodiscard]] static File getCurrentExecutable();
346 
356  [[nodiscard]] static File getCurrentModule();
357 
358 private:
359  [[nodiscard]] bool write_data (const char* const data, std::size_t numBytes, bool overwrite) const noexcept;
360 };
361 
362 /*-------------------------------------------------------------------------------------------------------------------------*/
363 
364 #pragma mark TempFile
365 #pragma endregion
366 #pragma region TempFile
367 
382 class LFILE_EXPORT TempFile final : public File
383 {
384 public:
396  explicit TempFile (const Path& filepath,
397  bool destroyOnDelete = true);
398 
402  ~TempFile() final;
403 
404  TempFile (const TempFile&) = delete;
405  TempFile& operator= (const TempFile&) = delete;
406 
409 
410  TempFile (TempFile&& other) noexcept;
411 
413  TempFile& operator= (TempFile&& other) noexcept;
415 
420  [[nodiscard]] static TempFile getNextFile();
421 
422 private:
423  bool shouldDelete { true };
424 };
425 
431 std::ostream& operator<< (std::ostream& os, const File& file);
432 
438 std::istream& operator>> (std::istream& is, const File& file);
439 
440 } // namespace limes::files
441 
442 namespace std
443 {
444 
450 template <>
451 struct LFILE_EXPORT hash<limes::files::File> final
452 {
453  size_t operator() (const limes::files::File& f) const noexcept;
454 };
455 } // namespace std
This class is a wrapper around a C-style FILE* that takes care of freeing the file when the object is...
This class represents a file on the filesystem.
bool hasFileExtension(const std::string_view &extension) const
Returns true if this file has the specified file extension.
std::string getFileExtension() const
Returns this file's file extension, if any.
bool hasFileExtension() const
Returns true if this file has a file extension.
static File getCurrentExecutable()
Returns a file representing the location of the executable file that launched the current process.
std::string getFilename(bool includeExtension=false) const
Returns this file's filename.
Iterator begin() const
Returns an iterator to the first line in this file.
Iterator end() const
Returns an iterator to the last line in this file.
bool isMacOSBundle() const noexcept
Returns true if this file is a MacOS bundle.
static File getCurrentModule()
Returns a file representing the location of the current code module.
The base class for any kind of object on the filesystem.
FilesystemEntry()=default
Creates a FilesystemEntry with an empty path.
Represents a temporary file.
TempFile(const Path &filepath, bool destroyOnDelete=true)
Creates a temporary file with the specified path.
~TempFile() final
The temporary file will be deleted from the filesystem when this object is destroyed,...
std::filesystem::path Path
Convenience typedef for filesystem paths.
This file defines the CFile class.
This file defines the FilesystemEntry class.
Filesystem utilities.
An iterator class that allows iterating a file like a standard C++ container.