api: add listKeysForSpecialCategory

This commit is contained in:
Vaxry 2024-02-17 19:01:35 +00:00
parent aeb3e012ad
commit 7b7e7cdd07
3 changed files with 64 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include <memory>
#include <string>
#include <fstream>
#include <vector>
class CConfigImpl;
struct SConfigDefaultValue;
@ -381,6 +382,31 @@ namespace Hyprlang {
*/
bool specialCategoryExistsForKey(const char* category, const char* key);
/*!
Get a vector with all registered keys for a special category
It's an error to query this for a static or non-existent category
\since 0.4.0
*/
std::vector<std::string> listKeysForSpecialCategory(const char* category) {
const char** cats = nullptr;
size_t len = 0;
retrieveKeysForCat(category, &cats, &len);
if (len == 0)
return {};
std::vector<std::string> result;
for (size_t i = 0; i < len; ++i) {
result.push_back(cats[i]);
}
free(cats);
return result;
}
private:
bool m_bCommenced = false;
@ -391,6 +417,7 @@ namespace Hyprlang {
CParseResult parseVariable(const std::string& lhs, const std::string& rhs, bool dynamic = false);
void clearState();
void applyDefaultsToCat(SSpecialCategory& cat);
void retrieveKeysForCat(const char* category, const char*** out, size_t* len);
};
};
#endif

View File

@ -661,3 +661,37 @@ bool CConfig::specialCategoryExistsForKey(const char* category, const char* key)
return false;
}
/* if len != 0, out needs to be freed */
void CConfig::retrieveKeysForCat(const char* category, const char*** out, size_t* len) {
size_t count = 0;
for (auto& sc : impl->specialCategories) {
if (sc->isStatic)
continue;
if (sc->name != category)
continue;
count++;
}
if (count == 0) {
*len = 0;
return;
}
*out = (const char**)calloc(1, count * sizeof(const char*));
size_t counter2 = 0;
for (auto& sc : impl->specialCategories) {
if (sc->isStatic)
continue;
if (sc->name != category)
continue;
// EVIL, but the pointers will be almost instantly discarded by the caller
(*out)[counter2++] = (const char*)sc->values[sc->key].m_pData;
}
*len = count;
}

View File

@ -191,6 +191,9 @@ int main(int argc, char** argv, char** envp) {
// test copying
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("specialGeneric:one", "copyTest")), 2);
// test listing keys
EXPECT(config.listKeysForSpecialCategory("special")[1], "b");
// test sourcing
std::cout << " → Testing sourcing\n";
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:pink")), (Hyprlang::INT)0xFFc800c8);