fix: memory issues & duplicate file loads.
This commit is contained in:
@@ -207,8 +207,8 @@ auto waybar::Client::setupCss(const std::string& css_file) -> void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
css_provider_ = Gtk::CssProvider::create();
|
css_provider_ = Gtk::CssProvider::create();
|
||||||
if (has_8bit_hex(css_file)) {
|
auto [modified_css, was_transformed] = transform_8bit_to_hex(css_file);
|
||||||
std::string modified_css = transform_8bit_to_hex(css_file);
|
if (was_transformed) {
|
||||||
css_provider_->load_from_data(modified_css);
|
css_provider_->load_from_data(modified_css);
|
||||||
} else {
|
} else {
|
||||||
if (!css_provider_->load_from_path(css_file)) {
|
if (!css_provider_->load_from_path(css_file)) {
|
||||||
|
|||||||
@@ -4,21 +4,40 @@
|
|||||||
#include <regex>
|
#include <regex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
std::string transform_8bit_to_hex(std::string file_path) {
|
|
||||||
|
struct TransformResult {
|
||||||
|
std::string css;
|
||||||
|
bool was_transformed;
|
||||||
|
};
|
||||||
|
|
||||||
|
TransformResult transform_8bit_to_hex(const std::string& file_path) {
|
||||||
std::ifstream f(file_path, std::ios::in | std::ios::binary);
|
std::ifstream f(file_path, std::ios::in | std::ios::binary);
|
||||||
const auto size = fs::file_size(file_path);
|
const auto size = fs::file_size(file_path);
|
||||||
std::string result(size, '\0');
|
std::string result(size, '\0');
|
||||||
|
if (!f.is_open() || !f.good()) {
|
||||||
|
throw std::runtime_error("Cannot open file: " + file_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size == 0) {
|
||||||
|
return {.css = result, .was_transformed = false};
|
||||||
|
}
|
||||||
|
|
||||||
f.read(result.data(), size);
|
f.read(result.data(), size);
|
||||||
|
|
||||||
std::regex pattern(
|
static std::regex pattern(
|
||||||
R"((?:\#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})))");
|
R"((\#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})))");
|
||||||
std::string final_output;
|
std::string final_output;
|
||||||
|
|
||||||
auto it = std::sregex_iterator(result.begin(), result.end(), pattern);
|
auto it = std::sregex_iterator(result.begin(), result.end(), pattern);
|
||||||
auto eof = std::sregex_iterator();
|
auto eof = std::sregex_iterator();
|
||||||
std::smatch match = *it;
|
|
||||||
|
|
||||||
|
if (it == eof) {
|
||||||
|
return {.css = result, .was_transformed = false};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::smatch match;
|
||||||
while (it != eof) {
|
while (it != eof) {
|
||||||
|
match = *it;
|
||||||
|
|
||||||
final_output += match.prefix().str();
|
final_output += match.prefix().str();
|
||||||
|
|
||||||
@@ -28,8 +47,8 @@ std::string transform_8bit_to_hex(std::string file_path) {
|
|||||||
double a = (stoi(match[4].str(), nullptr, 16) / 255.0);
|
double a = (stoi(match[4].str(), nullptr, 16) / 255.0);
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "rgba(" << r << "," << g << "," << b << "," << std::fixed
|
ss << "rgba(" << r << "," << g << "," << b << "," << std::fixed << std::setprecision(2) << a
|
||||||
<< std::setprecision(2) << a << ")";
|
<< ")";
|
||||||
final_output += ss.str();
|
final_output += ss.str();
|
||||||
|
|
||||||
++it;
|
++it;
|
||||||
@@ -37,5 +56,5 @@ std::string transform_8bit_to_hex(std::string file_path) {
|
|||||||
|
|
||||||
final_output += match.suffix().str();
|
final_output += match.suffix().str();
|
||||||
|
|
||||||
return final_output;
|
return {.css = final_output, .was_transformed = true};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user