|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r57944 - trunk/libs/spirit/example/karma
From: hartmut.kaiser_at_[hidden]
Date: 2009-11-26 09:30:56
Author: hkaiser
Date: 2009-11-26 09:30:55 EST (Thu, 26 Nov 2009)
New Revision: 57944
URL: http://svn.boost.org/trac/boost/changeset/57944
Log:
Spirit: added another Karma example
Added:
trunk/libs/spirit/example/karma/escaped_string.cpp (contents, props changed)
Text files modified:
trunk/libs/spirit/example/karma/Jamfile | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
Modified: trunk/libs/spirit/example/karma/Jamfile
==============================================================================
--- trunk/libs/spirit/example/karma/Jamfile (original)
+++ trunk/libs/spirit/example/karma/Jamfile 2009-11-26 09:30:55 EST (Thu, 26 Nov 2009)
@@ -27,4 +27,5 @@
exe quick_start1 : quick_start1.cpp ;
exe karma_reference : reference.cpp ;
exe karma_reorder_struct : reorder_struct.cpp ;
+exe karma_escaped_string : escaped_string.cpp ;
Added: trunk/libs/spirit/example/karma/escaped_string.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/karma/escaped_string.cpp 2009-11-26 09:30:55 EST (Thu, 26 Nov 2009)
@@ -0,0 +1,70 @@
+// Copyright (c) 2001-2009 Hartmut Kaiser
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// The purpose of this example is to show how any character sequence can be
+// printed while being properly quoted.
+
+#include <boost/config/warning_disable.hpp>
+#include <boost/spirit/include/karma.hpp>
+
+namespace client
+{
+ namespace karma = boost::spirit::karma;
+
+ template <typename OutputIterator>
+ struct escaped_string
+ : karma::grammar<OutputIterator, std::string(char)>
+ {
+ escaped_string()
+ : escaped_string::base_type(esc_str)
+ {
+ esc.add('\a', "\\a")
+ ('\b', "\\b")
+ ('\f', "\\f")
+ ('\n', "\\n")
+ ('\r', "\\r")
+ ('\t', "\\t")
+ ('\v', "\\v")
+ ('\'', "\\\'")
+ ('\"', "\\\"")
+ ;
+
+ esc_str = lit(karma::_r1)
+ << *(esc | karma::print | "\\x" << karma::hex)
+ << lit(karma::_r1)
+ ;
+ }
+
+ karma::rule<OutputIterator, std::string(char)> esc_str;
+ karma::symbols<char, char const*> esc;
+ };
+}
+
+///////////////////////////////////////////////////////////////////////////////
+int main()
+{
+ namespace karma = boost::spirit::karma;
+
+ typedef std::back_insert_iterator<std::string> sink_type;
+
+ std::string generated;
+ sink_type sink(generated);
+
+ std::string str("string to esacpe: \n\r\t\"\'\x19");
+ client::escaped_string<sink_type> g;
+ if (!karma::generate(sink, g('"'), str))
+ {
+ std::cout << "-------------------------\n";
+ std::cout << "Generating failed\n";
+ std::cout << "-------------------------\n";
+ }
+ else
+ {
+ std::cout << "-------------------------\n";
+ std::cout << "Generated: " << generated << "\n";
+ std::cout << "-------------------------\n";
+ }
+ return 0;
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk