headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / boot / loader / file_systems / packagefs / PackageSettingsItem.h
blob3a0b6bbba98c7f524f28c87db7bb4a34223daba3
1 /*
2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef BOOT_LOADER_FILE_SYSTEMS_PACKAGEFS_PACKAGE_SETTINGS_ITEM_H
6 #define BOOT_LOADER_FILE_SYSTEMS_PACKAGEFS_PACKAGE_SETTINGS_ITEM_H
9 #include <string.h>
11 #include <util/OpenHashTable.h>
12 #include <util/StringHash.h>
15 struct driver_parameter;
16 class Directory;
19 namespace PackageFS {
22 class PackageSettingsItem {
23 public:
24 class Entry {
25 public:
26 Entry(Entry* parent)
28 fParent(parent),
29 fName(NULL),
30 fIsBlackListed(false),
31 fHashNext(NULL)
35 ~Entry()
37 free(fName);
40 bool SetName(const char* name, size_t nameLength)
42 fName = (char*)malloc(nameLength + 1);
43 if (fName == NULL)
44 return false;
46 memcpy(fName, name, nameLength);
47 fName[nameLength] = '\0';
48 return true;
51 Entry* Parent() const
53 return fParent;
56 const char* Name() const
58 return fName;
61 bool IsBlackListed() const
63 return fIsBlackListed;
66 void SetBlackListed(bool blackListed)
68 fIsBlackListed = blackListed;
71 Entry*& HashNext()
73 return fHashNext;
76 private:
77 Entry* fParent;
78 char* fName;
79 bool fIsBlackListed;
80 Entry* fHashNext;
83 class EntryKey {
84 public:
85 EntryKey(Entry* parent, const char* name, size_t nameLength)
87 fParent(parent),
88 fName(name),
89 fNameLength(nameLength)
93 EntryKey(Entry* parent, const char* name)
95 fParent(parent),
96 fName(name),
97 fNameLength(strlen(name))
101 Entry* Parent() const
103 return fParent;
106 const char* Name() const
108 return fName;
111 size_t NameLength() const
113 return fNameLength;
116 size_t Hash() const
118 return (addr_t)fParent / 8
119 ^ hash_hash_string_part(fName, fNameLength);
122 private:
123 Entry* fParent;
124 const char* fName;
125 size_t fNameLength;
128 public:
129 PackageSettingsItem();
130 ~PackageSettingsItem();
132 static PackageSettingsItem* Load(::Directory* systemDirectory,
133 const char* name);
135 status_t Init(const driver_parameter& parameter);
137 void AddEntry(Entry* entry);
138 status_t AddEntry(const char* path, Entry*& _entry);
139 Entry* FindEntry(Entry* parent, const char* name)
140 const;
141 Entry* FindEntry(Entry* parent, const char* name,
142 size_t nameLength) const;
144 PackageSettingsItem*& HashNext()
145 { return fHashNext; }
147 private:
148 struct EntryHashDefinition {
149 typedef EntryKey KeyType;
150 typedef Entry ValueType;
152 size_t HashKey(const EntryKey& key) const
154 return key.Hash();
157 size_t Hash(const Entry* value) const
159 return HashKey(EntryKey(value->Parent(), value->Name()));
162 bool Compare(const EntryKey& key, const Entry* value) const
164 const char* name = value->Name();
165 return key.Parent() == value->Parent()
166 && strncmp(key.Name(), name, key.NameLength()) == 0
167 && name[key.NameLength()] == '\0';
170 Entry*& GetLink(Entry* value) const
172 return value->HashNext();
176 typedef BOpenHashTable<EntryHashDefinition> EntryTable;
178 private:
179 status_t _AddBlackListedEntries(
180 const driver_parameter& parameter);
182 private:
183 EntryTable fEntries;
184 PackageSettingsItem* fHashNext;
188 } // namespace PackageFS
191 #endif // BOOT_LOADER_FILE_SYSTEMS_PACKAGEFS_PACKAGE_SETTINGS_ITEM_H