From 0bce70a0e483294b83b8bf9d5468838a63405612 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 8 Mar 2009 17:23:30 +0200 Subject: Add support for binary representations xsde/cxx/hybrid/insertion-*: insertion operators generator xsde/cxx/hybrid/extraction-*: extraction operators generator libxsde/xsde/cxx/hybrid/cdr/: CDR support code libxsde/xsde/cxx/hybrid/xdr/: XDR support code tests/cxx/hybrid/binary/: new tests examples/cxx/hybrid/binary/: new examples documentation/cxx/hybrid/guide/: new chapter --- tests/cxx/hybrid/binary/xdr/driver.cxx | 147 +++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 tests/cxx/hybrid/binary/xdr/driver.cxx (limited to 'tests/cxx/hybrid/binary/xdr/driver.cxx') diff --git a/tests/cxx/hybrid/binary/xdr/driver.cxx b/tests/cxx/hybrid/binary/xdr/driver.cxx new file mode 100644 index 0000000..2f87dd9 --- /dev/null +++ b/tests/cxx/hybrid/binary/xdr/driver.cxx @@ -0,0 +1,147 @@ +// file : tests/cxx/hybrid/binary/xdr/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +// Test XDR insertion and extraction. +// + +#include // size_t +#include // memcpy +#include + +#include +#include + +#include "test.hxx" +#include "test-pimpl.hxx" +#include "test-simpl.hxx" + +using std::cerr; +using std::endl; + +extern "C" int +overflow (char* p, char* buf, int n) +{ + xml_schema::buffer* dst (reinterpret_cast (p)); + + size_t size (dst->size ()); + dst->size (size + n); + memcpy (dst->data () + size, buf, n); + + return n; +} + +struct underflow_info +{ + xml_schema::buffer* buf; + std::size_t pos; +}; + +extern "C" int +underflow (char* p, char* buf, int n) +{ + underflow_info* ui (reinterpret_cast (p)); + + size_t size (ui->buf->size () - ui->pos); + n = size > n ? n : size; + + memcpy (buf, ui->buf->data () + ui->pos, n); + ui->pos += n; + + return n; +} + +using namespace test; + +int +main (int argc, char* argv[]) +{ + /* + try + { + */ + + if (argc != 2) + { + cerr << "usage: " << argv[0] << " test.xml" << endl; + return 1; + } + + // Parse. + // + root_paggr root_p; + + xml_schema::document_pimpl doc_p ( + root_p.root_parser (), + root_p.root_namespace (), + root_p.root_name ()); + + root_p.pre (); + doc_p.parse (argv[1]); + std::auto_ptr r (root_p.post ()); + + // 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::oxdrstream oxdr (xdr); + oxdr << *r; + xdrrec_endofrecord (&xdr, true); // flush the data + xdr_destroy (&xdr); + + // 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::ixdrstream ixdr (xdr); + std::auto_ptr c (new type); + ixdr >> *c; + xdr_destroy (&xdr); + + // Serialize. + // + root_saggr root_s; + + xml_schema::document_simpl doc_s ( + root_s.root_serializer (), + root_s.root_namespace (), + root_s.root_name ()); + + doc_s.add_prefix ("t", "test"); + + root_s.pre (*c); + doc_s.serialize (std::cout); + root_s.post (); + + /* + } + catch (const xml_schema::xdr_exception&) + { + cerr << "XDR operation filed" << endl; + return 1; + } + catch (const xml_schema::parser_exception& e) + { + cerr << argv[0] << ":" << e.line () << ":" << e.column () << ": " + << e.text () << endl; + return 1; + } + catch (const xml_schema::serializer_exception& e) + { + cerr << "error: " << e.text () << endl; + return 1; + } + catch (const std::ios_base::failure&) + { + cerr << argv[0] << ": unable to open or read/write failure" << endl; + return 1; + } + */ +} -- cgit v1.1