#include "inputcsv.hh" #include "posutil.hh" #include "posexcept.hh" #include "stock.hh" #include "calibrationrequest.hh" #include #include using namespace std ; using std::tr1::unordered_map ; #define DEBUG /* *** Operations *** */ /** * This function reads the next Request in the CSV input file. Blank * lines and lines containing only spaces are skipped. * * #file should be opened before proceeding requests; otherwise, * #current_request is \link Request::clear() cleared\endlink and a * blank Request is returned. The file must be valid, * semicolon-separated (\em not comma-separated). * * @return The read Request, or a blank Request in case of error (file * not opened, end of file, invalid field or wrong number of fields in * the line). */ const Request& InputCSV::get_next_request() { clear_current_request() ; if (! file.next_line()) // End of file or error return *current_request ; ++current_line_nb ; // Read Mobile MAC field string mac_mobile ; if (! file.read_field(mac_mobile)) // Wrong number of fields { #ifdef DEBUG cerr << "InputCSV: cannot read mac_mobile.\n" ; #endif // DEBUG return *current_request ; } const Mobile &mobile = Stock::find_create_mobile(mac_mobile) ; current_request->set_mobile(&mobile) ; // Read request type uint8_t type ; uint16_t type_r ; if (! file.read_field(type_r)) { // Wrong number of fields: blank current request current_request->clear() ; #ifdef DEBUG cerr << "InputCSV: cannot read type.\n" ; #endif // DEBUG return *current_request ; } type = type_r ; current_request->set_type(type) ; // Read Timestamp field Timestamp timestamp ; if (! file.read_timestamp(timestamp)) { // Wrong number of fields: blank current request current_request->clear() ; #ifdef DEBUG cerr << "InputCSV: cannot read timestamp.\n" ; #endif // DEBUG return *current_request ; } current_request->set_time_sent(timestamp) ; // Read position fields Point3D pos ; if (! file.read_point3d(pos)) { // Wrong number of fields: blank current request current_request->clear() ; #ifdef DEBUG cerr << "InputCSV: cannot read coordinates.\n" ; #endif // DEBUG return *current_request ; } // Read direction field Direction direction ; int direction_int ; if (! file.read_field(direction_int)) { // Wrong number of fields: blank current request current_request->clear() ; #ifdef DEBUG cerr << "InputCSV: cannot read direction.\n" ; #endif // DEBUG return *current_request ; } if (direction_int != 0) direction = direction_int ; // Reading {MAC_AP;SS} couples unordered_map measurements ; string mac_ap ; while (file.read_field(mac_ap)) { int ss ; if (! file.read_field(ss)) { // Wrong number of fields: blank current request current_request->clear() ; #ifdef DEBUG cerr << "InputCSV: cannot read mac_ap.\n" ; #endif // DEBUG return *current_request ; } const AccessPoint &ap = Stock::find_create_ap(mac_ap) ; measurements[mac_ap].set_ap(&ap) ; measurements[mac_ap].add_ss(ss) ; } current_request->set_measurements(measurements) ; // Calibration request? if (type == OWL_REQUEST_CALIBRATION || type == OWL_REQUEST_AUTOCALIBRATION) { ReferencePoint position(pos) ; const ReferencePoint &reference_point = Stock::find_create_reference_point(position) ; current_request_to_calibration_request(&reference_point, direction, type) ; } // We set the real coordinates (if found) only for non-calibration // requests else if (pos) current_request->set_real_position(pos) ; return *current_request ; }