2023-10-11 21:50:35 +00:00
|
|
|
///
|
|
|
|
/// \file File.h
|
|
|
|
/// \author kyle
|
|
|
|
/// \created 10/11/23
|
|
|
|
/// \brief File contains configuration information for a file.
|
|
|
|
///
|
|
|
|
/// \section COPYRIGHT
|
|
|
|
/// Copyright 2023 K. Isom <kyle@imap.cc>
|
|
|
|
///
|
|
|
|
/// Permission to use, copy, modify, and/or distribute this software for
|
|
|
|
/// any purpose with or without fee is hereby granted, provided that the
|
|
|
|
/// above copyright notice and this permission notice appear in all copies.
|
|
|
|
///
|
|
|
|
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
|
|
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
|
|
|
/// BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
|
|
|
/// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
|
|
/// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
|
|
/// ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
/// SOFTWARE.
|
|
|
|
///
|
|
|
|
|
|
|
|
#ifndef KEPP__FILE_H_
|
|
|
|
#define KEPP__FILE_H_
|
|
|
|
|
|
|
|
|
2023-10-12 06:27:42 +00:00
|
|
|
#include <filesystem>
|
2023-10-11 21:50:35 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <ios>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "Defs.h"
|
|
|
|
|
|
|
|
|
2023-10-12 06:27:42 +00:00
|
|
|
typedef std::optional<std::ofstream *> OptOutFileStream;
|
|
|
|
typedef std::optional<std::ifstream *> OptInFileStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Files default to being read/write.
|
2023-10-11 21:50:35 +00:00
|
|
|
static constexpr std::ios::openmode DefaultMode =
|
|
|
|
std::ios::in|std::ios::out;
|
|
|
|
|
|
|
|
|
|
|
|
/// A File abstracts a concrete file on disk.
|
|
|
|
class File {
|
|
|
|
public:
|
|
|
|
File(std::string fPath);
|
2023-10-12 07:22:30 +00:00
|
|
|
File(std::filesystem::path fPath);
|
2023-10-11 21:50:35 +00:00
|
|
|
|
2023-10-12 06:27:42 +00:00
|
|
|
const std::string Path();
|
2023-10-11 21:50:35 +00:00
|
|
|
void SetPath(const std::string &fPath);
|
|
|
|
|
|
|
|
|
2023-10-12 06:27:42 +00:00
|
|
|
OptOutFileStream Flush();
|
|
|
|
OptInFileStream Refresh();
|
2023-10-11 21:50:35 +00:00
|
|
|
|
2023-10-12 06:27:42 +00:00
|
|
|
/// The readonly attribute on a File is a virtual write
|
|
|
|
/// protection, and does not reflect the permissions on
|
|
|
|
/// the file itself. For that, see IsWriteable.
|
2023-10-11 21:50:35 +00:00
|
|
|
[[nodiscard]] bool IsReadOnly() const { return this->readOnly; };
|
|
|
|
void MarkReadOnly() { this->readOnly = true; }
|
|
|
|
void ClearReadOnly() { this->readOnly = false; }
|
|
|
|
|
2023-10-12 06:27:42 +00:00
|
|
|
/// Size returns the size of the file on disk.
|
|
|
|
size_t Size();
|
|
|
|
|
|
|
|
/// Exists checks whether the file exists on disk.
|
|
|
|
bool Exists();
|
|
|
|
|
|
|
|
/// IsWriteable checks whether the current user has write
|
|
|
|
/// permissions for the file.
|
|
|
|
bool IsWriteable();
|
|
|
|
|
|
|
|
/// IsReadable checks whether the current user has read
|
|
|
|
/// permissions on the file.
|
|
|
|
bool IsReadable();
|
|
|
|
|
|
|
|
/// IsReadWriteable checks whether the current user has both
|
|
|
|
/// read and write permissions on the file.
|
|
|
|
bool IsReadWriteable();
|
2023-10-11 21:50:35 +00:00
|
|
|
private:
|
2023-10-12 06:27:42 +00:00
|
|
|
|
|
|
|
std::filesystem::path path;
|
2023-10-11 21:50:35 +00:00
|
|
|
bool readOnly;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef std::optional<File> OptFile;
|
|
|
|
|
|
|
|
#endif // KEPP__FILE_H_
|