aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/hybrid/binary/xdr/driver.cxx
blob: 0d05e577f11e6686c4133d7c63e794b5c77980fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// file      : examples/cxx/hybrid/binary/xdr/driver.cxx
// copyright : not copyrighted - public domain

#include <stddef.h>  // size_t
#include <string.h>  // memcpy

#include <rpc/types.h>
#include <rpc/xdr.h>

#include <memory>    // std::auto_ptr
#include <iostream>

#include "library.hxx"
#include "library-pimpl.hxx"
#include "library-simpl.hxx"

using std::cerr;
using std::endl;

// 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[])
{
  const char* input;

  if (argc < 2)
  {
    input = "STDIN";
    cerr << "XML file not specified, reading from STDIN" << endl;
  }
  else
    input = argv[1];

  try
  {
    using namespace library;

    // Parse.
    //
    catalog_paggr catalog_p;

    xml_schema::document_pimpl doc_p (
      catalog_p.root_parser (),
      catalog_p.root_namespace (),
      catalog_p.root_name ());

    catalog_p.pre ();

    if (argc < 2)
      doc_p.parse (std::cin);
    else
      doc_p.parse (argv[1]);

    std::auto_ptr<catalog> c (catalog_p.post ());

    // Save the object model to an XDR stream.
    //
    xdrrec_create_p xdrrec_create_ =
      reinterpret_cast<xdrrec_create_p> (::xdrrec_create);

    XDR xdr;
    xml_schema::buffer buf;

    xdrrec_create_ (&xdr, 0, 0, reinterpret_cast<char*> (&buf), 0, &overflow);
    xdr.x_op = XDR_ENCODE;
    xml_schema::oxdrstream 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 << "binary representation size: " << buf.size () << endl
         << endl;

    // Load the object model 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::ixdrstream ixdr (xdr);

    std::auto_ptr<catalog> copy (new catalog);
    ixdr >> *copy;

    xdr_destroy (&xdr);

    // Serialize the copy back to XML.
    //
    catalog_saggr catalog_s;

    xml_schema::document_simpl doc_s (
      catalog_s.root_serializer (),
      catalog_s.root_namespace (),
      catalog_s.root_name ());

    doc_s.add_prefix ("lib", "http://www.codesynthesis.com/library");
    doc_s.add_schema ("http://www.codesynthesis.com/library", "library.xsd");

    catalog_s.pre (*copy);
    doc_s.serialize (std::cout, xml_schema::document_simpl::pretty_print);
    catalog_s.post ();
  }
  catch (const xml_schema::xdr_exception&)
  {
    cerr << "XDR operation failed" << endl;
    return 1;
  }
  catch (const xml_schema::parser_exception& e)
  {
    cerr << input << ":" << 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 << input << ": unable to open or read/write failure" << endl;
    return 1;
  }

  return 0;
}

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);
  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;

  memcpy (buf, ui->buf->data () + ui->pos, n);
  ui->pos += n;

  return n;
}