aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2014-04-28 08:22:06 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2014-04-28 08:22:06 +0200
commitecdada3e6c4f1e57bad487d5225993e0bc646f6e (patch)
treeca25bef1138e7e0c23639198a13337d89f7a8ae8
parentf19abd10673c3a07f9d591c7ea076a609755171e (diff)
Add roundtrip example, root makefile
-rw-r--r--examples/makefile17
-rw-r--r--examples/roundtrip/driver.cxx104
-rw-r--r--examples/roundtrip/makefile66
-rw-r--r--examples/roundtrip/position.xml6
-rw-r--r--makefile37
5 files changed, 230 insertions, 0 deletions
diff --git a/examples/makefile b/examples/makefile
new file mode 100644
index 0000000..3b9b68f
--- /dev/null
+++ b/examples/makefile
@@ -0,0 +1,17 @@
+# file : examples/makefile
+# copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make
+
+tests := roundtrip
+
+default := $(out_base)/
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+$(default): $(addprefix $(out_base)/,$(addsuffix /,$(tests)))
+$(test): $(addprefix $(out_base)/,$(addsuffix /.test,$(tests)))
+$(clean): $(addprefix $(out_base)/,$(addsuffix /.clean,$(tests)))
+
+$(foreach t,$(tests),$(call import,$(src_base)/$t/makefile))
diff --git a/examples/roundtrip/driver.cxx b/examples/roundtrip/driver.cxx
new file mode 100644
index 0000000..5270147
--- /dev/null
+++ b/examples/roundtrip/driver.cxx
@@ -0,0 +1,104 @@
+// file : examples/roundtrip/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <string>
+#include <fstream>
+#include <iostream>
+
+#include <xml/parser.hxx>
+#include <xml/serializer.hxx>
+
+using namespace std;
+using namespace xml;
+
+int
+main (int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ cerr << "usage: " << argv[0] << " <xml-file>" << endl;
+ return 1;
+ }
+
+ try
+ {
+ ifstream ifs;
+ ifs.exceptions (ifstream::badbit | ifstream::failbit);
+ ifs.open (argv[1], ifstream::in | ifstream::binary);
+
+ // Configure the parser to receive attributes as events as well
+ // as to receive prefix-namespace mappings (namespace declarations
+ // in XML terminology).
+ //
+ parser p (ifs,
+ argv[1],
+ parser::receive_default |
+ parser::receive_attributes_event |
+ parser::receive_namespace_decls);
+
+ // Configure serializer not to perform indentation. Existing
+ // indentation, if any, will be preserved.
+ //
+ serializer s (cout, "out", 0);
+
+ for (parser::event_type e (p.next ()); e != parser::eof; e = p.next ())
+ {
+ switch (e)
+ {
+ case parser::start_element:
+ {
+ s.start_element (p.qname ());
+ break;
+ }
+ case parser::end_element:
+ {
+ s.end_element ();
+ break;
+ }
+ case parser::start_namespace_decl:
+ {
+ s.namespace_decl (p.namespace_ (), p.prefix ());
+ break;
+ }
+ case parser::end_namespace_decl:
+ {
+ // There is nothing in XML that indicates the end of namespace
+ // declaration since it is scope-based.
+ //
+ break;
+ }
+ case parser::start_attribute:
+ {
+ s.start_attribute (p.qname ());
+ break;
+ }
+ case parser::end_attribute:
+ {
+ s.end_attribute ();
+ break;
+ }
+ case parser::characters:
+ {
+ s.characters (p.value ());
+ break;
+ }
+ case parser::eof:
+ {
+ // Handled in the for loop.
+ //
+ break;
+ }
+ }
+ }
+ }
+ catch (const ios_base::failure& e)
+ {
+ cerr << "io failure" << endl;
+ return 1;
+ }
+ catch (const xml::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/examples/roundtrip/makefile b/examples/roundtrip/makefile
new file mode 100644
index 0000000..fc7749c
--- /dev/null
+++ b/examples/roundtrip/makefile
@@ -0,0 +1,66 @@
+# file : examples/roundtrip/makefile
+# copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make
+
+cxx_tun := driver.cxx
+
+#
+#
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o))
+cxx_od := $(cxx_obj:.o=.o.d)
+
+studxml.l := $(out_root)/xml/studxml.l
+studxml.l.cpp-options := $(out_root)/xml/studxml.l.cpp-options
+
+driver := $(out_base)/driver
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+# Build.
+#
+$(driver): $(cxx_obj) $(studxml.l)
+$(cxx_obj) $(cxx_od): $(studxml.l.cpp-options)
+
+$(call include-dep,$(cxx_od))
+
+# Alias for default target.
+#
+$(out_base)/: $(driver)
+
+
+# Test.
+#
+$(test): $(driver)
+ $(call message,test $<,$< $(src_base)/position.xml)
+
+# Clean.
+#
+$(clean): \
+ $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(cxx_obj)) \
+ $(addsuffix .cxx.clean,$(cxx_od))
+
+
+# Generated .gitignore.
+#
+ifeq ($(out_base),$(src_base))
+$(driver): | $(out_base)/.gitignore
+
+$(out_base)/.gitignore: files := driver
+$(clean): $(out_base)/.gitignore.clean
+
+$(call include,$(bld_root)/git/gitignore.make)
+endif
+
+
+# How to.
+#
+$(call include,$(bld_root)/cxx/o-e.make)
+$(call include,$(bld_root)/cxx/cxx-o.make)
+$(call include,$(bld_root)/cxx/cxx-d.make)
+
+# Dependencies.
+#
+$(call import,$(src_root)/xml/makefile)
diff --git a/examples/roundtrip/position.xml b/examples/roundtrip/position.xml
new file mode 100644
index 0000000..3d09684
--- /dev/null
+++ b/examples/roundtrip/position.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<object xmlns="http://www.example.com/xmlns/position" name="Lion's Head" type="mountain" id="123">
+ <position lat="-33.8569" lon="18.5083"/>
+ <position lat="-33.8568" lon="18.5083"/>
+ <position lat="-33.8568" lon="18.5082"/>
+</object>
diff --git a/makefile b/makefile
new file mode 100644
index 0000000..790b65f
--- /dev/null
+++ b/makefile
@@ -0,0 +1,37 @@
+# file : makefile
+# copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))build/bootstrap.make
+
+dirs := xml tests examples
+
+default := $(out_base)/
+test := $(out_base)/.test
+dist := $(out_base)/.dist
+clean := $(out_base)/.clean
+
+$(default): $(addprefix $(out_base)/,$(addsuffix /,$(dirs)))
+$(test): $(addprefix $(out_base)/,$(addsuffix /.test,$(dirs)))
+
+$(dist): export dirs := $(dirs)
+$(dist): export docs := LICENSE NEWS README INSTALL version
+$(dist): data_dist := libstudxml-vc9.sln libstudxml-vc10.sln
+$(dist): exec_dist := bootstrap
+$(dist): export extra_dist := $(data_dist) $(exec_dist)
+$(dist): export version = $(shell cat $(src_root)/version)
+
+$(dist): $(addprefix $(out_base)/,$(addsuffix /.dist,$(dist_dirs)))
+ $(call dist-data,$(docs) $(data_dist) libstudxml.pc.in)
+ $(call dist-exec,$(exec_dist))
+ $(call dist-dir,m4)
+ $(call meta-automake)
+ $(call meta-autoconf)
+
+$(clean): $(addprefix $(out_base)/,$(addsuffix /.clean,$(dirs)))
+
+$(call include,$(bld_root)/dist.make)
+$(call include,$(bld_root)/meta/automake.make)
+$(call include,$(bld_root)/meta/autoconf.make)
+
+$(foreach d,$(dirs),$(call import,$(src_base)/$d/makefile))