1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 //
3 // demo_xml.cpp
4 //
5 // (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
6 // Use, modification and distribution is subject to the Boost Software
7 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9
10 #include <iomanip>
11 #include <iostream>
12 #include <fstream>
13 #include <string>
14
15 #include <cstdio> // remove
16 #include <boost/config.hpp>
17 #if defined(BOOST_NO_STDC_NAMESPACE)
18 namespace std{
19 using ::remove;
20 }
21 #endif
22
23 #include <boost/archive/tmpdir.hpp>
24 #include <boost/archive/xml_iarchive.hpp>
25 #include <boost/archive/xml_oarchive.hpp>
26
27 #include "demo_gps.hpp"
28
save_schedule(const bus_schedule & s,const char * filename)29 void save_schedule(const bus_schedule &s, const char * filename){
30 // make an archive
31 std::ofstream ofs(filename);
32 assert(ofs.good());
33 boost::archive::xml_oarchive oa(ofs);
34 oa << BOOST_SERIALIZATION_NVP(s);
35 }
36
37 void
restore_schedule(bus_schedule & s,const char * filename)38 restore_schedule(bus_schedule &s, const char * filename)
39 {
40 // open the archive
41 std::ifstream ifs(filename);
42 assert(ifs.good());
43 boost::archive::xml_iarchive ia(ifs);
44
45 // restore the schedule from the archive
46 ia >> BOOST_SERIALIZATION_NVP(s);
47 }
48
main(int argc,char * argv[])49 int main(int argc, char *argv[])
50 {
51 // make the schedule
52 bus_schedule original_schedule;
53
54 // fill in the data
55 // make a few stops
56 bus_stop *bs0 = new bus_stop_corner(
57 gps_position(34, 135, 52.560f),
58 gps_position(134, 22, 78.30f),
59 "24th Street", "10th Avenue"
60 );
61 bus_stop *bs1 = new bus_stop_corner(
62 gps_position(35, 137, 23.456f),
63 gps_position(133, 35, 54.12f),
64 "State street", "Cathedral Vista Lane"
65 );
66 bus_stop *bs2 = new bus_stop_destination(
67 gps_position(35, 136, 15.456f),
68 gps_position(133, 32, 15.300f),
69 "White House"
70 );
71 bus_stop *bs3 = new bus_stop_destination(
72 gps_position(35, 134, 48.789f),
73 gps_position(133, 32, 16.230f),
74 "Lincoln Memorial"
75 );
76
77 // make a routes
78 bus_route route0;
79 route0.append(bs0);
80 route0.append(bs1);
81 route0.append(bs2);
82
83 // add trips to schedule
84 original_schedule.append("bob", 6, 24, &route0);
85 original_schedule.append("bob", 9, 57, &route0);
86 original_schedule.append("alice", 11, 02, &route0);
87
88 // make aother routes
89 bus_route route1;
90 route1.append(bs3);
91 route1.append(bs2);
92 route1.append(bs1);
93
94 // add trips to schedule
95 original_schedule.append("ted", 7, 17, &route1);
96 original_schedule.append("ted", 9, 38, &route1);
97 original_schedule.append("alice", 11, 47, &route1);
98
99 // display the complete schedule
100 std::cout << "original schedule";
101 std::cout << original_schedule;
102
103 std::string filename(boost::archive::tmpdir());
104 filename += "/demo.xml";
105
106 // save the schedule
107 save_schedule(original_schedule, filename.c_str());
108
109 // ... some time later
110 // make a new schedule
111 bus_schedule new_schedule;
112
113 restore_schedule(new_schedule, filename.c_str());
114
115 // and display
116 std::cout << "\nrestored schedule";
117 std::cout << new_schedule;
118 // should be the same as the old one. (except for the pointer values)
119
120 std::remove(filename.c_str());
121
122 delete bs0;
123 delete bs1;
124 delete bs2;
125 delete bs3;
126 return 0;
127 }
128