Skip to content
Snippets Groups Projects
ReadingMachine.cpp 965 B
Newer Older
#include "ReadingMachine.hpp"
#include "util.hpp"
Franck Dary's avatar
Franck Dary committed
#include "Strategy.hpp"

ReadingMachine::ReadingMachine(const std::string & filename)
{
  std::regex nameRegex("Name : (.+)[\n]");
Franck Dary's avatar
Franck Dary committed
  std::regex strategyRegex("Strategy : (.+)[\n]");
  std::regex classifierRegex("Classifier : (.+) (.+) (.+)[\n]");

  std::FILE * file = std::fopen(filename.c_str(), "r");

  char buffer[1024];
  while (!std::feof(file))
  {
    if (buffer != std::fgets(buffer, 1024, file))
      break;

    try
    {
      if (util::doIfNameMatch(nameRegex, buffer, [this](auto sm){name = sm[1];}))
        continue;
Franck Dary's avatar
Franck Dary committed
      if (util::doIfNameMatch(strategyRegex, buffer, [this](auto sm){}))
        continue;
      if (util::doIfNameMatch(classifierRegex, buffer, [this](auto sm){classifier.reset(new Classifier(sm[1], sm[2], sm[3]));}))
        continue;
    } catch(std::exception & e) {util::myThrow(fmt::format("during reading of '{}' : {}", filename, e.what()));}
  }

  std::fclose(file);
}