headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / boot / loader / PathBlacklist.cpp
blob82822ccf864582aae62e1bb8db8c6f7562a31e36
1 /*
2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <boot/PathBlacklist.h>
9 #include <stdlib.h>
11 #include <algorithm>
14 // #pragma mark - BlacklistedPath
17 BlacklistedPath::BlacklistedPath()
19 fPath(NULL),
20 fLength(0),
21 fCapacity(0)
26 BlacklistedPath::~BlacklistedPath()
28 free(fPath);
32 bool
33 BlacklistedPath::SetTo(const char* path)
35 size_t length = strlen(path);
36 if (length > 0 && path[length - 1] == '/')
37 length--;
39 if (!_Resize(length, false))
40 return false;
42 if (length > 0) {
43 memcpy(fPath, path, length);
44 fPath[length] = '\0';
47 return true;
51 bool
52 BlacklistedPath::Append(const char* component)
54 size_t componentLength = strlen(component);
55 if (componentLength > 0 && component[componentLength - 1] == '/')
56 componentLength--;
57 if (componentLength == 0)
58 return true;
60 size_t oldLength = fLength;
61 size_t length = (fLength > 0 ? fLength + 1 : 0) + componentLength;
62 if (!_Resize(length, true))
63 return false;
65 if (oldLength > 0)
66 fPath[oldLength++] = '/';
67 memcpy(fPath + oldLength, component, componentLength);
68 return true;
72 bool
73 BlacklistedPath::_Resize(size_t length, bool keepData)
75 if (length == 0) {
76 free(fPath);
77 fPath = NULL;
78 fLength = 0;
79 fCapacity = 0;
80 return true;
83 if (length < fCapacity) {
84 fPath[length] = '\0';
85 fLength = length;
86 return true;
89 size_t capacity = std::max(length + 1, 2 * fCapacity);
90 capacity = std::max(capacity, size_t(32));
92 char* path;
93 if (fLength > 0 && keepData) {
94 path = (char*)realloc(fPath, capacity);
95 if (path == NULL)
96 return false;
97 } else {
98 path = (char*)malloc(capacity);
99 if (path == NULL)
100 return false;
101 free(fPath);
104 fPath = path;
105 fPath[length] = '\0';
106 fLength = length;
107 fCapacity = capacity;
108 return true;
112 // #pragma mark - PathBlacklist
115 PathBlacklist::PathBlacklist()
120 PathBlacklist::~PathBlacklist()
122 MakeEmpty();
126 bool
127 PathBlacklist::Add(const char* path)
129 BlacklistedPath* blacklistedPath = _FindPath(path);
130 if (blacklistedPath != NULL)
131 return true;
133 blacklistedPath = new(std::nothrow) BlacklistedPath;
134 if (blacklistedPath == NULL || !blacklistedPath->SetTo(path)) {
135 delete blacklistedPath;
136 return false;
139 fPaths.Add(blacklistedPath);
140 return true;
144 void
145 PathBlacklist::Remove(const char* path)
147 BlacklistedPath* blacklistedPath = _FindPath(path);
148 if (blacklistedPath != NULL) {
149 fPaths.Remove(blacklistedPath);
150 delete blacklistedPath;
155 bool
156 PathBlacklist::Contains(const char* path) const
158 return _FindPath(path) != NULL;
162 void
163 PathBlacklist::MakeEmpty()
165 while (BlacklistedPath* blacklistedPath = fPaths.RemoveHead())
166 delete blacklistedPath;
170 BlacklistedPath*
171 PathBlacklist::_FindPath(const char* path) const
173 for (PathList::Iterator it = fPaths.GetIterator(); it.HasNext();) {
174 BlacklistedPath* blacklistedPath = it.Next();
175 if (*blacklistedPath == path)
176 return blacklistedPath;
179 return NULL;