forked from pixelsdb/pixels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigFactory.cpp
More file actions
111 lines (102 loc) · 2.92 KB
/
ConfigFactory.cpp
File metadata and controls
111 lines (102 loc) · 2.92 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
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
/*
* Copyright 2023 PixelsDB.
*
* This file is part of Pixels.
*
* Pixels is free software: you can redistribute it and/or modify
* it under the terms of the Affero GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* Pixels is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Affero GNU General Public License for more details.
*
* You should have received a copy of the Affero GNU General Public
* License along with Pixels. If not, see
* <https://www.gnu.org/licenses/>.
*/
/*
* @author liyu
* @create 2023-04-19
*/
#include "utils/ConfigFactory.h"
ConfigFactory &ConfigFactory::Instance()
{
static ConfigFactory instance;
return instance;
}
ConfigFactory::ConfigFactory()
{
if (std::getenv("PIXELS_SRC") == nullptr)
{
throw InvalidArgumentException("The environment variable 'PIXELS_SRC' is not set. ");
}
pixelsSrc = std::string(std::getenv("PIXELS_SRC"));
std::cout << "PIXELS_SRC is " << pixelsSrc << std::endl;
if (pixelsSrc.back() != '/')
{
pixelsSrc += "/";
}
if (std::getenv("PIXELS_HOME") == nullptr)
{
throw InvalidArgumentException("The environment variable 'PIXELS_HOME' is not set. ");
}
pixelsHome = std::string(std::getenv("PIXELS_HOME"));
std::cout << "PIXELS_HOME is " << pixelsHome << std::endl;
if (pixelsHome.back() != '/')
{
pixelsHome += "/";
}
std::ifstream infile(pixelsHome + "cpp/etc/pixels-cpp.properties");
std::cout << "pixels properties file is " << pixelsHome + "cpp/etc/pixels-cpp.properties" << std::endl;
std::string line;
while (std::getline(infile, line))
{
if (line.find('=') != std::string::npos && line.at(0) != '#')
{
std::string key = line.substr(0, line.find('='));
std::string value = line.substr(line.find('=') + 1, line.size() - line.find('=') - 1);
prop[key] = value;
}
}
}
void ConfigFactory::Print()
{
for (auto kv: prop)
{
std::cout << kv.first << " " << kv.second << std::endl;
}
}
std::string ConfigFactory::getProperty(std::string key)
{
if (prop.find(key) == prop.end())
{
throw InvalidArgumentException("ConfigFactory::getProperty: no key found: " + key);
}
return prop[key];
}
bool ConfigFactory::boolCheckProperty(std::string key)
{
if (getProperty(key) == "true")
{
return true;
}
else if (getProperty(key) == "false")
{
return false;
}
else
{
throw InvalidArgumentException("ConfigFactory: The key is not boolean type.");
}
}
std::string ConfigFactory::getPixelsDirectory()
{
return pixelsHome;
}
std::string ConfigFactory::getPixelsSourceDirectory()
{
return pixelsSrc;
}