AirInv Logo  1.00.0
C++ Simulated Airline Inventory Management System library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BomPropertyTree.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // Boost Property Tree
5 #include <boost/property_tree/ptree.hpp>
6 #include <boost/property_tree/json_parser.hpp>
7 // Boost ForEach
8 #include <boost/foreach.hpp>
9 // AirInvServer
11 
12 namespace bpt = boost::property_tree;
13 
14 namespace stdair {
15 
16  // Loads BomPropertyTree structure from the specified JSON file
17  void BomPropertyTree::load (const std::string& iBomTree) {
18  // Create an empty property tree object
19  bpt::ptree pt;
20 
21  // Load the JSON formatted string into the property tree. If reading fails
22  // (cannot open stream, parse error), an exception is thrown.
23  std::istringstream iStr (iBomTree);
24  read_json (iStr, pt);
25 
26  // Get the airline_code and store it in the _airlineCode variable.
27  // Note that we construct the path to the value by separating
28  // the individual keys with dots. If dots appear in the keys,
29  // a path type with a different separator can be used.
30  // If the flight_date.airline_code key is not found, an exception is thrown.
31  _airlineCode = pt.get<stdair::AirlineCode_T> ("flight_date.airline_code");
32 
33  // Get the departure_date and store it in the _departureDate variable.
34  // This is another version of the get method: if the value is
35  // not found, the default value (specified by the second
36  // parameter) is returned instead. The type of the value
37  // extracted is determined by the type of the second parameter,
38  // so we can simply write get(...) instead of get<int>(...).
40  pt.get<stdair::FlightNumber_T> ("flight_date.flight_number", 100);
41 
42  const std::string& lDepartureDateStr =
43  pt.get<std::string> ("flight_date.departure_date");
44  _departureDate = boost::gregorian::from_simple_string (lDepartureDateStr);
45 
46  // Iterate over the flight_date.airport_codes section and store all found
47  // codes in the _airportCodeList set. The get_child() function
48  // returns a reference to the child at the specified path; if
49  // there is no such child, it throws. Property tree iterators
50  // are models of BidirectionalIterator.
51  /*
52  BOOST_FOREACH (bpt::ptree::value_type &v,
53  pt.get_child ("flight_date.airport_codes")) {
54  _airportCodeList.insert (v.second.data());
55  }
56  */
57  }
58 
59  // Saves the BomPropertyTree structure to the specified JSON file
60  std::string BomPropertyTree::save() const {
61  std::ostringstream oStr;
62 
63  // Create an empty property tree object
64  bpt::ptree pt;
65 
66  // Put airline code in property tree
67  pt.put ("flight_date.airline_code", _airlineCode);
68 
69  // Put flight number level in property tree
70  pt.put ("flight_date.flight_number", _flightNumber);
71 
72  // Put the flight departure date in property tree
73  const std::string& lDepartureDateStr =
74  boost::gregorian::to_simple_string (_departureDate);
75  pt.put ("flight_date.departure_date", lDepartureDateStr);
76 
77  // Iterate over the airport codes in the set and put them in the
78  // property tree. Note that the put function places the new
79  // key at the end of the list of keys. This is fine most of
80  // the time. If you want to place an item at some other place
81  // (i.e. at the front or somewhere in the middle), this can
82  // be achieved using a combination of the insert and put_own
83  // functions.
84  bpt::ptree lAirportCodeArray;
85  BOOST_FOREACH (const std::string& name, _airportCodeList) {
86  lAirportCodeArray.push_back (std::pair<bpt::ptree::key_type,
87  bpt::ptree::data_type> ("", name));
88  }
89  pt.put_child ("flight_date.airport_codes", lAirportCodeArray);
90  //pt.push_back (std::make_pair ("flight_date.airport_codes", lAirportCodeArray));
91 
92  // Write the property tree to the JSON stream.
93  write_json (oStr, pt);
94 
95  return oStr.str();
96  }
97 
98 }