Radium Engine  1.5.0
FileManager.hpp
1 #pragma once
2 
3 #include <fstream>
4 #include <string>
5 
6 namespace Ra {
7 namespace IO {
8 
17 template <typename DATA, bool Binary = false>
19 {
20  public:
22  enum LogEntryType {
23  LogEntry_Normal,
24  LogEntry_Warning,
25  LogEntry_Error,
26  };
27 
29  inline FileManager() = default; // Default constructor.
30 
32  inline virtual ~FileManager() = default; // Destructor.
33 
35  inline bool load( const std::string& filename,
36  DATA& data,
37  const bool SAVE_LOG_FILE =
38  false ); // Return true if the data is correctly loaded, false otherwise.
39  inline bool save( const std::string& filename,
40  const DATA& data,
41  const bool SAVE_LOG_FILE =
42  false ); // Return true if the data is correctly stored, false otherwise.
43 
45  inline std::string log() const; // Return the log of the last action performed.
46 
47  protected:
49  inline void addLogEntry( const std::string& text ); // Add a line in the log file. A newline
50  // character will be automatically added.
51  inline void
52  addLogWarningEntry( const std::string& text ); // Add a warning line in the log file.
53  inline void addLogErrorEntry( const std::string& text ); // Add an error line in the log file.
54  inline void addLogEntry( const std::string& text, const LogEntryType type );
55 
57  virtual std::string fileExtension() const = 0; // Return the extension given to the files.
58  virtual bool importData( std::istream& file,
59  DATA& data ) = 0; // Load data from a given file. Return false if an
60  // error occurs, true otherwise.
61  virtual bool exportData( std::ostream& file,
62  const DATA& data ) = 0; // Store given data into a given file. Return
63  // false if an error occurs, true otherwise.
64 
65  private:
67  inline void resetLog(); // Reset the log string.
68  inline void saveLog( const std::string& filename ); // Save the log into a text file.
69 
71  std::string m_log { "" };
72 };
73 
75 // INTERFACE
77 template <typename DATA, bool Binary>
78 inline bool FileManager<DATA, Binary>::load( const std::string& filename,
79  DATA& data,
80  const bool SAVE_LOG_FILE ) {
81  bool status = true;
82  resetLog();
83  addLogEntry( "Filename : " + filename );
84  addLogEntry( "Expected Format : " + fileExtension() );
85  addLogEntry( "File Type : " + std::string( Binary ? "Binary" : "Text" ) );
86  addLogEntry( "Loading start..." );
87  std::ifstream file(
88  filename, std::ios_base::in | ( Binary ? std::ios_base::binary : std::ios_base::in ) );
89  if ( !( status = file.is_open() ) ) {
90  addLogWarningEntry(
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." );
96  }
97  }
98  if ( status ) {
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 ) );
104  file.close();
105  addLogEntry( "File closed." );
106  }
107  addLogEntry( "Loading " + filename + " ended." );
108  if ( SAVE_LOG_FILE ) { saveLog( filename + "_load" ); }
109  return status;
110 }
111 
112 template <typename DATA, bool Binary>
113 inline bool FileManager<DATA, Binary>::save( const std::string& filename,
114  const DATA& data,
115  const bool SAVE_LOG_FILE ) {
116  bool status = true;
117  resetLog();
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." );
127  }
128  if ( status ) {
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 ) );
134  file.close();
135  addLogEntry( "File closed." );
136  }
137  addLogEntry( "Saving " + filename + " ended." );
138  if ( SAVE_LOG_FILE ) { saveLog( filename + "_save" ); }
139  return status;
140 }
141 
143 // LOG
145 template <typename DATA, bool Binary>
146 inline std::string FileManager<DATA, Binary>::log() const {
147  return m_log;
148 }
149 
150 template <typename DATA, bool Binary>
151 inline void FileManager<DATA, Binary>::addLogEntry( const std::string& text ) {
152  addLogEntry( text, LogEntry_Normal );
153 }
154 
155 template <typename DATA, bool Binary>
156 inline void FileManager<DATA, Binary>::addLogWarningEntry( const std::string& text ) {
157  addLogEntry( text, LogEntry_Warning );
158 }
159 
160 template <typename DATA, bool Binary>
161 inline void FileManager<DATA, Binary>::addLogErrorEntry( const std::string& text ) {
162  addLogEntry( text, LogEntry_Error );
163 }
164 
165 template <typename DATA, bool Binary>
166 inline void FileManager<DATA, Binary>::addLogEntry( const std::string& text,
167  const LogEntryType type ) {
168  switch ( type ) {
169  case LogEntry_Normal: {
170  m_log += text;
171  } break;
172 
173  case LogEntry_Warning: {
174  m_log += "\n--- LogEntry_Warning : " + text + " ---\n";
175  } break;
176 
177  case LogEntry_Error: {
178  m_log += "\n### LogEntry_Error : " + text + " ###\n";
179  } break;
180 
181  default:
182  break;
183  }
184 
185  m_log += "\n";
186 }
187 
188 template <typename DATA, bool Binary>
189 inline void FileManager<DATA, Binary>::resetLog() {
190  m_log = "====== FILE MANAGER LOG ======\n";
191 }
192 
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; }
197  file << log();
198  file.close();
199 }
200 
201 } // namespace IO
202 } // namespace Ra
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.
Definition: FileManager.hpp:78
virtual ~FileManager()=default
DESTRUCTOR.
FileManager()=default
CONSTRUCTOR.
void addLogEntry(const std::string &text)
LOG.
Definition: Cage.cpp:3