6 Star 1 Fork 17

src-openEuler/kiran-cc-daemon

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0027-feature-systeminfo-hardware-use-glibmm-s-regex-to-re.patch 3.60 KB
一键复制 编辑 原始数据 按行查看 历史
From 66efa416ca17d10589df7eb6aab8a09a1ec3739e Mon Sep 17 00:00:00 2001
From: yangfeng <yangfeng@kylinsec.com.cn>
Date: Mon, 9 Sep 2024 11:23:53 +0800
Subject: [PATCH 27/27] feature(systeminfo-hardware):use glibmm's regex to
replace glib's regex
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 使用glibmm正则
---
plugins/systeminfo/systeminfo-hardware.cpp | 49 +++++++++-------------
1 file changed, 19 insertions(+), 30 deletions(-)
diff --git a/plugins/systeminfo/systeminfo-hardware.cpp b/plugins/systeminfo/systeminfo-hardware.cpp
index e494e52..a47a8ea 100644
--- a/plugins/systeminfo/systeminfo-hardware.cpp
+++ b/plugins/systeminfo/systeminfo-hardware.cpp
@@ -15,7 +15,7 @@
#include "plugins/systeminfo/systeminfo-hardware.h"
#include <gio/gunixinputstream.h>
-#include <glib.h>
+#include <glibmm/regex.h>
#include <glibtop/mem.h>
#include <gudev/gudev.h>
#include <cinttypes>
@@ -253,45 +253,34 @@ DiskInfoVec SystemInfoHardware::get_disks_info()
return disks_info;
}
- auto lines = StrUtils::split_lines(cmd_output);
- GRegex* pattern = g_regex_new(R"(NAME=\"([^\"]*)\" TYPE=\"([^\"]*)\" SIZE=\"(\d+)\" MODEL=\"([^\"]*)\" VENDOR=\"([^\"]*)\")", G_REGEX_MULTILINE, (GRegexMatchFlags)0, NULL);
-
- for (auto& line : lines)
+ auto lines = Glib::Regex::split_simple("\n", cmd_output);
+ Glib::RefPtr<Glib::Regex> regex = Glib::Regex::create(R"(NAME=\"([^\"]*)\" TYPE=\"([^\"]*)\" SIZE=\"(\d+)\" MODEL=\"([^\"]*)\" VENDOR=\"([^\"]*)\")");
+ for (const auto& line : lines)
{
- GMatchInfo* match_info;
- if (g_regex_match(pattern, line.c_str(), (GRegexMatchFlags)0, &match_info))
+ Glib::MatchInfo match_info;
+ if (regex->match(line, match_info))
{
- const gchar* name = g_match_info_fetch(match_info, 1);
- const gchar* type = g_match_info_fetch(match_info, 2);
- const gchar* size = g_match_info_fetch(match_info, 3);
- const gchar* model = g_match_info_fetch(match_info, 4);
- const gchar* vendor = g_match_info_fetch(match_info, 5);
-
- if (strcmp(type, "disk") == 0 &&
- (name && strlen(name) > 0) &&
- (size && strlen(size) > 0) &&
- ((model && strlen(model) > 0) || (vendor && strlen(vendor) > 0)))
+ std::string name = match_info.fetch(1);
+ std::string type = match_info.fetch(2);
+ std::string size = match_info.fetch(3);
+ std::string model = match_info.fetch(4);
+ std::string vendor = match_info.fetch(5);
+
+ if (type == "disk" &&
+ !name.empty() &&
+ !size.empty() &&
+ (!model.empty() || !vendor.empty()))
{
DiskInfo disk_info;
disk_info.name = name;
- disk_info.size = atoll(size);
- disk_info.model = (strlen(model) ? model : name);
- disk_info.vendor = (strlen(vendor) ? vendor : name);
+ disk_info.size = atoll(size.c_str());
+ disk_info.model = !model.empty() ? model : name;
+ disk_info.vendor = !vendor.empty() ? vendor : name;
disks_info.push_back(disk_info);
}
-
- if (name) g_free((gchar*)name);
- if (type) g_free((gchar*)type);
- if (size) g_free((gchar*)size);
- if (model) g_free((gchar*)model);
- if (vendor) g_free((gchar*)vendor);
-
- g_match_info_free(match_info);
}
}
- g_regex_unref(pattern);
-
return disks_info;
}
--
2.27.0
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/src-openeuler/kiran-cc-daemon.git
git@gitee.com:src-openeuler/kiran-cc-daemon.git
src-openeuler
kiran-cc-daemon
kiran-cc-daemon
master

搜索帮助