83 lines
1.7 KiB
C++
83 lines
1.7 KiB
C++
|
#include "inputcsv.hh"
|
||
|
|
||
|
#include <boost/tokenizer.hpp>
|
||
|
#include <boost/lexical_cast.hpp>
|
||
|
#include <iostream>
|
||
|
|
||
|
using namespace std ;
|
||
|
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(): Error opening input file « " << input_file_name
|
||
|
<< " »!" << endl ;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/*** Opérations ***/
|
||
|
|
||
|
|
||
|
const Measurement& InputCSV::get_next_measurement()
|
||
|
{
|
||
|
if (! input_file)
|
||
|
return current_measurement ;
|
||
|
|
||
|
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 measurement
|
||
|
current_measurement.clear() ;
|
||
|
return current_measurement ;
|
||
|
}
|
||
|
|
||
|
// Split read string into fields (semicolon-separated)
|
||
|
tokenizer<escaped_list_separator<char> > tok(
|
||
|
line, escaped_list_separator<char>('\\', ';', '\"')) ;
|
||
|
|
||
|
vector<int> ss_list ;
|
||
|
|
||
|
for (tokenizer<escaped_list_separator<char> >::iterator i(tok.begin()) ;
|
||
|
i != tok.end() ; ++i)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
ss_list.push_back(lexical_cast<int>(*i)) ;
|
||
|
}
|
||
|
catch (bad_lexical_cast &e)
|
||
|
{
|
||
|
cerr
|
||
|
<< "InputCSV::getNextMeasurement(): Bad value at line "
|
||
|
<< current_line
|
||
|
<< " of input file « " << input_file_name << " »!"
|
||
|
<< endl ;
|
||
|
current_measurement.clear() ; // Blank current measurement
|
||
|
return current_measurement ;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
current_measurement.set_ss_list(ss_list) ;
|
||
|
|
||
|
return current_measurement ;
|
||
|
}
|