From 26139eaaabfeab5035bb893ab22df7c135409d2b Mon Sep 17 00:00:00 2001
From: Martin Zak <zakmart1@fit.cvut.cz>
Date: Wed, 1 Jan 2014 16:59:57 +0100
Subject: [PATCH] acat, aconvert.* updates

---
 acat/src/acat.cpp                       | 86 +++++++++++--------------
 aconvert.dot/src/DotConverter.cpp       | 36 +++++------
 aconvert.dot/src/DotConverter.h         | 13 ++--
 aconvert.dot/src/aconvert.dot.cpp       | 42 ++++++------
 aconvert.gastex/src/GasTexConverter.cpp |  6 +-
 aconvert.gastex/src/GasTexConverter.h   | 15 ++---
 aconvert.gastex/src/aconvert.gastex.cpp | 41 ++++++------
 aconvert/src/aconvert.cpp               | 71 ++++++++++----------
 8 files changed, 149 insertions(+), 161 deletions(-)

diff --git a/acat/src/acat.cpp b/acat/src/acat.cpp
index 92ee523d54..e15151df24 100644
--- a/acat/src/acat.cpp
+++ b/acat/src/acat.cpp
@@ -6,22 +6,22 @@
 #include <iostream>
 #include <string>
 #include <set>
-
+#include <cstdlib>
+#include <unistd.h>
+#include <exception>
 #include "AutomatonFactory.h"
 #include "GrammarFactory.h"
 #include "AlibException.h"
-
 #include "regexp/RegExpParser.h"
-
 #include "automaton/AutomatonParser.h"
 #include "grammar/GrammarParser.h"
 #include "grammar/RightRegularGrammar.h"
-
 #include "sax/SaxInterface.h"
-
 #include "sax/ParserException.h"
 
-#include <exception>
+
+#define VERSION "0.0.1"
+#define USAGE "Usage: catPDA [-c] [input]";
 
 using namespace std;
 using namespace automaton;
@@ -52,51 +52,43 @@ void getRegExp(list<Token>& tokens) {
 }
 
 int main(int argc, char** argv) {
-
-	int fileParameterIndex = -1;
-	bool complexTypes = false;
-
-	try {
-		if (argc > 1) {
-			for (int i = 1; i < argc; i++) {
-				if (string("-h").compare(argv[i]) == 0) {
-					cout << "Automaton parsing.\nUsage: catPDA [automaton.xml]\n";
-					return -1;
-				} else if (string("-c").compare(argv[i]) == 0) {
-					complexTypes = true;
-				} else {
-					if(fileParameterIndex == -1) {
-						fileParameterIndex = i;
-					} else {
-						throw AlibException("Only one file can be passed as parameter - " + string(argv[i]) + " " + string(argv[fileParameterIndex]));
-					}
-				}
-			}
-		}
-
-		list<Token> tokens;
-
-		if(fileParameterIndex != -1) {
-			SaxInterface::parseFile(argv[fileParameterIndex],tokens);
-		} else {
-			string input(istreambuf_iterator<char>(cin), (istreambuf_iterator<char>()));
-			SaxInterface::parseMemory(input, tokens);
+	if(argc == 2 && string(argv[1])=="-v" ) {
+			cout << argv[0] << " version " << VERSION << "\n";
+			cout << USAGE;
+			cout << endl;
+			return EXIT_SUCCESS;
 		}
 
-		if (tokens.front().getData() == "automaton") {
-			getAutomaton(tokens, complexTypes);
-		} else if (tokens.front().getData() == "grammar") {
-			getGrammar(tokens);
-		} else if (tokens.front().getData() == "regexp") {
-			getRegExp(tokens);
-		} else {
-			throw AlibException("Expected root tag automaton, grammar or regexp. Read: " + tokens.front().getData());
+	bool concrete = false;
+
+	int i=getopt(argc, argv, "t:");
+	while(i!=-1) {
+		switch(i){
+		case 'c':
+			concrete = true;
+			break;
+		default:
+			optind--;
+			break;
 		}
+		i=getopt(argc, argv, "t:");
+	}
 
-	} catch (AlibException& e) {
-		cout << e.what() << endl;
-		return -1;
+	list<Token> tokens;
+	if(optind == argc) {
+		string input(istreambuf_iterator<char>(cin), (istreambuf_iterator<char>()));
+		SaxInterface::parseMemory(input, tokens);
+	} else {
+		SaxInterface::parseFile(argv[optind],tokens);
 	}
 
-	cout.flush();
+	if (tokens.front().getData() == "automaton") {
+		getAutomaton(tokens, concrete);
+	} else if (tokens.front().getData() == "grammar") {
+		getGrammar(tokens);
+	} else if (tokens.front().getData() == "regexp") {
+		getRegExp(tokens);
+	} else {
+		throw AlibException( "Expected root tag automaton, grammar or regexp. Read: " + tokens.front().getData());
+	}
 }
diff --git a/aconvert.dot/src/DotConverter.cpp b/aconvert.dot/src/DotConverter.cpp
index d5bf21a5d4..baeb3a21d3 100644
--- a/aconvert.dot/src/DotConverter.cpp
+++ b/aconvert.dot/src/DotConverter.cpp
@@ -2,7 +2,7 @@
  * DotConverter.cpp
  *
  *  Created on: Apr 1, 2013
- *      Author: martin
+ *      Author: Martin Zak
  */
 
 #include "DotConverter.h"
@@ -20,7 +20,7 @@
 using namespace std;
 using namespace automaton;
 
-void DotConverter::convert(const automaton::Automaton& a, std::ostream& out) {
+void DotConverter::convert(const Automaton& a, ostream& out) {
 	out << "digraph automaton {\n";
 	out << "rankdir=LR;\n";
 	int cnt = 1;
@@ -55,7 +55,7 @@ void DotConverter::convert(const automaton::Automaton& a, std::ostream& out) {
 		transitions(fsm, states, out);
 		out << "}";
 		return;
-	} catch (const std::bad_cast& e) {
+	} catch (const bad_cast& e) {
 	}
 
 	try {
@@ -63,7 +63,7 @@ void DotConverter::convert(const automaton::Automaton& a, std::ostream& out) {
 		transitions(pda, states, out);
 		out << "}";
 		return;
-	} catch (const std::bad_cast& e) {
+	} catch (const bad_cast& e) {
 	}
 
 	try {
@@ -71,19 +71,19 @@ void DotConverter::convert(const automaton::Automaton& a, std::ostream& out) {
 		transitions(tm, states, out);
 		out << "}";
 		return;
-	} catch (const std::bad_cast& e) {
+	} catch (const bad_cast& e) {
 	}
 
 }
 
-void DotConverter::transitions(const automaton::FSM& fsm, const std::map<automaton::State, int>& states,
-		std::ostream& out) {
+void DotConverter::transitions(const FSM& fsm, const map<State, int>& states,
+		ostream& out) {
 
 	map<pair<int, int>, string> transitions;
-	const set<automaton::TransitionFSM>& t = fsm.getTransitions();
+	const set<TransitionFSM>& t = fsm.getTransitions();
 
 	//put transitions from automaton to "transitions"
-	set<automaton::TransitionFSM>::const_iterator it = t.begin();
+	set<TransitionFSM>::const_iterator it = t.begin();
 	while (it != t.end()) {
 		string symbol;
 		if (it->getInput().getSymbol().compare("") == 0) {
@@ -112,13 +112,13 @@ void DotConverter::transitions(const automaton::FSM& fsm, const std::map<automat
 		it2++;
 	}
 }
-void DotConverter::transitions(const automaton::PDA& pda, const std::map<automaton::State, int>& states,
-		std::ostream& out) {
+void DotConverter::transitions(const PDA& pda, const map<State, int>& states,
+		ostream& out) {
 	map<pair<int, int>, string> transitions;
-	const set<automaton::TransitionPDA>& t = pda.getTransitions();
+	const set<TransitionPDA>& t = pda.getTransitions();
 
 	//put transitions from automaton to "transitions"
-	set<automaton::TransitionPDA>::const_iterator it = t.begin();
+	set<TransitionPDA>::const_iterator it = t.begin();
 	while (it != t.end()) {
 		string symbol;
 
@@ -175,13 +175,13 @@ void DotConverter::transitions(const automaton::PDA& pda, const std::map<automat
 	}
 }
 
-void DotConverter::transitions(const automaton::TM& tm, const std::map<automaton::State, int>& states,
-		std::ostream& out) {
+void DotConverter::transitions(const TM& tm, const map<State, int>& states,
+		ostream& out) {
 	map<pair<int, int>, string> transitions;
-	const set<automaton::TransitionTM>& t = tm.getTransitions();
+	const set<TransitionTM>& t = tm.getTransitions();
 
 	//put transitions from automaton to "transitions"
-	set<automaton::TransitionTM>::const_iterator it = t.begin();
+	set<TransitionTM>::const_iterator it = t.begin();
 	while (it != t.end()) {
 		string symbol;
 
@@ -190,7 +190,7 @@ void DotConverter::transitions(const automaton::TM& tm, const std::map<automaton
 		symbol += "/";
 		symbol += it->getOutput().getSymbol();
 		symbol += " ";
-		symbol += (std::string[] ) { "&larr;", "&rarr;", "&times;" } [it->getShift()];
+		symbol += (string[] ) { "&larr;", "&rarr;", "&times;" } [it->getShift()];
 
 		//Insert into map
 		pair<int, int> key(states.find(it->getFrom())->second, states.find(it->getTo())->second);
diff --git a/aconvert.dot/src/DotConverter.h b/aconvert.dot/src/DotConverter.h
index 63cebef764..a7794d9137 100644
--- a/aconvert.dot/src/DotConverter.h
+++ b/aconvert.dot/src/DotConverter.h
@@ -2,7 +2,7 @@
  * DotConverter.h
  *
  *  Created on: Apr 1, 2013
- *      Author: martin
+ *      Author: Martin Zak
  */
 
 #ifndef DOTCONVERTER_H_
@@ -16,13 +16,16 @@
 #include <map>
 #include <utility>
 
+using namespace std;
+using namespace automaton;
+
 class DotConverter {
 protected:
-	static void transitions(const automaton::FSM& fsm, const std::map<automaton::State, int>& states, std::ostream& out);
-	static void transitions(const automaton::PDA& pda, const std::map<automaton::State, int>& states, std::ostream& out);
-	static void transitions(const automaton::TM& tm, const std::map<automaton::State, int>& states, std::ostream& out);
+	static void transitions(const FSM& fsm, const map<State, int>& states, ostream& out);
+	static void transitions(const PDA& pda, const map<State, int>& states, ostream& out);
+	static void transitions(const TM& tm, const map<State, int>& states, ostream& out);
 public:
-	static void convert(const automaton::Automaton& a, std::ostream& out);
+	static void convert(const Automaton& a, ostream& out);
 };
 
 #endif /* DOTCONVERTER_H_ */
diff --git a/aconvert.dot/src/aconvert.dot.cpp b/aconvert.dot/src/aconvert.dot.cpp
index 04b3874f65..bae9afd3d1 100644
--- a/aconvert.dot/src/aconvert.dot.cpp
+++ b/aconvert.dot/src/aconvert.dot.cpp
@@ -1,10 +1,9 @@
 //============================================================================
-// Name        : aconverter.dot.cpp
+// Name        : aconvert.dot.cpp
 // Author      : Martin Zak
 //============================================================================
 
 #include <iostream>
-
 #include "automaton/AutomatonParser.h"
 #include "automaton/UnknownAutomaton.h"
 #include "AutomatonFactory.h"
@@ -13,6 +12,9 @@
 
 #include "DotConverter.h"
 
+#define VERSION "0.0.1"
+#define USAGE "aconvert.dot [inputfile]"
+
 using namespace std;
 using namespace automaton;
 using namespace sax;
@@ -20,28 +22,22 @@ using namespace alib;
 
 int main(int argc, char** argv) {
 	list<Token> tokens;
-	try {
-
-		if (argc > 1) {
-			if (string("-h").compare(argv[1]) == 0) {
-				cout
-						<< "Automaton converting tool. Supports only dot format at the moment.\nUsage: aconvert [automaton.xml]\n";
-				return -1;
-			}
-
-			SaxInterface::parseFile(argv[1], tokens);
-		} else {
-			string input(istreambuf_iterator<char>(cin),
-					(istreambuf_iterator<char>()));
-			SaxInterface::parseMemory(input, tokens);
-		}
 
-		UnknownAutomaton unknownAutomaton = AutomatonParser::parse(tokens);
-		Automaton* automaton = AutomatonFactory::buildAutomaton(unknownAutomaton);
-		DotConverter::convert(*automaton, cout);
+	if (argc > 1) {
+		if (string(argv[1]) == "-v") {
+			cout << USAGE;
+			cout << endl;
+			return EXIT_SUCCESS;
+		}
+		SaxInterface::parseFile(argv[1], tokens);
 
-	} catch (AlibException& e) {
-		cout << e.what() << endl;
-		return 0;
+	} else {
+		string input(istreambuf_iterator<char>(cin),
+				(istreambuf_iterator<char>()));
+		SaxInterface::parseMemory(input, tokens);
 	}
+
+	UnknownAutomaton unknownAutomaton = AutomatonParser::parse(tokens);
+	Automaton* automaton = AutomatonFactory::buildAutomaton(unknownAutomaton);
+	DotConverter::convert(*automaton, cout);
 }
diff --git a/aconvert.gastex/src/GasTexConverter.cpp b/aconvert.gastex/src/GasTexConverter.cpp
index c572d2f5a8..c84af4fec2 100644
--- a/aconvert.gastex/src/GasTexConverter.cpp
+++ b/aconvert.gastex/src/GasTexConverter.cpp
@@ -2,7 +2,7 @@
  * GasTexConverter.cpp
  *
  *  Created on: Jan 1, 2014
- *      Author: martin
+ *      Author: Martin Zak
  */
 
 #include "GasTexConverter.h"
@@ -18,8 +18,6 @@ void GasTexConverter::convert(const Automaton& a, ostream& out) {
 	out << "\\begin{center}\n";
 	out << "\\begin{picture}(,)(,)\n";
 
-
-
 	for (auto& state : a.getStates()) {
 		bool initial = false;
 		bool final = false;
@@ -45,7 +43,7 @@ void GasTexConverter::convert(const Automaton& a, ostream& out) {
 		}
 
 		out << state.getName();
-		out << ")( , ){";
+		out << ")(,){";
 		out << state.getName();
 		out << "}\n";
 	}
diff --git a/aconvert.gastex/src/GasTexConverter.h b/aconvert.gastex/src/GasTexConverter.h
index 41cf0f5029..b2cf1505a9 100644
--- a/aconvert.gastex/src/GasTexConverter.h
+++ b/aconvert.gastex/src/GasTexConverter.h
@@ -2,26 +2,25 @@
  * GasTexConverter.h
  *
  *  Created on: Jan 1, 2014
- *      Author: martin
+ *      Author: Martin Zak
  */
 
 #ifndef GASTEXCONVERTER_H_
 #define GASTEXCONVERTER_H_
 
 #include<ostream>
-
-#include <automaton/FSM/FSM.h>
-#include <automaton/PDA/PDA.h>
-#include <automaton/TM/TM.h>
-#include <map>
-#include <utility>
+#include<map>
+#include<utility>
+#include"automaton/FSM/FSM.h"
+#include"automaton/PDA/PDA.h"
+#include"automaton/TM/TM.h"
 
 using namespace std;
 using namespace automaton;
 
 class GasTexConverter {
 private:
-	static void printTransitionMap(const map<pair<string, string>, string> transitionMap, ostream& out);
+	static void printTransitionMap( const map<pair<string, string>, string> transitionMap, ostream& out);
 	static string checkEpsilon(const string& inputSymbol);
 	static string getStackSymbols(const list<Symbol>& stackSymbols);
 	static void transitions(const FSM& fsm, ostream& out);
diff --git a/aconvert.gastex/src/aconvert.gastex.cpp b/aconvert.gastex/src/aconvert.gastex.cpp
index 3cf5301271..3b9e52cdc0 100644
--- a/aconvert.gastex/src/aconvert.gastex.cpp
+++ b/aconvert.gastex/src/aconvert.gastex.cpp
@@ -4,7 +4,6 @@
 //============================================================================
 
 #include <iostream>
-
 #include "automaton/AutomatonParser.h"
 #include "automaton/UnknownAutomaton.h"
 #include "AutomatonFactory.h"
@@ -13,6 +12,9 @@
 
 #include "GasTexConverter.h"
 
+#define VERSION "0.0.1"
+#define USAGE "aconvert.gastex [inputfile]"
+
 using namespace std;
 using namespace automaton;
 using namespace sax;
@@ -20,28 +22,23 @@ using namespace alib;
 
 int main(int argc, char** argv) {
 	list<Token> tokens;
-	try {
-
-		if (argc > 1) {
-			if (string("-h").compare(argv[1]) == 0) {
-				cout
-						<< "Automaton converting tool. Supports only dot format at the moment.\nUsage: aconvert [automaton.xml]\n";
-				return -1;
-			}
-
-			SaxInterface::parseFile(argv[1], tokens);
-		} else {
-			string input(istreambuf_iterator<char>(cin),
-					(istreambuf_iterator<char>()));
-			SaxInterface::parseMemory(input, tokens);
-		}
 
-		UnknownAutomaton unknownAutomaton = AutomatonParser::parse(tokens);
-		Automaton* automaton = AutomatonFactory::buildAutomaton(unknownAutomaton);
-		GasTexConverter::convert(*automaton, cout);
+	if (argc > 1) {
+		if (string(argv[1]) == "-v") {
+			cout << USAGE;
+			cout << endl;
+			return EXIT_SUCCESS;
+		}
+		SaxInterface::parseFile(argv[1], tokens);
 
-	} catch (AlibException& e) {
-		cout << e.what() << endl;
-		return 0;
+	} else {
+		string input(istreambuf_iterator<char>(cin),
+				(istreambuf_iterator<char>()));
+		SaxInterface::parseMemory(input, tokens);
 	}
+
+	UnknownAutomaton unknownAutomaton = AutomatonParser::parse(tokens);
+	Automaton* automaton = AutomatonFactory::buildAutomaton(unknownAutomaton);
+	GasTexConverter::convert(*automaton, cout);
+
 }
diff --git a/aconvert/src/aconvert.cpp b/aconvert/src/aconvert.cpp
index 6cba8b8c23..515fd075e6 100644
--- a/aconvert/src/aconvert.cpp
+++ b/aconvert/src/aconvert.cpp
@@ -1,49 +1,52 @@
 //============================================================================
 // Name        : aconvert.cpp
 // Author      : Martin Zak
-// Version     :
-// Copyright   : 
-// Description :
 //============================================================================
 
 #include <iostream>
+#include <string>
+#include <cstdlib>
+#include <unistd.h>
 
-#include "automaton/AutomatonParser.h"
-#include "automaton/UnknownAutomaton.h"
-#include "AutomatonFactory.h"
-#include "AlibException.h"
-#include "sax/SaxInterface.h"
-
+#define PROGNAME "aconvert"
+#define VERSION "0.0.1"
+#define USAGE "Usage: aconvert -t outputtype input";
 
 using namespace std;
-using namespace automaton;
-using namespace sax;
-using namespace alib;
 
 int main(int argc, char** argv) {
-	list<Token> tokens;
-	try {
-
-		if (argc > 1) {
-			if (string("-h").compare(argv[1]) == 0) {
-				cout
-						<< "Automaton converting tool. Supports only dot format at the moment.\nUsage: aconvert [automaton.xml]\n";
-				return -1;
-			}
-
-			SaxInterface::parseFile(argv[1], tokens);
-		} else {
-			string input(istreambuf_iterator<char>(cin),
-					(istreambuf_iterator<char>()));
-			SaxInterface::parseMemory(input, tokens);
-		}
+	if(argc == 2 && string(argv[1])=="-v") {
+		cout << argv[0] << " version " << VERSION << "\n";
+		cout << USAGE;
+		cout << endl;
+		return EXIT_SUCCESS;
+	}
 
-		UnknownAutomaton unknownAutomaton = AutomatonParser::parse(tokens);
-		Automaton* automaton = AutomatonFactory::buildAutomaton(unknownAutomaton);
-		DotConverter::convert(*automaton, cout);
+	string outputType = "";
+
+	int i=getopt(argc, argv, "t:");
+	while(i!=-1) {
+		switch(i){
+		case 't':
+			outputType = string(optarg);
+			break;
+		default:
+			optind--;
+			break;
+		}
+		i=getopt(argc, argv, "t:");
+	}
 
-	} catch (AlibException& e) {
-		cout << e.what() << endl;
-		return 0;
+	if(optind == argc || outputType == "") {
+		cerr << USAGE;
+		cerr << endl;
+		return EXIT_FAILURE;
+	} else {
+		string programName = string(PROGNAME) + "." + string(outputType);
+		execv(programName.c_str(), argv + optind - 1);
 	}
+
+
+
+
 }
-- 
GitLab