From 3808c2ce6f7cff3646caa33ce643380555b91701 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Thu, 30 Aug 2012 10:23:09 +0200 Subject: [PATCH] [Positioner] Add Request::to_csv() Code moved from InputLogCSV. --- owlps-positioner/src/inputlogcsv.cc | 51 +---------------------------- owlps-positioner/src/inputlogcsv.hh | 4 +-- owlps-positioner/src/request.cc | 37 +++++++++++++++++++++ owlps-positioner/src/request.hh | 6 ++++ 4 files changed, 45 insertions(+), 53 deletions(-) diff --git a/owlps-positioner/src/inputlogcsv.cc b/owlps-positioner/src/inputlogcsv.cc index df2e87f..bea71b1 100644 --- a/owlps-positioner/src/inputlogcsv.cc +++ b/owlps-positioner/src/inputlogcsv.cc @@ -4,53 +4,4 @@ * (Université de Franche-Comté), France. */ - -#include "inputlogcsv.hh" -#include "posexcept.hh" -#include "mobile.hh" -#include "calibrationrequest.hh" -#include "referencepoint.hh" - -#include - -using namespace std ; -using std::tr1::unordered_map ; - - - -/* *** Operations *** */ - - -const string InputLogCSV::request_to_csv(const Request &request) const -{ - ostringstream csv_line ; - if (request.get_mobile() != NULL) - csv_line << request.get_mobile()->get_mac_addr() ; - csv_line - << ';' << static_cast(request.get_type()) - << ';' << request.get_nb_packets() - << ';' << request.get_time_sent() - << ';' ; - - const CalibrationRequest *calibration_request = - dynamic_cast(&request) ; - if (calibration_request == NULL) - csv_line << "0;0;0;0" ; - else - { - csv_line - << calibration_request->get_reference_point()->get_x() << ';' - << calibration_request->get_reference_point()->get_y() << ';' - << calibration_request->get_reference_point()->get_z() << ';' - << static_cast(calibration_request->get_direction()) ; - } - - const unordered_map &measurements = - request.get_measurements() ; - for (unordered_map::const_iterator i - = measurements.begin() ; i != measurements.end() ; ++i) - csv_line << i->second.to_csv() ; - - csv_line << '\n' ; - return csv_line.str() ; -} +// Place holder for potential additional code for InputLogCSV. diff --git a/owlps-positioner/src/inputlogcsv.hh b/owlps-positioner/src/inputlogcsv.hh index ea6c215..6c10a00 100644 --- a/owlps-positioner/src/inputlogcsv.hh +++ b/owlps-positioner/src/inputlogcsv.hh @@ -20,8 +20,6 @@ class InputLogCSV: public InputLogMedium protected: TextFileWriter file ; - const std::string request_to_csv(const Request &request) const ; - public: InputLogCSV(const std::string &filename): file(filename) {} @@ -39,7 +37,7 @@ public: inline bool InputLogCSV::log_request(const Request &request) { - return file.write_text(request_to_csv(request)) ; + return file.write_text(request.to_csv() + '\n') ; } diff --git a/owlps-positioner/src/request.cc b/owlps-positioner/src/request.cc index 0a6b42a..52dd6fe 100644 --- a/owlps-positioner/src/request.cc +++ b/owlps-positioner/src/request.cc @@ -6,6 +6,8 @@ #include "request.hh" +#include "calibrationrequest.hh" +#include "referencepoint.hh" #include "mobile.hh" using namespace std ; @@ -197,6 +199,41 @@ bool Request::operator==(const Request &source) const +const string Request::to_csv() const +{ + ostringstream csv_line ; + + if (mobile != NULL) + csv_line << mobile->get_mac_addr() ; + + csv_line + << ';' << static_cast(type) + << ';' << nb_packets + << ';' << time_sent + << ';' ; + + const CalibrationRequest *calibration_request = + dynamic_cast(this) ; + if (calibration_request == NULL) + csv_line << "0;0;0;0" ; + else + { + csv_line + << calibration_request->get_reference_point()->get_x() << ';' + << calibration_request->get_reference_point()->get_y() << ';' + << calibration_request->get_reference_point()->get_z() << ';' + << static_cast(calibration_request->get_direction()) ; + } + + for (unordered_map::const_iterator i + = measurements.begin() ; i != measurements.end() ; ++i) + csv_line << i->second.to_csv() ; + + return csv_line.str() ; +} + + + ostream& operator<<(ostream &os, const Request &r) { // Timestamp diff --git a/owlps-positioner/src/request.hh b/owlps-positioner/src/request.hh index 12b25a4..890161b 100644 --- a/owlps-positioner/src/request.hh +++ b/owlps-positioner/src/request.hh @@ -96,6 +96,12 @@ public: void clear(void) ; //@} + /** @name Conversion accessors */ + //@{ + /// Converts to a CSV string + const std::string to_csv(void) const ; + //@} + /** @name Operations */ //@{ /// Computes the similarity of two Request