-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathServerApplicationsTree.cpp
More file actions
153 lines (118 loc) · 3.21 KB
/
ServerApplicationsTree.cpp
File metadata and controls
153 lines (118 loc) · 3.21 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "ServerApplicationsTree.h"
namespace HttpServer
{
ServerApplicationsTree::ServerApplicationsTree() noexcept : app_sets(nullptr)
{
}
ServerApplicationsTree::~ServerApplicationsTree() noexcept {
this->clear();
}
bool ServerApplicationsTree::empty() const noexcept {
return this->list.empty();
}
void ServerApplicationsTree::addApplication(const std::string &name, ServerApplicationSettings *sets)
{
std::vector<std::string> name_parts;
size_t delimiter = name.find('.');
if (std::string::npos != delimiter)
{
size_t cur_pos = 0;
while (std::string::npos != delimiter)
{
std::string part = name.substr(cur_pos, delimiter - cur_pos);
if (part.empty() ) {
part = "*";
}
name_parts.emplace_back(std::move(part) );
cur_pos = delimiter + 1;
delimiter = name.find('.', cur_pos);
}
// Emplace last part
std::string part = name.substr(cur_pos);
name_parts.emplace_back(std::move(part) );
} else {
name_parts.emplace_back(name);
}
this->addApplication(name_parts, sets);
}
void ServerApplicationsTree::addApplication(std::vector<std::string> &nameParts, ServerApplicationSettings *sets)
{
if (nameParts.empty() ) {
this->app_sets = sets;
} else {
std::string &part = nameParts.back();
auto it = this->list.find(part);
ServerApplicationsTree *sub;
if (this->list.cend() != it) {
sub = it->second;
} else {
sub = new ServerApplicationsTree();
this->list.emplace(std::move(part), sub);
}
nameParts.pop_back();
sub->addApplication(nameParts, sets);
}
}
const ServerApplicationSettings *ServerApplicationsTree::find(const std::string &name) const
{
std::vector<std::string> name_parts;
size_t delimiter = name.find('.');
if (std::string::npos != delimiter)
{
size_t cur_pos = 0;
while (std::string::npos != delimiter)
{
std::string part = name.substr(cur_pos, delimiter - cur_pos);
name_parts.emplace_back(std::move(part) );
cur_pos = delimiter + 1;
delimiter = name.find('.', cur_pos);
}
std::string part = name.substr(cur_pos);
name_parts.emplace_back(std::move(part) );
} else {
name_parts.emplace_back(name);
}
return this->find(name_parts);
}
const ServerApplicationSettings *ServerApplicationsTree::find(std::vector<std::string> &nameParts) const
{
if (nameParts.empty() ) {
return this->app_sets;
} else {
const std::string part = std::move(nameParts.back() );
nameParts.pop_back();
auto it = this->list.find(part);
if (this->list.cend() == it) {
it = this->list.find("*");
if (this->list.end() != it) {
return this->app_sets;
}
} else {
return it->second->find(nameParts);
}
return nullptr;
}
}
void ServerApplicationsTree::collectApplicationSettings(std::unordered_set<ServerApplicationSettings *> &set) const
{
for (auto &node : this->list)
{
const ServerApplicationsTree *tree = node.second;
if (nullptr != tree->app_sets) {
set.emplace(tree->app_sets);
}
tree->collectApplicationSettings(set);
}
}
void ServerApplicationsTree::clear() noexcept
{
if (this->list.empty() == false)
{
for (auto &it : this->list) {
delete it.second;
}
this->list.clear();
}
}
}