xml_example.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 
00003 #include <ros/ros.h>
00004 
00005 #include <ros/package.h>
00006 
00007 #include <boost/property_tree/ptree.hpp>
00008 #include <boost/property_tree/xml_parser.hpp>
00009 #include <boost/foreach.hpp>
00010 #include <boost/shared_ptr.hpp>
00011 
00012 using namespace std;
00013 using boost::property_tree::ptree;
00014 
00015 class Calibration
00016 {
00017     public:
00018         
00019         double max;
00020         double min;
00021 };
00022 
00023 class Sensor
00024 {
00025     public:
00026         
00027         typedef boost::shared_ptr<Sensor> Ptr;
00028         
00029         string name;
00030         string type;
00031         Calibration calibration;
00032 
00033 };
00034 
00035 class Pedal
00036 {
00037     public:
00038         
00039         typedef boost::shared_ptr<Pedal> Ptr;
00040         
00041         vector<Sensor::Ptr> sensors;
00042         string name;
00043 };
00044 
00045 ostream& operator<<(ostream& o, Sensor::Ptr& sensor)
00046 {
00047     o<<"Sensor: "<<sensor->name<<" ";
00048     o<<"Type: "<<sensor->type<<" ";
00049     o<<"Min: "<<sensor->calibration.min<<" ";
00050     o<<"Max: "<<sensor->calibration.max<<" ";
00051     return o;
00052 }
00053 
00054 const ptree& empty_ptree()
00055 {
00056     static ptree t;
00057     return t;
00058 }
00059 
00060 // Read data from XML file
00061 vector<Pedal::Ptr> readPedalCalibration(string file_name,string& user_name, ptree& pt)
00062 {
00063     vector<Pedal::Ptr> pedals;
00064     
00065     // Try to read the file, if can't files doesn't exist!
00066     try
00067     {
00068         read_xml(file_name, pt, boost::property_tree::xml_parser::trim_whitespace);
00069     }catch(boost::property_tree::xml_parser::xml_parser_error& error)
00070     {
00071 //         cout<<"Cannot read XML file:\n\t"<<error.what()<<endl;
00072         cout << "Cannot read XML file" << endl;
00073         return pedals;
00074     }
00075     
00076     user_name = pt.get<std::string>("calibration.<xmlattr>.user_name");
00077     
00078     cout<<"Calibration for user: "<<user_name<<endl;
00079     
00080     BOOST_FOREACH(ptree::value_type const& v, pt.get_child("calibration"))
00081     {    
00082         if(v.first == "pedal")
00083         {
00084             Pedal::Ptr pedal(new Pedal);
00085             
00086             pedal->name = v.second.get<string>("name");
00087             
00088             for(auto it=v.second.begin();it!=v.second.end();it++)
00089             {
00090                 if(it->first == "sensor")
00091                 {
00092                     Sensor::Ptr sensor(new Sensor);
00093                     
00094                     sensor->name = it->second.get<string>("name");
00095                     sensor->type = it->second.get<string>("type");
00096                     sensor->calibration.max = it->second.get<double>("max");
00097                     sensor->calibration.min = it->second.get<double>("min");
00098                     
00099                     pedal->sensors.push_back(sensor);
00100                 }
00101             }
00102             
00103             pedals.push_back(pedal);
00104         }
00105     }
00106     
00107     return pedals;
00108 }
00109 
00110 // Overwrite an existing XML file
00111 void writeExistingCalibrationFile(string file_name, ptree& pt,const vector<Pedal::Ptr>& pedals)
00112 {
00113     boost::property_tree::ptree root_node;
00114 
00115     for(auto it = pt.begin(); it != pt.end();it++)
00116     {
00117 //         cout<<it->first<<endl; // debug
00118         if(it->first == "calibration")
00119         {
00120             for(auto cc_it = it->second.begin();cc_it!=it->second.end();cc_it++)//cc_it - calibration child iterator
00121             {
00122 //                 cout<<"  "<<cc_it->first<<endl; // debug
00123                 if(cc_it->first=="pedal")
00124                 {
00125                     //get the pedal name
00126                     string pedal_name = cc_it->second.get<string>("name");
00127                     
00128                     Pedal::Ptr pedal;
00129                     find_if(pedals.begin(),pedals.end(),[&](const Pedal::Ptr& p){if(p->name == pedal_name){pedal = p; return true;}});
00130                                         
00131                     for(auto pc_it = cc_it->second.begin(); pc_it!=cc_it->second.end();pc_it++)//pc_it - pedal child iterator
00132                     {
00133 //                         cout<<"    "<<pc_it->first<<endl; // debug
00134                         if(pc_it->first == "sensor")
00135                         {
00136                             string sensor_name = pc_it->second.get<string>("name");
00137                             
00138                             Sensor::Ptr sensor;
00139                             find_if(pedal->sensors.begin(),pedal->sensors.end(),[&](const Sensor::Ptr& s){if(s->name == sensor_name){sensor = s; return true;}});
00140                             
00141                             for(auto sc_it = pc_it->second.begin(); sc_it!=pc_it->second.end();sc_it++)
00142                             {
00143                                 if(sc_it->first=="type")
00144                                     sc_it->second.put_value(sensor->type);
00145                                 
00146                                 if(sc_it->first=="min")
00147                                     sc_it->second.put_value(sensor->calibration.min);
00148                                 
00149                                 if(sc_it->first=="max")
00150                                     sc_it->second.put_value(sensor->calibration.max);
00151                             }
00152                         }
00153                     }
00154                 }
00155             }
00156         }
00157     }
00158     
00159     boost::property_tree::xml_writer_settings<char> settings('\t', 1);
00160     
00161     // Write the property tree to the XML file.
00162     write_xml(file_name, pt,std::locale(), settings);
00163 }
00164 
00165 // Writing XML file for the first time
00166 void writeCalibrationFile(string file_name,const vector<Pedal::Ptr>& pedals)
00167 {
00168     ptree pt;
00169     
00170     boost::property_tree::ptree root_node;
00171    
00172     for(uint i=0;i<pedals.size();i++)
00173     {
00174         boost::property_tree::ptree pedal;
00175         
00176         for(uint h=0;h<pedals[i]->sensors.size();h++)
00177         {
00178             boost::property_tree::ptree sensor;
00179 
00180             sensor.add("name", pedals[i]->sensors[h]->name);
00181             sensor.add("type", pedals[i]->sensors[h]->type);
00182             sensor.add("min", pedals[i]->sensors[h]->calibration.min);
00183             sensor.add("max", pedals[i]->sensors[h]->calibration.max);
00184             
00185             pedal.add_child("sensor",sensor);
00186         }
00187         
00188         
00189         root_node.add_child("pedal",pedal);
00190     }
00191     
00192     pt.add_child("calibration",root_node);
00193 
00194     boost::property_tree::xml_writer_settings<char> settings('\t', 1);
00195     
00196     // Write the property tree to the XML file.
00197     write_xml(file_name, pt,std::locale(), settings);
00198 }
00199 
00200 int main()
00201 {
00202     // File name and username
00203     string file_name = ros::package::getPath("pedal_monitor") + "/src/calibration_test.xml";
00204     string user;
00205     
00206     ptree pt;
00207     
00208     // Read existing file.xml
00209     vector<Pedal::Ptr> pedals = readPedalCalibration(file_name,user,pt);
00210     
00211     // Get user and number of pedals (EXAMPLE)
00212     cout<<"The calibration file for "<<user<<" has: "<<pedals.size()<<" pedals."<<endl;
00213     
00214     // Get data from pedal 1 and sensor 1 (EXAMPLE)
00215     cout<<"Pedal 1 max: "<<pedals[0]->sensors[0]->calibration.max<<" min: "<<pedals[0]->sensors[0]->calibration.min<<endl;
00216     
00217     // Switching values of pedal 1 and sensor 1 (EXAMPLE)
00218     pedals[0]->sensors[0]->calibration.max = 23434;
00219     
00220     // Read all data of pedal sensors
00221     for(auto it=pedals.begin();it!=pedals.end();it++)
00222     {
00223         cout<<"Pedal: "<<(*it)->name<<endl;
00224         
00225         for(auto it_sensors= (*it)->sensors.begin(); it_sensors != (*it)->sensors.end(); it_sensors++)
00226              cout<<"\t"<<(*it_sensors)<<endl;
00227     }
00228     
00229     // Overwrite calibration file
00230     writeExistingCalibrationFile(ros::package::getPath("pedal_monitor") + "/src/test.xml",pt,pedals);
00231     
00232     return 0;
00233 }


pedal_monitor
Author(s): Pedro Mendes
autogenerated on Fri Jun 6 2014 18:37:21