2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include <boot/PathBlacklist.h>
14 // #pragma mark - BlacklistedPath
17 BlacklistedPath::BlacklistedPath()
26 BlacklistedPath::~BlacklistedPath()
33 BlacklistedPath::SetTo(const char* path
)
35 size_t length
= strlen(path
);
36 if (length
> 0 && path
[length
- 1] == '/')
39 if (!_Resize(length
, false))
43 memcpy(fPath
, path
, length
);
52 BlacklistedPath::Append(const char* component
)
54 size_t componentLength
= strlen(component
);
55 if (componentLength
> 0 && component
[componentLength
- 1] == '/')
57 if (componentLength
== 0)
60 size_t oldLength
= fLength
;
61 size_t length
= (fLength
> 0 ? fLength
+ 1 : 0) + componentLength
;
62 if (!_Resize(length
, true))
66 fPath
[oldLength
++] = '/';
67 memcpy(fPath
+ oldLength
, component
, componentLength
);
73 BlacklistedPath::_Resize(size_t length
, bool keepData
)
83 if (length
< fCapacity
) {
89 size_t capacity
= std::max(length
+ 1, 2 * fCapacity
);
90 capacity
= std::max(capacity
, size_t(32));
93 if (fLength
> 0 && keepData
) {
94 path
= (char*)realloc(fPath
, capacity
);
98 path
= (char*)malloc(capacity
);
105 fPath
[length
] = '\0';
107 fCapacity
= capacity
;
112 // #pragma mark - PathBlacklist
115 PathBlacklist::PathBlacklist()
120 PathBlacklist::~PathBlacklist()
127 PathBlacklist::Add(const char* path
)
129 BlacklistedPath
* blacklistedPath
= _FindPath(path
);
130 if (blacklistedPath
!= NULL
)
133 blacklistedPath
= new(std::nothrow
) BlacklistedPath
;
134 if (blacklistedPath
== NULL
|| !blacklistedPath
->SetTo(path
)) {
135 delete blacklistedPath
;
139 fPaths
.Add(blacklistedPath
);
145 PathBlacklist::Remove(const char* path
)
147 BlacklistedPath
* blacklistedPath
= _FindPath(path
);
148 if (blacklistedPath
!= NULL
) {
149 fPaths
.Remove(blacklistedPath
);
150 delete blacklistedPath
;
156 PathBlacklist::Contains(const char* path
) const
158 return _FindPath(path
) != NULL
;
163 PathBlacklist::MakeEmpty()
165 while (BlacklistedPath
* blacklistedPath
= fPaths
.RemoveHead())
166 delete 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
;