Skip to content

Commit

Permalink
core: Fix compilation and tests on 32bit architectures (#20)
Browse files Browse the repository at this point in the history
* Fix compilation on 32-bit architectures

The 1234L suffix creates a 'long', which is not 64-bit on 32-bit architectures.

* Use stoll instead of stol to fix colors on 32-bit systems

on 32 bit systems, 'long' is 32 bits and 'long long' is 64 bits,
so the 'long long' functions need to be used.

* Fix rgba and rgb values on 32-bit

* Use a cast to Hyprlang::INT
  • Loading branch information
earboxer authored Feb 12, 2024
1 parent 78c1656 commit 573cf83
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
12 changes: 6 additions & 6 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ static std::expected<int64_t, std::string> configStringToInt(const std::string&
if (VALUE.starts_with("0x")) {
// Values with 0x are hex
const auto VALUEWITHOUTHEX = VALUE.substr(2);
return stol(VALUEWITHOUTHEX, nullptr, 16);
return stoll(VALUEWITHOUTHEX, nullptr, 16);
} else if (VALUE.starts_with("rgba(") && VALUE.ends_with(')')) {
const auto VALUEWITHOUTFUNC = removeBeginEndSpacesTabs(VALUE.substr(5, VALUE.length() - 6));

Expand All @@ -197,9 +197,9 @@ static std::expected<int64_t, std::string> configStringToInt(const std::string&
if (!r.has_value() || !g.has_value() || !b.has_value())
return std::unexpected("failed parsing " + VALUEWITHOUTFUNC);

return a * 0x1000000L + r.value() * 0x10000L + g.value() * 0x100L + b.value();
return a * (Hyprlang::INT)0x1000000 + r.value() * (Hyprlang::INT)0x10000 + g.value() * (Hyprlang::INT)0x100 + b.value();
} else if (VALUEWITHOUTFUNC.length() == 8) {
const auto RGBA = std::stol(VALUEWITHOUTFUNC, nullptr, 16);
const auto RGBA = std::stoll(VALUEWITHOUTFUNC, nullptr, 16);

// now we need to RGBA -> ARGB. The config holds ARGB only.
return (RGBA >> 8) + 0x1000000 * (RGBA & 0xFF);
Expand All @@ -223,9 +223,9 @@ static std::expected<int64_t, std::string> configStringToInt(const std::string&
if (!r.has_value() || !g.has_value() || !b.has_value())
return std::unexpected("failed parsing " + VALUEWITHOUTFUNC);

return 0xFF000000L + r.value() * 0x10000L + g.value() * 0x100L + b.value();
return (Hyprlang::INT)0xFF000000 + r.value() * (Hyprlang::INT)0x10000 + g.value() * (Hyprlang::INT)0x100 + b.value();
} else if (VALUEWITHOUTFUNC.length() == 6) {
const auto RGB = std::stol(VALUEWITHOUTFUNC, nullptr, 16);
const auto RGB = std::stoll(VALUEWITHOUTFUNC, nullptr, 16);

return RGB + 0xFF000000;
}
Expand Down Expand Up @@ -655,4 +655,4 @@ bool CConfig::specialCategoryExistsForKey(const char* category, const char* key)
}

return false;
}
}
4 changes: 2 additions & 2 deletions tests/fuzz/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ std::string garbage() {
int main(int argc, char** argv, char** envp) {

Hyprlang::CConfig config("./eeeeeeeUnused", {.allowMissingConfig = true});
config.addConfigValue("test", {0L});
config.addConfigValue("test", {(Hyprlang::INT)0});

config.parseDynamic("");
config.parseDynamic("", "");
Expand All @@ -39,4 +39,4 @@ int main(int argc, char** argv, char** envp) {
std::cout << "Success, no fuzzing errors\n";

return 0;
}
}
62 changes: 31 additions & 31 deletions tests/parse/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,41 +81,41 @@ int main(int argc, char** argv, char** envp) {
currentPath = std::filesystem::canonical("./config/");

// setup config
config.addConfigValue("testInt", 0L);
config.addConfigValue("testInt", (Hyprlang::INT)0);
config.addConfigValue("testFloat", 0.F);
config.addConfigValue("testVec", Hyprlang::SVector2D{69, 420});
config.addConfigValue("testString", "");
config.addConfigValue("testEnv", "");
config.addConfigValue("testVar", 0L);
config.addConfigValue("testVar", (Hyprlang::INT)0);
config.addConfigValue("testStringQuotes", "");
config.addConfigValue("testCategory:testValueInt", 0L);
config.addConfigValue("testCategory:testValueHex", 0xAL);
config.addConfigValue("testCategory:nested1:testValueNest", 0L);
config.addConfigValue("testCategory:nested1:nested2:testValueNest", 0L);
config.addConfigValue("testDefault", 123L);
config.addConfigValue("testCategory:testColor1", 0L);
config.addConfigValue("testCategory:testColor2", 0L);
config.addConfigValue("testCategory:testColor3", 0L);
config.addConfigValue("myColors:pink", 0L);
config.addConfigValue("myColors:green", 0L);
config.addConfigValue("myColors:random", 0L);
config.addConfigValue("testCategory:testValueInt", (Hyprlang::INT)0);
config.addConfigValue("testCategory:testValueHex", (Hyprlang::INT)0xA);
config.addConfigValue("testCategory:nested1:testValueNest", (Hyprlang::INT)0);
config.addConfigValue("testCategory:nested1:nested2:testValueNest", (Hyprlang::INT)0);
config.addConfigValue("testDefault", (Hyprlang::INT)123);
config.addConfigValue("testCategory:testColor1", (Hyprlang::INT)0);
config.addConfigValue("testCategory:testColor2", (Hyprlang::INT)0);
config.addConfigValue("testCategory:testColor3", (Hyprlang::INT)0);
config.addConfigValue("myColors:pink", (Hyprlang::INT)0);
config.addConfigValue("myColors:green", (Hyprlang::INT)0);
config.addConfigValue("myColors:random", (Hyprlang::INT)0);
config.addConfigValue("customType", {Hyprlang::CConfigCustomValueType{&handleCustomValueSet, &handleCustomValueDestroy, "def"}});

config.registerHandler(&handleDoABarrelRoll, "doABarrelRoll", {false});
config.registerHandler(&handleFlagsTest, "flags", {true});
config.registerHandler(&handleSource, "source", {false});

config.addSpecialCategory("special", {"key"});
config.addSpecialConfigValue("special", "value", 0L);
config.addSpecialConfigValue("special", "value", (Hyprlang::INT)0);

config.commence();

config.addSpecialCategory("specialGeneric:one", {nullptr, true});
config.addSpecialConfigValue("specialGeneric:one", "value", 0L);
config.addSpecialConfigValue("specialGeneric:one", "value", (Hyprlang::INT)0);
config.addSpecialCategory("specialGeneric:two", {nullptr, true});
config.addSpecialConfigValue("specialGeneric:two", "value", 0L);
config.addSpecialConfigValue("specialGeneric:two", "value", (Hyprlang::INT)0);

const Hyprlang::CConfigValue copyTest = {1L};
const Hyprlang::CConfigValue copyTest = {(Hyprlang::INT)1};
config.addSpecialConfigValue("specialGeneric:one", "copyTest", copyTest);

const auto PARSERESULT = config.parse();
Expand All @@ -134,14 +134,14 @@ int main(int argc, char** argv, char** envp) {
EXPECT(std::any_cast<Hyprlang::SVector2D>(config.getConfigValue("testVec")), EXP);
EXPECT(std::any_cast<const char*>(config.getConfigValue("testString")), std::string{"Hello World! # This is not a comment!"});
EXPECT(std::any_cast<const char*>(config.getConfigValue("testStringQuotes")), std::string{"\"Hello World!\""});
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testValueInt")), 123456L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testValueHex")), 0xFFFFAABBL);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:nested1:testValueNest")), 1L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:nested1:nested2:testValueNest")), 1L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testDefault")), 123L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testColor1")), 0xFFFFFFFFL);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testColor2")), 0xFF000000L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testColor3")), 0x22ffeeffL);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testValueInt")), (Hyprlang::INT)123456);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testValueHex")), (Hyprlang::INT)0xFFFFAABB);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:nested1:testValueNest")), (Hyprlang::INT)1);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:nested1:nested2:testValueNest")), (Hyprlang::INT)1);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testDefault")), (Hyprlang::INT)123);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testColor1")), (Hyprlang::INT)0xFFFFFFFF);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testColor2")), (Hyprlang::INT)0xFF000000);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testColor3")), (Hyprlang::INT)0x22ffeeff);

// test static values
std::cout << " → Testing static values\n";
Expand All @@ -161,7 +161,7 @@ int main(int argc, char** argv, char** envp) {
EXPECT(config.parseDynamic("doABarrelRoll = woohoo, some, params").error, false);
EXPECT(barrelRoll, true);
EXPECT(config.parseDynamic("testCategory:testValueHex", "0xaabbccdd").error, false);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testValueHex")), 0xAABBCCDDL);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testValueHex")), (Hyprlang::INT)0xAABBCCDD);

// test variables
std::cout << " → Testing variables\n";
Expand All @@ -188,13 +188,13 @@ int main(int argc, char** argv, char** envp) {

// test sourcing
std::cout << " → Testing sourcing\n";
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:pink")), 0xFFc800c8L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:green")), 0xFF14f014L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:random")), 0xFFFF1337L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:pink")), (Hyprlang::INT)0xFFc800c8);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:green")), (Hyprlang::INT)0xFF14f014);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:random")), (Hyprlang::INT)0xFFFF1337);

// test custom type
std::cout << " → Testing custom types\n";
EXPECT(*reinterpret_cast<int64_t*>(std::any_cast<void*>(config.getConfigValue("customType"))), 1L);
EXPECT(*reinterpret_cast<int64_t*>(std::any_cast<void*>(config.getConfigValue("customType"))), (Hyprlang::INT)1);

std::cout << " → Testing error.conf\n";
Hyprlang::CConfig errorConfig("./config/error.conf", {.verifyOnly = true, .throwAllErrors = true});
Expand All @@ -212,4 +212,4 @@ int main(int argc, char** argv, char** envp) {
}

return ret;
}
}

0 comments on commit 573cf83

Please sign in to comment.