From f21738bd1cb53be98fab8510bb76a78616fd19d6 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Sat, 5 Jul 2014 10:30:02 +0200
Subject: [PATCH] acat2 include exception passing feature

---
 acat2/src/acat.cpp | 65 ++++++++++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 31 deletions(-)

diff --git a/acat2/src/acat.cpp b/acat2/src/acat.cpp
index 2715e50459..1f5aa5c136 100644
--- a/acat2/src/acat.cpp
+++ b/acat2/src/acat.cpp
@@ -10,29 +10,18 @@
 #include <unistd.h>
 #include <exception>
 #include "exception/AlibException.h"
-#include "FromXMLParsers.h"
+#include "factory/AutomatonFactory.h"
+#include "factory/RegExpFactory.h"
+#include "factory/StringFactory.h"
+#include "factory/ExceptionFactory.h"
 #include "sax/SaxParseInterface.h"
+#include "FromXMLParsers.h"
 #include "sax/ParserException.h"
 
 
 #define VERSION "0.0.1"
 #define USAGE "Usage: catPDA [-c] [input]";
 
-void getAutomaton(std::list<sax::Token>& tokens) {
-	automaton::Automaton automaton = alib::FromXMLParsers::automatonParser.parseValue(tokens);
-	std::cout << automaton;
-}
-
-void getRegExp(std::list<sax::Token>& tokens) {
-	regexp::RegExp regexp = alib::FromXMLParsers::regexpParser.parseValue(tokens);
-	std::cout << regexp;
-}
-
-void getString(std::list<sax::Token>& tokens) {
-	string::String string = alib::FromXMLParsers::stringParser.parseValue(tokens);
-	std::cout << string;
-}
-
 int main(int argc, char** argv) {
 	if(argc == 2 && std::string(argv[1])=="-v" ) {
 			std::cout << argv[0] << " version " << VERSION << "\n";
@@ -51,21 +40,35 @@ int main(int argc, char** argv) {
 		i=getopt(argc, argv, "t:");
 	}
 
-	std::list<sax::Token> tokens;
-	if(optind == argc) {
-		std::string input(std::istreambuf_iterator<char>(std::cin), std::istreambuf_iterator<char>());
-		sax::SaxParseInterface::parseMemory(input, tokens);
-	} else {
-		sax::SaxParseInterface::parseFile(argv[optind],tokens);
-	}
+	try {
+		std::list<sax::Token> tokens;
+		if(optind == argc) {
+			std::string input(std::istreambuf_iterator<char>(std::cin), std::istreambuf_iterator<char>());
+			sax::SaxParseInterface::parseMemory(input, tokens);
+		} else {
+			sax::SaxParseInterface::parseFile(argv[optind],tokens);
+		}
 
-	if (alib::FromXMLParsers::automatonParser.first(tokens)) {
-		getAutomaton(tokens);
-	} else if (alib::FromXMLParsers::regexpParser.first(tokens)) {
-		getRegExp(tokens);
-	} else if (alib::FromXMLParsers::stringParser.first(tokens)) {
-		getString(tokens);
-	} else {
-		throw exception::AlibException( "Expected root tag automaton, grammar, regexp or string. Read: " + tokens.front().getData());
+		if (alib::FromXMLParsers::automatonParser.first(tokens)) {
+			automaton::Automaton automaton = automaton::AutomatonFactory::fromTokens(tokens);
+			automaton::AutomatonFactory::toStdout(automaton);
+		} else if (alib::FromXMLParsers::regexpParser.first(tokens)) {
+			regexp::RegExp regexp = regexp::RegExpFactory::fromTokens(tokens);
+			regexp::RegExpFactory::toStdout(regexp);
+		} else if (alib::FromXMLParsers::stringParser.first(tokens)) {
+			string::String string = string::StringFactory::fromTokens(tokens);
+			string::StringFactory::StringFactory::toStdout(string);
+		} else if (alib::FromXMLParsers::exceptionParser.first(tokens)) {
+			exception::AlibException exception = exception::ExceptionFactory::fromTokens(tokens);
+			exception::ExceptionFactory::toStdout(exception);
+		} else {
+			throw exception::AlibException( "Expected root tag automaton, grammar, regexp, string or exception. Read: " + tokens.front().getData());
+		}
+	} catch(const exception::AlibException& exception) {
+		exception::ExceptionFactory::toStdout(exception);
+		return 1;
+	} catch(...) {
+		return 127;
 	}
+	return 0;
 }
-- 
GitLab