[Positioner] Refactor InputCSV::fill_current_request()

Use the new function InputCSV::read_field() to read the fields and
display error messages as needed.
This commit is contained in:
Matteo Cypriani 2013-06-14 13:12:50 -04:00
parent 5bf1bcb4d2
commit bd48479d2d
2 changed files with 66 additions and 58 deletions

View File

@ -44,15 +44,11 @@ bool InputCSV::fill_current_request()
return false ;
++current_line_nb ;
// Read CSV format version
/* CSV format version */
uint_fast16_t csv_format_version ;
if (! file.read_field(csv_format_version))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read the CSV format version.\n" ;
return false ;
}
if (! read_field(csv_format_version, "the CSV format version"))
return false ;
// Check the CSV format
switch (csv_format_version)
@ -66,14 +62,11 @@ bool InputCSV::fill_current_request()
return false ;
}
// Read Mobile MAC field
/* Mobile's MAC */
string mac_mobile ;
if (! file.read_field(mac_mobile)) // Wrong number of fields
{
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read mac_mobile.\n" ;
return false ;
}
if (! read_field(mac_mobile, "mobile's MAC address"))
return false ;
PosUtil::to_upper(mac_mobile) ;
if (! Configuration::bool_value("positioning.accept-new-mobiles") &&
@ -83,87 +76,68 @@ bool InputCSV::fill_current_request()
const Mobile &mobile = Stock::find_create_mobile(mac_mobile) ;
current_request->set_mobile(&mobile) ;
// Read request type
/* Request type */
uint_fast8_t type ;
uint_fast16_t type_r ;
if (! file.read_field(type_r))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read type.\n" ;
return false ;
}
if (! read_field(type_r, "the request's type"))
return false ;
type = type_r ;
current_request->set_type(type) ;
// Read the number of packets
/* Number of packets */
uint_fast16_t nb_packets ;
if (! file.read_field(nb_packets))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read the number of packets.\n" ;
return false ;
}
if (! read_field(nb_packets, "the number of packets"))
return false ;
current_request->set_nb_packets(nb_packets) ;
// Read Timestamp field
/* Timestamp */
Timestamp timestamp ;
if (! file.read_timestamp(timestamp))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read timestamp.\n" ;
cerr << "InputCSV: cannot read the request's timestamp.\n" ;
return false ;
}
current_request->set_time_sent(timestamp) ;
// Read position fields
/* Position */
Point3D position ;
if (! file.read_point3d(position))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read coordinates.\n" ;
cerr << "InputCSV: cannot read the mobile's coordinates.\n" ;
return false ;
}
// Read direction field
/* Direction */
Direction direction ;
int direction_int ;
if (! file.read_field(direction_int))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read direction.\n" ;
return false ;
}
if (! read_field(direction_int, "the mobile's direction"))
return false ;
if (direction_int != 0)
direction = direction_int ;
// Reading all the {AP_MAC;Packet_ID;SS}
/* Read all the {AP_MAC;Packet_ID;SS} */
unordered_map<string, Measurement> measurements ;
string mac_ap ;
while (file.read_field(mac_ap))
{
pkt_id_t packet_id ;
if (! file.read_field(packet_id))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read the packet ID.\n" ;
return false ;
}
if (! read_field(packet_id, "the packet ID"))
return false ;
int_fast16_t ss ;
if (! file.read_field(ss))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read the signal strength.\n" ;
return false ;
}
if (! read_field(ss, "the signal strength"))
return false ;
PosUtil::to_upper(mac_ap) ;
if (! Configuration::bool_value("positioning.accept-new-aps") &&

View File

@ -17,8 +17,10 @@
#include "inputmedium.hh"
#include "csvfilereader.hh"
#include "configuration.hh"
#include <string>
#include <iostream>
/// Reads [requests](@ref Request) from a CSV file
/**
@ -36,6 +38,9 @@ protected:
//@{
/// Reads data & fills the current request
bool fill_current_request(void) ;
/// Reads a field and handle error message printing
template<class T> bool read_field(T &field,
const std::string &message) ;
//@}
public:
@ -68,6 +73,35 @@ inline bool InputCSV::eof() const
/* *** Operations *** */
/**
* This functions tries to read `field` from the input CSV file; if it
* fails and the verbose mode is active, a message is displayed on the
* standard error.
*
* @param field The variable in which the read field will be stored.
* @param message The name of the field to be displayed in case of error.
*
* @returns `true` in case of success.
* @returns `false` in case of error.
*/
template<class T>
inline bool InputCSV::read_field(T &field, const std::string &message)
{
if (file.read_field(field))
return true ;
// Wrong number of fields
if (Configuration::is_configured("verbose"))
std::cerr << "InputCSV: cannot read " << message << ".\n" ;
return false ;
}
/* *** Operators *** */