owlps/owlps-positioning/src/csvfilereader.cc

136 lines
2.6 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS).
* OwlPS is a project of the University of Franche-Comté
* (Université de Franche-Comté), France.
*/
#include "csvfilereader.hh"
#include "point3d.hh"
#include <iostream>
using namespace std ;
using namespace boost ;
/* *** Constructors *** */
CSVFileReader::~CSVFileReader()
{
delete current_token ;
}
/* *** Operations *** */
/**
* Note that this function is not called by the constructor, so you
* must manually call it prior to use read_field().
* @return \em false in case of error (EOF, etc.), \em true else.
*/
bool CSVFileReader::next_line()
{
delete current_token ;
current_token = NULL ;
if (! read_nonblank_line(current_line))
return false ;
// Split read string into fields (semicolon-separated)
current_token = new tokenizer<escaped_list_separator<char> >(
current_line, escaped_list_separator<char>('\\', separator, '\"')) ;
token_iterator = current_token->begin() ;
current_field_nb = 0 ;
return true ;
}
bool CSVFileReader::read_timestamp(Timestamp &t)
{
string timestamp_str ;
uint_fast32_t time_s, time_ns ;
if (! read_field(timestamp_str))
return false ;
tokenizer<escaped_list_separator<char> > tok(
timestamp_str, escaped_list_separator<char>('\\', '.', '\"')) ;
tokenizer<escaped_list_separator<char> >::const_iterator tok_iter =
tok.begin() ;
if (tok_iter == tok.end())
return false ;
try
{
time_s = lexical_cast<uint_fast32_t>(*tok_iter) ;
}
catch (bad_lexical_cast &e)
{
print_error_cast() ;
return false ;
}
++tok_iter ;
if (tok_iter == tok.end())
return false ;
try
{
time_ns = lexical_cast<uint_fast32_t>(*tok_iter) ;
}
catch (bad_lexical_cast &e)
{
print_error_cast() ;
return false ;
}
t = Timestamp(time_s, time_ns) ;
return true ;
}
bool CSVFileReader::read_point3d(Point3D &p)
{
float coord[3] ;
for (unsigned int i = 0 ; i < 3 ; ++i)
{
if (token_iterator == current_token->end())
return false ;
++current_field_nb ;
try
{
coord[i] = lexical_cast<float>(*token_iterator) ;
}
catch (bad_lexical_cast &e)
{
print_error_cast() ;
return false ;
}
++token_iterator ;
}
p.set_coordinates(coord) ;
return true ;
}
void CSVFileReader::print_error_cast() const
{
cerr
<< "Bad value \""
<< *token_iterator << "\" at line "
<< current_line_nb << ", field #"
<< current_field_nb << ", of input file \""
<< file_name << "\"!" << endl ;
}