summaryrefslogtreecommitdiff
path: root/examples/cxx/tree/binary/xdr
diff options
context:
space:
mode:
Diffstat (limited to 'examples/cxx/tree/binary/xdr')
-rw-r--r--examples/cxx/tree/binary/xdr/README34
-rw-r--r--examples/cxx/tree/binary/xdr/driver.cxx149
-rw-r--r--examples/cxx/tree/binary/xdr/library.xml53
-rw-r--r--examples/cxx/tree/binary/xdr/library.xsd76
-rw-r--r--examples/cxx/tree/binary/xdr/makefile78
5 files changed, 390 insertions, 0 deletions
diff --git a/examples/cxx/tree/binary/xdr/README b/examples/cxx/tree/binary/xdr/README
new file mode 100644
index 0000000..22d5693
--- /dev/null
+++ b/examples/cxx/tree/binary/xdr/README
@@ -0,0 +1,34 @@
+This example shows how to save/load the object model to/from XDR
+(eXternal Data Representation) binary format using XDR streams.
+The XDR API is available out of the box on most UNIX and GNU/Linux
+systems as part of Sun RPC. On Windows you may need to install a
+third-party library which provides the XDR API.
+
+The example consists of the following files:
+
+library.xsd
+ XML Schema which describes a library of books.
+
+library.xml
+ Sample XML instance document.
+
+library.hxx
+library.cxx
+ C++ types that represent the given vocabulary as well as data
+ representation stream insertion and extraction operations. These
+ are generated by XSD from library.xsd. Note that the
+ --generate-insertion and --generate-extraction options are used
+ to generate the insertion and extraction operations for XDR
+ stream.
+
+driver.cxx
+ Driver for the example. It first calls one of the parsing functions
+ that constructs the object model from the input XML file. It then
+ saves the object model to the XDR representation and loads it back.
+ Additionally, it prints the content of the object model before saving
+ it to the XDR representation and after loading it from the XDR
+ representation.
+
+To run the example on the sample XML instance document simply execute:
+
+$ ./driver library.xml
diff --git a/examples/cxx/tree/binary/xdr/driver.cxx b/examples/cxx/tree/binary/xdr/driver.cxx
new file mode 100644
index 0000000..495eafc
--- /dev/null
+++ b/examples/cxx/tree/binary/xdr/driver.cxx
@@ -0,0 +1,149 @@
+// file : examples/cxx/tree/binary/xdr/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::auto_ptr
+#include <cstring> // std::memcpy
+#include <cstddef> // std::size_t
+#include <iostream>
+
+#include <rpc/types.h>
+#include <rpc/xdr.h>
+
+#include "library.hxx"
+
+using std::cerr;
+using std::endl;
+using std::size_t;
+
+// XDR output functions. Their implementations are provided after main().
+//
+struct underflow_info
+{
+ xml_schema::buffer* buf;
+ size_t pos;
+};
+
+extern "C" int
+overflow (void* user_data, char* buf, int n);
+
+extern "C" int
+underflow (void* user_data, char* buf, int n);
+
+// The xdrrec_create function (used below) has slightly different
+// prototypes on different platforms. To make this example portable
+// we will need to cast the actual function to the following common
+// prototype.
+//
+extern "C"
+typedef void (*xdrrec_create_p) (
+ XDR*,
+ unsigned int write_size,
+ unsigned int read_size,
+ void* user_data,
+ int (*read) (void* user_data, char* buf, int n),
+ int (*write) (void* user_data, char* buf, int n));
+
+int
+main (int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ cerr << "usage: " << argv[0] << " library.xml" << endl;
+ return 1;
+ }
+
+ try
+ {
+ using namespace library;
+
+ xdrrec_create_p xdrrec_create_ =
+ reinterpret_cast<xdrrec_create_p> (::xdrrec_create);
+
+ // Read in the file.
+ //
+ std::auto_ptr<catalog> c (catalog_ (argv[1]));
+
+ cerr << *c << endl;
+
+ // Save to an XDR stream.
+ //
+ XDR xdr;
+ xml_schema::buffer buf;
+
+ xdrrec_create_ (&xdr, 0, 0, reinterpret_cast<char*> (&buf), 0, &overflow);
+ xdr.x_op = XDR_ENCODE;
+
+ xml_schema::ostream<XDR> oxdr (xdr);
+
+ oxdr << *c;
+
+ xdrrec_endofrecord (&xdr, true); // Flush the data.
+ xdr_destroy (&xdr);
+
+ // The binary representation is now in the memory buffer 'buf'.
+ // To get to the raw data use buf.data() and buf.size().
+ //
+ cerr << endl
+ << "binary representation size: " << buf.size () << endl;
+
+ // Load from an XDR stream.
+ //
+ underflow_info ui;
+ ui.buf = &buf;
+ ui.pos = 0;
+
+ xdrrec_create_ (&xdr, 0, 0, reinterpret_cast<char*> (&ui), &underflow, 0);
+ xdr.x_op = XDR_DECODE;
+
+ xdrrec_skiprecord (&xdr);
+
+ xml_schema::istream<XDR> ixdr (xdr);
+
+ std::auto_ptr<catalog> copy (new catalog (ixdr));
+
+ xdr_destroy (&xdr);
+
+ cerr << *copy << endl;
+ }
+ catch (const xml_schema::exception& e)
+ {
+ cerr << e << endl;
+ return 1;
+ }
+}
+
+extern "C" int
+overflow (void* p, char* buf, int n_)
+{
+ xml_schema::buffer* dst (reinterpret_cast<xml_schema::buffer*> (p));
+ size_t n (static_cast<size_t> (n_));
+
+ size_t size (dst->size ());
+ size_t capacity (dst->capacity ());
+
+ // Implement exponential growth.
+ //
+ if (size + n > capacity && size + n < capacity * 2)
+ dst->capacity (capacity * 2);
+
+ dst->size (size + n);
+ std::memcpy (dst->data () + size, buf, n);
+
+ return n;
+}
+
+extern "C" int
+underflow (void* p, char* buf, int n_)
+{
+ underflow_info* ui (reinterpret_cast<underflow_info*> (p));
+ size_t n (static_cast<size_t> (n_));
+
+ size_t size (ui->buf->size () - ui->pos);
+ n = size > n ? n : size;
+
+ std::memcpy (buf, ui->buf->data () + ui->pos, n);
+ ui->pos += n;
+
+ return n;
+}
diff --git a/examples/cxx/tree/binary/xdr/library.xml b/examples/cxx/tree/binary/xdr/library.xml
new file mode 100644
index 0000000..9ddcd5a
--- /dev/null
+++ b/examples/cxx/tree/binary/xdr/library.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : examples/cxx/tree/binary/xdr/library.xml
+author : Boris Kolpackov <boris@codesynthesis.com>
+copyright : not copyrighted - public domain
+
+-->
+
+<lib:catalog xmlns:lib="http://www.codesynthesis.com/library"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.codesynthesis.com/library library.xsd">
+
+ <book id="MM" available="false">
+ <isbn>0679760806</isbn>
+ <title>The Master and Margarita</title>
+ <genre>fiction</genre>
+
+ <author recommends="WP">
+ <name>Mikhail Bulgakov</name>
+ <born>1891-05-15</born>
+ <died>1940-03-10</died>
+ </author>
+ </book>
+
+
+ <book id="WP">
+ <isbn>0679600841</isbn>
+ <title>War and Peace</title>
+ <genre>history</genre>
+
+ <author recommends="CP">
+ <name>Leo Tolstoy</name>
+ <born>1828-09-09</born>
+ <died>1910-11-20</died>
+ </author>
+ </book>
+
+
+ <book id="CP" available="false">
+ <isbn>0679420290</isbn>
+ <title>Crime and Punishment</title>
+ <genre>philosophy</genre>
+
+ <author>
+ <name>Fyodor Dostoevsky</name>
+ <born>1821-11-11</born>
+ <died>1881-02-09</died>
+ </author>
+ </book>
+
+</lib:catalog>
diff --git a/examples/cxx/tree/binary/xdr/library.xsd b/examples/cxx/tree/binary/xdr/library.xsd
new file mode 100644
index 0000000..9999e72
--- /dev/null
+++ b/examples/cxx/tree/binary/xdr/library.xsd
@@ -0,0 +1,76 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : examples/cxx/tree/binary/xdr/library.xsd
+author : Boris Kolpackov <boris@codesynthesis.com>
+copyright : not copyrighted - public domain
+
+-->
+
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ xmlns:xse="http://www.codesynthesis.com/xmlns/xml-schema-extension"
+ xmlns:lib="http://www.codesynthesis.com/library"
+ targetNamespace="http://www.codesynthesis.com/library">
+
+ <xsd:simpleType name="isbn">
+ <xsd:restriction base="xsd:unsignedInt"/>
+ </xsd:simpleType>
+
+
+ <xsd:complexType name="title">
+ <xsd:simpleContent>
+ <xsd:extension base="xsd:string">
+ <xsd:attribute name="lang" type="xsd:language"/>
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+
+ <xsd:simpleType name="genre">
+ <xsd:restriction base="xsd:string">
+ <xsd:enumeration value="romance"/>
+ <xsd:enumeration value="fiction"/>
+ <xsd:enumeration value="horror"/>
+ <xsd:enumeration value="history"/>
+ <xsd:enumeration value="philosophy"/>
+ </xsd:restriction>
+ </xsd:simpleType>
+
+ <xsd:complexType name="person">
+ <xsd:sequence>
+ <xsd:element name="name" type="xsd:string"/>
+ <xsd:element name="born" type="xsd:date"/>
+ <xsd:element name="died" type="xsd:date" minOccurs="0"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:complexType name="author">
+ <xsd:complexContent>
+ <xsd:extension base="lib:person">
+ <xsd:attribute name="recommends" type="xsd:IDREF" xse:refType="lib:book"/>
+ </xsd:extension>
+ </xsd:complexContent>
+ </xsd:complexType>
+
+ <xsd:complexType name="book">
+ <xsd:sequence>
+ <xsd:element name="isbn" type="lib:isbn"/>
+ <xsd:element name="title" type="lib:title"/>
+ <xsd:element name="genre" type="lib:genre"/>
+ <xsd:element name="author" type="lib:author" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ <xsd:attribute name="available" type="xsd:boolean" default="true"/>
+ <xsd:attribute name="id" type="xsd:ID" use="required"/>
+ </xsd:complexType>
+
+
+ <xsd:complexType name="catalog">
+ <xsd:sequence>
+ <xsd:element name="book" type="lib:book" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+
+ <xsd:element name="catalog" type="lib:catalog"/>
+
+</xsd:schema>
diff --git a/examples/cxx/tree/binary/xdr/makefile b/examples/cxx/tree/binary/xdr/makefile
new file mode 100644
index 0000000..6414fe9
--- /dev/null
+++ b/examples/cxx/tree/binary/xdr/makefile
@@ -0,0 +1,78 @@
+# file : examples/cxx/tree/binary/xdr/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC
+# license : GNU GPL v2 + exceptions; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../../../../build/bootstrap.make
+
+xsd := library.xsd
+cxx := driver.cxx
+
+obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=.o))
+dep := $(obj:.o=.o.d)
+
+driver := $(out_base)/driver
+clean := $(out_base)/.clean
+
+
+# Import.
+#
+$(call import,\
+ $(scf_root)/import/libxerces-c/stub.make,\
+ l: xerces_c.l,cpp-options: xerces_c.l.cpp-options)
+
+$(call import,\
+ $(scf_root)/import/libace/stub.make,\
+ l: ace.l,cpp-options: ace.l.cpp-options)
+
+
+# Build.
+#
+$(driver): $(obj) $(xerces_c.l) $(ace.l) -lnsl
+
+$(obj) $(dep): cpp_options := -I$(src_root)/libxsd
+$(obj) $(dep): $(xerces_c.l.cpp-options) $(ace.l.cpp-options)
+
+$(out_base)/$(xsd:.xsd=.hxx) \
+$(out_base)/$(xsd:.xsd=.ixx) \
+$(out_base)/$(xsd:.xsd=.cxx): xsd := $(out_root)/xsd/xsd
+
+$(out_base)/$(xsd:.xsd=.hxx) \
+$(out_base)/$(xsd:.xsd=.ixx) \
+$(out_base)/$(xsd:.xsd=.cxx): xsd_options := \
+--generate-ostream \
+--generate-insertion XDR \
+--generate-extraction XDR
+
+$(out_base)/$(xsd:.xsd=.hxx) \
+$(out_base)/$(xsd:.xsd=.ixx) \
+$(out_base)/$(xsd:.xsd=.cxx): $(out_root)/xsd/xsd
+
+$(call include-dep,$(dep))
+
+# Convenience alias for default target.
+#
+.PHONY: $(out_base)/
+$(out_base)/: $(driver)
+
+
+# Clean.
+#
+.PHONY: $(clean)
+
+$(clean): $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(obj)) \
+ $(addsuffix .cxx.clean,$(dep)) \
+ $(addprefix $(out_base)/,$(xsd:.xsd=.cxx.xsd.clean))
+
+
+# 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)
+$(call include,$(scf_root)/xsd/tree/xsd-cxx.make)
+
+# Dependencies.
+#
+$(call import,$(src_root)/xsd/makefile)