1//
2// FilesystemConfiguration.cpp
3//
4// Library: Util
5// Package: Configuration
6// Module: FilesystemConfiguration
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/FilesystemConfiguration.h"
16#include "Poco/File.h"
17#include "Poco/Path.h"
18#include "Poco/DirectoryIterator.h"
19#include "Poco/StringTokenizer.h"
20#include "Poco/FileStream.h"
21
22
23using Poco::Path;
24using Poco::File;
25using Poco::DirectoryIterator;
26using Poco::StringTokenizer;
27
28
29namespace Poco {
30namespace Util {
31
32
33FilesystemConfiguration::FilesystemConfiguration(const std::string& path):
34 _path(path)
35{
36 _path.makeDirectory();
37}
38
39
40FilesystemConfiguration::~FilesystemConfiguration()
41{
42}
43
44
45void FilesystemConfiguration::clear()
46{
47 File regDir(_path);
48 regDir.remove(true);
49}
50
51
52bool FilesystemConfiguration::getRaw(const std::string& key, std::string& value) const
53{
54 Path p(keyToPath(key));
55 p.setFileName("data");
56 File f(p);
57 if (f.exists())
58 {
59 value.reserve((std::string::size_type) f.getSize());
60 Poco::FileInputStream istr(p.toString());
61 int c = istr.get();
62 while (c != std::char_traits<char>::eof())
63 {
64 value += (char) c;
65 c = istr.get();
66 }
67 return true;
68 }
69 else return false;
70}
71
72
73void FilesystemConfiguration::setRaw(const std::string& key, const std::string& value)
74{
75 Path p(keyToPath(key));
76 File dir(p);
77 dir.createDirectories();
78 p.setFileName("data");
79 Poco::FileOutputStream ostr(p.toString());
80 ostr.write(value.data(), (std::streamsize) value.length());
81}
82
83
84void FilesystemConfiguration::enumerate(const std::string& key, Keys& range) const
85{
86 Path p(keyToPath(key));
87 File dir(p);
88 if (!dir.exists())
89 {
90 return;
91 }
92
93 DirectoryIterator it(p);
94 DirectoryIterator end;
95 while (it != end)
96 {
97 if (it->isDirectory())
98 range.push_back(it.name());
99 ++it;
100 }
101}
102
103
104void FilesystemConfiguration::removeRaw(const std::string& key)
105{
106 Path p(keyToPath(key));
107 File dir(p);
108 if (dir.exists())
109 {
110 dir.remove(true);
111 }
112}
113
114
115Path FilesystemConfiguration::keyToPath(const std::string& key) const
116{
117 Path result(_path);
118 StringTokenizer tokenizer(key, ".", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
119 for (StringTokenizer::Iterator it = tokenizer.begin(); it != tokenizer.end(); ++it)
120 {
121 result.pushDirectory(*it);
122 }
123 return result;
124}
125
126
127} } // namespace Poco::Util
128