kge/File.h

93 lines
2.5 KiB
C++

///
/// \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_
#include <filesystem>
#include <fstream>
#include <ios>
#include <string>
#include "Defs.h"
typedef std::optional<std::ofstream *> OptOutFileStream;
typedef std::optional<std::ifstream *> OptInFileStream;
/// Files default to being read/write.
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);
File(std::filesystem::path fPath);
const std::string Path();
void SetPath(const std::string &fPath);
OptOutFileStream Flush();
OptInFileStream Refresh();
/// 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.
[[nodiscard]] bool IsReadOnly() const { return this->readOnly; };
void MarkReadOnly() { this->readOnly = true; }
void ClearReadOnly() { this->readOnly = false; }
/// 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();
private:
std::filesystem::path path;
bool readOnly;
};
typedef std::optional<File> OptFile;
#endif // KEPP__FILE_H_