File size: 3,439 Bytes
9789ea9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
/*
Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
NVIDIA CORPORATION and its licensors retain all intellectual property
and proprietary rights in and to this software, related documentation
and any modifications thereto. Any use, reproduction, disclosure or
distribution of this software and related documentation without an express
license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#include "ConfigReader.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
bool ConfigReader::Load(const std::string & config_filename)
{
std::ifstream configFile;
configFile.open(config_filename, std::ios_base::in);
int line_number = 0;
if (configFile.is_open()) {
// read stream line by line
for (std::string line; std::getline(configFile, line);) {
line_number++;
// Strip whitespace
std::size_t start = line.find_first_not_of(" \n\r\t");
if (start == std::string::npos) {
continue;
}
line = line.substr(start);
// Ignore comments
if (line[0] == '#') {
continue;
}
// Get the name
std::size_t end = line.find_first_of(" \t");
if (end == std::string::npos) {
std::cerr << "Invalid config file at line " << line_number << std::endl;
return false;
}
std::string name = line.substr(start, end - start);
line = line.substr(end);
// Strip whitespace between name value pair
start = line.find_first_not_of(" \t");
if (start == std::string::npos) {
std::cerr << "Invalid config file at line " << line_number << std::endl;
return false;
}
std::string value = line.substr(start);
#ifdef __linux__
// Remove trailing \r s
if (value.size() && value[value.size() -1] == '\r')
{
value.pop_back();
}
#endif
config_dict_.insert({ name, value });
}
configFile.close();
}
else {
std::cerr << "Unable to open file: " << config_filename << std::endl;
return false;
}
loaded_ = true;
return true;
}
bool ConfigReader::IsConfigValueAvailable(const std::string& name) const {
if (!loaded_) {
std::cout << "Not loaded" << std::endl;
return false;
}
return config_dict_.find(name) != config_dict_.end();
}
bool ConfigReader::GetConfigValue(const std::string& name, std::string* value) const {
if (!loaded_) {
std::cout << "Not loaded" << std::endl;
return false;
}
auto iter = config_dict_.find(name);
if (iter == config_dict_.end()) {
std::cerr << "Unable to find \"" << name << " effect\" configuration" << std::endl;
return false;
}
*value = iter->second;
return true;
}
std::string ConfigReader::GetConfigValue(const std::string & name) const
{
std::string value;
if (GetConfigValue(name, &value) == false) {
std::cerr << "Config value not found" << std::endl;
return value;
}
return value;
}
std::vector<std::string> ConfigReader::GetConfigValueList(const std::string & name) const
{
std::string value_list;
if (GetConfigValue(name, &value_list) == false) {
std::cerr << "Config value list not found" << std::endl;
return std::vector<std::string>();
}
std::vector<std::string> list;
// Split string around spaces.
std::istringstream ss(value_list);
do {
std::string value;
ss >> value;
if (!value.empty())
list.push_back(value);
} while (ss);
return list;
}
|