1//
2// SystemConfiguration.cpp
3//
4// Library: Util
5// Package: Configuration
6// Module: SystemConfiguration
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/Util/SystemConfiguration.h"
16#include "Poco/Environment.h"
17#include "Poco/Path.h"
18#include "Poco/DateTime.h"
19#include "Poco/DateTimeFormatter.h"
20#include "Poco/DateTimeFormat.h"
21#include "Poco/NumberFormatter.h"
22#if !defined(POCO_VXWORKS)
23#include "Poco/Process.h"
24#endif
25#include "Poco/Exception.h"
26#include <cstdio>
27
28
29using Poco::Environment;
30using Poco::Path;
31
32
33namespace Poco {
34namespace Util {
35
36
37const std::string SystemConfiguration::OSNAME = "system.osName";
38const std::string SystemConfiguration::OSVERSION = "system.osVersion";
39const std::string SystemConfiguration::OSARCHITECTURE = "system.osArchitecture";
40const std::string SystemConfiguration::NODENAME = "system.nodeName";
41const std::string SystemConfiguration::NODEID = "system.nodeId";
42const std::string SystemConfiguration::CURRENTDIR = "system.currentDir";
43const std::string SystemConfiguration::HOMEDIR = "system.homeDir";
44const std::string SystemConfiguration::CONFIGHOMEDIR = "system.configHomeDir";
45const std::string SystemConfiguration::CACHEHOMEDIR = "system.cacheHomeDir";
46const std::string SystemConfiguration::DATAHOMEDIR = "system.dataHomeDir";
47const std::string SystemConfiguration::TEMPDIR = "system.tempDir";
48const std::string SystemConfiguration::CONFIGDIR = "system.configDir";
49const std::string SystemConfiguration::DATETIME = "system.dateTime";
50#if !defined(POCO_VXWORKS)
51const std::string SystemConfiguration::PID = "system.pid";
52#endif
53const std::string SystemConfiguration::ENV = "system.env.";
54
55
56SystemConfiguration::SystemConfiguration()
57{
58}
59
60
61SystemConfiguration::~SystemConfiguration()
62{
63}
64
65
66bool SystemConfiguration::getRaw(const std::string& key, std::string& value) const
67{
68 if (key == OSNAME)
69 {
70 value = Environment::osName();
71 }
72 else if (key == OSVERSION)
73 {
74 value = Environment::osVersion();
75 }
76 else if (key == OSARCHITECTURE)
77 {
78 value = Environment::osArchitecture();
79 }
80 else if (key == NODENAME)
81 {
82 value = Environment::nodeName();
83 }
84 else if (key == NODEID)
85 {
86 try
87 {
88 Poco::Environment::NodeId id;
89 Poco::Environment::nodeId(id);
90 char result[13];
91 std::sprintf(result, "%02x%02x%02x%02x%02x%02x",
92 id[0],
93 id[1],
94 id[2],
95 id[3],
96 id[4],
97 id[5]);
98 value = result;
99 }
100 catch (...)
101 {
102 value = "000000000000";
103 }
104 }
105 else if (key == CURRENTDIR)
106 {
107 value = Path::current();
108 }
109 else if (key == HOMEDIR)
110 {
111 value = Path::home();
112 }
113 else if (key == CONFIGHOMEDIR)
114 {
115 value = Path::configHome();
116 }
117 else if (key == CACHEHOMEDIR)
118 {
119 value = Path::cacheHome();
120 }
121 else if (key == DATAHOMEDIR)
122 {
123 value = Path::dataHome();
124 }
125 else if (key == TEMPDIR)
126 {
127 value = Path::temp();
128 }
129 else if (key == CONFIGDIR)
130 {
131 value = Path::config();
132 }
133 else if (key == DATETIME)
134 {
135 value = Poco::DateTimeFormatter::format(Poco::DateTime(), Poco::DateTimeFormat::ISO8601_FORMAT);
136 }
137#if !defined(POCO_VXWORKS)
138 else if (key == PID)
139 {
140 value = "0";
141 value = Poco::NumberFormatter::format(Poco::Process::id());
142 }
143#endif
144 else if (key.compare(0, ENV.size(), ENV) == 0)
145 {
146 return getEnv(key.substr(ENV.size()), value);
147 }
148 else return false;
149 return true;
150}
151
152
153void SystemConfiguration::setRaw(const std::string& key, const std::string& /*value*/)
154{
155 throw Poco::InvalidAccessException("Attempt to modify a system property", key);
156}
157
158
159void SystemConfiguration::enumerate(const std::string& key, Keys& range) const
160{
161 if (key.empty())
162 {
163 range.push_back("system");
164 }
165 else if (key == "system")
166 {
167 range.push_back("osName");
168 range.push_back("osVersion");
169 range.push_back("osArchitecture");
170 range.push_back("nodeName");
171 range.push_back("nodeId");
172 range.push_back("currentDir");
173 range.push_back("homeDir");
174 range.push_back("configHomeDir");
175 range.push_back("cacheHomeDir");
176 range.push_back("dataHomeDir");
177 range.push_back("tempDir");
178 range.push_back("configDir");
179 range.push_back("dateTime");
180#if !defined(POCO_VXWORKS)
181 range.push_back("pid");
182#endif
183 range.push_back("env");
184 }
185}
186
187
188void SystemConfiguration::removeRaw(const std::string& /*key*/)
189{
190 throw Poco::NotImplementedException("Removing a key in a SystemConfiguration");
191}
192
193
194bool SystemConfiguration::getEnv(const std::string& name, std::string& value)
195{
196 if (Environment::has(name))
197 {
198 value = Environment::get(name);
199 return true;
200 }
201 return false;
202}
203
204
205} } // namespace Poco::Util
206