-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadconfig.cpp
More file actions
86 lines (80 loc) · 2.23 KB
/
readconfig.cpp
File metadata and controls
86 lines (80 loc) · 2.23 KB
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
#include <iostream>
#include <fstream>
#include <filesystem>
#include <unistd.h>
#include "readconfig.h"
using namespace std;
ReadConfig::ReadConfig()
{
dbhost = "";
dbuser = "";
dbpasswd = "";
packet_filter = "";
readconfig();
}
string ReadConfig::getconfigparameter(string parametername, string content)
{
// find line with parameter
size_t firstpos = string::npos;
size_t secondpos = string::npos;
size_t param_pos1 = content.find(parametername, 0);
if (param_pos1 != string::npos)
{
firstpos = content.find("\"", param_pos1 + 2);
if (firstpos == string::npos)
{
cout << "\n"
<< parametername << " invalid! check `sipcollect.config`\n";
exit(1);
}
secondpos = content.find("\"", firstpos + 1);
if (secondpos == string::npos)
{
cout << "\n"
<< parametername << " invalid! check `sipcollect.config`\n";
exit(1);
}
}
else
{
cout << "\n"
<< parametername << " missing! check `sipcollect.config`\n";
exit(1);
}
return content.substr(firstpos + 1, secondpos - firstpos - 1);
}
#include <limits.h>
#include <unistd.h>
std::string getexepath()
{
char result[PATH_MAX];
ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
return std::string(result, (count > 0) ? count : 0);
}
int ReadConfig::readconfig()
{
configfilecontent = "";
string line;
string tmpstrname = getexepath();
string filename = tmpstrname + ".config";
ifstream myfile(filename);
if (myfile.is_open())
{
while (getline(myfile, line))
{
configfilecontent = configfilecontent + line;
}
dbhost = getconfigparameter("dbhost", configfilecontent);
dbname = getconfigparameter("dbname", configfilecontent);
dbuser = getconfigparameter("dbuser", configfilecontent);
dbpasswd = getconfigparameter("dbpasswd", configfilecontent);
packet_filter = getconfigparameter("packet_filter", configfilecontent);
myfile.close();
}
else
{
cout << "\n Unable to open config-file `sipcollect.config`\n";
exit(1);
}
return 0;
}