2011-10-01 18:45:20 +00:00
|
|
|
/*!
|
|
|
|
* \file string_converter.cc
|
2011-12-28 21:36:45 +00:00
|
|
|
* \brief Implementation of a class that interprets the contents of a string
|
|
|
|
* and converts it into different types.
|
2011-10-01 18:45:20 +00:00
|
|
|
* \author Carlos Aviles, 2010. carlos.avilesr(at)googlemail.com
|
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*
|
2018-05-13 20:49:11 +00:00
|
|
|
* Copyright (C) 2010-2018 (see AUTHORS file for a list of contributors)
|
2011-10-01 18:45:20 +00:00
|
|
|
*
|
|
|
|
* GNSS-SDR is a software defined Global Navigation
|
|
|
|
* Satellite Systems receiver
|
|
|
|
*
|
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
|
|
|
* GNSS-SDR is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2014-09-14 17:08:32 +00:00
|
|
|
* (at your option) any later version.
|
2011-10-01 18:45:20 +00:00
|
|
|
*
|
|
|
|
* GNSS-SDR is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2018-05-13 20:49:11 +00:00
|
|
|
* along with GNSS-SDR. If not, see <https://www.gnu.org/licenses/>.
|
2011-10-01 18:45:20 +00:00
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-12-28 21:36:45 +00:00
|
|
|
#include "string_converter.h"
|
2011-10-01 18:45:20 +00:00
|
|
|
#include <sstream>
|
2018-02-26 02:15:53 +00:00
|
|
|
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
StringConverter::StringConverter() {}
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
StringConverter::~StringConverter() {}
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2014-09-14 17:08:32 +00:00
|
|
|
bool StringConverter::convert(const std::string& value, bool default_value)
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (value.compare("true") == 0)
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return true;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
2018-03-03 01:03:39 +00:00
|
|
|
else if (value.compare("false") == 0)
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return false;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return default_value;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-14 17:08:32 +00:00
|
|
|
long StringConverter::convert(const std::string& value, long default_value)
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
std::stringstream stream(value);
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2011-12-28 21:36:45 +00:00
|
|
|
long result;
|
|
|
|
stream >> result;
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
if (stream.fail())
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return default_value;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return result;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
2012-01-25 03:16:08 +00:00
|
|
|
|
2014-09-14 17:08:32 +00:00
|
|
|
int StringConverter::convert(const std::string& value, int default_value)
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
std::stringstream stream(value);
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2011-12-28 21:36:45 +00:00
|
|
|
int result;
|
|
|
|
stream >> result;
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
if (stream.fail())
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return default_value;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return result;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-14 17:08:32 +00:00
|
|
|
unsigned int StringConverter::convert(const std::string& value, unsigned int default_value)
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
std::stringstream stream(value);
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2011-12-28 21:36:45 +00:00
|
|
|
unsigned int result;
|
|
|
|
stream >> result;
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
if (stream.fail())
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return default_value;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return result;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
2012-01-25 03:16:08 +00:00
|
|
|
|
2016-05-07 10:22:40 +00:00
|
|
|
unsigned short StringConverter::convert(const std::string& value, unsigned short default_value)
|
|
|
|
{
|
|
|
|
std::stringstream stream(value);
|
|
|
|
|
|
|
|
unsigned short result;
|
|
|
|
stream >> result;
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
if (stream.fail())
|
2016-05-07 10:22:40 +00:00
|
|
|
{
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2012-01-25 03:16:08 +00:00
|
|
|
|
|
|
|
|
2014-09-14 17:08:32 +00:00
|
|
|
float StringConverter::convert(const std::string& value, float default_value)
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
std::stringstream stream(value);
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2011-12-28 21:36:45 +00:00
|
|
|
float result;
|
|
|
|
stream >> result;
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
if (stream.fail())
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return default_value;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return result;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
2012-01-25 03:16:08 +00:00
|
|
|
|
2014-09-14 17:08:32 +00:00
|
|
|
double StringConverter::convert(const std::string& value, double default_value)
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
std::stringstream stream(value);
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2011-12-28 21:36:45 +00:00
|
|
|
double result;
|
|
|
|
stream >> result;
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
if (stream.fail())
|
2012-01-25 03:16:08 +00:00
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return default_value;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-28 21:36:45 +00:00
|
|
|
return result;
|
2012-01-25 03:16:08 +00:00
|
|
|
}
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|