Scheduler
configuration.cpp
Go to the documentation of this file.
1 
10 #include "configuration.h"
11 
12 #include <cstring>
13 #include <fstream>
14 #include <iostream>
15 #include <stdexcept>
16 #include <string>
17 
18 using namespace Utils;
19 
20 Configuration::Configuration(std::string file) : filename(file)
21 {
22  std::cout << "Creating a configuration from file "<< filename<<"\n";
23 }
24 
26 {
27 }
28 
29 
30 
31 std::string Configuration::getStringValue(std::string section, std::string key)
32 {
33  std::string result = "";
34  stream.open(filename);
35  if (!stream.is_open())
36  throw std::runtime_error("cannot open file");
37  std::string line;
38  while(std::getline(stream, line))
39  {
40  if (isNewSection(line) && isMatchingSection(line, section))
41  break;
42  }
43  key = key + " = ";
44  while(std::getline(stream, line))
45  {
46  if (isNewSection(line))
47  break;
48  if (!line.compare(0, key.length(), key))
49  {
50  result = line.substr(key.length());
51  }
52  }
53  stream.close();
54  return result;
55 }
56 
57 
58 std::vector<std::string> Configuration::getStringList(std::string section, std::string key)
59 {
60  std::vector<std::string> result;
61  std::string list = getStringValue(section, key);
62  char *charList = new char[list.size()]; //I'm sure there is an easier way to do all that...
63  //TODO what about using Utils::StringUtils::split()
64  charList = strcpy(charList, list.c_str());
65  char *token;
66  token = strtok(charList, " ");
67  while(token != NULL)
68  {
69  result.push_back(token);
70  token = strtok(NULL, " ");
71  }
72  delete[] charList;
73  return result;
74 }
75 
76 
77 
79 {
80  return line[0] == '[';
81 }
82 
83 bool Configuration::isMatchingSection(std::string line, std::string section)
84 {
85  section = "[" + section + "]";
86  int result = line.compare(section);
87  return result == 0;
88 }
89 
90 
91 
92 
93 
94 
95 double Configuration::getDoubleValue(std::string section, std::string key)
96 {
97  std::string str = getStringValue(section, key);
98  return std::stod(str, nullptr);
99 }
100 
101 
102 
103 
104 int Configuration::getIntValue(std::string section, std::string key)
105 {
106  std::string str = getStringValue(section, key);
107  int result = std::stoi(str, nullptr);
108  return result;
109 }
110 
111 
112 unsigned long long int Configuration::getUnsignedLongLongIntValue(std::string section, std::string key)
113 {
114  std::string str = getStringValue(section, key);
115  unsigned long long int result = 0;
116  result = stoull(str);
117  return result;
118 }
119 
120 
121 bool Configuration::getBoolValue(std::string section, std::string key, bool defaultValue)
122 {
123  std::string str = getStringValue(section, key);
124  if (!str.compare("yes") || !str.compare("1") || !str.compare("true" ))
125  return true;
126  if (!str.compare("no" ) || !str.compare("0") || !str.compare("false"))
127  return false;
128  return defaultValue;
129 }
130 
132 {
133  std::string prefix = filename;
134  prefix = stripDirectories(prefix);
135  return prefix;
136 }
137 
138 
139 std::string Configuration::stripDirectories(std::string str)
140 {
141  size_t pos;
142  while((pos = str.find('/')) != std::string::npos)
143  {
144  str = str.substr(pos + 1, std::string::npos);
145  }
146  return str;
147 }
148 
149 
150 
151 
152 
line
Definition: bigtemp.py:6
std::ifstream stream
Definition: configuration.h:39
virtual unsigned long long int getUnsignedLongLongIntValue(std::string section, std::string key)
virtual int getIntValue(std::string section, std::string key)
std::string filename
Definition: configuration.h:36
std::string getFilePrefix()
returns the name of the configuration file stripped of any directory
string filename
Definition: aging.py:5
virtual bool getBoolValue(std::string section, std::string key, bool defaultValue)
virtual double getDoubleValue(std::string section, std::string key)
std::string stripDirectories(std::string)
virtual std::vector< std::string > getStringList(std::string section, std::string key)
static bool isMatchingSection(std::string line, std::string section)
Definition: context.h:16
virtual std::string getStringValue(std::string section, std::string key)
Configuration(std::string file)
static bool isNewSection(std::string line)