From 3af0e9606049c89444201f0000d03f8c7553352b Mon Sep 17 00:00:00 2001 From: LHY0125 <3364451258@qq.com> Date: Thu, 26 Mar 2026 13:20:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E6=9C=AA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E5=B8=83=E5=B1=80=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=B9=B6=E5=A2=9E=E5=BC=BA=E6=97=A5=E5=BF=97=E4=B8=8E=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除未使用的 layout_config.h 头文件及其引用 - 在 main.c 和 callbacks.c 中添加日志记录以追踪程序启动、关闭和关键操作 - 将多处 strncpy 调用替换为安全的 safe_strcpy 函数 - 在 dialogs.c 中引入 safe_string.h 以使用安全字符串函数 --- README.md | 1 - include/core/layout_config.h | 19 ------------------- src/controller/callbacks.c | 22 +++++++++++++++++----- src/main.c | 9 +++++++++ src/ui/dialogs.c | 4 ++-- 5 files changed, 28 insertions(+), 27 deletions(-) delete mode 100644 include/core/layout_config.h diff --git a/README.md b/README.md index 8e77541..4004784 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,6 @@ * 统一错误码系统 (`utils/error_code.h`) * 安全字符串函数 (`utils/safe_string.h`) * 日志系统 (`utils/logger.h`) - * 布局配置结构体 (`core/layout_config.h`) ## 📦 下载与安装 diff --git a/include/core/layout_config.h b/include/core/layout_config.h deleted file mode 100644 index 3a7df3c..0000000 --- a/include/core/layout_config.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef LAYOUT_CONFIG_H -#define LAYOUT_CONFIG_H - -// 布局配置结构体 -typedef struct { - int vbox_gap; // 垂直布局间距 - int vbox_margin_width; // 垂直布局外边距宽度 - int vbox_margin_height; // 垂直布局外边距高度 - int hbox_gap; // 水平布局间距 - int hbox_margin_width; // 水平布局外边距宽度 - int hbox_margin_height; // 水平布局外边距高度 - int button_width; // 按钮宽度 - int button_height; // 按钮高度 -} LayoutConfig; - -// 默认布局配置 -extern const LayoutConfig DEFAULT_LAYOUT; - -#endif // LAYOUT_CONFIG_H \ No newline at end of file diff --git a/src/controller/callbacks.c b/src/controller/callbacks.c index 2584bae..e0d2e07 100644 --- a/src/controller/callbacks.c +++ b/src/controller/callbacks.c @@ -7,6 +7,8 @@ #include "utils/string_ext.h" #include "utils/os_env.h" #include "utils/error_code.h" +#include "utils/safe_string.h" +#include "utils/logger.h" #include "ui/ui_utils.h" #include "ui/dialogs.h" #include @@ -86,8 +88,7 @@ int btn_edit_cb(Ihandle *self) return IUP_DEFAULT; char buffer[4096]; - strncpy(buffer, raw_data->items[selected - 1], 4096); - buffer[4095] = '\0'; + safe_strcpy(buffer, sizeof(buffer), raw_data->items[selected - 1]); if (custom_input_dialog("编辑环境变量", "编辑路径:", buffer, sizeof(buffer))) { @@ -331,6 +332,7 @@ int btn_ok_cb(Ihandle *self) if (sys_ok == ERR_OK && user_ok == ERR_OK) { + log_info("Saved system paths: %d, user paths: %d", ctx->sys_paths.count, ctx->user_paths.count); SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)L"Environment", SMTO_ABORTIFHUNG, 5000, NULL); IupMessage("成功", "系统和用户 PATH 环境变量均已更新!"); if (lbl_status) @@ -346,6 +348,7 @@ int btn_ok_cb(Ihandle *self) } else { + log_error("Failed to save paths: sys=%d, user=%d", sys_ok, user_ok); IupMessage("错误", "保存失败!"); if (lbl_status) IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "error")); @@ -495,8 +498,7 @@ int btn_export_cb(Ihandle *self) } else { - strncpy(final_path, filepath, sizeof(final_path) - 1); - final_path[sizeof(final_path) - 1] = '\0'; + safe_strcpy(final_path, sizeof(final_path), filepath); } filepath = final_path; @@ -529,9 +531,19 @@ void load_all_paths(void) if (load_system_paths(&ctx->sys_paths) != ERR_OK) { + log_error("Failed to load system paths"); IupMessage("错误", "无法打开系统环境变量注册表键,请尝试以管理员身份运行。"); } - load_user_paths(&ctx->user_paths); + else + { + log_info("Loaded system paths: %d", ctx->sys_paths.count); + } + + ErrorCode user_result = load_user_paths(&ctx->user_paths); + if (user_result == ERR_OK) + { + log_info("Loaded user paths: %d", ctx->user_paths.count); + } Ihandle *list_sys = IupGetDialogChild(dlg, "LIST_SYS"); Ihandle *list_user = IupGetDialogChild(dlg, "LIST_USER"); diff --git a/src/main.c b/src/main.c index 8556ba6..510d106 100644 --- a/src/main.c +++ b/src/main.c @@ -7,6 +7,7 @@ #include "core/lua_config.h" #include "utils/string_ext.h" #include "utils/os_env.h" +#include "utils/logger.h" #include "controller/callbacks.h" #include "ui/main_window.h" @@ -33,6 +34,10 @@ build\\PathEditor.exe // 主函数 int main(int argc, char **argv) { + // 初始化日志系统 + log_init(NULL, LOG_LEVEL_INFO); + log_info("PathEditor starting..."); + // 强制设置 UTF8MODE 环境变量,必须在 IupOpen 之前 putenv("IUP_UTF8MODE=YES"); @@ -44,6 +49,8 @@ int main(int argc, char **argv) IupMessage("警告", "Lua 配置系统初始化失败,将使用默认值"); } + log_info("Lua config initialized"); + // 在管理员模式下,解决无法拖拽文件到列表框的问题 (UIPI) // 需要加载 User32.dll 获取 ChangeWindowMessageFilter 函数 HMODULE hUser32 = LoadLibraryW(L"user32.dll"); @@ -132,8 +139,10 @@ int main(int argc, char **argv) IupMainLoop(); + log_info("PathEditor exiting..."); destroy_app_context(ctx); lua_config_destroy(); + log_destroy(); IupClose(); return 0; diff --git a/src/ui/dialogs.c b/src/ui/dialogs.c index 30b80ec..5846345 100644 --- a/src/ui/dialogs.c +++ b/src/ui/dialogs.c @@ -1,5 +1,6 @@ #include "ui/dialogs.h" #include "core/lua_config.h" +#include "utils/safe_string.h" #include #include @@ -61,8 +62,7 @@ int custom_input_dialog(const char *title, const char *label_text, char *buffer, char *val = IupGetAttribute(text, "VALUE"); if (val) { - strncpy(buffer, val, buffer_size); - buffer[buffer_size - 1] = '\0'; + safe_strcpy(buffer, buffer_size, val); } }