#ifndef CFILEMANAGER_HPP
#define CFILEMANAGER_HPP

#include "../Exception/CFileException.hpp"
#include "../../Context/CContext.hpp"

#include <cstdio>
#include <string>
#include <stdexcept>



/**
 * @file CFileManager.hpp
 * @brief File of CException, a class to manage program file.
 */

/**
 * @brief Class to manage file.
 */
class CFileManager {
private:
	// The pointer to file.
	FILE* m_filePtr;

	// The file path.
	std::string m_filePath;

	/**
	 * @brief Open the file with the given mode
	 * @param[in] mode The mode of opening file, must be 'r' or 'w';
	 * @post throw a std::invalid_argument if mode is invalid, or a CFileException if the file can't be opened.
	 */
	void open(const char mode);

	/**
	 * @brief Close the file
	 * @post Do nothing if the file isn't opened.
	 */
	void close(void);

public:
	CFileManager(void) = delete;
	CFileManager(const CFileManager& param) = delete;
	CFileManager& operator=(const CFileManager& param) = delete;

	/**
	 * @brief Build a file manager to the file's given path.
	 * @param[in] path The path of the file;
	 */
	CFileManager(std::string path);

	/**
	 * @brief the destructor.
	 * @pre The file must be close before destroying this.
	 */
	~CFileManager(void);

	/**
	 * @brief The file path getter.
	 * @return std::string The path of the file;
	 */
	std::string getPath(void) const;

	/**
	 * @brief Write the file function.
	 * @param[in] content The new content of the file.
	 * 
	 * This function erase the old content of the file and replace it by "content"
	 */
	void write(std::string content);

	/**
	 * @brief Read the file function.
	 * @return std::string, The content of the file.
	 */
	std::string read(void);
};

#endif