-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfighandler.cpp
194 lines (154 loc) · 6.01 KB
/
confighandler.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/******************************************************************************
* *
* Copyright ( C ) 2000, 2001 *
* Tejas Networks India Pvt. Ltd *
* *
* Proprietary and Confidential. All Rights Reserved *
* *
******************************************************************************/
/******************************************************************************
* Name : app/common/nm/nm/ConfigHandler.cpp *
* Desc : Common utility functions to perform action like read, write, *
* edit, delete data from .ini configuration files.Includes *
* declaration of each function. *
* History : *
* - Initial version : Abhishek Kumar Mishra *
* *
* ****************************************************************************/
#include<iostream>
#include<string>
#include "iniparser.hpp"
#include "confighandler.h"
using namespace HandlerNamespce;
#define NULL_PTR 0
// In the constructor define initial variables
ConfigHandler::ConfigHandler()
{
ConfigHandler::delay = 60000; // Change the timing between each sync
ConfigHandler::threadlist.push_back(std::thread(&ConfigHandler::reinit, this)); // List storing all the asyn operations
}
// In the destructor we intend to close all the running operations including threads
ConfigHandler::~ConfigHandler()
{
for(auto& t: threadlist)
{
if(t.joinable()){
t.join();
}
}
}
ConfigHandler *ConfigHandler::instance = 0;
ConfigHandler* ConfigHandler::getInstance(void) {
if (!instance) {
instance = new ConfigHandler();
}
return instance;
}
// Function for parsing data from given file
bool ConfigHandler::parseData(std::string filePath)
{
if(filePath == ""){
return false;
}
ConfigHandler::fileName = filePath;
if (!ConfigHandler::file.Load(filePath))
{
// Loading from stream
std::ifstream op("opts.ini",std::ios::in);
if (op.is_open())
op >> file;
}
return true;
}
// Function for reading the data from the file in a certain interval
bool ConfigHandler::reinit(){
bool unload_prev = true;
if(ConfigHandler::getInstance()->fileName == ""){ return NULL; } ConfigHandler::getInstance()->file.Unload();
ConfigHandler::getInstance()->file.Load(ConfigHandler::getInstance()->fileName, unload_prev);
}
// Function for fetching a value using sectionName and feature specifications value is then stored in value arguement
bool ConfigHandler::getValue(std::string sectionName,
std::string featureName,
std::string str1,
std::string str2,
std::string str3,
std::string str4,
std::string str5,
std::string &value)
{
ConfigHandler::errorstate = "insufficient information \n";
if(sectionName ==""||featureName=="")
{
cout<<errorstate;
return false;
}
string args[5];
std::string tag="";
tag = tag + featureName;
args[0]= str1;
args[1]= str2;
args[2]= str3;
args[3]= str4;
args[4]= str5;
for(int i = 0; i< 5; i++){
if(args[i] != ""){
tag += "_";
tag += args[i];
}
else
break;
}
value = ConfigHandler::file.GetSection(sectionName)->GetValue(tag,1).AsString();
return true;
}
// Function for setting a value using sectionName and tag
void ConfigHandler::setValue(std::string sectionName,
std::string tag,
std::string& value)
{
ConfigHandler::file.GetSection(sectionName)->SetValue(tag,value);
}
// Function for modifying value of a feature using tag and sectionName
void ConfigHandler::editValue(std::string sectionName,
std::string tag,
std::string& value)
{
ConfigHandler::file.GetSection(sectionName)->SetValue(tag,value);
}
// Function to remove a key value pair from the store using sectionName and key
void ConfigHandler::deleteKey(std::string sectionName, std::string key)
{
if(sectionName == ""|| key == "")
{
std::cout<<"Please enter sectionName and key correctly \n";
return;
}
ConfigHandler::file.GetSection(sectionName)->RemoveValue(key);
}
// Function to remove a section from the file
void ConfigHandler::deleteSec(std::string sectionName)
{
if(sectionName == "")
{
std::cout<<"Please enter sectionName correctly \n";
return;
}
ConfigHandler::file.DeleteSection(sectionName);
}
// Function to display all the from the data store.
void ConfigHandler::displayValues(){
for (INI::File::sections_iter it = ConfigHandler::file.SectionsBegin(); it != ConfigHandler::file.SectionsEnd(); ++it)
{
std::cout << "Section name: " << it->first << std::endl;
INI::Section* sect = it->second;
// iterate over all entries in specific section
for (INI::Section::values_iter it2 = sect->ValuesBegin(); it2 != sect->ValuesEnd(); ++it2)
std::cout << "Entry name: " << it2->first << ", Entry value: "
<< it2->second.AsString() << std::endl;
}
}
// Function for manually sync all the data from datastore to file
void ConfigHandler::syncData()
{
ConfigHandler::file.Save(fileName);
}