Project
Loading...
Searching...
No Matches
ConfigurableParamReaders.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14#include <fairlogger/Logger.h>
15#include <filesystem>
16
17#include <boost/algorithm/string/predicate.hpp>
18#include <boost/property_tree/ini_parser.hpp>
19#include <boost/property_tree/json_parser.hpp>
20
21namespace o2::conf
22{
23// ------------------------------------------------------------------
24
25boost::property_tree::ptree ConfigurableParamReaders::readINI(std::string const& filepath)
26{
27 boost::property_tree::ptree pt;
28 try {
29 boost::property_tree::read_ini(filepath, pt);
30 } catch (const boost::property_tree::ptree_error& e) {
31 LOG(fatal) << "Failed to read INI config file " << filepath << " (" << e.what() << ")";
32 } catch (...) {
33 LOG(fatal) << "Unknown error when reading INI config file ";
34 }
35
36 return pt;
37}
38
39// ------------------------------------------------------------------
40
41boost::property_tree::ptree ConfigurableParamReaders::readJSON(std::string const& filepath)
42{
43 boost::property_tree::ptree pt;
44
45 try {
46 boost::property_tree::read_json(filepath, pt);
47 } catch (const boost::property_tree::ptree_error& e) {
48 LOG(fatal) << "Failed to read JSON config file " << filepath << " (" << e.what() << ")";
49 }
50
51 return pt;
52}
53
54boost::property_tree::ptree ConfigurableParamReaders::readConfigFile(std::string const& filepath)
55{
56 auto inpfilename = o2::utils::Str::concat_string(sInputDir, filepath);
57 if (!std::filesystem::exists(inpfilename)) {
58 LOG(fatal) << inpfilename << " : config file does not exist!";
59 }
60
61 boost::property_tree::ptree pt;
62
63 if (boost::iends_with(inpfilename, ".ini")) {
64 pt = ConfigurableParamReaders::readINI(inpfilename);
65 } else if (boost::iends_with(inpfilename, ".json")) {
66 pt = ConfigurableParamReaders::readJSON(inpfilename);
67 } else {
68 LOG(fatal) << "Configuration file must have either .ini or .json extension";
69 }
70
71 return pt;
72}
73
74std::string ConfigurableParamReaders::sInputDir = "";
75
76} // namespace o2::conf
static boost::property_tree::ptree readJSON(std::string const &filepath)
static boost::property_tree::ptree readConfigFile(std::string const &filepath)
static boost::property_tree::ptree readINI(std::string const &filepath)
static std::string concat_string(Ts const &... ts)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"