Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
astat.cpp 5.01 KiB
/*
 * astat.cpp
 *
 *  Created on: 26. 3. 2014
 *	  Author: Jan Travnicek
 */

#include <tclap/CmdLine.h>
#include <vector>

#include <factory/DataFactory.hpp>
#include <exception/AlibException.h>

#include "Settings.h"
#include "AutomataStat.h"

int main(int argc, char* argv[]) {
	try {
		TCLAP::CmdLine cmd("Prints usefull information about automata, grammars, regexps, ...", ' ', "0.01");

		std::vector<std::string> allowed;
		allowed.push_back("noop");
		allowed.push_back("print");
		allowed.push_back("quantity");
		allowed.push_back("both");
		TCLAP::ValuesConstraint<std::string> allowedVals( allowed );

		TCLAP::ValueArg<std::string> states(		"s",	"states",		"Print or display number of states",			false,	"noop",		&allowedVals);
		cmd.add(states);

		TCLAP::ValueArg<std::string> finalStates(	"f",	"finalStates",		"Print or display number of final states",		false,	"noop",		&allowedVals);
		cmd.add(finalStates);

		TCLAP::ValueArg<std::string> initialStates(	"i",	"initialStates",	"Print or display number of initial states",		false,	"noop",		&allowedVals);
		cmd.add(initialStates);

		TCLAP::ValueArg<std::string> alphabet(		"a",	"alphabet",		"Print or display number of symbols in alphabet",	false,	"noop",		&allowedVals);
		cmd.add(alphabet);

		TCLAP::ValueArg<std::string> transitions(	"t",	"transitions",		"Print or display number of transitions",		false,	"noop",		&allowedVals);
		cmd.add(transitions);

		TCLAP::UnlabeledValueArg<std::string> file( "file", "Read from file", false, "-", "file");
                cmd.add( file );

		cmd.parse(argc,argv);

		Settings settings = {PrintOptions::NOOP, PrintOptions::NOOP, PrintOptions::NOOP, PrintOptions::NOOP, PrintOptions::NOOP };

		if(states.isSet()) {
			if(states.getValue() == "noop") {
				settings.states = PrintOptions::NOOP;
			} else if(states.getValue() == "print") {
				settings.states = PrintOptions::PRINT;
			} else if(states.getValue() == "quantity") {
				settings.states = PrintOptions::QUANTITY;
			} else if(states.getValue() == "both") {
				settings.states = PrintOptions::BOTH;
			}
		}

		if(finalStates.isSet()) {
			if(finalStates.getValue() == "noop") {
				settings.finalStates = PrintOptions::NOOP;
			} else if(finalStates.getValue() == "print") {
				settings.finalStates = PrintOptions::PRINT;
			} else if(finalStates.getValue() == "quantity") {
				settings.finalStates = PrintOptions::QUANTITY;
			} else if(finalStates.getValue() == "both") {
				settings.finalStates = PrintOptions::BOTH;
			}
		}

		if(initialStates.isSet()) {
			if(initialStates.getValue() == "noop") {
				settings.initialStates = PrintOptions::NOOP;
			} else if(initialStates.getValue() == "print") {
				settings.initialStates = PrintOptions::PRINT;
			} else if(initialStates.getValue() == "quantity") {
				settings.initialStates = PrintOptions::QUANTITY;
			} else if(initialStates.getValue() == "both") {
				settings.initialStates = PrintOptions::BOTH;
			}
		}

		if(alphabet.isSet()) {
			if(alphabet.getValue() == "noop") {
				settings.alphabet = PrintOptions::NOOP;
			} else if(alphabet.getValue() == "print") {
				settings.alphabet = PrintOptions::PRINT;
			} else if(alphabet.getValue() == "quantity") {
				settings.alphabet = PrintOptions::QUANTITY;
			} else if(alphabet.getValue() == "both") {
				settings.alphabet = PrintOptions::BOTH;
			}
		}

		if(transitions.isSet()) {
			if(transitions.getValue() == "noop") {
				settings.transitions = PrintOptions::NOOP;
			} else if(transitions.getValue() == "print") {
				settings.transitions = PrintOptions::PRINT;
			} else if(transitions.getValue() == "quantity") {
				settings.transitions = PrintOptions::QUANTITY;
			} else if(transitions.getValue() == "both") {
				settings.transitions = PrintOptions::BOTH;
			}
		}

		std::list<sax::Token> tokens;
		if(file.isSet()) {
			if(file.getValue() == "-") {
				sax::SaxParseInterface::parseStdin(tokens);
			} else {
				sax::SaxParseInterface::parseFile(file.getValue(), tokens);
			}
		} else {
			sax::SaxParseInterface::parseStdin(tokens);
		}

		if( alib::FromXMLParsers::automatonParser.first(tokens)) {
			automaton::Automaton automaton = alib::DataFactory::fromTokens<automaton::Automaton>(tokens);

			AutomataStat::stat(automaton, settings);

			return 0;
		} else if( alib::FromXMLParsers::grammarParser.first(tokens)) {
			grammar::Grammar grammar = alib::DataFactory::fromTokens<grammar::Grammar>(tokens);

			return 0;
		} else if( alib::FromXMLParsers::regexpParser.first(tokens)) {
			regexp::RegExp regexp = alib::DataFactory::fromTokens<regexp::RegExp>(tokens);

			return 0;
		} else {
			throw exception::AlibException( "Input not recognized." );
			return 1;
		}
	} catch( const exception::AlibException & exception ) {
		alib::DataFactory::toStdout( exception );
		return 1;
	} catch(const TCLAP::ArgException& exception) {
		std::cout << exception.error() << std::endl;
		return 2;
	} catch (const std::exception& exception) {
		std::cerr << "Exception caught: " << exception.what() << std::endl;
		return 3;
	} catch(...) {
		std::cerr << "Unknown exception caught." << std::endl;
		return 127;
	}
}