owlps/owlps-positioning/inputcsv.cc

168 lines
3.9 KiB
C++

#include "inputcsv.hh"
#include "posutil.hh"
#include <stdint.h>
#include <iostream>
#include <unordered_map>
using namespace std ;
using std::tr1::unordered_map ;
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>
using namespace boost ;
/*** Constructeurs ***/
InputCSV::InputCSV(const string &filename)
{
input_file_name = filename ;
input_file.open(input_file_name.c_str()) ;
if (! input_file)
cerr
<< "InputCSV::InputCSV(): Error opening input file « "
<< input_file_name << " »!" << endl ;
}
/*** Opérations ***/
const Request& InputCSV::get_next_request()
{
if (! input_file)
return current_request ;
string line ;
// Skipping blank lines
do
{
++current_line ;
getline(input_file, line) ;
}
while (! input_file.eof()
&& line.find_first_not_of(" \t") == string::npos) ;
if (input_file.eof())
{
// End of file reached: blank current request
current_request.clear() ;
return current_request ;
}
// Split read string into fields (semicolon-separated)
tokenizer<escaped_list_separator<char> > tok(
line, escaped_list_separator<char>('\\', ';', '\"')) ;
tokenizer<escaped_list_separator<char> >::iterator ti(tok.begin()) ;
// Read Mobile MAC field
if (ti == tok.end())
{
// Wrong number of fields: blank current request
current_request.clear() ;
return current_request ;
}
// FIXME! We need to search for the mobile corresponding to the read
// MAC address and set the mobile pointer in the Request.
//current_request.set_mobile(NULL) ;
// Read Timestamp field
if (++ti == tok.end())
{
// Wrong number of fields: blank current request
current_request.clear() ;
return current_request ;
}
try
{
current_request.set_timestamp(PosUtil::ns_to_timespec
(lexical_cast<uint64_t>(*ti))) ;
}
catch (bad_lexical_cast &e)
{
cerr
<< "InputCSV::get_next_request(): Bad value « "
<< *ti << " » at line "
<< current_line << ", field « Timestamp », of input file « "
<< input_file_name << " »!" << endl ;
current_request.clear() ; // Blank current request
return current_request ;
}
// Read position fields
float pos[4] ;
for (int i = 0 ; i < 4 ; ++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) ;
}
catch (bad_lexical_cast &e)
{
cerr
<< "InputCSV::get_next_request(): Bad value « "
<< *ti << " » at line "
<< current_line << ", position field #" << i
<< ", of input file « " << input_file_name << " »!"
<< endl ;
current_request.clear() ; // Blank current request
return current_request ;
}
}
unordered_map<string, Measurement> measurements ;
// Reading {MAC_AP;SS} couples
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
<< " of input file « " << input_file_name << " »!"
<< endl ;
current_request.clear() ; // Blank current request
return current_request ;
}
// Adding value
measurements[mac_ap].add_ss(ss) ;
}
current_request.set_measurements(measurements) ;
return current_request ;
}