Repository: hyprwm/Hyprland
File: src/helpers/MiscFunctions.cpp (C++)

Code before:
        return false;
    }

    rw_fd_ptr = std::move(fd);
    ro_fd_ptr = std::move(ro_fd);
    return true;
}

float stringToPercentage(const std::string& VALUE, const float REL) {
    if (VALUE.ends_with('%'))
        return (std::stof(VALUE.substr(0, VALUE.length() - 1)) * REL) / 100.f;
    else
        return std::stof(VALUE);
}

// Checks if Nvidia driver major version is at least given version.
// Useful for explicit_sync_kms and ctm_animation as they only work
// past certain driver versions.
bool isNvidiaDriverVersionAtLeast(int threshold) {
    static int  driverMajor = 0;
    static bool once        = true;

    if (once) {
        once = false;

        if (std::filesystem::exists("/sys/module/nvidia_drm/version")) {
            std::ifstream ifs("/sys/module/nvidia_drm/version");
            if (ifs.good()) {
                try {
                    std::string driverInfo((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));

                    size_t      firstDot = driverInfo.find('.');
                    if (firstDot != std::string::npos) {
                        driverMajor = std::stoi(driverInfo.substr(0, firstDot));
                    }

                    Debug::log(LOG, "Parsed NVIDIA major version: {}", driverMajor);

                } catch (std::exception& e) {
                    driverMajor = 0; // Default to 0 if parsing fails
                }

                ifs.close();
            }
        }
    }

    return driverMajor >= threshold;
}


Review comment (bug):
while we are redoing this, this can throw.

```
std::error_code ec;
if (std::filesystem::exists(..., ec) && !ec)
```

Code after:
        return false;
    }

    rw_fd_ptr = std::move(fd);
    ro_fd_ptr = std::move(ro_fd);
    return true;
}

float stringToPercentage(const std::string& VALUE, const float REL) {
    if (VALUE.ends_with('%'))
        return (std::stof(VALUE.substr(0, VALUE.length() - 1)) * REL) / 100.f;
    else
        return std::stof(VALUE);
}

// Checks if Nvidia driver major version is at least given version.
// Useful for explicit_sync_kms and ctm_animation as they only work
// past certain driver versions.
bool isNvidiaDriverVersionAtLeast(int threshold) {
    static int  driverMajor = 0;
    static bool once        = true;

    if (once) {
        once = false;

        std::error_code ec;
        if (std::filesystem::exists("/sys/module/nvidia_drm/version", ec) && !ec) {
            std::ifstream ifs("/sys/module/nvidia_drm/version");
            if (ifs.good()) {
                try {
                    std::string driverInfo((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));

                    size_t      firstDot = driverInfo.find('.');
                    if (firstDot != std::string::npos)
                        driverMajor = std::stoi(driverInfo.substr(0, firstDot));

                    Debug::log(LOG, "Parsed NVIDIA major version: {}", driverMajor);

                } catch (std::exception& e) {
                    driverMajor = 0; // Default to 0 if parsing fails
                }

                ifs.close();
            }
        }
    }

    return driverMajor >= threshold;
}