2011-10-01 18:45:20 +00:00
|
|
|
/*!
|
|
|
|
* \file file_configuration.cc
|
2011-12-28 21:36:45 +00:00
|
|
|
* \brief Implementation of the interface ConfigurationInterface that reads the
|
|
|
|
* configuration from a file.
|
2011-10-01 18:45:20 +00:00
|
|
|
* \author Carlos Aviles, 2010. carlos.avilesr(at)googlemail.com
|
|
|
|
*
|
|
|
|
* This implementation has a text file as the source for the values of the parameters.
|
|
|
|
* The file is in the INI format, containing sections and pairs of names and values.
|
2020-02-05 20:24:46 +00:00
|
|
|
* For more information about the INI format, see https://en.wikipedia.org/wiki/INI_file
|
2011-10-01 18:45:20 +00:00
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2011-10-01 18:45:20 +00:00
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
|
2011-10-01 18:45:20 +00:00
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
#include "file_configuration.h"
|
2020-06-23 07:47:58 +00:00
|
|
|
#include "gnss_sdr_make_unique.h"
|
2018-02-26 02:15:53 +00:00
|
|
|
#include <glog/logging.h>
|
2021-03-13 20:35:55 +00:00
|
|
|
#include <iostream>
|
2019-08-17 11:56:54 +00:00
|
|
|
#include <utility>
|
2018-02-26 02:15:53 +00:00
|
|
|
|
2011-10-01 18:45:20 +00:00
|
|
|
|
|
|
|
FileConfiguration::FileConfiguration(std::string filename)
|
2021-12-14 16:03:29 +00:00
|
|
|
: filename_(std::move(filename))
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2012-11-01 18:26:58 +00:00
|
|
|
|
2011-10-01 18:45:20 +00:00
|
|
|
FileConfiguration::FileConfiguration()
|
2021-12-14 16:03:29 +00:00
|
|
|
: filename_("./default_config_file.txt")
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2012-11-01 18:26:58 +00:00
|
|
|
|
2020-06-23 07:47:58 +00:00
|
|
|
void FileConfiguration::init()
|
|
|
|
{
|
|
|
|
converter_ = std::make_unique<StringConverter>();
|
|
|
|
overrided_ = std::make_unique<InMemoryConfiguration>();
|
|
|
|
ini_reader_ = std::make_unique<INIReader>(filename_);
|
|
|
|
error_ = ini_reader_->ParseError();
|
|
|
|
if (error_ == 0)
|
|
|
|
{
|
|
|
|
DLOG(INFO) << "Configuration file " << filename_ << " opened with no errors";
|
|
|
|
}
|
|
|
|
else if (error_ > 0)
|
|
|
|
{
|
|
|
|
LOG(WARNING) << "Configuration file " << filename_ << " contains errors in line " << error_;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-13 20:35:55 +00:00
|
|
|
std::cerr << "Unable to open configuration file " << filename_ << '\n';
|
2020-06-23 07:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-24 23:49:36 +00:00
|
|
|
bool FileConfiguration::has_section() const
|
|
|
|
{
|
|
|
|
return ini_reader_->HasSection("GNSS-SDR");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 09:05:31 +00:00
|
|
|
std::string FileConfiguration::property(std::string property_name, std::string default_value) const
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (overrided_->is_present(property_name))
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
|
|
|
return overrided_->property(property_name, default_value);
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
return ini_reader_->Get("GNSS-SDR", property_name, default_value);
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 18:26:58 +00:00
|
|
|
|
2020-06-23 09:05:31 +00:00
|
|
|
bool FileConfiguration::property(std::string property_name, bool default_value) const
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (overrided_->is_present(property_name))
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
|
|
|
return overrided_->property(property_name, default_value);
|
|
|
|
}
|
2020-07-19 23:20:15 +00:00
|
|
|
const std::string empty;
|
2018-12-03 21:08:19 +00:00
|
|
|
return converter_->convert(property(property_name, empty), default_value);
|
2018-12-02 04:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 09:05:31 +00:00
|
|
|
int64_t FileConfiguration::property(std::string property_name, int64_t default_value) const
|
2018-12-02 04:29:11 +00:00
|
|
|
{
|
|
|
|
if (overrided_->is_present(property_name))
|
|
|
|
{
|
|
|
|
return overrided_->property(property_name, default_value);
|
|
|
|
}
|
2020-07-19 23:20:15 +00:00
|
|
|
const std::string empty;
|
2018-12-03 21:08:19 +00:00
|
|
|
return converter_->convert(property(property_name, empty), default_value);
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 18:26:58 +00:00
|
|
|
|
2020-06-23 09:05:31 +00:00
|
|
|
uint64_t FileConfiguration::property(std::string property_name, uint64_t default_value) const
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (overrided_->is_present(property_name))
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
|
|
|
return overrided_->property(property_name, default_value);
|
|
|
|
}
|
2020-07-19 23:20:15 +00:00
|
|
|
const std::string empty;
|
2018-12-03 21:08:19 +00:00
|
|
|
return converter_->convert(property(property_name, empty), default_value);
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 09:05:31 +00:00
|
|
|
int FileConfiguration::property(std::string property_name, int default_value) const
|
2012-11-01 18:26:58 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (overrided_->is_present(property_name))
|
2012-11-01 18:26:58 +00:00
|
|
|
{
|
2011-10-01 18:45:20 +00:00
|
|
|
return overrided_->property(property_name, default_value);
|
2012-11-01 18:26:58 +00:00
|
|
|
}
|
2020-07-19 23:20:15 +00:00
|
|
|
const std::string empty;
|
2018-12-03 21:08:19 +00:00
|
|
|
return converter_->convert(property(property_name, empty), default_value);
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 18:26:58 +00:00
|
|
|
|
2020-06-23 09:05:31 +00:00
|
|
|
unsigned int FileConfiguration::property(std::string property_name, unsigned int default_value) const
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (overrided_->is_present(property_name))
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
|
|
|
return overrided_->property(property_name, default_value);
|
|
|
|
}
|
2020-07-19 23:20:15 +00:00
|
|
|
const std::string empty;
|
2018-12-03 21:08:19 +00:00
|
|
|
return converter_->convert(property(property_name, empty), default_value);
|
2018-12-02 04:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 09:05:31 +00:00
|
|
|
uint16_t FileConfiguration::property(std::string property_name, uint16_t default_value) const
|
2018-12-02 04:29:11 +00:00
|
|
|
{
|
|
|
|
if (overrided_->is_present(property_name))
|
|
|
|
{
|
|
|
|
return overrided_->property(property_name, default_value);
|
|
|
|
}
|
2020-07-19 23:20:15 +00:00
|
|
|
const std::string empty;
|
2018-12-03 21:08:19 +00:00
|
|
|
return converter_->convert(property(property_name, empty), default_value);
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 18:26:58 +00:00
|
|
|
|
2020-06-23 09:05:31 +00:00
|
|
|
int16_t FileConfiguration::property(std::string property_name, int16_t default_value) const
|
2016-05-07 10:22:40 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (overrided_->is_present(property_name))
|
2016-05-07 10:22:40 +00:00
|
|
|
{
|
|
|
|
return overrided_->property(property_name, default_value);
|
|
|
|
}
|
2020-07-19 23:20:15 +00:00
|
|
|
const std::string empty;
|
2018-12-03 21:08:19 +00:00
|
|
|
return converter_->convert(property(property_name, empty), default_value);
|
2016-05-07 10:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 09:05:31 +00:00
|
|
|
float FileConfiguration::property(std::string property_name, float default_value) const
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (overrided_->is_present(property_name))
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
|
|
|
return overrided_->property(property_name, default_value);
|
|
|
|
}
|
2020-07-19 23:20:15 +00:00
|
|
|
const std::string empty;
|
2018-12-03 21:08:19 +00:00
|
|
|
return converter_->convert(property(property_name, empty), default_value);
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 18:26:58 +00:00
|
|
|
|
2020-06-23 09:05:31 +00:00
|
|
|
double FileConfiguration::property(std::string property_name, double default_value) const
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (overrided_->is_present(property_name))
|
2011-10-01 18:45:20 +00:00
|
|
|
{
|
|
|
|
return overrided_->property(property_name, default_value);
|
|
|
|
}
|
2020-07-19 23:20:15 +00:00
|
|
|
const std::string empty;
|
2018-12-03 21:08:19 +00:00
|
|
|
return converter_->convert(property(property_name, empty), default_value);
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 18:26:58 +00:00
|
|
|
|
2011-10-01 18:45:20 +00:00
|
|
|
void FileConfiguration::set_property(std::string property_name, std::string value)
|
|
|
|
{
|
|
|
|
overrided_->set_property(property_name, value);
|
|
|
|
}
|
2020-06-23 09:05:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool FileConfiguration::is_present(const std::string& property_name) const
|
|
|
|
{
|
|
|
|
return (overrided_->is_present(property_name));
|
|
|
|
}
|