Skip to content
Snippets Groups Projects
ReadingMachine.cpp 1.23 KiB
Newer Older
#include "ReadingMachine.hpp"
#include "util.hpp"

ReadingMachine::ReadingMachine(const std::string & filename)
{
  std::FILE * file = std::fopen(filename.c_str(), "r");

  char buffer[1024];
Franck Dary's avatar
Franck Dary committed
  std::string fileContent;
  while (!std::feof(file))
  {
    if (buffer != std::fgets(buffer, 1024, file))
      break;

Franck Dary's avatar
Franck Dary committed
    fileContent += buffer;
Franck Dary's avatar
Franck Dary committed

  auto lines = util::split(fileContent, '\n');

  try
  {
    unsigned int curLine = 0;
    if (!util::doIfNameMatch(std::regex("Name : (.+)"), lines[curLine++], [this](auto sm){name = sm[1];}))
      util::myThrow("No name specified");

    while (util::doIfNameMatch(std::regex("Classifier : (.+) (.+) (.+)"), lines[curLine++], [this](auto sm){classifier.reset(new Classifier(sm[1], sm[2], sm[3]));}));
    if (!classifier.get())
      util::myThrow("No Classifier specified");

    std::vector<std::string_view> restOfFile(lines.begin()+curLine-1, lines.end());
    strategy.reset(new Strategy(restOfFile));

  } catch(std::exception & e) {util::myThrow(fmt::format("during reading of '{}' : {}", filename, e.what()));}
TransitionSet & ReadingMachine::getTransitionSet()
{
  return classifier->getTransitionSet();
}

Strategy & ReadingMachine::getStrategy()
{
  return *strategy;
}