From 0339d3945eac0b91589367ffb2dc1141fe436a52 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Sun, 19 Apr 2015 18:41:27 +0200
Subject: [PATCH] speedup parsing stdin and composing to stdout

---
 alib2data/src/factory/XmlDataFactory.hpp  | 12 ++++++++----
 alib2data/src/sax/SaxComposeInterface.cpp |  2 +-
 alib2data/src/sax/SaxParseInterface.cpp   |  2 +-
 3 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/alib2data/src/factory/XmlDataFactory.hpp b/alib2data/src/factory/XmlDataFactory.hpp
index 4d6cf565e7..ce15081d4e 100644
--- a/alib2data/src/factory/XmlDataFactory.hpp
+++ b/alib2data/src/factory/XmlDataFactory.hpp
@@ -53,7 +53,9 @@ public:
 	 */
 	template<class T>
 	static T fromStdin() {
-		return fromStream<T>(std::cin);
+		std::list<sax::Token> tokens;
+		sax::SaxParseInterface::parseStdin(tokens);
+		return fromTokens<T>(tokens);
 	}
 	
 	/**
@@ -62,8 +64,9 @@ public:
 	 */
 	template<class T>
 	static T fromStream(std::istream& in) {
-		std::string input{std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()};
-		return fromString<T>(input);
+		std::list<sax::Token> tokens;
+		sax::SaxParseInterface::parseStream(in, tokens);
+		return fromTokens<T>(tokens);
 	}
 
 	/**
@@ -122,7 +125,8 @@ public:
 	 */
 	template<class T>
 	static void toStdout(const T& data) {
-		toStream<T>(data, std::cout);
+		std::list<sax::Token> tokens = toTokens<T>(data);
+		sax::SaxComposeInterface::printStdout(tokens);
 	}
 	
 	/**
diff --git a/alib2data/src/sax/SaxComposeInterface.cpp b/alib2data/src/sax/SaxComposeInterface.cpp
index 353d478cc2..d7ae20faf8 100644
--- a/alib2data/src/sax/SaxComposeInterface.cpp
+++ b/alib2data/src/sax/SaxComposeInterface.cpp
@@ -36,7 +36,7 @@ void SaxComposeInterface::printFile(const std::string& filename, const std::list
 }
 
 void SaxComposeInterface::printStdout(const std::list<Token>& in) {
-	SaxComposeInterface::printStream(std::cout, in);
+	SaxComposeInterface::printFile("-", in);
 }
 
 void SaxComposeInterface::printStream(std::ostream& out, const std::list<Token>& in) {
diff --git a/alib2data/src/sax/SaxParseInterface.cpp b/alib2data/src/sax/SaxParseInterface.cpp
index 53376cdf7f..88c4968ad1 100644
--- a/alib2data/src/sax/SaxParseInterface.cpp
+++ b/alib2data/src/sax/SaxParseInterface.cpp
@@ -50,7 +50,7 @@ void SaxParseInterface::parseFile(const std::string& filename, std::list<Token>&
 }
 
 void SaxParseInterface::parseStdin(std::list<Token>& out) {
-	SaxParseInterface::parseStream(std::cin, out);
+	SaxParseInterface::parseFile("-", out);
 }
 
 void SaxParseInterface::parseStream(std::istream& in, std::list<Token>& out) {
-- 
GitLab