Army ant simulation
Config.cpp
1 /*
2  * Config.cpp
3  *
4  * Created on: 12 Nov 2018
5  * Author: https://wiki.calculquebec.ca/w/C%2B%2B_:_fichier_de_configuration
6  */
7 
8 #include <map>
9 #include <string>
10 #include <fstream>
11 #include <iostream>
12 
13 #include "Config.h"
14 using namespace std;
15 
16 
17 void config::Configuration::Clear()
18 {
19  data.clear();
20 }
21 
22 bool config::Configuration::Load(const string& file)
23 {
24  ifstream inFile(file.c_str());
25 
26  if (!inFile.good())
27  {
28  cout << "Cannot read configuration file " << file << endl;
29  return false;
30  }
31 
32  while (inFile.good() && ! inFile.eof())
33  {
34  string line;
35  getline(inFile, line);
36 
37  // filter out comments
38  if (!line.empty())
39  {
40  int pos = line.find('#');
41 
42  if (pos != string::npos)
43  {
44  line = line.substr(0, pos);
45  }
46  }
47 
48  // split line into key and value
49  if (!line.empty())
50  {
51  int pos = line.find('=');
52 
53  if (pos != string::npos)
54  {
55  string key = Trim(line.substr(0, pos));
56  string value = Trim(line.substr(pos + 1));
57 
58  if (!key.empty() && !value.empty())
59  {
60  data[key] = value;
61  }
62  }
63  }
64  }
65 
66  return true;
67 }
68 
69 bool config::Configuration::Contains(const string& key) const
70 {
71  return data.find(key) != data.end();
72 }
73 
74 bool config::Configuration::Get(const string& key, string& value) const
75 {
76  map<string,string>::const_iterator iter = data.find(key);
77 
78  if (iter != data.end())
79  {
80  value = iter->second;
81  return true;
82  }
83  else
84  {
85  return false;
86  }
87 }
88 
89 bool config::Configuration::Get(const string& key, int& value) const
90 {
91  string str;
92 
93  if (Get(key, str))
94  {
95  value = atoi(str.c_str());
96  return true;
97  }
98  else
99  {
100  return false;
101  }
102 }
103 
104 bool config::Configuration::Get(const string& key, long& value) const
105 {
106  string str;
107 
108  if (Get(key, str))
109  {
110  value = atol(str.c_str());
111  return true;
112  }
113  else
114  {
115  return false;
116  }
117 }
118 
119 bool config::Configuration::Get(const string& key, double& value) const
120 {
121  string str;
122 
123  if (Get(key, str))
124  {
125  value = atof(str.c_str());
126  return true;
127  }
128  else
129  {
130  return false;
131  }
132 }
133 
134 bool config::Configuration::Get(const string& key, bool& value) const
135 {
136  string str;
137 
138  if (Get(key, str))
139  {
140  value = (str == "true");
141  return true;
142  }
143  else
144  {
145  return false;
146  }
147 }
148 
149 string config::Configuration::Trim(const string& str)
150 {
151  int first = str.find_first_not_of(" \t");
152 
153  if (first != string::npos)
154  {
155  int last = str.find_last_not_of(" \t");
156 
157  return str.substr(first, last - first + 1);
158  }
159  else
160  {
161  return "";
162  }
163 }
164 
Implementation of the Configuration class used to parse the configuration file and the sConfig struct...