DisCostiC v2.0.0
Distributed Cost in Cluster: a cross-architecture simulator for massively parallel MPI programs
Loading...
Searching...
No Matches
ConfigParser.hpp
Go to the documentation of this file.
1
7
8#ifndef CONFIGPARSER_HPP
9#define CONFIGPARSER_HPP
10
11#pragma once
12
13#include <sstream>
14#include <fstream>
15#include <map>
16#include <typeinfo>
17#include <mpi.h>
18#include "macro.hpp"
19
20
24namespace UserInterface {
29 {
30 public:
31 template <typename T>
32 static T stringTOScalarT(std::string const &val)
33 {
34 std::istringstream is(val);
35 T Val;
36 if (!(is >> Val)) throw std::runtime_error("CFG: invalid " + (std::string)typeid(T).name() + "\n");
37 return Val;
38 }
39
40 template <typename T>
41 static std::vector<T> stringTOArray(std::string const &val)
42 {
43 std::istringstream iss(val);
44 std::vector<T> array;
45 char c;
46 T Val;
47 if (iss >> c && c == '[')
48 while (iss >> Val)
49 array.push_back(Val);
50 if (iss && iss >> c && c == ']' && !(iss >> c))
51 return array;
52 }
53 };
54
59 {
60 private:
61 std::map<std::string, std::string> data;
62 std::string fileName;
63
67 void removeComment(std::string &line) const
68 {
69 if (line.find('#') != line.npos) line.erase(line.find('#'));
70 }
71
75 bool whitespace(const std::string &line) const
76 {
77 return (line.find_first_not_of(' ') == line.npos);
78 }
79
83 void extractKey(std::string &key, size_t const &sepPos, const std::string &line) const
84 {
85 key = line.substr(0, sepPos);
86 if (key.find('\t') != line.npos || key.find(' ') != line.npos)
87 key.erase(key.find_first_of("\t "));
88 }
92 void extractValue(std::string &value, size_t const &sepPos, const std::string &line) const
93 {
94 value = line.substr(sepPos + 1); // creates a substring starting from positon of '=' + 1, to the end of the line
95 value.erase(0, value.find_first_not_of("\t ")); // removes the leading whitespace
96 value.erase(value.find_last_not_of("\t ") + 1); // removes everything starting with the position of the last non-space character
97 }
98
102 void parseLine(const std::string &line, size_t const lineNum)
103 {
104 std::string temp = line;
105 temp.erase(0, temp.find_first_not_of("\t "));
106 size_t sepPos = temp.find('=');
107 std::string key, value;
108 extractKey(key, sepPos, temp);
109 extractValue(value, sepPos, temp);
110
111 if (!getKey(key)) // it checks if a key given as parameter already exists in the std::map (data)
112 data.insert(std::pair<std::string, std::string>(key, value));
113 else
114 throw std::runtime_error("CFG: only unique key names are allowed!\n");
115 }
116
117 public:
121 ConfigParser(const std::string &fileName)
122 {
123
124 std::ifstream file;
125 fileopen(file,fileName);
126
127 std::string line;
128 size_t lineNum = 0;
129 while (std::getline(file, line))
130 {
131 lineNum++;
132 std::string temp = line;
133 if (temp.empty()) continue;
134
135 removeComment(temp);
136
137 if (whitespace(temp)) continue;
138
139 parseLine(temp, lineNum);
140 }
141
142 fileclose(file,fileName);
143
144 }
148 bool getKey(const std::string &key) const
149 {
150 return data.find(key) != data.end();
151 }
155 template <typename scalarT>
156 scalarT getValue(const std::string &key, scalarT const &defaultValue = scalarT()) const
157 {
158 if (!getKey(key)) return defaultValue;
159 return Conversion::stringTOScalarT<scalarT>(data.find(key)->second);
160 }
161
162 };
163}
164#endif
a wrapper class which contains functions for parsing the configuration file end class configParser
void extractValue(std::string &value, size_t const &sepPos, const std::string &line) const
a function that extracts the value from the pair of key = value
bool whitespace(const std::string &line) const
a function that returns false if a non-space character was found, true otherwise. The function is "co...
scalarT getValue(const std::string &key, scalarT const &defaultValue=scalarT()) const
a function that retrieves the value of a specific key
std::map< std::string, std::string > data
which will hold pairs of key-value
bool getKey(const std::string &key) const
a function for finding the key
ConfigParser(const std::string &fileName)
a class to set the name of the configuration file and extracts and parses the data
void extractKey(std::string &key, size_t const &sepPos, const std::string &line) const
a function that extracts the key from the pair of key = value
std::string fileName
As member variables, we will only have a std::string, which will hold the name of the configuration f...
void parseLine(const std::string &line, size_t const lineNum)
a function that parse the line by calling above mentioed functions
void removeComment(std::string &line) const
a function that removes everything from the semicolon (including it) to the end of the line.
a wrapper class which contain function for the conversion of std::string to primitive types (int,...
static std::vector< T > stringTOArray(std::string const &val)
static T stringTOScalarT(std::string const &val)
#define fileopen(myfile, filename)
open an open file
Definition macro.hpp:233
#define fileclose(myfile, filename)
close an open file
Definition macro.hpp:251
it parses the user-defined configuration file (.cfg)