VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
ConfigurationFile.cpp
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#include "ConfigurationFile.h"
35
36#include <filesystem>
37#include <fstream>
38#include <map>
39#include <sstream>
40#include <stdexcept>
41#include <string>
42#include <vector>
43
45
46namespace vf::basics
47{
48
49template <>
50bool convert_to<bool>(const std::string& value)
51{
52 return value == "true";
53}
54
56{
57 data.clear();
58}
60void ConfigurationFile::load(const std::string& file)
61{
62 std::ifstream inFile(file.c_str());
63
64 if (!inFile.good()) {
65 const std::string error = "Cannot read configuration file " + file + "! Your current directory is " +
66 std::filesystem::current_path().string() + "\n" +
67 "For further information on how to run VirtualFluids please visit: "
68 "https://irmb.gitlab-pages.rz.tu-bs.de/VirtualFluids/build-and-run.html#run-the-examples";
69
70 throw std::invalid_argument(error);
71 }
72
73 while (inFile.good() && !inFile.eof()) {
74 std::string line;
75 getline(inFile, line);
76
77 // filter out comments
78 if (!line.empty()) {
79 size_t pos = line.find('#');
80
81 if (pos != std::string::npos) {
82 line = line.substr(0, pos);
83 }
84 }
85
86 // split line into key and value
87 if (!line.empty()) {
88 size_t pos = line.find('=');
89
90 if (pos != std::string::npos) {
91 std::string key = trim(line.substr(0, pos));
92 std::string value = trim(line.substr(pos + 1));
93
94 if (!key.empty() && !value.empty()) {
95 data[key] = value;
96 }
97 }
98 }
99 }
100}
101
103bool ConfigurationFile::contains(const std::string& key) const
104{
105 return data.find(key) != data.end();
106}
108std::string ConfigurationFile::getValue(const std::string& key) const
109{
110 std::map<std::string, std::string>::const_iterator iter = data.find(key);
111
112 if (iter != data.end()) {
113 return iter->second;
114 }
115 UB_THROW(UbException(UB_EXARGS, "The parameter \"" + key + "\" is missing!"));
116}
118std::string ConfigurationFile::trim(const std::string& str)
119{
120 size_t first = str.find_first_not_of(" \t\n\r");
121
122 if (first != std::string::npos) {
123 size_t last = str.find_last_not_of(" \t\n\r");
124
125 return str.substr(first, last - first + 1);
126 }
127 return "";
128}
130void ConfigurationFile::split(std::vector<std::string>& lst, const std::string& input, const std::string& separators,
131 bool remove_empty) const
132{
133 std::ostringstream word;
134 for (size_t n = 0; n < input.size(); ++n) {
135 if (std::string::npos == separators.find(input[n]))
136 word << input[n];
137 else {
138 if (!word.str().empty() || !remove_empty)
139 lst.push_back(word.str());
140 word.str("");
141 }
142 }
143 if (!word.str().empty() || !remove_empty)
144 lst.push_back(word.str());
145}
146
148
150{
151 // the config file's default path can be replaced by passing a command line argument
152
153 if (argc > 1) {
154 configPath = argv[1];
155 VF_LOG_TRACE("Using command line argument for config path: {}", configPath);
156 } else {
157 VF_LOG_TRACE("Using default config path: {}", configPath);
158 }
159
161 config.load(configPath);
162 return config;
163}
164} // namespace vf::basics
165
#define VF_LOG_TRACE(...)
Definition Logger.h:48
std::map< std::string, std::string > data
the container is public to test this class
bool contains(const std::string &key) const
check if value associated with given key exists
void load(const std::string &File)
load a configuration file
T getValue(const std::string &key) const
get value with key
std::shared_ptr< T > SPtr
#define UB_THROW(e)
Definition UbException.h:78
#define UB_EXARGS
Definition UbException.h:73
Simple configuration file.
ConfigurationFile loadConfig(int argc, char *argv[], std::string configPath)
bool convert_to< bool >(const std::string &value)