207 lines
5.0 KiB
C++
207 lines
5.0 KiB
C++
#include "inputcsv.hh"
|
|
#include "posutil.hh"
|
|
#include "stock.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 ;
|
|
|
|
|
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
/**
|
|
* Prepares the InputCSV to read a CSV file.
|
|
* @param filename The name of the file to open.
|
|
* @throw runtime_error if the file cannot be opened.
|
|
*/
|
|
InputCSV::InputCSV(const string &filename)
|
|
{
|
|
input_file_name = filename ;
|
|
|
|
input_file.open(input_file_name.c_str()) ;
|
|
if (! input_file)
|
|
throw runtime_error("Error opening input file « " +
|
|
input_file_name + " »!") ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
/**
|
|
* 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()
|
|
{
|
|
if (! input_file)
|
|
{
|
|
current_request.clear() ;
|
|
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 ;
|
|
}
|
|
|
|
// If the mobile did not exist, we create it
|
|
try
|
|
{
|
|
Stock::get_mobile(*ti) ;
|
|
}
|
|
catch (out_of_range e)
|
|
{
|
|
Stock::getw_mobile(*ti).set_mac_addr(*ti) ;
|
|
}
|
|
current_request.set_mobile(&Stock::get_mobile(*ti)) ;
|
|
|
|
// 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::ms_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 ;
|
|
}
|
|
|
|
// If the AP did not exist, we create it
|
|
try
|
|
{
|
|
Stock::get_ap(mac_ap) ;
|
|
}
|
|
catch (out_of_range e)
|
|
{
|
|
Stock::getw_ap(mac_ap).set_mac_addr(mac_ap) ;
|
|
}
|
|
measurements[mac_ap].set_ap(&Stock::get_ap(mac_ap)) ;
|
|
|
|
// Adding value
|
|
measurements[mac_ap].add_ss(ss) ;
|
|
}
|
|
|
|
current_request.set_measurements(measurements) ;
|
|
|
|
return current_request ;
|
|
}
|