188 lines
4.7 KiB
C++
188 lines
4.7 KiB
C++
/*
|
|
* This file is part of the Owl Positioning System (OwlPS).
|
|
* OwlPS is a project of the University of Franche-Comte
|
|
* (Université de Franche-Comté), France.
|
|
*
|
|
* Copyright © Université de Franche-Comté 2007-2012.
|
|
*
|
|
* Corresponding author: Matteo Cypriani <mcy@lm7.fr>
|
|
*
|
|
***********************************************************************
|
|
*
|
|
* This software is governed by the CeCILL license under French law and
|
|
* abiding by the rules of distribution of free software. You can use,
|
|
* modify and/or redistribute the software under the terms of the CeCILL
|
|
* license as circulated by CEA, CNRS and INRIA at the following URL:
|
|
* http://www.cecill.info
|
|
*
|
|
* As a counterpart to the access to the source code and rights to copy,
|
|
* modify and redistribute granted by the license, users are provided
|
|
* only with a limited warranty and the software's authors, the holder
|
|
* of the economic rights, and the successive licensors have only
|
|
* limited liability.
|
|
*
|
|
* In this respect, the user's attention is drawn to the risks
|
|
* associated with loading, using, modifying and/or developing or
|
|
* reproducing the software by the user in light of its specific status
|
|
* of free software, that may mean that it is complicated to manipulate,
|
|
* and that also therefore means that it is reserved for developers and
|
|
* experienced professionals having in-depth computer knowledge. Users
|
|
* are therefore encouraged to load and test the software's suitability
|
|
* as regards their requirements in conditions enabling the security of
|
|
* their systems and/or data to be ensured and, more generally, to use
|
|
* and operate it in the same conditions as regards security.
|
|
*
|
|
* The fact that you are presently reading this means that you have had
|
|
* knowledge of the CeCILL license and that you accept its terms.
|
|
*
|
|
***********************************************************************
|
|
*/
|
|
|
|
|
|
#include "csvstringreader.hh"
|
|
#include "point3d.hh"
|
|
|
|
#include <iostream>
|
|
|
|
using namespace std ;
|
|
using namespace boost ;
|
|
|
|
|
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
CSVStringReader::
|
|
CSVStringReader(const std::string &_str, const char _separator):
|
|
separator(_separator), current_token(NULL), current_field_nb(0)
|
|
{
|
|
set_str(_str) ;
|
|
}
|
|
|
|
CSVStringReader::~CSVStringReader()
|
|
{
|
|
delete current_token ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Accessors *** */
|
|
|
|
|
|
/**
|
|
* @returns \em false in case of error (EOF, etc.), \em true else.
|
|
*/
|
|
void CSVStringReader::set_str(const string &_str)
|
|
{
|
|
str = _str ;
|
|
|
|
delete current_token ;
|
|
current_token = NULL ;
|
|
|
|
// Split read string into fields (semicolon-separated)
|
|
current_token = new tokenizer<escaped_list_separator<char> >(
|
|
str, escaped_list_separator<char>('\\', separator, '\"')) ;
|
|
|
|
token_iterator = current_token->begin() ;
|
|
current_field_nb = 0 ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
bool CSVStringReader::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 ;
|
|
}
|
|
|
|
|
|
/**
|
|
* The first field (X) can start with an opening parenthesis, and the
|
|
* last one (Z) can finish with a closing parenthesis.
|
|
*/
|
|
bool CSVStringReader::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 ;
|
|
|
|
string field = *token_iterator ;
|
|
if (field.empty())
|
|
return false ;
|
|
|
|
/* Handle a leading '(' and a trailing ')' */
|
|
if (i == 0 && field.front() == '(')
|
|
field.erase(field.begin()) ;
|
|
if (i == 2 && field.back() == ')')
|
|
field.pop_back() ;
|
|
|
|
try
|
|
{
|
|
coord[i] = lexical_cast<float>(field) ;
|
|
}
|
|
catch (bad_lexical_cast &e)
|
|
{
|
|
print_error_cast() ;
|
|
return false ;
|
|
}
|
|
|
|
++token_iterator ;
|
|
}
|
|
|
|
p.set_coordinates(coord) ;
|
|
return true ;
|
|
}
|
|
|
|
|
|
void CSVStringReader::print_error_cast() const
|
|
{
|
|
cerr
|
|
<< "Bad value \"" << *token_iterator
|
|
<< "\" at field #" << current_field_nb
|
|
<< ", of CSV string \"" << str << "\"!" << endl ;
|
|
}
|