2010-01-07 16:34:31 +01:00
|
|
|
#include "inputcsv.hh"
|
2010-01-11 19:40:45 +01:00
|
|
|
#include "posutil.hh"
|
2010-01-12 00:31:22 +01:00
|
|
|
#include "stock.hh"
|
2010-01-11 19:40:45 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2010-01-07 16:34:31 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
2010-02-01 17:36:45 +01:00
|
|
|
#include <boost/tr1/unordered_map.hpp>
|
2010-01-07 16:34:31 +01:00
|
|
|
|
|
|
|
using namespace std ;
|
2010-01-11 19:40:45 +01:00
|
|
|
using std::tr1::unordered_map ;
|
|
|
|
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
2010-01-07 16:34:31 +01:00
|
|
|
using namespace boost ;
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/* *** Constructors *** */
|
2010-01-07 16:34:31 +01:00
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/**
|
|
|
|
* Prepares the InputCSV to read a CSV file.
|
|
|
|
* @param filename The name of the file to open.
|
2010-01-18 14:19:57 +01:00
|
|
|
* @throw runtime_error if the file cannot be opened.
|
2010-01-16 12:23:07 +01:00
|
|
|
*/
|
2010-01-07 16:34:31 +01:00
|
|
|
InputCSV::InputCSV(const string &filename)
|
|
|
|
{
|
|
|
|
input_file_name = filename ;
|
|
|
|
|
|
|
|
input_file.open(input_file_name.c_str()) ;
|
|
|
|
if (! input_file)
|
2010-01-18 14:19:57 +01:00
|
|
|
throw runtime_error("Error opening input file « " +
|
|
|
|
input_file_name + " »!") ;
|
2010-02-02 19:37:37 +01:00
|
|
|
|
|
|
|
read_next_line() ;
|
2010-01-07 16:34:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/* *** Operations *** */
|
2010-01-07 16:34:31 +01:00
|
|
|
|
|
|
|
|
2010-02-02 19:37:37 +01:00
|
|
|
/**
|
|
|
|
* Tests if #input_file is opened and there is something more to
|
|
|
|
* read. If the end of file is reached, the stream is closed.
|
|
|
|
* @return \em true if the end of file is reached or if the file is
|
|
|
|
* already closed.
|
|
|
|
* @return \em false if there is something more to read.
|
|
|
|
*/
|
|
|
|
bool InputCSV::eof_close()
|
|
|
|
{
|
|
|
|
if (! input_file)
|
|
|
|
return false ;
|
|
|
|
|
|
|
|
if (input_file.eof())
|
|
|
|
{
|
|
|
|
input_file.close() ;
|
|
|
|
return true ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
{
|
|
|
|
if (eof_close())
|
|
|
|
{
|
|
|
|
current_line.clear() ;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
string::size_type first_non_blank = 0 ;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
++current_line_nb ;
|
|
|
|
getline(input_file, current_line) ;
|
|
|
|
}
|
|
|
|
while (! input_file.eof() &&
|
|
|
|
(first_non_blank = current_line.find_first_not_of(" \t"))
|
|
|
|
== string::npos) ;
|
|
|
|
|
|
|
|
if (eof_close())
|
|
|
|
current_line.clear() ;
|
|
|
|
else
|
|
|
|
current_line.erase(0, first_non_blank) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/**
|
2010-01-18 14:19:57 +01:00
|
|
|
* 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).
|
2010-01-16 12:23:07 +01:00
|
|
|
*/
|
2010-01-11 19:40:45 +01:00
|
|
|
const Request& InputCSV::get_next_request()
|
2010-01-07 16:34:31 +01:00
|
|
|
{
|
2010-02-02 19:37:37 +01:00
|
|
|
if (eof_close())
|
2010-01-18 14:19:57 +01:00
|
|
|
{
|
2010-02-02 19:37:37 +01:00
|
|
|
// End of file or error: blank current request
|
2010-01-11 19:40:45 +01:00
|
|
|
current_request.clear() ;
|
|
|
|
return current_request ;
|
2010-01-07 16:34:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split read string into fields (semicolon-separated)
|
|
|
|
tokenizer<escaped_list_separator<char> > tok(
|
2010-02-02 19:37:37 +01:00
|
|
|
current_line, escaped_list_separator<char>('\\', ';', '\"')) ;
|
2010-01-07 16:34:31 +01:00
|
|
|
|
2010-02-02 20:02:41 +01:00
|
|
|
tokenizer<escaped_list_separator<char> >::const_iterator ti(tok.begin()) ;
|
2010-01-07 16:34:31 +01:00
|
|
|
|
2010-01-11 19:40:45 +01:00
|
|
|
// Read Mobile MAC field
|
|
|
|
if (ti == tok.end())
|
2010-01-07 16:34:31 +01:00
|
|
|
{
|
2010-01-11 19:40:45 +01:00
|
|
|
// Wrong number of fields: blank current request
|
|
|
|
current_request.clear() ;
|
|
|
|
return current_request ;
|
|
|
|
}
|
|
|
|
|
2010-01-12 00:31:22 +01:00
|
|
|
// If the mobile did not exist, we create it
|
2010-01-18 14:19:57 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Stock::get_mobile(*ti) ;
|
|
|
|
}
|
|
|
|
catch (out_of_range e)
|
|
|
|
{
|
|
|
|
Stock::getw_mobile(*ti).set_mac_addr(*ti) ;
|
|
|
|
}
|
2010-01-12 00:31:22 +01:00
|
|
|
current_request.set_mobile(&Stock::get_mobile(*ti)) ;
|
2010-01-11 19:40:45 +01:00
|
|
|
|
|
|
|
// Read Timestamp field
|
|
|
|
if (++ti == tok.end())
|
|
|
|
{
|
|
|
|
// Wrong number of fields: blank current request
|
|
|
|
current_request.clear() ;
|
|
|
|
return current_request ;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
2010-02-03 17:03:31 +01:00
|
|
|
current_request.set_timestamp(Timestamp
|
2010-01-11 19:40:45 +01:00
|
|
|
(lexical_cast<uint64_t>(*ti))) ;
|
|
|
|
}
|
|
|
|
catch (bad_lexical_cast &e)
|
|
|
|
{
|
|
|
|
cerr
|
|
|
|
<< "InputCSV::get_next_request(): Bad value « "
|
|
|
|
<< *ti << " » at line "
|
2010-02-02 19:37:37 +01:00
|
|
|
<< current_line_nb << ", field « Timestamp », of input file « "
|
2010-01-11 19:40:45 +01:00
|
|
|
<< 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 "
|
2010-02-02 19:37:37 +01:00
|
|
|
<< current_line_nb << ", position field #" << i
|
2010-01-11 19:40:45 +01:00
|
|
|
<< ", 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 ;
|
2010-01-07 16:34:31 +01:00
|
|
|
try
|
|
|
|
{
|
2010-01-11 19:40:45 +01:00
|
|
|
ss = lexical_cast<int>(*ti) ;
|
2010-01-07 16:34:31 +01:00
|
|
|
}
|
|
|
|
catch (bad_lexical_cast &e)
|
|
|
|
{
|
|
|
|
cerr
|
2010-01-11 19:40:45 +01:00
|
|
|
<< "InputCSV::get_next_request(): Bad value « "
|
|
|
|
<< *ti << " » at line "
|
2010-02-02 19:37:37 +01:00
|
|
|
<< current_line_nb
|
2010-01-07 16:34:31 +01:00
|
|
|
<< " of input file « " << input_file_name << " »!"
|
|
|
|
<< endl ;
|
2010-01-11 19:40:45 +01:00
|
|
|
current_request.clear() ; // Blank current request
|
|
|
|
return current_request ;
|
2010-01-07 16:34:31 +01:00
|
|
|
}
|
2010-01-11 19:40:45 +01:00
|
|
|
|
2010-01-12 00:31:22 +01:00
|
|
|
// If the AP did not exist, we create it
|
2010-01-18 14:19:57 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Stock::get_ap(mac_ap) ;
|
|
|
|
}
|
|
|
|
catch (out_of_range e)
|
|
|
|
{
|
|
|
|
Stock::getw_ap(mac_ap).set_mac_addr(mac_ap) ;
|
|
|
|
}
|
2010-01-12 00:31:22 +01:00
|
|
|
measurements[mac_ap].set_ap(&Stock::get_ap(mac_ap)) ;
|
|
|
|
|
2010-01-11 19:40:45 +01:00
|
|
|
// Adding value
|
|
|
|
measurements[mac_ap].add_ss(ss) ;
|
2010-01-07 16:34:31 +01:00
|
|
|
}
|
|
|
|
|
2010-01-11 19:40:45 +01:00
|
|
|
current_request.set_measurements(measurements) ;
|
2010-01-07 16:34:31 +01:00
|
|
|
|
2010-02-02 19:37:37 +01:00
|
|
|
read_next_line() ;
|
|
|
|
|
2010-01-11 19:40:45 +01:00
|
|
|
return current_request ;
|
2010-01-07 16:34:31 +01:00
|
|
|
}
|