Skip to content
Snippets Groups Projects
Commit 355bd3cf authored by Franck Dary's avatar Franck Dary
Browse files

Added classes ReadingMachine and Classifier, and started to implement their constructors

parent 85c149fc
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,7 @@
#include <vector>
#include <array>
#include <unordered_map>
#include <regex>
#include <experimental/source_location>
#include <boost/flyweight.hpp>
#include "fmt/core.h"
......@@ -76,6 +77,8 @@ bool isEmpty(const boost::flyweight<T> & s)
return isEmpty(s.get());
}
bool doIfNameMatch(const std::regex & reg, const std::string & name, const std::function<void(const std::smatch &)> & f);
};
template <>
......
......@@ -114,5 +114,17 @@ std::string int2HumanStr(int number)
return result;
}
bool doIfNameMatch(const std::regex & reg, const std::string & name, const std::function<void(const std::smatch &)> & f)
{
std::smatch sm;
std::regex_match(name, sm, reg);
if (sm.empty())
return false;
f(sm);
return true;
};
};
......@@ -4,13 +4,11 @@
#include "BaseConfig.hpp"
#include "SubConfig.hpp"
#include "TransitionSet.hpp"
#include "ReadingMachine.hpp"
int main(int argc, char * argv[])
{
TransitionSet ts(argv[1]);
return 0;
/*
BaseConfig goldConfig(argv[3], argv[1], argv[2]);
SubConfig config(goldConfig);
......@@ -24,6 +22,9 @@ int main(int argc, char * argv[])
fmt::print(stderr, "ok\n");
std::scanf("%*c");
*/
ReadingMachine machine(argv[1]);
return 0;
}
......
#ifndef CLASSIFIER__H
#define CLASSIFIER__H
#include <string>
#include "TransitionSet.hpp"
class Classifier
{
private :
std::string name;
std::unique_ptr<TransitionSet> transitionSet;
public :
Classifier(const std::string & name, const std::string & topology, const std::string & tsFile);
};
#endif
#ifndef READING_MACHINE__H
#define READING_MACHINE__H
#include <memory>
#include "Classifier.hpp"
class ReadingMachine
{
private :
std::string name;
std::unique_ptr<Classifier> classifier;
public :
ReadingMachine(const std::string & filename);
};
#endif
#include "Classifier.hpp"
Classifier::Classifier(const std::string & name, const std::string & topology, const std::string & tsFile)
{
this->name = name;
this->transitionSet.reset(new TransitionSet(tsFile));
}
#include "ReadingMachine.hpp"
#include "util.hpp"
ReadingMachine::ReadingMachine(const std::string & filename)
{
std::regex nameRegex("Name : (.+)[\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;
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);
}
......@@ -7,22 +7,10 @@ Transition::Transition(const std::string & name)
std::regex writeRegex("WRITE ([bs])\\.(.+) (.+) (.+)");
auto doIfNameMatch = [name](auto & reg, auto init)
{
std::smatch sm;
std::regex_match(name, sm, reg);
if (sm.empty())
return false;
init(sm);
return true;
};
try
{
if (doIfNameMatch(writeRegex, [this](auto sm){initWrite(sm[3], sm[1], sm[2], sm[4]);}))
if (util::doIfNameMatch(writeRegex, name, [this](auto sm){initWrite(sm[3], sm[1], sm[2], sm[4]);}))
return;
throw std::invalid_argument("no match");
......
......@@ -3,6 +3,8 @@
TransitionSet::TransitionSet(const std::string & filename)
{
FILE * file = std::fopen(filename.c_str(), "r");
if (!file)
util::myThrow(fmt::format("cannot open file '{}'", filename));
char readBuffer[1024];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment