mirror of
https://github.com/pocoproject/poco.git
synced 2025-11-03 11:26:20 +01:00
merge some changes from develop branch; modernize and clean-up code; remove support for compiling without POCO_WIN32_UTF8
This commit is contained in:
@@ -336,15 +336,15 @@ void AbstractConfiguration::keys(const std::string& key, Keys& range) const
|
||||
}
|
||||
|
||||
|
||||
const AbstractConfiguration* AbstractConfiguration::createView(const std::string& prefix) const
|
||||
const AbstractConfiguration::Ptr AbstractConfiguration::createView(const std::string& prefix) const
|
||||
{
|
||||
return new ConfigurationView(prefix, const_cast<AbstractConfiguration*>(this));
|
||||
return new ConfigurationView(prefix, AbstractConfiguration::Ptr(const_cast<AbstractConfiguration*>(this), true));
|
||||
}
|
||||
|
||||
|
||||
AbstractConfiguration* AbstractConfiguration::createView(const std::string& prefix)
|
||||
AbstractConfiguration::Ptr AbstractConfiguration::createView(const std::string& prefix)
|
||||
{
|
||||
return new ConfigurationView(prefix, this);
|
||||
return new ConfigurationView(prefix, AbstractConfiguration::Ptr(this, true));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -41,9 +41,7 @@
|
||||
#if defined(POCO_OS_FAMILY_UNIX) && !defined(POCO_VXWORKS)
|
||||
#include "Poco/SignalHandler.h"
|
||||
#endif
|
||||
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
|
||||
#include "Poco/UnicodeConverter.h"
|
||||
#endif
|
||||
|
||||
|
||||
using Poco::Logger;
|
||||
@@ -97,8 +95,8 @@ void Application::setup()
|
||||
{
|
||||
poco_assert (_pInstance == 0);
|
||||
|
||||
_pConfig->add(new SystemConfiguration, PRIO_SYSTEM, false, false);
|
||||
_pConfig->add(new MapConfiguration, PRIO_APPLICATION, true, false);
|
||||
_pConfig->add(new SystemConfiguration, PRIO_SYSTEM, false);
|
||||
_pConfig->add(new MapConfiguration, PRIO_APPLICATION, true);
|
||||
|
||||
addSubsystem(new LoggingSubsystem);
|
||||
|
||||
@@ -134,7 +132,7 @@ void Application::init(int argc, char* argv[])
|
||||
}
|
||||
|
||||
|
||||
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
|
||||
#if defined(_WIN32)
|
||||
void Application::init(int argc, wchar_t* argv[])
|
||||
{
|
||||
std::vector<std::string> args;
|
||||
@@ -180,10 +178,10 @@ const char* Application::name() const
|
||||
|
||||
void Application::initialize(Application& self)
|
||||
{
|
||||
for (SubsystemVec::iterator it = _subsystems.begin(); it != _subsystems.end(); ++it)
|
||||
for (auto& pSub: _subsystems)
|
||||
{
|
||||
_pLogger->debug(std::string("Initializing subsystem: ") + (*it)->name());
|
||||
(*it)->initialize(self);
|
||||
_pLogger->debug(std::string("Initializing subsystem: ") + pSub->name());
|
||||
pSub->initialize(self);
|
||||
}
|
||||
_initialized = true;
|
||||
}
|
||||
@@ -193,10 +191,10 @@ void Application::uninitialize()
|
||||
{
|
||||
if (_initialized)
|
||||
{
|
||||
for (SubsystemVec::reverse_iterator it = _subsystems.rbegin(); it != _subsystems.rend(); ++it)
|
||||
for (auto& pSub: _subsystems)
|
||||
{
|
||||
_pLogger->debug(std::string("Uninitializing subsystem: ") + (*it)->name());
|
||||
(*it)->uninitialize();
|
||||
_pLogger->debug(std::string("Uninitializing subsystem: ") + pSub->name());
|
||||
pSub->uninitialize();
|
||||
}
|
||||
_initialized = false;
|
||||
}
|
||||
@@ -205,10 +203,10 @@ void Application::uninitialize()
|
||||
|
||||
void Application::reinitialize(Application& self)
|
||||
{
|
||||
for (SubsystemVec::iterator it = _subsystems.begin(); it != _subsystems.end(); ++it)
|
||||
for (auto& pSub: _subsystems)
|
||||
{
|
||||
_pLogger->debug(std::string("Re-initializing subsystem: ") + (*it)->name());
|
||||
(*it)->reinitialize(self);
|
||||
_pLogger->debug(std::string("Re-initializing subsystem: ") + pSub->name());
|
||||
pSub->reinitialize(self);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,27 +225,27 @@ int Application::loadConfiguration(int priority)
|
||||
Path confPath;
|
||||
if (findAppConfigFile(appPath.getBaseName(), "properties", confPath))
|
||||
{
|
||||
_pConfig->add(new PropertyFileConfiguration(confPath.toString()), priority, false, false);
|
||||
_pConfig->add(new PropertyFileConfiguration(confPath.toString()), priority, false);
|
||||
++n;
|
||||
}
|
||||
#ifndef POCO_UTIL_NO_INIFILECONFIGURATION
|
||||
if (findAppConfigFile(appPath.getBaseName(), "ini", confPath))
|
||||
{
|
||||
_pConfig->add(new IniFileConfiguration(confPath.toString()), priority, false, false);
|
||||
_pConfig->add(new IniFileConfiguration(confPath.toString()), priority, false);
|
||||
++n;
|
||||
}
|
||||
#endif
|
||||
#ifndef POCO_UTIL_NO_JSONCONFIGURATION
|
||||
if (findAppConfigFile(appPath.getBaseName(), "json", confPath))
|
||||
{
|
||||
_pConfig->add(new JSONConfiguration(confPath.toString()), priority, false, false);
|
||||
_pConfig->add(new JSONConfiguration(confPath.toString()), priority, false);
|
||||
++n;
|
||||
}
|
||||
#endif
|
||||
#ifndef POCO_UTIL_NO_XMLCONFIGURATION
|
||||
if (findAppConfigFile(appPath.getBaseName(), "xml", confPath))
|
||||
{
|
||||
_pConfig->add(new XMLConfiguration(confPath.toString()), priority, false, false);
|
||||
_pConfig->add(new XMLConfiguration(confPath.toString()), priority, false);
|
||||
++n;
|
||||
}
|
||||
#endif
|
||||
@@ -269,27 +267,27 @@ void Application::loadConfiguration(const std::string& path, int priority)
|
||||
std::string ext = confPath.getExtension();
|
||||
if (icompare(ext, "properties") == 0)
|
||||
{
|
||||
_pConfig->add(new PropertyFileConfiguration(confPath.toString()), priority, false, false);
|
||||
_pConfig->add(new PropertyFileConfiguration(confPath.toString()), priority, false);
|
||||
++n;
|
||||
}
|
||||
#ifndef POCO_UTIL_NO_INIFILECONFIGURATION
|
||||
else if (icompare(ext, "ini") == 0)
|
||||
{
|
||||
_pConfig->add(new IniFileConfiguration(confPath.toString()), priority, false, false);
|
||||
_pConfig->add(new IniFileConfiguration(confPath.toString()), priority, false);
|
||||
++n;
|
||||
}
|
||||
#endif
|
||||
#ifndef POCO_UTIL_NO_JSONCONFIGURATION
|
||||
else if (icompare(ext, "json") == 0)
|
||||
{
|
||||
_pConfig->add(new JSONConfiguration(confPath.toString()), priority, false, false);
|
||||
_pConfig->add(new JSONConfiguration(confPath.toString()), priority, false);
|
||||
++n;
|
||||
}
|
||||
#endif
|
||||
#ifndef POCO_UTIL_NO_XMLCONFIGURATION
|
||||
else if (icompare(ext, "xml") == 0)
|
||||
{
|
||||
_pConfig->add(new XMLConfiguration(confPath.toString()), priority, false, false);
|
||||
_pConfig->add(new XMLConfiguration(confPath.toString()), priority, false);
|
||||
++n;
|
||||
}
|
||||
#endif
|
||||
@@ -437,7 +435,6 @@ void Application::getApplicationPath(Poco::Path& appPath) const
|
||||
appPath.makeAbsolute();
|
||||
}
|
||||
#elif defined(POCO_OS_FAMILY_WINDOWS)
|
||||
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
|
||||
wchar_t path[1024];
|
||||
int n = GetModuleFileNameW(0, path, sizeof(path)/sizeof(wchar_t));
|
||||
if (n > 0)
|
||||
@@ -447,14 +444,6 @@ void Application::getApplicationPath(Poco::Path& appPath) const
|
||||
appPath = p;
|
||||
}
|
||||
else throw SystemException("Cannot get application file name.");
|
||||
#else
|
||||
char path[1024];
|
||||
int n = GetModuleFileNameA(0, path, sizeof(path));
|
||||
if (n > 0)
|
||||
appPath = path;
|
||||
else
|
||||
throw SystemException("Cannot get application file name.");
|
||||
#endif
|
||||
#else
|
||||
appPath = _command;
|
||||
#endif
|
||||
@@ -532,9 +521,9 @@ bool Application::findAppConfigFile(const Path& basePath, const std::string& app
|
||||
|
||||
void Application::defineOptions(OptionSet& options)
|
||||
{
|
||||
for (SubsystemVec::iterator it = _subsystems.begin(); it != _subsystems.end(); ++it)
|
||||
for (auto& pSub: _subsystems)
|
||||
{
|
||||
(*it)->defineOptions(options);
|
||||
pSub->defineOptions(options);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Poco {
|
||||
namespace Util {
|
||||
|
||||
|
||||
ConfigurationMapper::ConfigurationMapper(const std::string& fromPrefix, const std::string& toPrefix, AbstractConfiguration* pConfig):
|
||||
ConfigurationMapper::ConfigurationMapper(const std::string& fromPrefix, const std::string& toPrefix, AbstractConfiguration::Ptr pConfig):
|
||||
_fromPrefix(fromPrefix),
|
||||
_toPrefix(toPrefix),
|
||||
_pConfig(pConfig)
|
||||
@@ -28,14 +28,11 @@ ConfigurationMapper::ConfigurationMapper(const std::string& fromPrefix, const st
|
||||
|
||||
if (!_fromPrefix.empty()) _fromPrefix += '.';
|
||||
if (!_toPrefix.empty()) _toPrefix += '.';
|
||||
|
||||
_pConfig->duplicate();
|
||||
}
|
||||
|
||||
|
||||
ConfigurationMapper::~ConfigurationMapper()
|
||||
{
|
||||
_pConfig->release();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -19,19 +19,16 @@ namespace Poco {
|
||||
namespace Util {
|
||||
|
||||
|
||||
ConfigurationView::ConfigurationView(const std::string& prefix, AbstractConfiguration* pConfig):
|
||||
ConfigurationView::ConfigurationView(const std::string& prefix, AbstractConfiguration::Ptr pConfig):
|
||||
_prefix(prefix),
|
||||
_pConfig(pConfig)
|
||||
{
|
||||
poco_check_ptr (pConfig);
|
||||
|
||||
_pConfig->duplicate();
|
||||
}
|
||||
|
||||
|
||||
ConfigurationView::~ConfigurationView()
|
||||
{
|
||||
_pConfig->release();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -116,9 +116,9 @@ Path FilesystemConfiguration::keyToPath(const std::string& key) const
|
||||
{
|
||||
Path result(_path);
|
||||
StringTokenizer tokenizer(key, ".", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
||||
for (StringTokenizer::Iterator it = tokenizer.begin(); it != tokenizer.end(); ++it)
|
||||
for (const auto& tok: tokenizer)
|
||||
{
|
||||
result.pushDirectory(*it);
|
||||
result.pushDirectory(tok);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -122,23 +122,23 @@ void HelpFormatter::setUnixStyle(bool flag)
|
||||
int HelpFormatter::calcIndent() const
|
||||
{
|
||||
int indent = 0;
|
||||
for (OptionSet::Iterator it = _options.begin(); it != _options.end(); ++it)
|
||||
for (const auto& opt: _options)
|
||||
{
|
||||
int shortLen = (int) it->shortName().length();
|
||||
int fullLen = (int) it->fullName().length();
|
||||
int n = 0;
|
||||
auto shortLen = opt.shortName().length();
|
||||
auto fullLen = opt.fullName().length();
|
||||
std::size_t n = 0;
|
||||
if (_unixStyle && shortLen > 0)
|
||||
{
|
||||
n += shortLen + (int) shortPrefix().length();
|
||||
if (it->takesArgument())
|
||||
n += (int) it->argumentName().length() + (it->argumentRequired() ? 0 : 2);
|
||||
n += shortLen + shortPrefix().length();
|
||||
if (opt.takesArgument())
|
||||
n += opt.argumentName().length() + (opt.argumentRequired() ? 0 : 2);
|
||||
if (fullLen > 0) n += 2;
|
||||
}
|
||||
if (fullLen > 0)
|
||||
{
|
||||
n += fullLen + (int) longPrefix().length();
|
||||
if (it->takesArgument())
|
||||
n += 1 + (int) it->argumentName().length() + (it->argumentRequired() ? 0 : 2);
|
||||
n += fullLen + longPrefix().length();
|
||||
if (opt.takesArgument())
|
||||
n += 1 + opt.argumentName().length() + (opt.argumentRequired() ? 0 : 2);
|
||||
}
|
||||
n += 2;
|
||||
if (n > indent)
|
||||
@@ -151,17 +151,17 @@ int HelpFormatter::calcIndent() const
|
||||
void HelpFormatter::formatOptions(std::ostream& ostr) const
|
||||
{
|
||||
int optWidth = calcIndent();
|
||||
for (OptionSet::Iterator it = _options.begin(); it != _options.end(); ++it)
|
||||
for (const auto& opt: _options)
|
||||
{
|
||||
formatOption(ostr, *it, optWidth);
|
||||
formatOption(ostr, opt, optWidth);
|
||||
if (_indent < optWidth)
|
||||
{
|
||||
ostr << '\n' << std::string(_indent, ' ');
|
||||
formatText(ostr, it->description(), _indent, _indent);
|
||||
formatText(ostr, opt.description(), _indent, _indent);
|
||||
}
|
||||
else
|
||||
{
|
||||
formatText(ostr, it->description(), _indent, optWidth);
|
||||
formatText(ostr, opt.description(), _indent, optWidth);
|
||||
}
|
||||
ostr << '\n';
|
||||
}
|
||||
@@ -216,16 +216,16 @@ void HelpFormatter::formatText(std::ostream& ostr, const std::string& text, int
|
||||
int pos = firstIndent;
|
||||
int maxWordLen = _width - indent;
|
||||
std::string word;
|
||||
for (std::string::const_iterator it = text.begin(); it != text.end(); ++it)
|
||||
for (auto ch: text)
|
||||
{
|
||||
if (*it == '\n')
|
||||
if (ch == '\n')
|
||||
{
|
||||
clearWord(ostr, pos, word, indent);
|
||||
ostr << '\n';
|
||||
pos = 0;
|
||||
while (pos < indent) { ostr << ' '; ++pos; }
|
||||
}
|
||||
else if (*it == '\t')
|
||||
else if (ch == '\t')
|
||||
{
|
||||
clearWord(ostr, pos, word, indent);
|
||||
if (pos < _width) ++pos;
|
||||
@@ -235,7 +235,7 @@ void HelpFormatter::formatText(std::ostream& ostr, const std::string& text, int
|
||||
++pos;
|
||||
}
|
||||
}
|
||||
else if (*it == ' ')
|
||||
else if (ch == ' ')
|
||||
{
|
||||
clearWord(ostr, pos, word, indent);
|
||||
if (pos < _width) { ostr << ' '; ++pos; }
|
||||
@@ -246,7 +246,7 @@ void HelpFormatter::formatText(std::ostream& ostr, const std::string& text, int
|
||||
{
|
||||
clearWord(ostr, pos, word, indent);
|
||||
}
|
||||
else word += *it;
|
||||
else word += ch;
|
||||
}
|
||||
}
|
||||
clearWord(ostr, pos, word, indent);
|
||||
|
||||
@@ -102,16 +102,16 @@ void IniFileConfiguration::enumerate(const std::string& key, Keys& range) const
|
||||
std::string prefix = key;
|
||||
if (!prefix.empty()) prefix += '.';
|
||||
std::string::size_type psize = prefix.size();
|
||||
for (IStringMap::const_iterator it = _map.begin(); it != _map.end(); ++it)
|
||||
for (const auto& p: _map)
|
||||
{
|
||||
if (icompare(it->first, psize, prefix) == 0)
|
||||
if (icompare(p.first, psize, prefix) == 0)
|
||||
{
|
||||
std::string subKey;
|
||||
std::string::size_type end = it->first.find('.', psize);
|
||||
std::string::size_type end = p.first.find('.', psize);
|
||||
if (end == std::string::npos)
|
||||
subKey = it->first.substr(psize);
|
||||
subKey = p.first.substr(psize);
|
||||
else
|
||||
subKey = it->first.substr(psize, end - psize);
|
||||
subKey = p.first.substr(psize, end - psize);
|
||||
if (keys.find(subKey) == keys.end())
|
||||
{
|
||||
range.push_back(subKey);
|
||||
|
||||
@@ -35,101 +35,57 @@ LayeredConfiguration::~LayeredConfiguration()
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::add(AbstractConfiguration* pConfig)
|
||||
void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig)
|
||||
{
|
||||
add(pConfig, highest(), false, true);
|
||||
add(pConfig, highest(), false);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label)
|
||||
void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, const std::string& label)
|
||||
{
|
||||
add(pConfig, label, highest(), false, true);
|
||||
add(pConfig, label, highest(), false);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::add(AbstractConfiguration* pConfig, bool shared)
|
||||
void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, int priority)
|
||||
{
|
||||
add(pConfig, highest(), false, shared);
|
||||
add(pConfig, priority, false);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label, bool shared)
|
||||
void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, const std::string& label, int priority)
|
||||
{
|
||||
add(pConfig, label, highest(), false, shared);
|
||||
add(pConfig, label, priority, false);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::add(AbstractConfiguration* pConfig, int priority)
|
||||
void LayeredConfiguration::addWriteable(AbstractConfiguration::Ptr pConfig, int priority)
|
||||
{
|
||||
add(pConfig, priority, false, true);
|
||||
add(pConfig, priority, true);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label, int priority)
|
||||
void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, int priority, bool writeable)
|
||||
{
|
||||
add(pConfig, label, priority, false, true);
|
||||
add(pConfig, std::string(), priority, writeable);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::add(AbstractConfiguration* pConfig, int priority, bool shared)
|
||||
{
|
||||
add(pConfig, priority, false, shared);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label, int priority, bool shared)
|
||||
{
|
||||
add(pConfig, label, priority, false, shared);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::addFront(AbstractConfiguration* pConfig)
|
||||
{
|
||||
add(pConfig, lowest(), false, true);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::addFront(AbstractConfiguration* pConfig, bool shared)
|
||||
{
|
||||
add(pConfig, lowest(), false, shared);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::addWriteable(AbstractConfiguration* pConfig, int priority)
|
||||
{
|
||||
add(pConfig, priority, true, true);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::addWriteable(AbstractConfiguration* pConfig, int priority, bool shared)
|
||||
{
|
||||
add(pConfig, priority, true, shared);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::add(AbstractConfiguration* pConfig, int priority, bool writeable, bool shared)
|
||||
{
|
||||
add(pConfig, std::string(), priority, writeable, shared);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::add(AbstractConfiguration* pConfig, const std::string& label, int priority, bool writeable, bool shared)
|
||||
void LayeredConfiguration::add(AbstractConfiguration::Ptr pConfig, const std::string& label, int priority, bool writeable)
|
||||
{
|
||||
ConfigItem item;
|
||||
item.pConfig = ConfigPtr(pConfig, shared);
|
||||
item.pConfig = pConfig;
|
||||
item.priority = priority;
|
||||
item.writeable = writeable;
|
||||
item.label = label;
|
||||
|
||||
ConfigList::iterator it = _configs.begin();
|
||||
while (it != _configs.end() && it->priority < priority)
|
||||
++it;
|
||||
|
||||
while (it != _configs.end() && it->priority < priority) ++it;
|
||||
_configs.insert(it, item);
|
||||
}
|
||||
|
||||
|
||||
void LayeredConfiguration::removeConfiguration(AbstractConfiguration* pConfig)
|
||||
void LayeredConfiguration::removeConfiguration(AbstractConfiguration::Ptr pConfig)
|
||||
{
|
||||
for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it)
|
||||
{
|
||||
@@ -142,14 +98,11 @@ void LayeredConfiguration::removeConfiguration(AbstractConfiguration* pConfig)
|
||||
}
|
||||
|
||||
|
||||
LayeredConfiguration::ConfigPtr LayeredConfiguration::find(const std::string& label) const
|
||||
AbstractConfiguration::Ptr LayeredConfiguration::find(const std::string& label) const
|
||||
{
|
||||
for (ConfigList::const_iterator it = _configs.begin(); it != _configs.end(); ++it)
|
||||
for (const auto& conf: _configs)
|
||||
{
|
||||
if (it->label == label)
|
||||
{
|
||||
return it->pConfig;
|
||||
}
|
||||
if (conf.label == label) return conf.pConfig;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -157,10 +110,9 @@ LayeredConfiguration::ConfigPtr LayeredConfiguration::find(const std::string& la
|
||||
|
||||
bool LayeredConfiguration::getRaw(const std::string& key, std::string& value) const
|
||||
{
|
||||
for (ConfigList::const_iterator it = _configs.begin(); it != _configs.end(); ++it)
|
||||
for (const auto& conf: _configs)
|
||||
{
|
||||
if (it->pConfig->getRaw(key, value))
|
||||
return true;
|
||||
if (conf.pConfig->getRaw(key, value)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -168,11 +120,11 @@ bool LayeredConfiguration::getRaw(const std::string& key, std::string& value) co
|
||||
|
||||
void LayeredConfiguration::setRaw(const std::string& key, const std::string& value)
|
||||
{
|
||||
for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it)
|
||||
for (auto& conf: _configs)
|
||||
{
|
||||
if (it->writeable)
|
||||
if (conf.writeable)
|
||||
{
|
||||
it->pConfig->setRaw(key, value);
|
||||
conf.pConfig->setRaw(key, value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -183,16 +135,16 @@ void LayeredConfiguration::setRaw(const std::string& key, const std::string& val
|
||||
void LayeredConfiguration::enumerate(const std::string& key, Keys& range) const
|
||||
{
|
||||
std::set<std::string> keys;
|
||||
for (ConfigList::const_iterator itc = _configs.begin(); itc != _configs.end(); ++itc)
|
||||
for (const auto& conf: _configs)
|
||||
{
|
||||
Keys partRange;
|
||||
itc->pConfig->enumerate(key, partRange);
|
||||
for (Keys::const_iterator itr = partRange.begin(); itr != partRange.end(); ++itr)
|
||||
conf.pConfig->enumerate(key, partRange);
|
||||
for (const auto& k: partRange)
|
||||
{
|
||||
if (keys.find(*itr) == keys.end())
|
||||
if (keys.find(k) == keys.end())
|
||||
{
|
||||
range.push_back(*itr);
|
||||
keys.insert(*itr);
|
||||
range.push_back(k);
|
||||
keys.insert(k);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -201,11 +153,11 @@ void LayeredConfiguration::enumerate(const std::string& key, Keys& range) const
|
||||
|
||||
void LayeredConfiguration::removeRaw(const std::string& key)
|
||||
{
|
||||
for (ConfigList::iterator it = _configs.begin(); it != _configs.end(); ++it)
|
||||
for (auto& conf: _configs)
|
||||
{
|
||||
if (it->writeable)
|
||||
if (conf.writeable)
|
||||
{
|
||||
it->pConfig->remove(key);
|
||||
conf.pConfig->remove(key);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
|
||||
#include "Poco/Util/LoggingConfigurator.h"
|
||||
#include "Poco/Util/AbstractConfiguration.h"
|
||||
#include "Poco/AutoPtr.h"
|
||||
#include "Poco/Channel.h"
|
||||
#include "Poco/FormattingChannel.h"
|
||||
@@ -49,152 +48,148 @@ LoggingConfigurator::~LoggingConfigurator()
|
||||
}
|
||||
|
||||
|
||||
void LoggingConfigurator::configure(AbstractConfiguration* pConfig)
|
||||
void LoggingConfigurator::configure(AbstractConfiguration::Ptr pConfig)
|
||||
{
|
||||
poco_check_ptr (pConfig);
|
||||
|
||||
AutoPtr<AbstractConfiguration> pFormattersConfig(pConfig->createView("logging.formatters"));
|
||||
AbstractConfiguration::Ptr pFormattersConfig(pConfig->createView("logging.formatters"));
|
||||
configureFormatters(pFormattersConfig);
|
||||
|
||||
AutoPtr<AbstractConfiguration> pChannelsConfig(pConfig->createView("logging.channels"));
|
||||
AbstractConfiguration::Ptr pChannelsConfig(pConfig->createView("logging.channels"));
|
||||
configureChannels(pChannelsConfig);
|
||||
|
||||
AutoPtr<AbstractConfiguration> pLoggersConfig(pConfig->createView("logging.loggers"));
|
||||
AbstractConfiguration::Ptr pLoggersConfig(pConfig->createView("logging.loggers"));
|
||||
configureLoggers(pLoggersConfig);
|
||||
}
|
||||
|
||||
|
||||
void LoggingConfigurator::configureFormatters(AbstractConfiguration* pConfig)
|
||||
void LoggingConfigurator::configureFormatters(AbstractConfiguration::Ptr pConfig)
|
||||
{
|
||||
AbstractConfiguration::Keys formatters;
|
||||
pConfig->keys(formatters);
|
||||
for (AbstractConfiguration::Keys::const_iterator it = formatters.begin(); it != formatters.end(); ++it)
|
||||
for (const auto& f: formatters)
|
||||
{
|
||||
AutoPtr<AbstractConfiguration> pFormatterConfig(pConfig->createView(*it));
|
||||
AutoPtr<AbstractConfiguration> pFormatterConfig(pConfig->createView(f));
|
||||
AutoPtr<Formatter> pFormatter(createFormatter(pFormatterConfig));
|
||||
LoggingRegistry::defaultRegistry().registerFormatter(*it, pFormatter);
|
||||
LoggingRegistry::defaultRegistry().registerFormatter(f, pFormatter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LoggingConfigurator::configureChannels(AbstractConfiguration* pConfig)
|
||||
void LoggingConfigurator::configureChannels(AbstractConfiguration::Ptr pConfig)
|
||||
{
|
||||
AbstractConfiguration::Keys channels;
|
||||
pConfig->keys(channels);
|
||||
for (AbstractConfiguration::Keys::const_iterator it = channels.begin(); it != channels.end(); ++it)
|
||||
for (const auto& c: channels)
|
||||
{
|
||||
AutoPtr<AbstractConfiguration> pChannelConfig(pConfig->createView(*it));
|
||||
AutoPtr<AbstractConfiguration> pChannelConfig(pConfig->createView(c));
|
||||
AutoPtr<Channel> pChannel = createChannel(pChannelConfig);
|
||||
LoggingRegistry::defaultRegistry().registerChannel(*it, pChannel);
|
||||
LoggingRegistry::defaultRegistry().registerChannel(c, pChannel);
|
||||
}
|
||||
for (AbstractConfiguration::Keys::const_iterator it = channels.begin(); it != channels.end(); ++it)
|
||||
for (const auto& c: channels)
|
||||
{
|
||||
AutoPtr<AbstractConfiguration> pChannelConfig(pConfig->createView(*it));
|
||||
Channel* pChannel = LoggingRegistry::defaultRegistry().channelForName(*it);
|
||||
AutoPtr<AbstractConfiguration> pChannelConfig(pConfig->createView(c));
|
||||
Channel::Ptr pChannel = LoggingRegistry::defaultRegistry().channelForName(c);
|
||||
configureChannel(pChannel, pChannelConfig);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LoggingConfigurator::configureLoggers(AbstractConfiguration* pConfig)
|
||||
void LoggingConfigurator::configureLoggers(AbstractConfiguration::Ptr pConfig)
|
||||
{
|
||||
typedef std::map<std::string, AutoPtr<AbstractConfiguration> > LoggerMap;
|
||||
using LoggerMap = std::map<std::string, AutoPtr<AbstractConfiguration>>;
|
||||
|
||||
AbstractConfiguration::Keys loggers;
|
||||
pConfig->keys(loggers);
|
||||
// use a map to sort loggers by their name, ensuring initialization in correct order (parents before children)
|
||||
LoggerMap loggerMap;
|
||||
for (AbstractConfiguration::Keys::const_iterator it = loggers.begin(); it != loggers.end(); ++it)
|
||||
for (const auto& l: loggers)
|
||||
{
|
||||
AutoPtr<AbstractConfiguration> pLoggerConfig(pConfig->createView(*it));
|
||||
AutoPtr<AbstractConfiguration> pLoggerConfig(pConfig->createView(l));
|
||||
loggerMap[pLoggerConfig->getString("name", "")] = pLoggerConfig;
|
||||
}
|
||||
for (LoggerMap::iterator it = loggerMap.begin(); it != loggerMap.end(); ++it)
|
||||
for (const auto& p: loggerMap)
|
||||
{
|
||||
configureLogger(it->second);
|
||||
configureLogger(p.second);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Formatter* LoggingConfigurator::createFormatter(AbstractConfiguration* pConfig)
|
||||
Formatter::Ptr LoggingConfigurator::createFormatter(AbstractConfiguration::Ptr pConfig)
|
||||
{
|
||||
AutoPtr<Formatter> pFormatter(LoggingFactory::defaultFactory().createFormatter(pConfig->getString("class")));
|
||||
Formatter::Ptr pFormatter(LoggingFactory::defaultFactory().createFormatter(pConfig->getString("class")));
|
||||
AbstractConfiguration::Keys props;
|
||||
pConfig->keys(props);
|
||||
for (AbstractConfiguration::Keys::const_iterator it = props.begin(); it != props.end(); ++it)
|
||||
for (const auto& p: props)
|
||||
{
|
||||
if (*it != "class")
|
||||
pFormatter->setProperty(*it, pConfig->getString(*it));
|
||||
if (p != "class")
|
||||
pFormatter->setProperty(p, pConfig->getString(p));
|
||||
}
|
||||
return pFormatter.duplicate();
|
||||
return pFormatter;
|
||||
}
|
||||
|
||||
|
||||
Channel* LoggingConfigurator::createChannel(AbstractConfiguration* pConfig)
|
||||
Channel::Ptr LoggingConfigurator::createChannel(AbstractConfiguration::Ptr pConfig)
|
||||
{
|
||||
AutoPtr<Channel> pChannel(LoggingFactory::defaultFactory().createChannel(pConfig->getString("class")));
|
||||
AutoPtr<Channel> pWrapper(pChannel);
|
||||
Channel::Ptr pChannel(LoggingFactory::defaultFactory().createChannel(pConfig->getString("class")));
|
||||
Channel::Ptr pWrapper(pChannel);
|
||||
AbstractConfiguration::Keys props;
|
||||
pConfig->keys(props);
|
||||
for (AbstractConfiguration::Keys::const_iterator it = props.begin(); it != props.end(); ++it)
|
||||
for (const auto& p: props)
|
||||
{
|
||||
if (*it == "pattern")
|
||||
if (p == "pattern")
|
||||
{
|
||||
AutoPtr<Formatter> pPatternFormatter(new PatternFormatter(pConfig->getString(*it)));
|
||||
AutoPtr<Formatter> pPatternFormatter(new PatternFormatter(pConfig->getString(p)));
|
||||
pWrapper = new FormattingChannel(pPatternFormatter, pChannel);
|
||||
}
|
||||
else if (*it == "formatter")
|
||||
else if (p == "formatter")
|
||||
{
|
||||
AutoPtr<FormattingChannel> pFormattingChannel(new FormattingChannel(0, pChannel));
|
||||
if (pConfig->hasProperty("formatter.class"))
|
||||
{
|
||||
AutoPtr<AbstractConfiguration> pFormatterConfig(pConfig->createView(*it));
|
||||
AutoPtr<AbstractConfiguration> pFormatterConfig(pConfig->createView(p));
|
||||
AutoPtr<Formatter> pFormatter(createFormatter(pFormatterConfig));
|
||||
pFormattingChannel->setFormatter(pFormatter);
|
||||
}
|
||||
else pFormattingChannel->setProperty(*it, pConfig->getString(*it));
|
||||
#if defined(__GNUC__) && __GNUC__ < 3
|
||||
pWrapper = pFormattingChannel.duplicate();
|
||||
#else
|
||||
else pFormattingChannel->setProperty(p, pConfig->getString(p));
|
||||
pWrapper = pFormattingChannel;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
return pWrapper.duplicate();
|
||||
return pWrapper;
|
||||
}
|
||||
|
||||
|
||||
void LoggingConfigurator::configureChannel(Channel* pChannel, AbstractConfiguration* pConfig)
|
||||
void LoggingConfigurator::configureChannel(Channel::Ptr pChannel, AbstractConfiguration::Ptr pConfig)
|
||||
{
|
||||
AbstractConfiguration::Keys props;
|
||||
pConfig->keys(props);
|
||||
for (AbstractConfiguration::Keys::const_iterator it = props.begin(); it != props.end(); ++it)
|
||||
for (const auto& p: props)
|
||||
{
|
||||
if (*it != "pattern" && *it != "formatter" && *it != "class")
|
||||
if (p != "pattern" && p != "formatter" && p != "class")
|
||||
{
|
||||
pChannel->setProperty(*it, pConfig->getString(*it));
|
||||
pChannel->setProperty(p, pConfig->getString(p));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LoggingConfigurator::configureLogger(AbstractConfiguration* pConfig)
|
||||
void LoggingConfigurator::configureLogger(AbstractConfiguration::Ptr pConfig)
|
||||
{
|
||||
Logger& logger = Logger::get(pConfig->getString("name", ""));
|
||||
AbstractConfiguration::Keys props;
|
||||
pConfig->keys(props);
|
||||
for (AbstractConfiguration::Keys::const_iterator it = props.begin(); it != props.end(); ++it)
|
||||
for (const auto& p: props)
|
||||
{
|
||||
if (*it == "channel" && pConfig->hasProperty("channel.class"))
|
||||
if (p == "channel" && pConfig->hasProperty("channel.class"))
|
||||
{
|
||||
AutoPtr<AbstractConfiguration> pChannelConfig(pConfig->createView(*it));
|
||||
AutoPtr<AbstractConfiguration> pChannelConfig(pConfig->createView(p));
|
||||
AutoPtr<Channel> pChannel(createChannel(pChannelConfig));
|
||||
configureChannel(pChannel, pChannelConfig);
|
||||
Logger::setChannel(logger.name(), pChannel);
|
||||
}
|
||||
else if (*it != "name")
|
||||
else if (p != "name")
|
||||
{
|
||||
Logger::setProperty(logger.name(), *it, pConfig->getString(*it));
|
||||
Logger::setProperty(logger.name(), p, pConfig->getString(p));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ const char* LoggingSubsystem::name() const
|
||||
void LoggingSubsystem::initialize(Application& app)
|
||||
{
|
||||
LoggingConfigurator configurator;
|
||||
configurator.configure(&app.config());
|
||||
configurator.configure(app.configPtr());
|
||||
std::string logger = app.config().getString("application.logger", "Application");
|
||||
app.setLogger(Logger::get(logger));
|
||||
}
|
||||
|
||||
@@ -32,9 +32,9 @@ MapConfiguration::~MapConfiguration()
|
||||
|
||||
void MapConfiguration::copyTo(AbstractConfiguration& config)
|
||||
{
|
||||
for (iterator it = _map.begin(); it != _map.end(); ++it)
|
||||
for (const auto& p: _map)
|
||||
{
|
||||
config.setString(it->first, it->second);
|
||||
config.setString(p.first, p.second);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,16 +69,16 @@ void MapConfiguration::enumerate(const std::string& key, Keys& range) const
|
||||
std::string prefix = key;
|
||||
if (!prefix.empty()) prefix += '.';
|
||||
std::string::size_type psize = prefix.size();
|
||||
for (StringMap::const_iterator it = _map.begin(); it != _map.end(); ++it)
|
||||
for (const auto& p: _map)
|
||||
{
|
||||
if (it->first.compare(0, psize, prefix) == 0)
|
||||
if (p.first.compare(0, psize, prefix) == 0)
|
||||
{
|
||||
std::string subKey;
|
||||
std::string::size_type end = it->first.find('.', psize);
|
||||
std::string::size_type end = p.first.find('.', psize);
|
||||
if (end == std::string::npos)
|
||||
subKey = it->first.substr(psize);
|
||||
subKey = p.first.substr(psize);
|
||||
else
|
||||
subKey = it->first.substr(psize, end - psize);
|
||||
subKey = p.first.substr(psize, end - psize);
|
||||
if (keys.find(subKey) == keys.end())
|
||||
{
|
||||
range.push_back(subKey);
|
||||
|
||||
@@ -60,10 +60,10 @@ bool OptionProcessor::process(const std::string& argument, std::string& optionNa
|
||||
|
||||
void OptionProcessor::checkRequired() const
|
||||
{
|
||||
for (OptionSet::Iterator it = _options.begin(); it != _options.end(); ++it)
|
||||
for (const auto& opt: _options)
|
||||
{
|
||||
if (it->required() && _specifiedOptions.find(it->fullName()) == _specifiedOptions.end())
|
||||
throw MissingOptionException(it->fullName());
|
||||
if (opt.required() && _specifiedOptions.find(opt.fullName()) == _specifiedOptions.end())
|
||||
throw MissingOptionException(opt.fullName());
|
||||
}
|
||||
if (!_deferredOption.empty())
|
||||
{
|
||||
|
||||
@@ -65,9 +65,9 @@ void OptionSet::addOption(const Option& option)
|
||||
bool OptionSet::hasOption(const std::string& name, bool matchShort) const
|
||||
{
|
||||
bool found = false;
|
||||
for (Iterator it = _options.begin(); it != _options.end(); ++it)
|
||||
for (const auto& opt: _options)
|
||||
{
|
||||
if ((matchShort && it->matchesShort(name)) || (!matchShort && it->matchesFull(name)))
|
||||
if ((matchShort && opt.matchesShort(name)) || (!matchShort && opt.matchesFull(name)))
|
||||
{
|
||||
if (!found)
|
||||
found = true;
|
||||
@@ -82,19 +82,19 @@ bool OptionSet::hasOption(const std::string& name, bool matchShort) const
|
||||
const Option& OptionSet::getOption(const std::string& name, bool matchShort) const
|
||||
{
|
||||
const Option* pOption = 0;
|
||||
for (Iterator it = _options.begin(); it != _options.end(); ++it)
|
||||
for (const auto& opt: _options)
|
||||
{
|
||||
if ((matchShort && it->matchesShort(name)) || (!matchShort && it->matchesPartial(name)))
|
||||
if ((matchShort && opt.matchesShort(name)) || (!matchShort && opt.matchesPartial(name)))
|
||||
{
|
||||
if (!pOption)
|
||||
{
|
||||
pOption = &*it;
|
||||
if (!matchShort && it->matchesFull(name))
|
||||
pOption = &opt;
|
||||
if (!matchShort && opt.matchesFull(name))
|
||||
break;
|
||||
}
|
||||
else if (!matchShort && it->matchesFull(name))
|
||||
else if (!matchShort && opt.matchesFull(name))
|
||||
{
|
||||
pOption = &*it;
|
||||
pOption = &opt;
|
||||
break;
|
||||
}
|
||||
else throw AmbiguousOptionException(name);
|
||||
|
||||
@@ -78,9 +78,9 @@ void PropertyFileConfiguration::save(std::ostream& ostr) const
|
||||
while (it != ed)
|
||||
{
|
||||
ostr << it->first << ": ";
|
||||
for (std::string::const_iterator its = it->second.begin(); its != it->second.end(); ++its)
|
||||
for (auto ch: it->second)
|
||||
{
|
||||
switch (*its)
|
||||
switch (ch)
|
||||
{
|
||||
case '\t':
|
||||
ostr << "\\t";
|
||||
@@ -98,7 +98,7 @@ void PropertyFileConfiguration::save(std::ostream& ostr) const
|
||||
ostr << "\\\\";
|
||||
break;
|
||||
default:
|
||||
ostr << *its;
|
||||
ostr << ch;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,9 +41,7 @@
|
||||
#include "Poco/UnWindows.h"
|
||||
#include <cstring>
|
||||
#endif
|
||||
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
|
||||
#include "Poco/UnicodeConverter.h"
|
||||
#endif
|
||||
|
||||
|
||||
using Poco::NumberFormatter;
|
||||
@@ -147,21 +145,13 @@ void ServerApplication::ServiceControlHandler(DWORD control)
|
||||
}
|
||||
|
||||
|
||||
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
|
||||
void ServerApplication::ServiceMain(DWORD argc, LPWSTR* argv)
|
||||
#else
|
||||
void ServerApplication::ServiceMain(DWORD argc, LPTSTR* argv)
|
||||
#endif
|
||||
{
|
||||
ServerApplication& app = static_cast<ServerApplication&>(Application::instance());
|
||||
|
||||
app.config().setBool("application.runAsService", true);
|
||||
|
||||
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
|
||||
_serviceStatusHandle = RegisterServiceCtrlHandlerW(L"", ServiceControlHandler);
|
||||
#else
|
||||
_serviceStatusHandle = RegisterServiceCtrlHandlerA("", ServiceControlHandler);
|
||||
#endif
|
||||
if (!_serviceStatusHandle)
|
||||
throw SystemException("cannot register service control handler");
|
||||
|
||||
@@ -176,7 +166,6 @@ void ServerApplication::ServiceMain(DWORD argc, LPTSTR* argv)
|
||||
|
||||
try
|
||||
{
|
||||
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
|
||||
std::vector<std::string> args;
|
||||
for (DWORD i = 0; i < argc; ++i)
|
||||
{
|
||||
@@ -185,9 +174,6 @@ void ServerApplication::ServiceMain(DWORD argc, LPTSTR* argv)
|
||||
args.push_back(arg);
|
||||
}
|
||||
app.init(args);
|
||||
#else
|
||||
app.init(argc, argv);
|
||||
#endif
|
||||
_serviceStatus.dwCurrentState = SERVICE_RUNNING;
|
||||
SetServiceStatus(_serviceStatusHandle, &_serviceStatus);
|
||||
int rc = app.run();
|
||||
@@ -291,7 +277,6 @@ int ServerApplication::run(const std::vector<std::string>& args)
|
||||
}
|
||||
|
||||
|
||||
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
|
||||
int ServerApplication::run(int argc, wchar_t** argv)
|
||||
{
|
||||
if (!hasConsole() && isService())
|
||||
@@ -326,26 +311,16 @@ int ServerApplication::run(int argc, wchar_t** argv)
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
bool ServerApplication::isService()
|
||||
{
|
||||
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
|
||||
SERVICE_TABLE_ENTRYW svcDispatchTable[2];
|
||||
svcDispatchTable[0].lpServiceName = L"";
|
||||
svcDispatchTable[0].lpServiceProc = ServiceMain;
|
||||
svcDispatchTable[1].lpServiceName = NULL;
|
||||
svcDispatchTable[1].lpServiceProc = NULL;
|
||||
return StartServiceCtrlDispatcherW(svcDispatchTable) != 0;
|
||||
#else
|
||||
SERVICE_TABLE_ENTRY svcDispatchTable[2];
|
||||
svcDispatchTable[0].lpServiceName = "";
|
||||
svcDispatchTable[0].lpServiceProc = ServiceMain;
|
||||
svcDispatchTable[1].lpServiceName = NULL;
|
||||
svcDispatchTable[1].lpServiceProc = NULL;
|
||||
return StartServiceCtrlDispatcherA(svcDispatchTable) != 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -497,7 +472,6 @@ int ServerApplication::run(const std::vector<std::string>& args)
|
||||
}
|
||||
|
||||
|
||||
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
|
||||
int ServerApplication::run(int argc, wchar_t** argv)
|
||||
{
|
||||
try
|
||||
@@ -511,7 +485,6 @@ int ServerApplication::run(int argc, wchar_t** argv)
|
||||
}
|
||||
return run();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif // _WIN32_WCE
|
||||
@@ -615,9 +588,9 @@ int ServerApplication::run(int argc, char** argv)
|
||||
int ServerApplication::run(const std::vector<std::string>& args)
|
||||
{
|
||||
bool runAsDaemon = false;
|
||||
for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
|
||||
for (const auto& arg: args)
|
||||
{
|
||||
if (*it == "--daemon")
|
||||
if (arg == "--daemon")
|
||||
{
|
||||
runAsDaemon = true;
|
||||
break;
|
||||
@@ -720,11 +693,11 @@ void ServerApplication::handleDaemon(const std::string& name, const std::string&
|
||||
void ServerApplication::handleUMask(const std::string& name, const std::string& value)
|
||||
{
|
||||
int mask = 0;
|
||||
for (std::string::const_iterator it = value.begin(); it != value.end(); ++it)
|
||||
for (const auto ch: value)
|
||||
{
|
||||
mask *= 8;
|
||||
if (*it >= '0' && *it <= '7')
|
||||
mask += *it - '0';
|
||||
if (*it >= '0' && ch <= '7')
|
||||
mask += ch - '0';
|
||||
else
|
||||
throw Poco::InvalidArgumentException("umask contains non-octal characters", value);
|
||||
}
|
||||
|
||||
@@ -15,9 +15,7 @@
|
||||
#include "Poco/Util/WinRegistryKey.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/Buffer.h"
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
#include "Poco/UnicodeConverter.h"
|
||||
#endif
|
||||
|
||||
|
||||
using Poco::SystemException;
|
||||
@@ -90,17 +88,12 @@ WinRegistryKey::~WinRegistryKey()
|
||||
void WinRegistryKey::setString(const std::string& name, const std::string& value)
|
||||
{
|
||||
open();
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(name, uname);
|
||||
std::wstring uvalue;
|
||||
Poco::UnicodeConverter::toUTF16(value, uvalue);
|
||||
if (RegSetValueExW(_hKey, uname.c_str(), 0, REG_SZ, (CONST BYTE*) uvalue.c_str(), (DWORD) (uvalue.size() + 1)*sizeof(wchar_t)) != ERROR_SUCCESS)
|
||||
handleSetError(name);
|
||||
#else
|
||||
if (RegSetValueExA(_hKey, name.c_str(), 0, REG_SZ, (CONST BYTE*) value.c_str(), (DWORD) value.size() + 1) != ERROR_SUCCESS)
|
||||
handleSetError(name);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -109,7 +102,6 @@ std::string WinRegistryKey::getString(const std::string& name)
|
||||
open();
|
||||
DWORD type;
|
||||
DWORD size;
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(name, uname);
|
||||
if (RegQueryValueExW(_hKey, uname.c_str(), NULL, &type, NULL, &size) != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ && type != REG_LINK))
|
||||
@@ -125,18 +117,6 @@ std::string WinRegistryKey::getString(const std::string& name)
|
||||
Poco::UnicodeConverter::toUTF8(uresult, result);
|
||||
return result;
|
||||
}
|
||||
#else
|
||||
if (RegQueryValueExA(_hKey, name.c_str(), NULL, &type, NULL, &size) != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ && type != REG_LINK))
|
||||
throw NotFoundException(key(name));
|
||||
if (size > 0)
|
||||
{
|
||||
Poco::Buffer<char> buffer(size + 1);
|
||||
RegQueryValueExA(_hKey, name.c_str(), NULL, NULL, (BYTE*) buffer.begin(), &size);
|
||||
buffer[size] = 0;
|
||||
std::string result(buffer.begin());
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
return std::string();
|
||||
}
|
||||
|
||||
@@ -144,17 +124,12 @@ std::string WinRegistryKey::getString(const std::string& name)
|
||||
void WinRegistryKey::setStringExpand(const std::string& name, const std::string& value)
|
||||
{
|
||||
open();
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(name, uname);
|
||||
std::wstring uvalue;
|
||||
Poco::UnicodeConverter::toUTF16(value, uvalue);
|
||||
if (RegSetValueExW(_hKey, uname.c_str(), 0, REG_EXPAND_SZ, (CONST BYTE*) uvalue.c_str(), (DWORD) (uvalue.size() + 1)*sizeof(wchar_t)) != ERROR_SUCCESS)
|
||||
handleSetError(name);
|
||||
#else
|
||||
if (RegSetValueExA(_hKey, name.c_str(), 0, REG_EXPAND_SZ, (CONST BYTE*) value.c_str(), (DWORD) value.size() + 1) != ERROR_SUCCESS)
|
||||
handleSetError(name);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -163,7 +138,6 @@ std::string WinRegistryKey::getStringExpand(const std::string& name)
|
||||
open();
|
||||
DWORD type;
|
||||
DWORD size;
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(name, uname);
|
||||
if (RegQueryValueExW(_hKey, uname.c_str(), NULL, &type, NULL, &size) != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ && type != REG_LINK))
|
||||
@@ -187,22 +161,6 @@ std::string WinRegistryKey::getStringExpand(const std::string& name)
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
#else
|
||||
if (RegQueryValueExA(_hKey, name.c_str(), NULL, &type, NULL, &size) != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ && type != REG_LINK))
|
||||
throw NotFoundException(key(name));
|
||||
if (size > 0)
|
||||
{
|
||||
Poco::Buffer<char> buffer(size + 1);
|
||||
RegQueryValueExA(_hKey, name.c_str(), NULL, NULL, (BYTE*) buffer.begin(), &size);
|
||||
buffer[size] = 0;
|
||||
char temp;
|
||||
DWORD expSize = ExpandEnvironmentStringsA(buffer, &temp, 1);
|
||||
Poco::Buffer<char> expBuffer(expSize);
|
||||
ExpandEnvironmentStringsA(buffer.begin(), expBuffer.begin(), expSize);
|
||||
std::string result(expBuffer.begin());
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
return std::string();
|
||||
}
|
||||
|
||||
@@ -211,15 +169,10 @@ std::string WinRegistryKey::getStringExpand(const std::string& name)
|
||||
void WinRegistryKey::setBinary( const std::string& name, const std::vector<char>& value )
|
||||
{
|
||||
open();
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(name, uname);
|
||||
if (RegSetValueExW(_hKey, uname.c_str(), 0, REG_BINARY, (CONST BYTE*) &value[0], (DWORD) value.size()) != ERROR_SUCCESS)
|
||||
handleSetError(name);
|
||||
#else
|
||||
if (RegSetValueExA(_hKey, name.c_str(), 0, REG_BINARY, (CONST BYTE*) &value[0], (DWORD) value.size()) != ERROR_SUCCESS)
|
||||
handleSetError(name);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -230,7 +183,6 @@ std::vector<char> WinRegistryKey::getBinary( const std::string& name )
|
||||
DWORD size;
|
||||
std::vector<char> result;
|
||||
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(name, uname);
|
||||
if (RegQueryValueExW(_hKey, uname.c_str(), NULL, &type, NULL, &size) != ERROR_SUCCESS || type != REG_BINARY)
|
||||
@@ -240,15 +192,6 @@ std::vector<char> WinRegistryKey::getBinary( const std::string& name )
|
||||
result.resize(size);
|
||||
RegQueryValueExW(_hKey, uname.c_str(), NULL, NULL, (BYTE*) &result[0], &size);
|
||||
}
|
||||
#else
|
||||
if (RegQueryValueExA(_hKey, name.c_str(), NULL, &type, NULL, &size) != ERROR_SUCCESS || type != REG_BINARY)
|
||||
throw NotFoundException(key(name));
|
||||
if (size > 0)
|
||||
{
|
||||
result.resize(size);
|
||||
RegQueryValueExA(_hKey, name.c_str(), NULL, NULL, (BYTE*) &result[0], &size);
|
||||
}
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -257,15 +200,10 @@ void WinRegistryKey::setInt(const std::string& name, int value)
|
||||
{
|
||||
open();
|
||||
DWORD data = value;
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(name, uname);
|
||||
if (RegSetValueExW(_hKey, uname.c_str(), 0, REG_DWORD, (CONST BYTE*) &data, sizeof(data)) != ERROR_SUCCESS)
|
||||
handleSetError(name);
|
||||
#else
|
||||
if (RegSetValueExA(_hKey, name.c_str(), 0, REG_DWORD, (CONST BYTE*) &data, sizeof(data)) != ERROR_SUCCESS)
|
||||
handleSetError(name);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -275,15 +213,10 @@ int WinRegistryKey::getInt(const std::string& name)
|
||||
DWORD type;
|
||||
DWORD data;
|
||||
DWORD size = sizeof(data);
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(name, uname);
|
||||
if (RegQueryValueExW(_hKey, uname.c_str(), NULL, &type, (BYTE*) &data, &size) != ERROR_SUCCESS || (type != REG_DWORD && type != REG_DWORD_BIG_ENDIAN))
|
||||
throw NotFoundException(key(name));
|
||||
#else
|
||||
if (RegQueryValueExA(_hKey, name.c_str(), NULL, &type, (BYTE*) &data, &size) != ERROR_SUCCESS || (type != REG_DWORD && type != REG_DWORD_BIG_ENDIAN))
|
||||
throw NotFoundException(key(name));
|
||||
#endif
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -294,15 +227,10 @@ int WinRegistryKey::getInt(const std::string& name)
|
||||
void WinRegistryKey::setInt64(const std::string& name, Poco::Int64 value)
|
||||
{
|
||||
open();
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(name, uname);
|
||||
if (RegSetValueExW(_hKey, uname.c_str(), 0, REG_QWORD, (CONST BYTE*) &value, sizeof(value)) != ERROR_SUCCESS)
|
||||
handleSetError(name);
|
||||
#else
|
||||
if (RegSetValueExA(_hKey, name.c_str(), 0, REG_QWORD, (CONST BYTE*) &value, sizeof(value)) != ERROR_SUCCESS)
|
||||
handleSetError(name);
|
||||
#endif
|
||||
}
|
||||
|
||||
Poco::Int64 WinRegistryKey::getInt64(const std::string& name)
|
||||
@@ -311,15 +239,10 @@ Poco::Int64 WinRegistryKey::getInt64(const std::string& name)
|
||||
DWORD type;
|
||||
Poco::Int64 data;
|
||||
DWORD size = sizeof(data);
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(name, uname);
|
||||
if (RegQueryValueExW(_hKey, uname.c_str(), NULL, &type, (BYTE*) &data, &size) != ERROR_SUCCESS || type != REG_QWORD)
|
||||
throw NotFoundException(key(name));
|
||||
#else
|
||||
if (RegQueryValueExA(_hKey, name.c_str(), NULL, &type, (BYTE*) &data, &size) != ERROR_SUCCESS || type != REG_QWORD)
|
||||
throw NotFoundException(key(name));
|
||||
#endif
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -330,15 +253,10 @@ Poco::Int64 WinRegistryKey::getInt64(const std::string& name)
|
||||
void WinRegistryKey::deleteValue(const std::string& name)
|
||||
{
|
||||
open();
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(name, uname);
|
||||
if (RegDeleteValueW(_hKey, uname.c_str()) != ERROR_SUCCESS)
|
||||
throw NotFoundException(key(name));
|
||||
#else
|
||||
if (RegDeleteValueA(_hKey, name.c_str()) != ERROR_SUCCESS)
|
||||
throw NotFoundException(key(name));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -347,11 +265,11 @@ void WinRegistryKey::deleteKey()
|
||||
Keys keys;
|
||||
subKeys(keys);
|
||||
close();
|
||||
for (Keys::iterator it = keys.begin(); it != keys.end(); ++it)
|
||||
for (const auto& k: keys)
|
||||
{
|
||||
std::string subKey(_subKey);
|
||||
subKey += "\\";
|
||||
subKey += *it;
|
||||
subKey += k;
|
||||
WinRegistryKey subRegKey(_hRootKey, subKey, _readOnly, _extraSam);
|
||||
subRegKey.deleteKey();
|
||||
}
|
||||
@@ -360,7 +278,6 @@ void WinRegistryKey::deleteKey()
|
||||
// We cannot call it directly as this would prevent the code running on Windows XP 32-bit.
|
||||
// Therefore, if we need to call RegDeleteKeyEx (_extraSam != 0) we need to check for
|
||||
// its existence in ADVAPI32.DLL and call it indirectly.
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring usubKey;
|
||||
Poco::UnicodeConverter::toUTF16(_subKey, usubKey);
|
||||
|
||||
@@ -383,32 +300,12 @@ void WinRegistryKey::deleteKey()
|
||||
#endif
|
||||
if (RegDeleteKeyW(_hRootKey, usubKey.c_str()) != ERROR_SUCCESS)
|
||||
throw NotFoundException(key());
|
||||
#else
|
||||
typedef LONG (WINAPI *RegDeleteKeyExAFunc)(HKEY hKey, const char* lpSubKey, REGSAM samDesired, DWORD Reserved);
|
||||
if (_extraSam != 0)
|
||||
{
|
||||
AutoHandle advAPI32(LoadLibraryA("ADVAPI32.DLL"));
|
||||
if (advAPI32.handle())
|
||||
{
|
||||
RegDeleteKeyExAFunc pRegDeleteKeyExA = reinterpret_cast<RegDeleteKeyExAFunc>(GetProcAddress(advAPI32.handle() , "RegDeleteKeyExA"));
|
||||
if (pRegDeleteKeyExA)
|
||||
{
|
||||
if ((*pRegDeleteKeyExA)(_hRootKey, _subKey.c_str(), _extraSam, 0) != ERROR_SUCCESS)
|
||||
throw NotFoundException(key());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (RegDeleteKey(_hRootKey, _subKey.c_str()) != ERROR_SUCCESS)
|
||||
throw NotFoundException(key());
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool WinRegistryKey::exists()
|
||||
{
|
||||
HKEY hKey;
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring usubKey;
|
||||
Poco::UnicodeConverter::toUTF16(_subKey, usubKey);
|
||||
if (RegOpenKeyExW(_hRootKey, usubKey.c_str(), 0, KEY_READ | _extraSam, &hKey) == ERROR_SUCCESS)
|
||||
@@ -416,13 +313,6 @@ bool WinRegistryKey::exists()
|
||||
RegCloseKey(hKey);
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
if (RegOpenKeyExA(_hRootKey, _subKey.c_str(), 0, KEY_READ | _extraSam, &hKey) == ERROR_SUCCESS)
|
||||
{
|
||||
RegCloseKey(hKey);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -432,15 +322,10 @@ WinRegistryKey::Type WinRegistryKey::type(const std::string& name)
|
||||
open();
|
||||
DWORD type = REG_NONE;
|
||||
DWORD size;
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(name, uname);
|
||||
if (RegQueryValueExW(_hKey, uname.c_str(), NULL, &type, NULL, &size) != ERROR_SUCCESS)
|
||||
throw NotFoundException(key(name));
|
||||
#else
|
||||
if (RegQueryValueExA(_hKey, name.c_str(), NULL, &type, NULL, &size) != ERROR_SUCCESS)
|
||||
throw NotFoundException(key(name));
|
||||
#endif
|
||||
|
||||
Type aType = (Type)type;
|
||||
return aType;
|
||||
@@ -451,7 +336,6 @@ bool WinRegistryKey::exists(const std::string& name)
|
||||
{
|
||||
bool exists = false;
|
||||
HKEY hKey;
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring usubKey;
|
||||
Poco::UnicodeConverter::toUTF16(_subKey, usubKey);
|
||||
if (RegOpenKeyExW(_hRootKey, usubKey.c_str(), 0, KEY_READ | _extraSam, &hKey) == ERROR_SUCCESS)
|
||||
@@ -461,13 +345,6 @@ bool WinRegistryKey::exists(const std::string& name)
|
||||
exists = RegQueryValueExW(hKey, uname.c_str(), NULL, NULL, NULL, NULL) == ERROR_SUCCESS;
|
||||
RegCloseKey(hKey);
|
||||
}
|
||||
#else
|
||||
if (RegOpenKeyExA(_hRootKey, _subKey.c_str(), 0, KEY_READ | _extraSam, &hKey) == ERROR_SUCCESS)
|
||||
{
|
||||
exists = RegQueryValueExA(hKey, name.c_str(), NULL, NULL, NULL, NULL) == ERROR_SUCCESS;
|
||||
RegCloseKey(hKey);
|
||||
}
|
||||
#endif
|
||||
return exists;
|
||||
}
|
||||
|
||||
@@ -476,7 +353,6 @@ void WinRegistryKey::open()
|
||||
{
|
||||
if (!_hKey)
|
||||
{
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring usubKey;
|
||||
Poco::UnicodeConverter::toUTF16(_subKey, usubKey);
|
||||
if (_readOnly)
|
||||
@@ -489,18 +365,6 @@ void WinRegistryKey::open()
|
||||
if (RegCreateKeyExW(_hRootKey, usubKey.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE | _extraSam, NULL, &_hKey, NULL) != ERROR_SUCCESS)
|
||||
throw SystemException("Cannot open registry key: ", key());
|
||||
}
|
||||
#else
|
||||
if (_readOnly)
|
||||
{
|
||||
if (RegOpenKeyExA(_hRootKey, _subKey.c_str(), 0, KEY_READ | _extraSam, &_hKey) != ERROR_SUCCESS)
|
||||
throw NotFoundException("Cannot open registry key: ", key());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (RegCreateKeyExA(_hRootKey, _subKey.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE | _extraSam, NULL, &_hKey, NULL) != ERROR_SUCCESS)
|
||||
throw SystemException("Cannot open registry key: ", key());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -601,7 +465,6 @@ void WinRegistryKey::subKeys(WinRegistryKey::Keys& keys)
|
||||
if (RegQueryInfoKey(_hKey, NULL, NULL, NULL, &subKeyCount, NULL, NULL, &valueCount, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
|
||||
return;
|
||||
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
wchar_t buf[256];
|
||||
DWORD bufSize = sizeof(buf)/sizeof(wchar_t);
|
||||
for (DWORD i = 0; i< subKeyCount; ++i)
|
||||
@@ -615,19 +478,6 @@ void WinRegistryKey::subKeys(WinRegistryKey::Keys& keys)
|
||||
}
|
||||
bufSize = sizeof(buf)/sizeof(wchar_t);
|
||||
}
|
||||
#else
|
||||
char buf[256];
|
||||
DWORD bufSize = sizeof(buf);
|
||||
for (DWORD i = 0; i< subKeyCount; ++i)
|
||||
{
|
||||
if (RegEnumKeyExA(_hKey, i, buf, &bufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
|
||||
{
|
||||
std::string name(buf);
|
||||
keys.push_back(name);
|
||||
}
|
||||
bufSize = sizeof(buf);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -640,7 +490,6 @@ void WinRegistryKey::values(WinRegistryKey::Values& vals)
|
||||
if (RegQueryInfoKey(_hKey, NULL, NULL, NULL, NULL, NULL, NULL, &valueCount, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
|
||||
return ;
|
||||
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
wchar_t buf[256];
|
||||
DWORD bufSize = sizeof(buf)/sizeof(wchar_t);
|
||||
for (DWORD i = 0; i< valueCount; ++i)
|
||||
@@ -654,19 +503,6 @@ void WinRegistryKey::values(WinRegistryKey::Values& vals)
|
||||
}
|
||||
bufSize = sizeof(buf)/sizeof(wchar_t);
|
||||
}
|
||||
#else
|
||||
char buf[256];
|
||||
DWORD bufSize = sizeof(buf);
|
||||
for (DWORD i = 0; i< valueCount; ++i)
|
||||
{
|
||||
if (RegEnumValueA(_hKey, i, buf, &bufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
|
||||
{
|
||||
std::string name(buf);
|
||||
vals.push_back(name);
|
||||
}
|
||||
bufSize = sizeof(buf);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -19,9 +19,7 @@
|
||||
#include "Poco/Util/WinRegistryKey.h"
|
||||
#include "Poco/Thread.h"
|
||||
#include "Poco/Exception.h"
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
#include "Poco/UnicodeConverter.h"
|
||||
#endif
|
||||
|
||||
|
||||
using Poco::Thread;
|
||||
@@ -47,6 +45,7 @@ WinService::WinService(const std::string& name):
|
||||
if (!_scmHandle) throw SystemException("cannot open Service Control Manager");
|
||||
}
|
||||
|
||||
|
||||
WinService::WinService(SC_HANDLE scmHandle, const std::string& name):
|
||||
_scmHandle(scmHandle),
|
||||
_name(name),
|
||||
@@ -55,6 +54,7 @@ WinService::WinService(SC_HANDLE scmHandle, const std::string& name):
|
||||
if (!_scmHandle) throw SystemException("Service Control Manager not connected");
|
||||
}
|
||||
|
||||
|
||||
WinService::~WinService()
|
||||
{
|
||||
close();
|
||||
@@ -71,13 +71,9 @@ const std::string& WinService::name() const
|
||||
std::string WinService::displayName() const
|
||||
{
|
||||
POCO_LPQUERY_SERVICE_CONFIG pSvcConfig = config();
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring udispName(pSvcConfig->lpDisplayName);
|
||||
std::string dispName;
|
||||
Poco::UnicodeConverter::toUTF8(udispName, dispName);
|
||||
#else
|
||||
std::string dispName(pSvcConfig->lpDisplayName);
|
||||
#endif
|
||||
LocalFree(pSvcConfig);
|
||||
return dispName;
|
||||
}
|
||||
@@ -86,13 +82,9 @@ std::string WinService::displayName() const
|
||||
std::string WinService::path() const
|
||||
{
|
||||
POCO_LPQUERY_SERVICE_CONFIG pSvcConfig = config();
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring upath(pSvcConfig->lpBinaryPathName);
|
||||
std::string path;
|
||||
UnicodeConverter::toUTF8(upath, path);
|
||||
#else
|
||||
std::string path(pSvcConfig->lpBinaryPathName);
|
||||
#endif
|
||||
LocalFree(pSvcConfig);
|
||||
return path;
|
||||
}
|
||||
@@ -101,7 +93,6 @@ std::string WinService::path() const
|
||||
void WinService::registerService(const std::string& path, const std::string& displayName)
|
||||
{
|
||||
close();
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(_name, uname);
|
||||
std::wstring udisplayName;
|
||||
@@ -118,18 +109,6 @@ void WinService::registerService(const std::string& path, const std::string& dis
|
||||
SERVICE_ERROR_NORMAL,
|
||||
upath.c_str(),
|
||||
NULL, NULL, NULL, NULL, NULL);
|
||||
#else
|
||||
_svcHandle = CreateServiceA(
|
||||
_scmHandle,
|
||||
_name.c_str(),
|
||||
displayName.c_str(),
|
||||
SERVICE_ALL_ACCESS,
|
||||
SERVICE_WIN32_OWN_PROCESS,
|
||||
SERVICE_DEMAND_START,
|
||||
SERVICE_ERROR_NORMAL,
|
||||
path.c_str(),
|
||||
NULL, NULL, NULL, NULL, NULL);
|
||||
#endif
|
||||
if (!_svcHandle)
|
||||
throw SystemException("cannot register service", _name);
|
||||
}
|
||||
@@ -259,7 +238,6 @@ void WinService::setFailureActions(FailureActionVector failureActions, const std
|
||||
{
|
||||
open();
|
||||
auto actions = new SC_ACTION[3];
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
SERVICE_FAILURE_ACTIONSW ac;
|
||||
|
||||
std::wstring urebootMessage;
|
||||
@@ -271,15 +249,7 @@ void WinService::setFailureActions(FailureActionVector failureActions, const std
|
||||
Poco::UnicodeConverter::toUTF16(command, uComamnd);
|
||||
std::vector<wchar_t> commandVector{ uComamnd.begin(), uComamnd.end() };
|
||||
commandVector.push_back('\0');
|
||||
#else
|
||||
SERVICE_FAILURE_ACTIONSA ac;
|
||||
|
||||
std::vector<char> rebootMessageVector{ rebootMessage.begin(), rebootMessage.end() };
|
||||
rebootMessageVector.push_back('\0');
|
||||
|
||||
std::vector<char> commandVector{ command.begin(), command.end() };
|
||||
commandVector.push_back('\0');
|
||||
#endif
|
||||
for (auto i = 0; i < 3; i++)
|
||||
{
|
||||
switch (failureActions[i].type)
|
||||
@@ -287,11 +257,7 @@ void WinService::setFailureActions(FailureActionVector failureActions, const std
|
||||
case SVC_REBOOT:
|
||||
actions[i].Type = SC_ACTION_REBOOT;
|
||||
actions[i].Delay = failureActions[i].delay;
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
ac.lpRebootMsg = &rebootMessageVector[0];
|
||||
#else
|
||||
ac.lpRebootMsg = &rebootMessageVector[0];
|
||||
#endif
|
||||
break;
|
||||
case SVC_RESTART:
|
||||
actions[i].Type = SC_ACTION_RESTART;
|
||||
@@ -300,11 +266,7 @@ void WinService::setFailureActions(FailureActionVector failureActions, const std
|
||||
case SVC_RUN_COMMAND:
|
||||
actions[i].Type = SC_ACTION_RUN_COMMAND;
|
||||
actions[i].Delay = failureActions[i].delay;
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
ac.lpCommand = &commandVector[0];
|
||||
#else
|
||||
ac.lpCommand = &commandVector[0];
|
||||
#endif
|
||||
break;
|
||||
default:
|
||||
actions[i].Type = SC_ACTION_NONE;
|
||||
@@ -317,11 +279,7 @@ void WinService::setFailureActions(FailureActionVector failureActions, const std
|
||||
ac.cActions = 3;
|
||||
ac.lpsaActions = actions;
|
||||
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
if (!ChangeServiceConfig2W(_svcHandle, SERVICE_CONFIG_FAILURE_ACTIONS, &ac))
|
||||
#else
|
||||
if (!ChangeServiceConfig2A(_svcHandle, SERVICE_CONFIG_FAILURE_ACTIONS, &ac))
|
||||
#endif
|
||||
{
|
||||
delete[] actions;
|
||||
throw SystemException("cannot configure service", _name);
|
||||
@@ -336,11 +294,7 @@ WinService::FailureActionTypeVector WinService::getFailureActions() const {
|
||||
POCO_LPSERVICE_FAILURE_ACTION pSvcFailureAction = (POCO_LPSERVICE_FAILURE_ACTION)LocalAlloc(LPTR, size);
|
||||
if (!pSvcFailureAction) throw OutOfMemoryException("cannot allocate service failure action buffer");
|
||||
try {
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
while (!QueryServiceConfig2W(_svcHandle, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)pSvcFailureAction, size, &bytesNeeded))
|
||||
#else
|
||||
while (!QueryServiceConfig2A(_svcHandle, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)pSvcFailureAction, size, &bytesNeeded))
|
||||
#endif
|
||||
{
|
||||
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
||||
LocalFree(pSvcFailureAction);
|
||||
@@ -411,13 +365,9 @@ bool WinService::tryOpen() const
|
||||
{
|
||||
if (!_svcHandle)
|
||||
{
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
std::wstring uname;
|
||||
Poco::UnicodeConverter::toUTF16(_name, uname);
|
||||
_svcHandle = OpenServiceW(_scmHandle, uname.c_str(), SERVICE_ALL_ACCESS);
|
||||
#else
|
||||
_svcHandle = OpenServiceA(_scmHandle, _name.c_str(), SERVICE_ALL_ACCESS);
|
||||
#endif
|
||||
}
|
||||
return _svcHandle != 0;
|
||||
}
|
||||
@@ -442,11 +392,7 @@ POCO_LPQUERY_SERVICE_CONFIG WinService::config() const
|
||||
if (!pSvcConfig) throw OutOfMemoryException("cannot allocate service config buffer");
|
||||
try
|
||||
{
|
||||
#if defined(POCO_WIN32_UTF8)
|
||||
while (!QueryServiceConfigW(_svcHandle, pSvcConfig, size, &bytesNeeded))
|
||||
#else
|
||||
while (!QueryServiceConfigA(_svcHandle, pSvcConfig, size, &bytesNeeded))
|
||||
#endif
|
||||
{
|
||||
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user