127 lines
3.3 KiB
C++
127 lines
3.3 KiB
C++
#ifndef MATADOR_FILE_HPP
|
|
#define MATADOR_FILE_HPP
|
|
|
|
#include <string>
|
|
|
|
namespace matador {
|
|
|
|
/**
|
|
* File class representing file stream
|
|
*
|
|
* The open methods uses internally fopen() thus
|
|
* the open modes are the same:
|
|
*
|
|
* "r" read: Open file for input operations. The file must exist.
|
|
* "w" write: Create an empty file for output operations. If a
|
|
* file with the same name already exists, its contents are discarded and
|
|
* the file is treated as a new empty file.
|
|
* "a" append: Open file for output at the end of a file. Output operations
|
|
* always write data at the end of the file, expanding it. Repositioning
|
|
* operations (fseek, fsetpos, rewind) are ignored. The file is created
|
|
* if it does not exist.
|
|
* "r+" read/update: Open a file for update (both for input and output).
|
|
* The file must exist.
|
|
* "w+" write/update: Create an empty file and open it for update (both for input
|
|
* and output). If a file with the same name already exists its contents
|
|
* are discarded and the file is treated as a new empty file.
|
|
* "a+" append/update: Open a file for update (both for input and output) with
|
|
* all output operations writing data at the end of the file. Repositioning
|
|
* operations (fseek, fsetpos, rewind) affects the next input operations, but
|
|
* output operations move the position back to the end of file. The file is
|
|
* created if it does not exist.
|
|
*/
|
|
class file final
|
|
{
|
|
public:
|
|
/**
|
|
* Creates an uninitialized file.
|
|
*/
|
|
file() = default;
|
|
|
|
/**
|
|
* Creates and open a file stream.
|
|
*
|
|
* @param path Path of the file to create
|
|
* @param mode The file mode to open
|
|
*/
|
|
file(const char *path, const char *mode);
|
|
|
|
/**
|
|
* Creates and open a file stream.
|
|
*
|
|
* @param path Path of the file to create
|
|
* @param mode The file mode to open
|
|
*/
|
|
file(const std::string &path, const char *mode);
|
|
file(const file&) = delete;
|
|
file operator=(const file&) = delete;
|
|
~file();
|
|
|
|
/**
|
|
* Opens a file stream with the given path.
|
|
* If the file is already open it is closed
|
|
*
|
|
* @param path Path of the file to create
|
|
* @param mode The file mode to open
|
|
*/
|
|
void open(const char *path, const char *mode);
|
|
|
|
/**
|
|
* Opens a file stream with the given path.
|
|
* If the file is already open it is closed
|
|
*
|
|
* @param path Path of the file to create
|
|
* @param mode The file mode to open
|
|
*/
|
|
void open(const std::string &path, const char *mode);
|
|
|
|
/**
|
|
* Closes the file stream if it is open
|
|
*/
|
|
void close();
|
|
|
|
/**
|
|
* Returns the size of the file
|
|
*
|
|
* @return The size of the file
|
|
*/
|
|
[[nodiscard]] size_t size() const;
|
|
|
|
/**
|
|
* Returns the path to the file
|
|
*
|
|
* @return The path to the file
|
|
*/
|
|
[[nodiscard]] std::string path() const;
|
|
|
|
/**
|
|
* Returns the internal file stream pointer
|
|
*
|
|
* @return The internal file stream pointer
|
|
*/
|
|
[[nodiscard]] FILE* stream() const;
|
|
|
|
/**
|
|
* Returns true if file is open.
|
|
*
|
|
* @return True if file is open
|
|
*/
|
|
[[nodiscard]] bool is_open() const;
|
|
|
|
private:
|
|
std::string path_;
|
|
FILE *stream_ = nullptr;
|
|
};
|
|
|
|
/**
|
|
* Reads a given file as text and
|
|
* returns its content as string
|
|
*
|
|
* @param f File to read in
|
|
* @return The content of the file as string
|
|
*/
|
|
std::string read_as_text(const file &f);
|
|
|
|
}
|
|
#endif //MATADOR_FILE_HPP
|