pattern_recognition / source / main.cc
main.cc
Raw
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

#include "parser.h"
#include "automaton.h"

// Leest een reguliere expressie in en bepaalt een passende eindige automaat
void exp(Automaton &automaton) {
    automaton.clear();
    std::string expressie; // Hierin komt de expressie
    std::cin >> expressie; // Zet de ingevoerde expressie in expressie
    automaton = Parser(expressie).parse();
} // exp

// Slaat de eindige automaat op naar de gegeven file in DOT-notatie
void dot(Automaton &automaton) {
    std::string bestandsnaam; // Bestandsnaam waar uitvoer heen gaat
    std::ofstream uitvoer; // Uitvoer stream
    std::streambuf *coutbuf = std::cout.rdbuf(); // Slaat oude buf op voor reset van buf

    std::cin >> bestandsnaam;
    uitvoer.open(bestandsnaam);
    if(uitvoer.fail()) {
        std::cout << "\nEr is iets mis gegaan met het uitvoeren naar deze file" << std::endl;
        return;
    } // Checkt of de de uitvoer stream wel goed gaat

    std::cout.rdbuf(uitvoer.rdbuf()); // Redirect std::cout naar uitvoer
    automaton.dotAutomaton(); // Maakt DOT-notatie
    std::cout.rdbuf(coutbuf); // Reset rdbuf
    uitvoer.close();
} // dot

// Checkt of de string wordt gaccepteerd door de automaat. 
// De uitvoer is de string match of geen match
void mat(Automaton &automaton) {
    std::string expressie; // Hierin komt de expressie
    std::cin >> expressie; // Zet de ingevoerde expressie in expressie
    std::cout << std::endl << automaton.match(expressie) << std::endl;
} // mat

// Keuzemenu van het programma
void menu(Automaton &automaton, bool debugMode) {
    std::string invoer; 
    while(!(invoer == "exit" || invoer == "end")) {
        if(!debugMode) {
            std::cout << "\nTyp een keuze in: " << std::endl
                    << "  exp <expressie> | dot <bestandsnaam> |" << std::endl
                    << "  mat <string> | end/exit" << std::endl;
        } // Print menu alleen als debugMode false is
        std::cin >> invoer; // Keuze inlezen

        if(invoer == "end" || invoer == "exit") {
            return;
        } // Stoppen met het programma
        else if(invoer == "exp") {
            exp(automaton);
        } // Leest een reguliere expressie in en bepaalt een passende eindige automaat
        else if(invoer == "dot") {
            dot(automaton);
        } // Slaat de eindige automaat op naar de gegeven file in DOT-notatie
        else if(invoer == "mat") {
            mat(automaton);
        } // Checkt of de string wordt gaccepteerd door de automaat.
        else {
            std::cout << "\nDat is een onbekende keuze." << std::endl;
        } // Onbekende keuze
    } // Loop van menu
} // menu

int main(int argc, char** argv) {
    bool debugMode = false;
    if(argc > 1 && std::string(argv[1]) == "d") {
        debugMode = true;
    } // debugMode check
    std::cout << debugMode << std::endl;

    Automaton automaton;
    menu(automaton, debugMode);
} // main