Skip to content
Snippets Groups Projects
Commit 23f5481f authored by Franck Dary's avatar Franck Dary
Browse files

Finished documentation for File

parent 9340683e
No related branches found
No related tags found
No related merge requests found
/// @file File.hpp
/// @author Franck Dary
/// @version 1.0
/// @date 2018-08-03
#ifndef FILE__H #ifndef FILE__H
#define FILE__H #define FILE__H
...@@ -7,39 +12,96 @@ ...@@ -7,39 +12,96 @@
#include <functional> #include <functional>
#include "util.hpp" #include "util.hpp"
/// @brief A class for opening and reading a file.
///
/// It can simply be seen as a wrapper for FILE *\n
/// It provides usefull functions and also permit to unget a large number of char
/// in a row, something that we can't do with FILE * alone.
class File class File
{ {
public : public :
/// @brief Instance of File wrapped around stdout.
static File stdOut; static File stdOut;
private : private :
/// @brief Construct a File from a FILE *
///
/// @param file Already opened file.
File(FILE * file); File(FILE * file);
private : private :
/// @brief Pointer to the file description.
FILE * file; FILE * file;
/// @brief A buffer used to store char from ungetChar.
std::stack<char> buffer; std::stack<char> buffer;
/// @brief Whether or not the whole file has been read.
bool endHasBeenReached; bool endHasBeenReached;
/// @brief The filename, usefull for the rewind function.
std::string filename; std::string filename;
public : public :
/// @brief Construct and open a new file.
///
/// @param filename The filename of the file.
/// @param mode C's style opening mode. "r"ead or "w"rite.
File(const std::string & filename, const std::string & mode); File(const std::string & filename, const std::string & mode);
/// @brief File is not moveable.
///
/// @param model
File(File && model) = delete; File(File && model) = delete;
/// @brief File is not copyable.
///
/// @param model
///
/// @return
File & operator=(const File & model) = delete; File & operator=(const File & model) = delete;
/// @brief The destructor, in which we close the file.
~File(); ~File();
/// @brief Peek the next char in the file but without consuming it.
///
/// @return The next char in the file.
char peek(); char peek();
/// @brief Whether or not the whole file has been read.
///
/// @return Whether or not the whole file has been read.
bool isFinished(); bool isFinished();
/// @brief Get the next char in the file and consume it.
///
/// @return The next char in the file.
char getChar(); char getChar();
/// @brief Put back a char in the File.
/// It is not mandatory to use a char that was read from the file.\n
/// The next call the getChar will return c.\n
/// You can call ungetChar successively without problem.
/// @param c The char that will be pulled back.
void ungetChar(char c); void ungetChar(char c);
/// @brief Return the underlying file descriptor.
/// This is usefull if you need to use fscanf or fprintf.
/// @return The file descriptor.
FILE * getDescriptor(); FILE * getDescriptor();
/// @brief Get the filename of the File.
///
/// @return The filename.
const std::string & getName(); const std::string & getName();
/// @brief Read the file until the character c is met.
/// The next call to getChar will return c.
/// @param c The character to read until.
void readUntil(char c); void readUntil(char c);
/// @brief Read the file until the current character meets a condition.
/// The next call to condition(getChar()) will return true.
/// @param condition The condition the read char will have to meet.
void readUntil(const std::function<bool(char)> & condition); void readUntil(const std::function<bool(char)> & condition);
/// @brief Read the file until the current character meets a condition.
/// The next call to condition(getChar()) will return true.
/// @param dest Where to store all the read characters.
/// @param condition The condition the read char will have to meet.
void readUntil(std::string & dest, const std::function<bool(char)> & condition); void readUntil(std::string & dest, const std::function<bool(char)> & condition);
/// @brief Read the file again from the start, in "r"ead mode.
void rewind(); void rewind();
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment