From b587d7cc07a7823780a890cc493ea40115208e04 Mon Sep 17 00:00:00 2001
From: Adam Wardynski <award@.(B-4.4.46a)>
Date: Fri, 10 Dec 2010 16:22:42 +0100
Subject: [PATCH] Rework dumping variables a bit, plus add string version.

---
 libwccl/ops/parsedexpression.h | 38 ++++++++++++++++++++++++++--------
 wcclparser/main.cpp            |  2 +-
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/libwccl/ops/parsedexpression.h b/libwccl/ops/parsedexpression.h
index b3b007a..5a41d0c 100644
--- a/libwccl/ops/parsedexpression.h
+++ b/libwccl/ops/parsedexpression.h
@@ -139,18 +139,19 @@ public:
 	 */
 	boost::shared_ptr<ParsedExpression> clone_clean_ptr() const;
 	
+
 	/**
-	 * Dup all variables to error stream.
+	 * @returns String with representation of all held variables,
+	 * in form of "variable = value" pairs, each pair on one line.
 	 */
-	void dumpvariables(const Corpus2::Tagset& tagset)
-	{
-		typedef std::pair<std::string, boost::shared_ptr<Wccl::Value> > v_t;
+	std::string variables_string(const Corpus2::Tagset& tagset) const;
 
-		foreach (const v_t& v, variables_->get_all<Wccl::Value>()) {
-			std::cerr << v.second->make_var_repr(v.first) << "="
-				<< v.second->to_string(tagset) << "\n";
-		}
-	}
+	/**
+	 * Dump all variables to an output stream, in form of
+	 * "variable = value" pairs, each pair on another line.
+	 * @returns Stream written to.
+	 */
+	std::ostream& dump_variables(std::ostream& ostream, const Corpus2::Tagset& tagset) const;
 
 protected:
 	explicit ParsedExpression(const Variables& variables);
@@ -231,6 +232,25 @@ boost::shared_ptr<ParsedExpression> ParsedExpression::clone_clean_ptr() const {
 	return copy;
 }
 
+inline
+std::ostream& ParsedExpression::dump_variables(
+		std::ostream &ostream,
+		const Corpus2::Tagset &tagset) const {
+	typedef std::pair<std::string, boost::shared_ptr<Wccl::Value> > v_t;
+	foreach (const v_t& v, variables_->get_all<Wccl::Value>()) {
+		ostream << v.second->make_var_repr(v.first) << "="
+			<< v.second->to_string(tagset) << "\n";
+	}
+	return ostream;
+}
+
+inline
+std::string ParsedExpression::variables_string(const Corpus2::Tagset &tagset) const {
+	std::ostringstream oss;
+	dump_variables(oss, tagset);
+	return oss.str();
+}
+
 } /* end ns Wccl */
 
 #endif // LIBWCCL_OPS_PARSEDEXPRESSION_H
diff --git a/wcclparser/main.cpp b/wcclparser/main.cpp
index fdd2e40..2f3412a 100644
--- a/wcclparser/main.cpp
+++ b/wcclparser/main.cpp
@@ -126,7 +126,7 @@ bool process_line(const std::string& line, Wccl::Parser& parser,
 						<< retVal->to_string(parser.tagset())
 						<< std::endl;
 					if (dump_variables) {
-						retOp->dumpvariables(parser.tagset());
+						retOp->dump_variables(std::cerr, parser.tagset());
 					}
 				} else {
 					std::cerr << "Problem while parsing -- "
-- 
GitLab