Molecular Dynamics Simulation  1.0
FileInputHandler.cpp
Go to the documentation of this file.
1 #include "FileInputHandler.h"
2 
3 #include <filesystem>
4 #include <iostream>
5 
6 #include "io/logger/Logger.h"
7 
8 std::tuple<std::vector<Particle>, std::optional<SimulationParams>> FileInputHandler::readFile(const std::filesystem::path& input_file_path,
9  bool fresh, bool allow_recursion) {
10  if (!std::filesystem::exists(input_file_path)) {
11  Logger::logger->error("Error: file '{}' does not exist.", input_file_path.string());
12  throw FileReader::FileFormatException("File does not exist");
13  }
14 
15  std::string file_extension = input_file_path.extension().string();
16 
17  if (get_supported_input_file_extensions().find(file_extension) == get_supported_input_file_extensions().end()) {
18  Logger::logger->error("Error: file extension '{}' is not supported.", file_extension);
19  throw FileReader::FileFormatException("File extension is not supported");
20  }
21 
22  std::unique_ptr<FileReader> file_reader;
23 
24  if (file_extension == ".xml") {
25  file_reader = std::make_unique<XMLFileReader>(fresh, allow_recursion);
26  } else {
27  Logger::logger->error("Error: file extension '{}' is not supported.", file_extension);
28  throw FileReader::FileFormatException("File extension is not supported");
29  }
30 
31  try {
32  auto [particles, config] = file_reader->readFile(input_file_path);
33  Logger::logger->info("Loaded {} particles from file {}", particles.size(), input_file_path.string());
34  return std::make_tuple(particles, config);
35  } catch (const FileReader::FileFormatException& e) {
36  Logger::logger->error("Error: file '{}' is not a valid {} file.", input_file_path.string(), file_extension);
37  Logger::logger->error("FileFormatException:\n{}", std::string(e.what()));
38  throw;
39  }
40 }
const std::set< std::string > get_supported_input_file_extensions()
Returns a list of supported input file extensions.
Definition: InputFormats.cpp:3
static std::tuple< std::vector< Particle >, std::optional< SimulationParams > > readFile(const std::filesystem::path &input_file_path, bool fresh=false, bool allow_recursion=true)
Reads the input file and stores the particles in the given ParticleContainer. Other simulation parame...
Exception to be thrown when the file format is invalid.
Definition: FileReader.h:31
const char * what() const noexcept override
Definition: FileReader.h:35
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35