From 5e527213a2430bb3018e5eebd909aef294edf9b5 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 18 Dec 2020 18:48:46 +0300 Subject: Switch to build2 --- xsd-examples/cxx/tree/binary/xdr/.gitignore | 1 + xsd-examples/cxx/tree/binary/xdr/README | 36 +++++++ xsd-examples/cxx/tree/binary/xdr/buildfile | 30 ++++++ xsd-examples/cxx/tree/binary/xdr/driver.cxx | 148 +++++++++++++++++++++++++++ xsd-examples/cxx/tree/binary/xdr/library.xml | 52 ++++++++++ xsd-examples/cxx/tree/binary/xdr/library.xsd | 75 ++++++++++++++ 6 files changed, 342 insertions(+) create mode 100644 xsd-examples/cxx/tree/binary/xdr/.gitignore create mode 100644 xsd-examples/cxx/tree/binary/xdr/README create mode 100644 xsd-examples/cxx/tree/binary/xdr/buildfile create mode 100644 xsd-examples/cxx/tree/binary/xdr/driver.cxx create mode 100644 xsd-examples/cxx/tree/binary/xdr/library.xml create mode 100644 xsd-examples/cxx/tree/binary/xdr/library.xsd (limited to 'xsd-examples/cxx/tree/binary/xdr') diff --git a/xsd-examples/cxx/tree/binary/xdr/.gitignore b/xsd-examples/cxx/tree/binary/xdr/.gitignore new file mode 100644 index 0000000..c116ec1 --- /dev/null +++ b/xsd-examples/cxx/tree/binary/xdr/.gitignore @@ -0,0 +1 @@ +library.?xx diff --git a/xsd-examples/cxx/tree/binary/xdr/README b/xsd-examples/cxx/tree/binary/xdr/README new file mode 100644 index 0000000..e02b2b9 --- /dev/null +++ b/xsd-examples/cxx/tree/binary/xdr/README @@ -0,0 +1,36 @@ +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 on most UNIX and GNU/Linux systems as part +of Sun RPC (libtirpc-dev package on Debian/Ubuntu, libtirpc-devel +package on Fedora/RHEL, and as a part of the Standard C Library on +FreeBSD and MacOS). 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/xsd-examples/cxx/tree/binary/xdr/buildfile b/xsd-examples/cxx/tree/binary/xdr/buildfile new file mode 100644 index 0000000..92f0c48 --- /dev/null +++ b/xsd-examples/cxx/tree/binary/xdr/buildfile @@ -0,0 +1,30 @@ +# file : cxx/tree/binary/xdr/buildfile +# license : not copyrighted - public domain + +import libs = libxsd%lib{xsd} +import libs += libxerces-c%lib{xerces-c} + +if ($cxx.target.class == 'linux') + import libs += libtirpc%lib{tirpc} + +./: exe{driver} doc{README} + +exe{driver}: {hxx cxx}{* -library} {hxx ixx cxx}{library} $libs + +exe{driver}: xml{library}: test.input = true + +<{hxx ixx cxx}{library}>: xsd{library} $xsd +{{ + diag xsd ($<[0]) # @@ TMP + + $xsd cxx-tree --std c++11 \ + --generate-inline \ + --generate-ostream \ + --generate-insertion 'XDR' \ + --generate-extraction 'XDR' \ + --generate-comparison \ + --output-dir $out_base \ + $path($<[0]) +}} + +cxx.poptions =+ "-I$out_base" diff --git a/xsd-examples/cxx/tree/binary/xdr/driver.cxx b/xsd-examples/cxx/tree/binary/xdr/driver.cxx new file mode 100644 index 0000000..f706ebf --- /dev/null +++ b/xsd-examples/cxx/tree/binary/xdr/driver.cxx @@ -0,0 +1,148 @@ +// file : cxx/tree/binary/xdr/driver.cxx +// copyright : not copyrighted - public domain + +#include // std::unique_ptr +#include // std::memcpy +#include // std::size_t +#include + +#include +#include + +#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); + + // Read in the file. + // + std::unique_ptr 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 (&buf), 0, &overflow); + xdr.x_op = XDR_ENCODE; + + xml_schema::ostream 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 (&ui), &underflow, 0); + xdr.x_op = XDR_DECODE; + + xdrrec_skiprecord (&xdr); + + xml_schema::istream ixdr (xdr); + + std::unique_ptr 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 (p)); + size_t n (static_cast (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 (p)); + size_t n (static_cast (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/xsd-examples/cxx/tree/binary/xdr/library.xml b/xsd-examples/cxx/tree/binary/xdr/library.xml new file mode 100644 index 0000000..05ed593 --- /dev/null +++ b/xsd-examples/cxx/tree/binary/xdr/library.xml @@ -0,0 +1,52 @@ + + + + + + + + 0679760806 + The Master and Margarita + fiction + + + Mikhail Bulgakov + 1891-05-15 + 1940-03-10 + + + + + + 0679600841 + War and Peace + history + + + Leo Tolstoy + 1828-09-09 + 1910-11-20 + + + + + + 0679420290 + Crime and Punishment + philosophy + + + Fyodor Dostoevsky + 1821-11-11 + 1881-02-09 + + + + diff --git a/xsd-examples/cxx/tree/binary/xdr/library.xsd b/xsd-examples/cxx/tree/binary/xdr/library.xsd new file mode 100644 index 0000000..8e1b316 --- /dev/null +++ b/xsd-examples/cxx/tree/binary/xdr/library.xsd @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.1