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
|
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2011-10-01 18:45:20 +00:00
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* Copyright (C) 2010-2020 (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.
|
|
|
|
*
|
2020-02-08 00:20:02 +00:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2011-10-01 18:45:20 +00:00
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
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
|
|
|
|
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-12-02 14:52:32 +00:00
|
|
|
if (value == "true")
|
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-12-03 21:08:19 +00:00
|
|
|
if (value == "false")
|
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
|
|
|
}
|
2018-12-08 17:49:31 +00:00
|
|
|
|
|
|
|
return default_value;
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-02 04:29:11 +00:00
|
|
|
int64_t StringConverter::convert(const std::string& value, int64_t 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
|
|
|
|
2018-12-02 04:29:11 +00:00
|
|
|
int64_t result;
|
2011-12-28 21:36:45 +00:00
|
|
|
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
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
|
|
|
|
return result;
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
2012-01-25 03:16:08 +00:00
|
|
|
|
2018-12-02 04:29:11 +00:00
|
|
|
uint64_t StringConverter::convert(const std::string& value, uint64_t default_value)
|
|
|
|
{
|
|
|
|
std::stringstream stream(value);
|
|
|
|
|
|
|
|
uint64_t result;
|
|
|
|
stream >> result;
|
|
|
|
|
|
|
|
if (stream.fail())
|
|
|
|
{
|
|
|
|
return default_value;
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
|
|
|
|
return result;
|
2018-12-02 04:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32_t StringConverter::convert(const std::string& value, int32_t 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
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
|
|
|
|
return result;
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-02 04:29:11 +00:00
|
|
|
uint32_t StringConverter::convert(const std::string& value, uint32_t default_value)
|
|
|
|
{
|
|
|
|
std::stringstream stream(value);
|
|
|
|
|
|
|
|
uint32_t result;
|
|
|
|
stream >> result;
|
|
|
|
|
|
|
|
if (stream.fail())
|
|
|
|
{
|
|
|
|
return default_value;
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
|
|
|
|
return result;
|
2018-12-02 04:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t StringConverter::convert(const std::string& value, uint16_t 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
|
|
|
|
2018-12-02 04:29:11 +00:00
|
|
|
uint16_t result;
|
2011-12-28 21:36:45 +00:00
|
|
|
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
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
|
|
|
|
return result;
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
2012-01-25 03:16:08 +00:00
|
|
|
|
2018-12-02 04:29:11 +00:00
|
|
|
int16_t StringConverter::convert(const std::string& value, int16_t default_value)
|
2016-05-07 10:22:40 +00:00
|
|
|
{
|
|
|
|
std::stringstream stream(value);
|
|
|
|
|
2018-12-02 04:29:11 +00:00
|
|
|
int16_t result;
|
2016-05-07 10:22:40 +00:00
|
|
|
stream >> result;
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
if (stream.fail())
|
2016-05-07 10:22:40 +00:00
|
|
|
{
|
|
|
|
return default_value;
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
|
|
|
|
return result;
|
2016-05-07 10:22:40 +00:00
|
|
|
}
|
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
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
|
|
|
|
return result;
|
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
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
|
|
|
|
return result;
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|