aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/hybrid/minimal/driver.cxx
blob: 38afd9a13fd079a6710642bbb5c8ecc4072f783d (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// file      : examples/cxx/hybrid/minimal/driver.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : not copyrighted - public domain

#include <stdio.h>

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

// eVC++ 4.0 does not like using xml_schema::strdupx;
//
inline char*
strdupx (const char* s)
{
  return xml_schema::strdupx (s);
}

struct writer: xml_schema::writer
{
  virtual bool
  write (const char* s, size_t n)
  {
    return fwrite (s, n, 1, stdout) == 1;
  }

  virtual bool
  flush ()
  {
    return fflush (stdout) == 0;
  }
};

int
main (int argc, char* argv[])
{
  const char* input;

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

  // Open the file or use STDIN.
  //
  FILE* f = argc > 1 ? fopen (argv[1], "rb") : stdin;

  if (f == 0)
  {
    fprintf (stderr, "%s: unable to open\n", input);
    return 1;
  }

  // Parse.
  //
  using xml_schema::parser_error;

  parser_error pe;
  bool io_error = false;
  people* ppl = 0;

  do
  {
    people_paggr people_p;
    xml_schema::document_pimpl doc_p (people_p.root_parser (),
                                      people_p.root_name ());

    if (pe = doc_p._error ())
      break;

    people_p.pre ();

    if (pe = people_p._error ())
      break;

    char buf[4096];

    do
    {
      size_t s = fread (buf, 1, sizeof (buf), f);

      if (s != sizeof (buf) && ferror (f))
      {
        io_error = true;
        break;
      }

      doc_p.parse (buf, s, feof (f) != 0);
      pe = doc_p._error ();

    } while (!pe && !feof (f));

    if (io_error || pe)
      break;

    ppl = people_p.post ();

    pe = people_p._error ();

  } while (false);

  if (argc > 1)
    fclose (f);

  // Handle parsing errors.
  //
  if (io_error)
  {
    fprintf (stderr, "%s: read failure\n", input);
    return 1;
  }

  if (pe)
  {
    switch (pe.type ())
    {
    case parser_error::sys:
      {
        fprintf (stderr, "%s: %s\n", input, pe.sys_text ());
        break;
      }
    case parser_error::xml:
      {
        fprintf (stderr, "%s:%lu:%lu: %s\n",
                 input, pe.line (), pe.column (), pe.xml_text ());
        break;
      }
#ifdef XSDE_PARSER_VALIDATION
    case parser_error::schema:
      {
        fprintf (stderr, "%s:%lu:%lu: %s\n",
                 input, pe.line (), pe.column (), pe.schema_text ());
        break;
      }
#endif
    case parser_error::app:
      {
        fprintf (stderr, "%s:%lu:%lu: application error %d\n",
                 input, pe.line (), pe.column (), pe.app_code ());
        break;
      }
    default:
      break;
    }

    return 1;
  }

  // Print what we've got.
  //
  people::person_sequence& ps = ppl->person ();

  for (people::person_const_iterator i = ps.begin (); i != ps.end (); ++i)
  {
    printf ("first: %s\n" "last: %s\n" "gender: %s\n" "age: %hu\n\n",
            i->first_name (),
            i->last_name (),
            i->gender ().string (),
            i->age ());
  }

  // Remove people that are younger than 30.
  //
  for (people::person_iterator j = ps.begin (); j != ps.end ();)
  {
    if (j->age () < 30)
      j = ps.erase (j);
    else
      ++j;
  }

  // Insert a new person.
  //
  {
    person* p = 0;
    bool mem_error = false;

    do
    {
      p = new person;

      if (p == 0)
      {
        mem_error = true;
        break;
      }

      char* s = strdupx ("Joe");

      if (s == 0)
      {
        mem_error = true;
        break;
      }

      p->first_name (s);

      s = strdupx ("Dirt");

      if (s == 0)
      {
        mem_error = true;
        break;
      }

      p->last_name (s);

      p->age (36);
      p->gender (gender::male);

      if (ps.insert (ps.begin (), p) != 0)
      {
        mem_error = true;
        p = 0; // The sequence has already deleted the object.
        break;
      }

    } while (false);

    if (mem_error)
    {
      fprintf (stderr, "error: no memory\n");
      delete p;
      delete ppl;
      return 1;
    }
  }

  // Serialize.
  //
  using xml_schema::serializer_error;

  serializer_error se;
  writer w;

  do
  {
    people_saggr people_s;
    xml_schema::document_simpl doc_s (people_s.root_serializer (),
                                      people_s.root_name ());

    doc_s.add_no_namespace_schema ("people.xsd");

    se = doc_s._error ();
    if (se)
      break;

    people_s.pre (*ppl);

    se = people_s._error ();
    if (se)
      break;

    doc_s.serialize (w, xml_schema::document_simpl::pretty_print);

    se = doc_s._error ();
    if (se)
      break;

    people_s.post ();

    se = people_s._error ();

  } while (false);

  delete ppl;

  // Handle serializer errors.
  //
  if (se)
  {
    switch (se.type ())
    {
    case serializer_error::sys:
      {
        fprintf (stderr, "error: %s\n", se.sys_text ());
        break;
      }
    case serializer_error::xml:
      {
        fprintf (stderr, "error: %s\n", se.xml_text ());
        break;
      }
#ifdef XSDE_SERIALIZER_VALIDATION
    case serializer_error::schema:
      {
        fprintf (stderr, "error: %s\n", se.schema_text ());
        break;
      }
#endif
    case serializer_error::app:
      {
        fprintf (stderr, "application error: %d\n", se.app_code ());
        break;
      }
    default:
      break;
    }

    return 1;
  }

  return 0;
}