17 template <
typename DATA,
bool Binary = false>
35 inline bool load(
const std::string& filename,
37 const bool SAVE_LOG_FILE =
39 inline bool save(
const std::string& filename,
41 const bool SAVE_LOG_FILE =
45 inline std::string
log()
const;
52 addLogWarningEntry(
const std::string& text );
53 inline void addLogErrorEntry(
const std::string& text );
58 virtual bool importData( std::istream& file,
61 virtual bool exportData( std::ostream& file,
62 const DATA& data ) = 0;
67 inline void resetLog();
68 inline void saveLog(
const std::string& filename );
71 std::string m_log {
"" };
77 template <
typename DATA,
bool Binary>
80 const bool SAVE_LOG_FILE ) {
83 addLogEntry(
"Filename : " + filename );
84 addLogEntry(
"Expected Format : " + fileExtension() );
85 addLogEntry(
"File Type : " + std::string( Binary ?
"Binary" :
"Text" ) );
86 addLogEntry(
"Loading start..." );
88 filename, std::ios_base::in | ( Binary ? std::ios_base::binary : std::ios_base::in ) );
89 if ( !( status = file.is_open() ) ) {
91 "Error occurred while opening the file. Trying to see if extension is missing..." );
92 file.open( filename +
"." + fileExtension(),
93 std::ios_base::in | ( Binary ? std::ios_base::binary : std::ios_base::in ) );
94 if ( !( status = file.is_open() ) ) {
95 addLogErrorEntry(
"Error occured while opening the file. HINT: FILENAME IS WRONG." );
99 addLogEntry(
"File opened successfully." );
100 addLogEntry(
"Starting to import the data." );
101 status = importData( file, data );
102 addLogEntry(
"Import " + ( ( status ) ? std::string(
"DONE." ) : std::string(
"FAILED." ) ),
103 ( ( status ) ? LogEntry_Normal : LogEntry_Error ) );
105 addLogEntry(
"File closed." );
107 addLogEntry(
"Loading " + filename +
" ended." );
108 if ( SAVE_LOG_FILE ) { saveLog( filename +
"_load" ); }
112 template <
typename DATA,
bool Binary>
115 const bool SAVE_LOG_FILE ) {
118 addLogEntry(
"Filename : " + filename );
119 addLogEntry(
"Exporting Format : " + fileExtension() );
120 addLogEntry(
"File Type : " + std::string( Binary ?
"Binary" :
"Text" ) );
121 addLogEntry(
"Saving start..." );
122 std::ofstream file( filename +
"." + fileExtension(),
123 std::ios_base::out | std::ios_base::trunc |
124 ( Binary ? std::ios_base::binary : std::ios_base::out ) );
125 if ( !( status = file.is_open() ) ) {
126 addLogErrorEntry(
"Error occured while opening the file." );
129 addLogEntry(
"File opened successfully." );
130 addLogEntry(
"Starting to export the data..." );
131 status = exportData( file, data );
132 addLogEntry(
"Export " + ( ( status ) ? std::string(
"DONE." ) : std::string(
"FAILED." ) ),
133 ( ( status ) ? LogEntry_Normal : LogEntry_Error ) );
135 addLogEntry(
"File closed." );
137 addLogEntry(
"Saving " + filename +
" ended." );
138 if ( SAVE_LOG_FILE ) { saveLog( filename +
"_save" ); }
145 template <
typename DATA,
bool Binary>
150 template <
typename DATA,
bool Binary>
152 addLogEntry( text, LogEntry_Normal );
155 template <
typename DATA,
bool Binary>
157 addLogEntry( text, LogEntry_Warning );
160 template <
typename DATA,
bool Binary>
161 inline void FileManager<DATA, Binary>::addLogErrorEntry(
const std::string& text ) {
162 addLogEntry( text, LogEntry_Error );
165 template <
typename DATA,
bool Binary>
167 const LogEntryType type ) {
169 case LogEntry_Normal: {
173 case LogEntry_Warning: {
174 m_log +=
"\n--- LogEntry_Warning : " + text +
" ---\n";
177 case LogEntry_Error: {
178 m_log +=
"\n### LogEntry_Error : " + text +
" ###\n";
188 template <
typename DATA,
bool Binary>
189 inline void FileManager<DATA, Binary>::resetLog() {
190 m_log =
"====== FILE MANAGER LOG ======\n";
193 template <
typename DATA,
bool Binary>
194 inline void FileManager<DATA, Binary>::saveLog(
const std::string& filename ) {
195 std::ofstream file( filename +
".log" );
196 if ( !file.is_open() ) {
return; }
std::string log() const
LOG.
virtual std::string fileExtension() const =0
INTERFACE.
bool load(const std::string &filename, DATA &data, const bool SAVE_LOG_FILE=false)
INTERFACE.
virtual ~FileManager()=default
DESTRUCTOR.
FileManager()=default
CONSTRUCTOR.
void addLogEntry(const std::string &text)
LOG.