owlps/owlps-positioning/src/inputcsv.cc

230 lines
5.9 KiB
C++
Raw Normal View History

#include "inputcsv.hh"
#include "posutil.hh"
#include "posexcept.hh"
#include "stock.hh"
#include "calibrationrequest.hh"
#include <iostream>
#include <boost/tr1/unordered_map.hpp>
using namespace std ;
using std::tr1::unordered_map ;
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>
using namespace boost ;
/* *** Constructors *** */
/**
* Prepares the InputCSV to read a CSV file.
* @param filename The name of the file to open.
* @throw error_opening_input_file if the file cannot be opened.
*/
InputCSV::InputCSV(const string &filename):
file(filename)
{
read_next_line() ;
}
/* *** Operations *** */
/**
* Read the next non-blank line. Tabs and spaces at the begining of a
* line are skipped.
* #current_line is set to empty string in case of error.
*/
void InputCSV::read_next_line()
{
string::size_type first_non_blank = 0 ;
do
{
if (! file.read_line(current_line))
{
current_line.clear() ;
return ;
}
++current_line_nb ;
}
while ((first_non_blank = current_line.find_first_not_of(" \t"))
== string::npos) ;
current_line.erase(0, first_non_blank) ;
}
/**
* This function reads the next Request in the CSV input file. Blank
* lines and lines containing only spaces are skipped.
*
* #input_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 (current_line.empty()) // End of file or error
return *current_request ;
// Split read string into fields (semicolon-separated)
tokenizer<escaped_list_separator<char> > tok(
current_line, escaped_list_separator<char>('\\', ';', '\"')) ;
tokenizer<escaped_list_separator<char> >::const_iterator ti(tok.begin()) ;
// Read Mobile MAC field
if (ti == tok.end()) // Wrong number of fields
return *current_request ;
const Mobile &mobile = Stock::find_create_mobile(*ti) ;
current_request->set_mobile(&mobile) ;
// Read Timestamp field
if (++ti == tok.end())
{
// Wrong number of fields: blank current request
current_request->clear() ;
return *current_request ;
}
try
{
current_request->set_time_sent(Timestamp
(lexical_cast<uint64_t>(*ti))) ;
}
catch (bad_lexical_cast &e)
{
cerr
<< "InputCSV::get_next_request(): Bad value « "
<< *ti << " » at line "
<< current_line_nb << ", field « Timestamp », of input file « "
<< file.get_name() << " »!" << endl ;
current_request->clear() ;
return *current_request ;
}
// Read position fields
bool is_calibration_request = false ;
float pos[3] ;
for (int i = 0 ; i < 3 ; ++i)
{
if (++ti == tok.end())
{
// Wrong number of fields: blank current request
current_request->clear() ;
return *current_request ;
}
try
{
pos[i] = lexical_cast<float>(*ti) ;
if (pos[i] != 0)
is_calibration_request = true ;
}
catch (bad_lexical_cast &e)
{
cerr
<< "InputCSV::get_next_request(): Bad value « "
<< *ti << " » at line "
<< current_line_nb << ", position field #" << i
<< ", of input file « " << file.get_name() << " »!"
<< endl ;
current_request->clear() ;
return *current_request ;
}
}
// Read direction field
Direction direction ;
if (++ti == tok.end())
{
// Wrong number of fields: blank current request
current_request->clear() ;
return *current_request ;
}
try
{
int d = lexical_cast<int>(*ti) ;
if (d != 0)
{
is_calibration_request = true ;
direction = d ;
}
}
catch (bad_lexical_cast &e)
{
cerr
<< "InputCSV::get_next_request(): Bad value « "
<< *ti << " » at line "
<< current_line_nb << ", field « Direction », of input file « "
<< file.get_name() << " »!" << endl ;
current_request->clear() ;
return *current_request ;
}
// Reading {MAC_AP;SS} couples
unordered_map<string, Measurement> measurements ;
for (++ti ; ti != tok.end() ; ++ti)
{
string mac_ap(*ti) ;
if (++ti == tok.end())
{
// Wrong number of fields: blank current request
current_request->clear() ;
return *current_request ;
}
int ss ;
try
{
ss = lexical_cast<int>(*ti) ;
}
catch (bad_lexical_cast &e)
{
cerr
<< "InputCSV::get_next_request(): Bad value « "
<< *ti << " » at line "
<< current_line_nb
<< " of input file « " << file.get_name() << " »!"
<< endl ;
current_request->clear() ; // Blank current request
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) ;
if (is_calibration_request)
{
current_request_to_calibration_request() ;
CalibrationRequest *request =
static_cast<CalibrationRequest*>(current_request) ;
request->set_direction(direction) ;
ReferencePoint position(pos) ;
const ReferencePoint &reference_point =
Stock::find_create_reference_point(position) ;
request->set_reference_point(&reference_point) ;
}
read_next_line() ;
return *current_request ;
}