aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/hybrid/library/driver.cxx
blob: 41e3e1ba6f6ae6a7915781d41811309a83fdb02f (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
// file      : examples/cxx/hybrid/library/driver.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : not copyrighted - public domain

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

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

using namespace std;

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 (cin);
    else
      doc_p.parse (argv[1]);

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

    // Print what we've got.
    //
    catalog::book_sequence& books (c->book ());

    for (catalog::book_const_iterator i = books.begin ();
         i != books.end ();
         ++i)
    {
      cerr << "ISBN         : " << i->isbn () << endl
           << "Title        : " << i->title () << endl
           << "Genre        : " << i->genre ().string () << endl;

      for (book::author_const_iterator j = i->author ().begin ();
           j != i->author ().end ();
           ++j)
      {
        cerr << "Author       : " << j->name () << endl;

        xml_schema::date born (j->born ());

        cerr << "  Born       : "
             << born.year () << '-'
             << born.month () << '-'
             << born.day () << endl;

        if (j->died_present ())
        {
          xml_schema::date died (j->died ());

          cerr << "  Died       : "
               << died.year () << '-'
               << died.month () << '-'
               << died.day () << endl;
        }
      }

      cerr  << "Available    : " << i->available () << endl;
    }

    // Remove all unavailable books.
    //
    for (catalog::book_iterator j = books.begin (); j != books.end ();)
    {
      if (!j->available ())
        j = books.erase (j);
      else
        ++j;
    }

    // Insert a new book.
    //
    {
      auto_ptr<book> b (new book);

      isbn n;
      n.base_value (679776443);
      b->isbn (n);

      title t;
      t.assign ("Dead Souls");
      b->title (t);

      b->genre (genre::philosophy);

      author a;
      a.name ("Nikolai Gogol");
      a.born (xml_schema::date (1809, 3, 31));
      b->author ().push_back (a);

      books.insert (books.begin (), b.release ());
    }

    // Serialize.
    //
    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 (*c);
    doc_s.serialize (cout, xml_schema::document_simpl::pretty_print);
    catalog_s.post ();
  }
  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;
}