mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-05-10 10:19:47 +08:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9a78b88c4a | |||
| 3af0e96060 | |||
| 6ba7e702f2 | |||
| 9aa1e208ba | |||
| d934d21323 | |||
| 8767271e96 | |||
| 55ff64b92d |
@@ -14,6 +14,8 @@ set(SOURCES
|
|||||||
src/main.c
|
src/main.c
|
||||||
src/utils/string_ext.c
|
src/utils/string_ext.c
|
||||||
src/utils/os_env.c
|
src/utils/os_env.c
|
||||||
|
src/utils/safe_string.c
|
||||||
|
src/utils/logger.c
|
||||||
src/ui/ui_utils.c
|
src/ui/ui_utils.c
|
||||||
src/ui/dialogs.c
|
src/ui/dialogs.c
|
||||||
src/ui/main_window.c
|
src/ui/main_window.c
|
||||||
|
|||||||
@@ -24,6 +24,11 @@
|
|||||||
* **实时搜索**:顶部搜索框支持不区分大小写的实时过滤查找。
|
* **实时搜索**:顶部搜索框支持不区分大小写的实时过滤查找。
|
||||||
* **快捷键**:支持 Delete 键快速删除选中项。
|
* **快捷键**:支持 Delete 键快速删除选中项。
|
||||||
|
|
||||||
|
* **🔄 导入导出**:
|
||||||
|
* **导出备份**:将 PATH 导出为 JSON 文件,方便备份和迁移。
|
||||||
|
* **导入恢复**:从 JSON 文件导入路径配置。
|
||||||
|
* **格式兼容**:支持旧版 TXT 格式导入。
|
||||||
|
|
||||||
* **便捷管理**:
|
* **便捷管理**:
|
||||||
* ➕ **新建**:添加新路径到列表。
|
* ➕ **新建**:添加新路径到列表。
|
||||||
* 📂 **浏览**:直接从文件资源管理器选择目录添加。
|
* 📂 **浏览**:直接从文件资源管理器选择目录添加。
|
||||||
@@ -33,7 +38,7 @@
|
|||||||
|
|
||||||
* **轻量级**:原生 C 语言编写,无臃肿依赖,运行速度极快。
|
* **轻量级**:原生 C 语言编写,无臃肿依赖,运行速度极快。
|
||||||
|
|
||||||
## 🏗️ 架构与二次开发
|
## 🛠️ 架构与二次开发
|
||||||
|
|
||||||
本项目注重代码的模块化和可维护性,采用了经典的 **MVC 分层架构**,非常适合作为 C 语言桌面程序开发的参考:
|
本项目注重代码的模块化和可维护性,采用了经典的 **MVC 分层架构**,非常适合作为 C 语言桌面程序开发的参考:
|
||||||
|
|
||||||
@@ -42,8 +47,12 @@
|
|||||||
* `src/ui/` (View): 负责界面布局与组件的纯视觉展示。
|
* `src/ui/` (View): 负责界面布局与组件的纯视觉展示。
|
||||||
* `src/controller/` (Controller): 负责连接用户交互与底层数据。
|
* `src/controller/` (Controller): 负责连接用户交互与底层数据。
|
||||||
* `src/utils/` (Utils): 纯粹的底层工具类封装(系统级调用、字符串处理)。
|
* `src/utils/` (Utils): 纯粹的底层工具类封装(系统级调用、字符串处理)。
|
||||||
* **统一配置中心**:所有的 UI 尺寸、间距、颜色等常量配置均提取在 `include/config.h` 中,只需修改宏定义即可轻松定制属于你的专属界面风格。
|
* **热配置系统**:所有 UI 参数(窗口大小、按钮文本、布局间距等)均通过 `lua/config.lua` 配置,修改无需重新编译即可生效。
|
||||||
* **清晰的应用状态**:摒弃了脆弱的全局变量模式,采用 `AppContext` 统一管理应用运行时的上下文状态,通过指针传递,安全可靠。
|
* **清晰的应用状态**:摒弃了脆弱的全局变量模式,采用 `AppContext` 统一管理应用运行时的上下文状态,通过指针传递,安全可靠。
|
||||||
|
* **开发工具库**:
|
||||||
|
* 统一错误码系统 (`utils/error_code.h`)
|
||||||
|
* 安全字符串函数 (`utils/safe_string.h`)
|
||||||
|
* 日志系统 (`utils/logger.h`)
|
||||||
|
|
||||||
## 📦 下载与安装
|
## 📦 下载与安装
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,19 @@
|
|||||||
#define IMPORT_EXPORT_H
|
#define IMPORT_EXPORT_H
|
||||||
|
|
||||||
#include "utils/string_ext.h"
|
#include "utils/string_ext.h"
|
||||||
|
#include "utils/error_code.h"
|
||||||
|
|
||||||
#define EXPORT_VERSION "1.0"
|
#define EXPORT_VERSION "1.0"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
StringList system;
|
||||||
|
StringList user;
|
||||||
|
} ExportData;
|
||||||
|
|
||||||
// 导出 PATH 到文件
|
// 导出 PATH 到文件
|
||||||
int export_paths_to_file(const StringList *list, const char *filepath, int is_system);
|
ErrorCode export_paths_to_file(const ExportData *data, const char *filepath);
|
||||||
|
|
||||||
// 从文件导入 PATH
|
// 从文件导入 PATH
|
||||||
int import_paths_from_file(const char *filepath, StringList *list);
|
ErrorCode import_paths_from_file(const char *filepath, ExportData *data);
|
||||||
|
|
||||||
#endif // IMPORT_EXPORT_H
|
#endif // IMPORT_EXPORT_H
|
||||||
@@ -2,18 +2,18 @@
|
|||||||
#define PATH_MANAGER_H
|
#define PATH_MANAGER_H
|
||||||
|
|
||||||
#include "utils/string_ext.h"
|
#include "utils/string_ext.h"
|
||||||
|
#include "utils/error_code.h"
|
||||||
|
|
||||||
// 移除列表中指定索引的项
|
// 移除列表中指定索引的项
|
||||||
void path_manager_remove_at(StringList *list, int index);
|
ErrorCode path_manager_remove_at(StringList *list, int index);
|
||||||
|
|
||||||
// 上移指定索引的项
|
// 上移指定索引的项
|
||||||
void path_manager_move_up(StringList *list, int index);
|
ErrorCode path_manager_move_up(StringList *list, int index);
|
||||||
|
|
||||||
// 下移指定索引的项
|
// 下移指定索引的项
|
||||||
void path_manager_move_down(StringList *list, int index);
|
ErrorCode path_manager_move_down(StringList *list, int index);
|
||||||
|
|
||||||
// 清理无效和重复的路径
|
// 清理无效和重复的路径
|
||||||
// 返回被清理的项数
|
ErrorCode path_manager_clean(StringList *list);
|
||||||
int path_manager_clean(StringList *list);
|
|
||||||
|
|
||||||
#endif // PATH_MANAGER_H
|
#endif // PATH_MANAGER_H
|
||||||
|
|||||||
@@ -2,13 +2,14 @@
|
|||||||
#define REGISTRY_SERVICE_H
|
#define REGISTRY_SERVICE_H
|
||||||
|
|
||||||
#include "utils/string_ext.h"
|
#include "utils/string_ext.h"
|
||||||
|
#include "utils/error_code.h"
|
||||||
|
|
||||||
// 加载系统变量和用户变量到字符串列表
|
// 加载系统变量和用户变量到字符串列表
|
||||||
int load_system_paths(StringList *list);
|
ErrorCode load_system_paths(StringList *list);
|
||||||
int load_user_paths(StringList *list);
|
ErrorCode load_user_paths(StringList *list);
|
||||||
|
|
||||||
// 从字符串列表保存系统变量和用户变量
|
// 从字符串列表保存系统变量和用户变量
|
||||||
int save_system_paths(const StringList *list);
|
ErrorCode save_system_paths(const StringList *list);
|
||||||
int save_user_paths(const StringList *list);
|
ErrorCode save_user_paths(const StringList *list);
|
||||||
|
|
||||||
#endif // REGISTRY_SERVICE_H
|
#endif // REGISTRY_SERVICE_H
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef ERROR_CODE_H
|
||||||
|
#define ERROR_CODE_H
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ERR_OK = 0, // 成功
|
||||||
|
ERR_FAILED = -1, // 失败
|
||||||
|
ERR_NULL_PTR = -2, // 空指针
|
||||||
|
ERR_OUT_OF_MEMORY = -3, // 内存不足
|
||||||
|
ERR_FILE_NOT_FOUND = -4, // 文件不存在
|
||||||
|
ERR_PERMISSION_DENIED = -5, // 权限拒绝
|
||||||
|
ERR_INVALID_FORMAT = -6, // 无效格式
|
||||||
|
ERR_REGISTRY_FAILED = -7, // 注册表操作失败
|
||||||
|
ERR_NOT_FOUND = -8, // 未找到
|
||||||
|
ERR_EXISTS = -9 // 已存在
|
||||||
|
} ErrorCode;
|
||||||
|
|
||||||
|
const char* error_code_to_string(ErrorCode code);
|
||||||
|
|
||||||
|
#endif // ERROR_CODE_H
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef LOGGER_H
|
||||||
|
#define LOGGER_H
|
||||||
|
|
||||||
|
// 日志级别
|
||||||
|
typedef enum {
|
||||||
|
LOG_LEVEL_DEBUG, // 调试日志级别
|
||||||
|
LOG_LEVEL_INFO, // 信息日志级别
|
||||||
|
LOG_LEVEL_WARN, // 警告日志级别
|
||||||
|
LOG_LEVEL_ERROR // 错误日志级别
|
||||||
|
} LogLevel;
|
||||||
|
|
||||||
|
// 初始化日志系统
|
||||||
|
void log_init(const char *log_file, LogLevel level);
|
||||||
|
|
||||||
|
// 销毁日志系统
|
||||||
|
void log_destroy(void);
|
||||||
|
|
||||||
|
// 日志函数
|
||||||
|
void log_debug(const char *fmt, ...);
|
||||||
|
|
||||||
|
// 信息日志函数
|
||||||
|
void log_info(const char *fmt, ...);
|
||||||
|
|
||||||
|
// 警告日志函数
|
||||||
|
void log_warn(const char *fmt, ...);
|
||||||
|
|
||||||
|
// 错误日志函数
|
||||||
|
void log_error(const char *fmt, ...);
|
||||||
|
|
||||||
|
// 设置日志级别
|
||||||
|
void log_set_level(LogLevel level);
|
||||||
|
|
||||||
|
#endif // LOGGER_H
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef SAFE_STRING_H
|
||||||
|
#define SAFE_STRING_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
// 安全字符串操作函数
|
||||||
|
char* safe_strcpy(char *dst, size_t dst_size, const char *src);
|
||||||
|
|
||||||
|
// 安全字符串拼接函数
|
||||||
|
char* safe_strcat(char *dst, size_t dst_size, const char *src);
|
||||||
|
|
||||||
|
// 安全字符串复制函数
|
||||||
|
char* safe_strdup(const char *src);
|
||||||
|
|
||||||
|
#endif // SAFE_STRING_H
|
||||||
+117
-31
@@ -6,6 +6,9 @@
|
|||||||
#include "core/import_export.h"
|
#include "core/import_export.h"
|
||||||
#include "utils/string_ext.h"
|
#include "utils/string_ext.h"
|
||||||
#include "utils/os_env.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/ui_utils.h"
|
||||||
#include "ui/dialogs.h"
|
#include "ui/dialogs.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -85,8 +88,7 @@ int btn_edit_cb(Ihandle *self)
|
|||||||
return IUP_DEFAULT;
|
return IUP_DEFAULT;
|
||||||
|
|
||||||
char buffer[4096];
|
char buffer[4096];
|
||||||
strncpy(buffer, raw_data->items[selected - 1], 4096);
|
safe_strcpy(buffer, sizeof(buffer), raw_data->items[selected - 1]);
|
||||||
buffer[4095] = '\0';
|
|
||||||
|
|
||||||
if (custom_input_dialog("编辑环境变量", "编辑路径:", buffer, sizeof(buffer)))
|
if (custom_input_dialog("编辑环境变量", "编辑路径:", buffer, sizeof(buffer)))
|
||||||
{
|
{
|
||||||
@@ -156,7 +158,11 @@ int btn_del_cb(Ihandle *self)
|
|||||||
}
|
}
|
||||||
|
|
||||||
StringList *raw_data = get_current_raw_data(dlg);
|
StringList *raw_data = get_current_raw_data(dlg);
|
||||||
path_manager_remove_at(raw_data, selected - 1);
|
ErrorCode result = path_manager_remove_at(raw_data, selected - 1);
|
||||||
|
if (result != ERR_OK)
|
||||||
|
{
|
||||||
|
log_error("Failed to remove path at index %d", selected - 1);
|
||||||
|
}
|
||||||
|
|
||||||
sync_string_list_to_ui(current_list, raw_data);
|
sync_string_list_to_ui(current_list, raw_data);
|
||||||
|
|
||||||
@@ -177,7 +183,11 @@ int btn_up_cb(Ihandle *self)
|
|||||||
return IUP_DEFAULT;
|
return IUP_DEFAULT;
|
||||||
|
|
||||||
StringList *raw_data = get_current_raw_data(dlg);
|
StringList *raw_data = get_current_raw_data(dlg);
|
||||||
path_manager_move_up(raw_data, selected - 1);
|
ErrorCode result = path_manager_move_up(raw_data, selected - 1);
|
||||||
|
if (result != ERR_OK)
|
||||||
|
{
|
||||||
|
log_error("Failed to move path up at index %d", selected - 1);
|
||||||
|
}
|
||||||
|
|
||||||
sync_string_list_to_ui(current_list, raw_data);
|
sync_string_list_to_ui(current_list, raw_data);
|
||||||
IupSetInt(current_list, "VALUE", selected - 1);
|
IupSetInt(current_list, "VALUE", selected - 1);
|
||||||
@@ -196,7 +206,11 @@ int btn_down_cb(Ihandle *self)
|
|||||||
if (selected == 0 || selected >= raw_data->count)
|
if (selected == 0 || selected >= raw_data->count)
|
||||||
return IUP_DEFAULT;
|
return IUP_DEFAULT;
|
||||||
|
|
||||||
path_manager_move_down(raw_data, selected - 1);
|
ErrorCode result = path_manager_move_down(raw_data, selected - 1);
|
||||||
|
if (result != ERR_OK)
|
||||||
|
{
|
||||||
|
log_error("Failed to move path down at index %d", selected - 1);
|
||||||
|
}
|
||||||
|
|
||||||
sync_string_list_to_ui(current_list, raw_data);
|
sync_string_list_to_ui(current_list, raw_data);
|
||||||
IupSetInt(current_list, "VALUE", selected + 1);
|
IupSetInt(current_list, "VALUE", selected + 1);
|
||||||
@@ -217,7 +231,9 @@ int btn_clean_cb(Ihandle *self)
|
|||||||
return IUP_DEFAULT;
|
return IUP_DEFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
int removed = path_manager_clean(raw_data);
|
int before_count = raw_data->count;
|
||||||
|
path_manager_clean(raw_data);
|
||||||
|
int removed = before_count - raw_data->count;
|
||||||
|
|
||||||
Ihandle *current_list = get_current_list(dlg);
|
Ihandle *current_list = get_current_list(dlg);
|
||||||
sync_string_list_to_ui(current_list, raw_data);
|
sync_string_list_to_ui(current_list, raw_data);
|
||||||
@@ -323,28 +339,30 @@ int btn_ok_cb(Ihandle *self)
|
|||||||
|
|
||||||
backup_registry();
|
backup_registry();
|
||||||
|
|
||||||
int sys_ok = save_system_paths(&ctx->sys_paths);
|
ErrorCode sys_ok = save_system_paths(&ctx->sys_paths);
|
||||||
int user_ok = save_user_paths(&ctx->user_paths);
|
ErrorCode user_ok = save_user_paths(&ctx->user_paths);
|
||||||
|
|
||||||
Ihandle *lbl_status = IupGetDialogChild(dlg, "LBL_STATUS");
|
Ihandle *lbl_status = IupGetDialogChild(dlg, "LBL_STATUS");
|
||||||
|
|
||||||
if (sys_ok && user_ok)
|
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);
|
SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)L"Environment", SMTO_ABORTIFHUNG, 5000, NULL);
|
||||||
IupMessage("成功", "系统和用户 PATH 环境变量均已更新!");
|
IupMessage("成功", "系统和用户 PATH 环境变量均已更新!");
|
||||||
if (lbl_status)
|
if (lbl_status)
|
||||||
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "saved"));
|
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "saved"));
|
||||||
}
|
}
|
||||||
else if (sys_ok)
|
else if (sys_ok == ERR_OK)
|
||||||
{
|
{
|
||||||
IupMessage("提示", "系统变量保存成功,但用户变量保存失败。");
|
IupMessage("提示", "系统变量保存成功,但用户变量保存失败。");
|
||||||
}
|
}
|
||||||
else if (user_ok)
|
else if (user_ok == ERR_OK)
|
||||||
{
|
{
|
||||||
IupMessage("提示", "用户变量保存成功,但系统变量保存失败。");
|
IupMessage("提示", "用户变量保存成功,但系统变量保存失败。");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
log_error("Failed to save paths: sys=%d, user=%d", sys_ok, user_ok);
|
||||||
IupMessage("错误", "保存失败!");
|
IupMessage("错误", "保存失败!");
|
||||||
if (lbl_status)
|
if (lbl_status)
|
||||||
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "error"));
|
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "error"));
|
||||||
@@ -386,18 +404,62 @@ int btn_import_cb(Ihandle *self)
|
|||||||
char *filepath = IupGetAttribute(filedlg, "VALUE");
|
char *filepath = IupGetAttribute(filedlg, "VALUE");
|
||||||
if (filepath)
|
if (filepath)
|
||||||
{
|
{
|
||||||
Ihandle *tabs_main = IupGetDialogChild(dlg, "TABS_MAIN");
|
ExportData imported;
|
||||||
int pos = IupGetInt(tabs_main, "VALUEPOS");
|
ErrorCode import_result = import_paths_from_file(filepath, &imported);
|
||||||
|
if (import_result == ERR_OK)
|
||||||
StringList *target_list = (pos == 0) ? &ctx->sys_paths : &ctx->user_paths;
|
|
||||||
|
|
||||||
if (import_paths_from_file(filepath, target_list) == 0)
|
|
||||||
{
|
{
|
||||||
Ihandle *current_list = get_current_list(dlg);
|
int has_system = imported.system.count > 0;
|
||||||
sync_string_list_to_ui(current_list, target_list);
|
int has_user = imported.user.count > 0;
|
||||||
|
|
||||||
|
if (!has_system && !has_user)
|
||||||
|
{
|
||||||
|
IupMessage("错误", "文件中没有找到有效的路径!");
|
||||||
|
return IUP_DEFAULT;
|
||||||
|
}
|
||||||
|
|
||||||
|
int choice = 0;
|
||||||
|
if (has_system && has_user)
|
||||||
|
{
|
||||||
|
choice = IupAlarm("导入选项", "请选择导入目标:",
|
||||||
|
"仅系统变量", "仅用户变量", "全部导入");
|
||||||
|
}
|
||||||
|
else if (has_system)
|
||||||
|
{
|
||||||
|
choice = 3;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
choice = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
int total_imported = 0;
|
||||||
|
|
||||||
|
if (choice == 1 || choice == 3)
|
||||||
|
{
|
||||||
|
clear_string_list(&ctx->sys_paths);
|
||||||
|
for (int i = 0; i < imported.system.count; i++)
|
||||||
|
{
|
||||||
|
add_string_list(&ctx->sys_paths, imported.system.items[i]);
|
||||||
|
}
|
||||||
|
Ihandle *list_sys = IupGetDialogChild(dlg, "LIST_SYS");
|
||||||
|
sync_string_list_to_ui(list_sys, &ctx->sys_paths);
|
||||||
|
total_imported += imported.system.count;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (choice == 2 || choice == 3)
|
||||||
|
{
|
||||||
|
clear_string_list(&ctx->user_paths);
|
||||||
|
for (int i = 0; i < imported.user.count; i++)
|
||||||
|
{
|
||||||
|
add_string_list(&ctx->user_paths, imported.user.items[i]);
|
||||||
|
}
|
||||||
|
Ihandle *list_user = IupGetDialogChild(dlg, "LIST_USER");
|
||||||
|
sync_string_list_to_ui(list_user, &ctx->user_paths);
|
||||||
|
total_imported += imported.user.count;
|
||||||
|
}
|
||||||
|
|
||||||
char msg[256];
|
char msg[256];
|
||||||
snprintf(msg, sizeof(msg), "成功导入 %d 个路径!", target_list->count);
|
snprintf(msg, sizeof(msg), "成功导入 %d 个路径!", total_imported);
|
||||||
IupMessage("导入成功", msg);
|
IupMessage("导入成功", msg);
|
||||||
|
|
||||||
Ihandle *lbl_status = IupGetDialogChild(dlg, "LBL_STATUS");
|
Ihandle *lbl_status = IupGetDialogChild(dlg, "LBL_STATUS");
|
||||||
@@ -406,6 +468,7 @@ int btn_import_cb(Ihandle *self)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
log_error("Import failed: error code %d", import_result);
|
||||||
IupMessage("错误", "导入失败,请检查文件格式是否正确!");
|
IupMessage("错误", "导入失败,请检查文件格式是否正确!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -422,11 +485,9 @@ int btn_export_cb(Ihandle *self)
|
|||||||
if (!ctx)
|
if (!ctx)
|
||||||
return IUP_DEFAULT;
|
return IUP_DEFAULT;
|
||||||
|
|
||||||
Ihandle *tabs_main = IupGetDialogChild(dlg, "TABS_MAIN");
|
ExportData data;
|
||||||
int pos = IupGetInt(tabs_main, "VALUEPOS");
|
data.system = ctx->sys_paths;
|
||||||
|
data.user = ctx->user_paths;
|
||||||
StringList *source_list = (pos == 0) ? &ctx->sys_paths : &ctx->user_paths;
|
|
||||||
int is_system = (pos == 0);
|
|
||||||
|
|
||||||
Ihandle *filedlg = IupFileDlg();
|
Ihandle *filedlg = IupFileDlg();
|
||||||
IupSetAttribute(filedlg, "DIALOGTYPE", "SAVE");
|
IupSetAttribute(filedlg, "DIALOGTYPE", "SAVE");
|
||||||
@@ -436,7 +497,7 @@ int btn_export_cb(Ihandle *self)
|
|||||||
IupSetAttribute(filedlg, "DEFAULTEXT", "json");
|
IupSetAttribute(filedlg, "DEFAULTEXT", "json");
|
||||||
|
|
||||||
char default_name[64];
|
char default_name[64];
|
||||||
snprintf(default_name, sizeof(default_name), "path_%s.json", is_system ? "system" : "user");
|
snprintf(default_name, sizeof(default_name), "path_all.json");
|
||||||
IupSetAttribute(filedlg, "VALUE", default_name);
|
IupSetAttribute(filedlg, "VALUE", default_name);
|
||||||
|
|
||||||
IupPopup(filedlg, IUP_CENTER, IUP_CENTER);
|
IupPopup(filedlg, IUP_CENTER, IUP_CENTER);
|
||||||
@@ -446,14 +507,28 @@ int btn_export_cb(Ihandle *self)
|
|||||||
char *filepath = IupGetAttribute(filedlg, "VALUE");
|
char *filepath = IupGetAttribute(filedlg, "VALUE");
|
||||||
if (filepath)
|
if (filepath)
|
||||||
{
|
{
|
||||||
if (export_paths_to_file(source_list, filepath, is_system) == 0)
|
char final_path[MAX_PATH];
|
||||||
|
if (strchr(filepath, '.') == NULL)
|
||||||
{
|
{
|
||||||
char msg[256];
|
snprintf(final_path, sizeof(final_path), "%s.json", filepath);
|
||||||
snprintf(msg, sizeof(msg), "成功导出 %d 个路径到:\n%s", source_list->count, filepath);
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
safe_strcpy(final_path, sizeof(final_path), filepath);
|
||||||
|
}
|
||||||
|
filepath = final_path;
|
||||||
|
|
||||||
|
ErrorCode export_result = export_paths_to_file(&data, filepath);
|
||||||
|
if (export_result == ERR_OK)
|
||||||
|
{
|
||||||
|
char msg[512];
|
||||||
|
snprintf(msg, sizeof(msg), "成功导出!\n系统变量: %d 个\n用户变量: %d 个\n\n保存位置: %s",
|
||||||
|
data.system.count, data.user.count, filepath);
|
||||||
IupMessage("导出成功", msg);
|
IupMessage("导出成功", msg);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
log_error("Export failed: error code %d", export_result);
|
||||||
IupMessage("错误", "导出失败!");
|
IupMessage("错误", "导出失败!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -472,11 +547,21 @@ void load_all_paths(void)
|
|||||||
if (!ctx)
|
if (!ctx)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!load_system_paths(&ctx->sys_paths))
|
if (load_system_paths(&ctx->sys_paths) != ERR_OK)
|
||||||
{
|
{
|
||||||
|
log_error("Failed to load system paths");
|
||||||
IupMessage("错误", "无法打开系统环境变量注册表键,请尝试以管理员身份运行。");
|
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_sys = IupGetDialogChild(dlg, "LIST_SYS");
|
||||||
Ihandle *list_user = IupGetDialogChild(dlg, "LIST_USER");
|
Ihandle *list_user = IupGetDialogChild(dlg, "LIST_USER");
|
||||||
@@ -509,5 +594,6 @@ int btn_help_cb(Ihandle *self)
|
|||||||
"邮箱:3364451258@qq.com\n"
|
"邮箱:3364451258@qq.com\n"
|
||||||
"GitHub:https://github.com/LHY0125/PathEditor\n"
|
"GitHub:https://github.com/LHY0125/PathEditor\n"
|
||||||
"记得给我的项目点个star!");
|
"记得给我的项目点个star!");
|
||||||
|
|
||||||
return IUP_DEFAULT;
|
return IUP_DEFAULT;
|
||||||
}
|
}
|
||||||
+122
-51
@@ -1,5 +1,7 @@
|
|||||||
#include "core/import_export.h"
|
#include "core/import_export.h"
|
||||||
#include "utils/os_env.h"
|
#include "utils/os_env.h"
|
||||||
|
#include "utils/error_code.h"
|
||||||
|
#include "utils/logger.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -58,43 +60,65 @@ static char *escape_json_string(const char *str)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 导出 PATH 到 JSON 文件
|
// 导出路径数据到 JSON 文件
|
||||||
int export_paths_to_file(const StringList *list, const char *filepath, int is_system)
|
ErrorCode export_paths_to_file(const ExportData *data, const char *filepath)
|
||||||
{
|
{
|
||||||
if (!list || !filepath)
|
if (!data || !filepath)
|
||||||
return -1;
|
return ERR_NULL_PTR;
|
||||||
|
|
||||||
FILE *fp = fopen(filepath, "w, ccs=UTF-8");
|
FILE *fp = fopen(filepath, "w");
|
||||||
if (!fp)
|
if (!fp)
|
||||||
return -1;
|
{
|
||||||
|
log_error("Failed to open file for export: %s", filepath);
|
||||||
|
return ERR_FILE_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(fp, "\xEF\xBB\xBF");
|
||||||
|
|
||||||
char datetime[64];
|
char datetime[64];
|
||||||
get_current_datetime(datetime, sizeof(datetime));
|
get_current_datetime(datetime, sizeof(datetime));
|
||||||
|
|
||||||
fprintf(fp, "{\n");
|
fprintf(fp, "{\n");
|
||||||
fprintf(fp, " \"version\": \"%s\",\n", EXPORT_VERSION);
|
fprintf(fp, " \"version\": \"%s\",\n", EXPORT_VERSION);
|
||||||
fprintf(fp, " \"type\": \"%s\",\n", is_system ? "SYSTEM" : "USER");
|
fprintf(fp, " \"type\": \"ALL\",\n");
|
||||||
fprintf(fp, " \"exported\": \"%s\",\n", datetime);
|
fprintf(fp, " \"exported\": \"%s\",\n", datetime);
|
||||||
fprintf(fp, " \"paths\": [\n");
|
|
||||||
|
|
||||||
for (int i = 0; i < list->count; i++)
|
fprintf(fp, " \"system\": [\n");
|
||||||
|
for (int i = 0; i < data->system.count; i++)
|
||||||
{
|
{
|
||||||
if (list->items[i])
|
if (data->system.items[i])
|
||||||
{
|
{
|
||||||
char *escaped = escape_json_string(list->items[i]);
|
char *escaped = escape_json_string(data->system.items[i]);
|
||||||
if (escaped)
|
if (escaped)
|
||||||
{
|
{
|
||||||
fprintf(fp, " \"%s\"%s\n", escaped, (i < list->count - 1) ? "," : "");
|
fprintf(fp, " \"%s\"%s\n", escaped, (i < data->system.count - 1) ? "," : "");
|
||||||
free(escaped);
|
free(escaped);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fprintf(fp, " ],\n");
|
||||||
|
|
||||||
|
fprintf(fp, " \"user\": [\n");
|
||||||
|
for (int i = 0; i < data->user.count; i++)
|
||||||
|
{
|
||||||
|
if (data->user.items[i])
|
||||||
|
{
|
||||||
|
char *escaped = escape_json_string(data->user.items[i]);
|
||||||
|
if (escaped)
|
||||||
|
{
|
||||||
|
fprintf(fp, " \"%s\"%s\n", escaped, (i < data->user.count - 1) ? "," : "");
|
||||||
|
free(escaped);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
fprintf(fp, " ]\n");
|
fprintf(fp, " ]\n");
|
||||||
|
|
||||||
fprintf(fp, "}\n");
|
fprintf(fp, "}\n");
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return 0;
|
log_info("Exported paths to file: sys=%d, user=%d, file=%s",
|
||||||
|
data->system.count, data->user.count, filepath);
|
||||||
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 移除字符串首尾的空格、制表符、换行符和回车符
|
// 移除字符串首尾的空格、制表符、换行符和回车符
|
||||||
@@ -112,7 +136,7 @@ static void trim_whitespace(char *str)
|
|||||||
memmove(str, start, strlen(start) + 1);
|
memmove(str, start, strlen(start) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否为注释行或空行
|
// 检查字符串是否为注释行或空行
|
||||||
static int is_comment_or_empty(const char *line)
|
static int is_comment_or_empty(const char *line)
|
||||||
{
|
{
|
||||||
while (*line == ' ' || *line == '\t')
|
while (*line == ' ' || *line == '\t')
|
||||||
@@ -124,7 +148,7 @@ static int is_comment_or_empty(const char *line)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否为 JSON 文件
|
// 检查文件是否为 JSON 格式
|
||||||
static int is_json_file(const char *filepath)
|
static int is_json_file(const char *filepath)
|
||||||
{
|
{
|
||||||
const char *ext = strrchr(filepath, '.');
|
const char *ext = strrchr(filepath, '.');
|
||||||
@@ -132,21 +156,52 @@ static int is_json_file(const char *filepath)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 从文件导入 PATH
|
// 从文件导入 PATH
|
||||||
int import_paths_from_file(const char *filepath, StringList *list)
|
ErrorCode import_paths_from_file(const char *filepath, ExportData *data)
|
||||||
{
|
{
|
||||||
if (!filepath || !list)
|
if (!filepath || !data)
|
||||||
return -1;
|
return ERR_NULL_PTR;
|
||||||
|
|
||||||
if (is_json_file(filepath))
|
init_string_list(&data->system);
|
||||||
|
init_string_list(&data->user);
|
||||||
|
|
||||||
|
if (!is_json_file(filepath))
|
||||||
{
|
{
|
||||||
FILE *fp = fopen(filepath, "r, ccs=UTF-8");
|
FILE *fp = fopen(filepath, "rb");
|
||||||
if (!fp)
|
if (!fp)
|
||||||
return -1;
|
{
|
||||||
|
log_error("Failed to open file for import: %s", filepath);
|
||||||
|
return ERR_FILE_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
clear_string_list(list);
|
StringList list;
|
||||||
|
init_string_list(&list);
|
||||||
|
|
||||||
|
char line[4096];
|
||||||
|
while (fgets(line, sizeof(line), fp))
|
||||||
|
{
|
||||||
|
trim_whitespace(line);
|
||||||
|
if (is_comment_or_empty(line))
|
||||||
|
continue;
|
||||||
|
add_string_list(&list, line);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
data->system = list;
|
||||||
|
log_info("Imported paths from TXT file: %d paths, file=%s", list.count, filepath);
|
||||||
|
return ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *fp = fopen(filepath, "rb");
|
||||||
|
if (!fp)
|
||||||
|
{
|
||||||
|
log_error("Failed to open file for import: %s", filepath);
|
||||||
|
return ERR_FILE_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
char buffer[8192];
|
char buffer[8192];
|
||||||
int in_paths = 0;
|
int in_system = 0;
|
||||||
|
int in_user = 0;
|
||||||
int depth = 0;
|
int depth = 0;
|
||||||
int in_string = 0;
|
int in_string = 0;
|
||||||
char path_buffer[4096];
|
char path_buffer[4096];
|
||||||
@@ -180,9 +235,20 @@ int import_paths_from_file(const char *filepath, StringList *list)
|
|||||||
depth++;
|
depth++;
|
||||||
else if (*p == '}' || *p == ']')
|
else if (*p == '}' || *p == ']')
|
||||||
depth--;
|
depth--;
|
||||||
else if (*p == ':')
|
else if (depth == 1 && *p == '"')
|
||||||
in_paths = (depth == 1);
|
{
|
||||||
else if (in_paths && depth == 2 && *p == '"')
|
if (strncmp(p, "\"system\"", 8) == 0)
|
||||||
|
{
|
||||||
|
in_system = 1;
|
||||||
|
in_user = 0;
|
||||||
|
}
|
||||||
|
else if (strncmp(p, "\"user\"", 6) == 0)
|
||||||
|
{
|
||||||
|
in_user = 1;
|
||||||
|
in_system = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (in_system && depth == 2 && *p == '"')
|
||||||
{
|
{
|
||||||
path_len = 0;
|
path_len = 0;
|
||||||
p++;
|
p++;
|
||||||
@@ -205,7 +271,33 @@ int import_paths_from_file(const char *filepath, StringList *list)
|
|||||||
if (path_len > 0)
|
if (path_len > 0)
|
||||||
{
|
{
|
||||||
path_buffer[path_len] = '\0';
|
path_buffer[path_len] = '\0';
|
||||||
add_string_list(list, path_buffer);
|
add_string_list(&data->system, path_buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (in_user && depth == 2 && *p == '"')
|
||||||
|
{
|
||||||
|
path_len = 0;
|
||||||
|
p++;
|
||||||
|
while (*p && path_len < (int)sizeof(path_buffer) - 1)
|
||||||
|
{
|
||||||
|
if (*p == '"' && *(p - 1) != '\\')
|
||||||
|
break;
|
||||||
|
if (*p == '\\' && *(p + 1))
|
||||||
|
{
|
||||||
|
p++;
|
||||||
|
if (*p == 'n')
|
||||||
|
*p = '\n';
|
||||||
|
else if (*p == 'r')
|
||||||
|
*p = '\r';
|
||||||
|
else if (*p == 't')
|
||||||
|
*p = '\t';
|
||||||
|
}
|
||||||
|
path_buffer[path_len++] = *p++;
|
||||||
|
}
|
||||||
|
if (path_len > 0)
|
||||||
|
{
|
||||||
|
path_buffer[path_len] = '\0';
|
||||||
|
add_string_list(&data->user, path_buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -214,28 +306,7 @@ int import_paths_from_file(const char *filepath, StringList *list)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return 0;
|
log_info("Imported paths from JSON file: sys=%d, user=%d, file=%s",
|
||||||
}
|
data->system.count, data->user.count, filepath);
|
||||||
else
|
return ERR_OK;
|
||||||
{
|
|
||||||
FILE *fp = fopen(filepath, "r, ccs=UTF-8");
|
|
||||||
if (!fp)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
clear_string_list(list);
|
|
||||||
|
|
||||||
char line[4096];
|
|
||||||
while (fgets(line, sizeof(line), fp))
|
|
||||||
{
|
|
||||||
trim_whitespace(line);
|
|
||||||
|
|
||||||
if (is_comment_or_empty(line))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
add_string_list(list, line);
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(fp);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+19
-12
@@ -1,51 +1,57 @@
|
|||||||
#include "core/path_manager.h"
|
#include "core/path_manager.h"
|
||||||
#include "utils/os_env.h"
|
#include "utils/os_env.h"
|
||||||
|
#include "utils/error_code.h"
|
||||||
|
#include "utils/logger.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// 删除指定索引的路径项
|
// 删除指定索引的路径项
|
||||||
void path_manager_remove_at(StringList *list, int index)
|
ErrorCode path_manager_remove_at(StringList *list, int index)
|
||||||
{
|
{
|
||||||
if (!list || index < 0 || index >= list->count)
|
if (!list || index < 0 || index >= list->count)
|
||||||
return;
|
return ERR_NULL_PTR;
|
||||||
|
|
||||||
free(list->items[index]);
|
free(list->items[index]);
|
||||||
for (int i = index; i < list->count - 1; i++)
|
for (int i = index; i < list->count - 1; i++)
|
||||||
{
|
{
|
||||||
list->items[i] = list->items[i + 1];
|
list->items[i] = list->items[i + 1];
|
||||||
}
|
}
|
||||||
|
list->items[list->count - 1] = NULL;
|
||||||
list->count--;
|
list->count--;
|
||||||
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 向上移动路径项
|
// 向上移动路径项
|
||||||
void path_manager_move_up(StringList *list, int index)
|
ErrorCode path_manager_move_up(StringList *list, int index)
|
||||||
{
|
{
|
||||||
if (!list || index <= 0 || index >= list->count)
|
if (!list || index <= 0 || index >= list->count)
|
||||||
return;
|
return ERR_NULL_PTR;
|
||||||
|
|
||||||
char *temp = list->items[index];
|
char *temp = list->items[index];
|
||||||
list->items[index] = list->items[index - 1];
|
list->items[index] = list->items[index - 1];
|
||||||
list->items[index - 1] = temp;
|
list->items[index - 1] = temp;
|
||||||
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 向下移动路径项
|
// 向下移动路径项
|
||||||
void path_manager_move_down(StringList *list, int index)
|
ErrorCode path_manager_move_down(StringList *list, int index)
|
||||||
{
|
{
|
||||||
if (!list || index < 0 || index >= list->count - 1)
|
if (!list || index < 0 || index >= list->count - 1)
|
||||||
return;
|
return ERR_NULL_PTR;
|
||||||
|
|
||||||
char *temp = list->items[index];
|
char *temp = list->items[index];
|
||||||
list->items[index] = list->items[index + 1];
|
list->items[index] = list->items[index + 1];
|
||||||
list->items[index + 1] = temp;
|
list->items[index + 1] = temp;
|
||||||
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清理无效路径项
|
// 清理无效路径项
|
||||||
int path_manager_clean(StringList *list)
|
ErrorCode path_manager_clean(StringList *list)
|
||||||
{
|
{
|
||||||
if (!list) return 0;
|
if (!list) return ERR_NULL_PTR;
|
||||||
|
|
||||||
int removed_count = 0;
|
int removed_count = 0;
|
||||||
|
|
||||||
// 从后往前遍历,方便删除
|
|
||||||
for (int i = list->count - 1; i >= 0; i--)
|
for (int i = list->count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
char *item = list->items[i];
|
char *item = list->items[i];
|
||||||
@@ -53,14 +59,12 @@ int path_manager_clean(StringList *list)
|
|||||||
|
|
||||||
int should_remove = 0;
|
int should_remove = 0;
|
||||||
|
|
||||||
// 1. 检查有效性
|
|
||||||
if (!is_path_valid(item))
|
if (!is_path_valid(item))
|
||||||
{
|
{
|
||||||
should_remove = 1;
|
should_remove = 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 2. 检查重复 (检查当前项之前是否出现过)
|
|
||||||
for (int j = 0; j < i; j++)
|
for (int j = 0; j < i; j++)
|
||||||
{
|
{
|
||||||
char *prev_item = list->items[j];
|
char *prev_item = list->items[j];
|
||||||
@@ -78,5 +82,8 @@ int path_manager_clean(StringList *list)
|
|||||||
removed_count++;
|
removed_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return removed_count;
|
|
||||||
|
log_info("Cleaned paths: removed %d invalid/duplicate paths, remaining %d",
|
||||||
|
removed_count, list->count);
|
||||||
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
+15
-13
@@ -1,5 +1,6 @@
|
|||||||
#include "core/registry_service.h"
|
#include "core/registry_service.h"
|
||||||
#include "utils/string_ext.h"
|
#include "utils/string_ext.h"
|
||||||
|
#include "utils/error_code.h"
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -9,7 +10,7 @@
|
|||||||
#define REG_VALUE L"Path"
|
#define REG_VALUE L"Path"
|
||||||
|
|
||||||
// 内部辅助函数:加载单个列表
|
// 内部辅助函数:加载单个列表
|
||||||
static int load_single_path(HKEY hKeyRoot, const wchar_t *regPath, StringList *list)
|
static ErrorCode load_single_path(HKEY hKeyRoot, const wchar_t *regPath, StringList *list)
|
||||||
{
|
{
|
||||||
clear_string_list(list);
|
clear_string_list(list);
|
||||||
|
|
||||||
@@ -17,7 +18,7 @@ static int load_single_path(HKEY hKeyRoot, const wchar_t *regPath, StringList *l
|
|||||||
LONG res = RegOpenKeyExW(hKeyRoot, regPath, 0, KEY_READ, &hKey);
|
LONG res = RegOpenKeyExW(hKeyRoot, regPath, 0, KEY_READ, &hKey);
|
||||||
if (res != ERROR_SUCCESS)
|
if (res != ERROR_SUCCESS)
|
||||||
{
|
{
|
||||||
return 0; // 失败
|
return ERR_REGISTRY_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD type, size;
|
DWORD type, size;
|
||||||
@@ -59,25 +60,26 @@ static int load_single_path(HKEY hKeyRoot, const wchar_t *regPath, StringList *l
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
RegCloseKey(hKey);
|
RegCloseKey(hKey);
|
||||||
return 1; // 成功
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载系统环境变量路径
|
// 加载系统环境变量路径
|
||||||
int load_system_paths(StringList *list)
|
ErrorCode load_system_paths(StringList *list)
|
||||||
{
|
{
|
||||||
return load_single_path(HKEY_LOCAL_MACHINE, REG_PATH_SYS, list);
|
return load_single_path(HKEY_LOCAL_MACHINE, REG_PATH_SYS, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载用户环境变量路径
|
// 加载用户环境变量路径
|
||||||
int load_user_paths(StringList *list)
|
ErrorCode load_user_paths(StringList *list)
|
||||||
{
|
{
|
||||||
return load_single_path(HKEY_CURRENT_USER, REG_PATH_USER, list);
|
return load_single_path(HKEY_CURRENT_USER, REG_PATH_USER, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 内部辅助函数:保存单个列表
|
// 内部辅助函数:保存单个列表
|
||||||
static int save_single_path(HKEY hKeyRoot, const wchar_t *regPath, const StringList *list)
|
static ErrorCode save_single_path(HKEY hKeyRoot, const wchar_t *regPath, const StringList *list)
|
||||||
{
|
{
|
||||||
if (!list) return 0;
|
if (!list)
|
||||||
|
return ERR_NULL_PTR;
|
||||||
|
|
||||||
// 计算大小
|
// 计算大小
|
||||||
size_t total_len = 0;
|
size_t total_len = 0;
|
||||||
@@ -97,7 +99,7 @@ static int save_single_path(HKEY hKeyRoot, const wchar_t *regPath, const StringL
|
|||||||
|
|
||||||
wchar_t *buffer = (wchar_t *)malloc(total_len * sizeof(wchar_t));
|
wchar_t *buffer = (wchar_t *)malloc(total_len * sizeof(wchar_t));
|
||||||
if (!buffer)
|
if (!buffer)
|
||||||
return 0;
|
return ERR_OUT_OF_MEMORY;
|
||||||
|
|
||||||
buffer[0] = L'\0';
|
buffer[0] = L'\0';
|
||||||
for (int i = 0; i < list->count; i++)
|
for (int i = 0; i < list->count; i++)
|
||||||
@@ -116,29 +118,29 @@ static int save_single_path(HKEY hKeyRoot, const wchar_t *regPath, const StringL
|
|||||||
}
|
}
|
||||||
|
|
||||||
HKEY hKey;
|
HKEY hKey;
|
||||||
int success = 0;
|
ErrorCode result = ERR_PERMISSION_DENIED;
|
||||||
if (RegOpenKeyExW(hKeyRoot, regPath, 0, KEY_WRITE, &hKey) == ERROR_SUCCESS)
|
if (RegOpenKeyExW(hKeyRoot, regPath, 0, KEY_WRITE, &hKey) == ERROR_SUCCESS)
|
||||||
{
|
{
|
||||||
DWORD size = (DWORD)((wcslen(buffer) + 1) * sizeof(wchar_t));
|
DWORD size = (DWORD)((wcslen(buffer) + 1) * sizeof(wchar_t));
|
||||||
if (RegSetValueExW(hKey, REG_VALUE, 0, REG_EXPAND_SZ, (LPBYTE)buffer, size) == ERROR_SUCCESS)
|
if (RegSetValueExW(hKey, REG_VALUE, 0, REG_EXPAND_SZ, (LPBYTE)buffer, size) == ERROR_SUCCESS)
|
||||||
{
|
{
|
||||||
success = 1;
|
result = ERR_OK;
|
||||||
}
|
}
|
||||||
RegCloseKey(hKey);
|
RegCloseKey(hKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(buffer);
|
free(buffer);
|
||||||
return success;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存系统环境变量路径
|
// 保存系统环境变量路径
|
||||||
int save_system_paths(const StringList *list)
|
ErrorCode save_system_paths(const StringList *list)
|
||||||
{
|
{
|
||||||
return save_single_path(HKEY_LOCAL_MACHINE, REG_PATH_SYS, list);
|
return save_single_path(HKEY_LOCAL_MACHINE, REG_PATH_SYS, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存用户环境变量路径
|
// 保存用户环境变量路径
|
||||||
int save_user_paths(const StringList *list)
|
ErrorCode save_user_paths(const StringList *list)
|
||||||
{
|
{
|
||||||
return save_single_path(HKEY_CURRENT_USER, REG_PATH_USER, list);
|
return save_single_path(HKEY_CURRENT_USER, REG_PATH_USER, list);
|
||||||
}
|
}
|
||||||
+10
-1
@@ -7,6 +7,7 @@
|
|||||||
#include "core/lua_config.h"
|
#include "core/lua_config.h"
|
||||||
#include "utils/string_ext.h"
|
#include "utils/string_ext.h"
|
||||||
#include "utils/os_env.h"
|
#include "utils/os_env.h"
|
||||||
|
#include "utils/logger.h"
|
||||||
#include "controller/callbacks.h"
|
#include "controller/callbacks.h"
|
||||||
#include "ui/main_window.h"
|
#include "ui/main_window.h"
|
||||||
|
|
||||||
@@ -17,7 +18,7 @@ cmake --build build
|
|||||||
!打包命令:
|
!打包命令:
|
||||||
build_installer.bat
|
build_installer.bat
|
||||||
!运行命令:
|
!运行命令:
|
||||||
build\\PathEditor.exe
|
powershell -Command "Start-Process 'build\\PathEditor.exe' -Verb RunAs"
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// 定义 Windows 消息常量
|
// 定义 Windows 消息常量
|
||||||
@@ -33,6 +34,10 @@ build\\PathEditor.exe
|
|||||||
// 主函数
|
// 主函数
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
// 初始化日志系统
|
||||||
|
log_init(NULL, LOG_LEVEL_INFO);
|
||||||
|
log_info("PathEditor starting...");
|
||||||
|
|
||||||
// 强制设置 UTF8MODE 环境变量,必须在 IupOpen 之前
|
// 强制设置 UTF8MODE 环境变量,必须在 IupOpen 之前
|
||||||
putenv("IUP_UTF8MODE=YES");
|
putenv("IUP_UTF8MODE=YES");
|
||||||
|
|
||||||
@@ -44,6 +49,8 @@ int main(int argc, char **argv)
|
|||||||
IupMessage("警告", "Lua 配置系统初始化失败,将使用默认值");
|
IupMessage("警告", "Lua 配置系统初始化失败,将使用默认值");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log_info("Lua config initialized");
|
||||||
|
|
||||||
// 在管理员模式下,解决无法拖拽文件到列表框的问题 (UIPI)
|
// 在管理员模式下,解决无法拖拽文件到列表框的问题 (UIPI)
|
||||||
// 需要加载 User32.dll 获取 ChangeWindowMessageFilter 函数
|
// 需要加载 User32.dll 获取 ChangeWindowMessageFilter 函数
|
||||||
HMODULE hUser32 = LoadLibraryW(L"user32.dll");
|
HMODULE hUser32 = LoadLibraryW(L"user32.dll");
|
||||||
@@ -132,8 +139,10 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
IupMainLoop();
|
IupMainLoop();
|
||||||
|
|
||||||
|
log_info("PathEditor exiting...");
|
||||||
destroy_app_context(ctx);
|
destroy_app_context(ctx);
|
||||||
lua_config_destroy();
|
lua_config_destroy();
|
||||||
|
log_destroy();
|
||||||
IupClose();
|
IupClose();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+2
-2
@@ -1,5 +1,6 @@
|
|||||||
#include "ui/dialogs.h"
|
#include "ui/dialogs.h"
|
||||||
#include "core/lua_config.h"
|
#include "core/lua_config.h"
|
||||||
|
#include "utils/safe_string.h"
|
||||||
#include <iup.h>
|
#include <iup.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -61,8 +62,7 @@ int custom_input_dialog(const char *title, const char *label_text, char *buffer,
|
|||||||
char *val = IupGetAttribute(text, "VALUE");
|
char *val = IupGetAttribute(text, "VALUE");
|
||||||
if (val)
|
if (val)
|
||||||
{
|
{
|
||||||
strncpy(buffer, val, buffer_size);
|
safe_strcpy(buffer, buffer_size, val);
|
||||||
buffer[buffer_size - 1] = '\0';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,126 @@
|
|||||||
|
#include "utils/logger.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
// 全局日志文件指针
|
||||||
|
static FILE *G_log_file = NULL;
|
||||||
|
// 全局日志级别
|
||||||
|
static LogLevel G_log_level = LOG_LEVEL_INFO;
|
||||||
|
|
||||||
|
// 将日志级别转换为字符串
|
||||||
|
static const char *level_to_string(LogLevel level)
|
||||||
|
{
|
||||||
|
switch (level)
|
||||||
|
{
|
||||||
|
case LOG_LEVEL_DEBUG:
|
||||||
|
return "DEBUG";
|
||||||
|
case LOG_LEVEL_INFO:
|
||||||
|
return "INFO ";
|
||||||
|
case LOG_LEVEL_WARN:
|
||||||
|
return "WARN ";
|
||||||
|
case LOG_LEVEL_ERROR:
|
||||||
|
return "ERROR";
|
||||||
|
default:
|
||||||
|
return "UNKN ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前时间戳
|
||||||
|
static void get_timestamp(char *buffer, size_t size)
|
||||||
|
{
|
||||||
|
time_t now = time(NULL);
|
||||||
|
struct tm *tm_info = localtime(&now);
|
||||||
|
strftime(buffer, size, "%Y-%m-%d %H:%M:%S", tm_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入日志
|
||||||
|
static void log_write(LogLevel level, const char *fmt, va_list args)
|
||||||
|
{
|
||||||
|
if (level < G_log_level)
|
||||||
|
return;
|
||||||
|
|
||||||
|
char timestamp[64];
|
||||||
|
get_timestamp(timestamp, sizeof(timestamp));
|
||||||
|
|
||||||
|
char message[1024];
|
||||||
|
vsnprintf(message, sizeof(message), fmt, args);
|
||||||
|
|
||||||
|
const char *level_str = level_to_string(level);
|
||||||
|
|
||||||
|
if (G_log_file)
|
||||||
|
{
|
||||||
|
fprintf(G_log_file, "[%s] [%s] %s\n", timestamp, level_str, message);
|
||||||
|
fflush(G_log_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stdout, "[%s] [%s] %s\n", timestamp, level_str, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化日志系统
|
||||||
|
void log_init(const char *log_file, LogLevel level)
|
||||||
|
{
|
||||||
|
if (log_file)
|
||||||
|
{
|
||||||
|
G_log_file = fopen(log_file, "a");
|
||||||
|
if (!G_log_file)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to open log file: %s\n", log_file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
G_log_level = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 销毁日志系统
|
||||||
|
void log_destroy(void)
|
||||||
|
{
|
||||||
|
if (G_log_file)
|
||||||
|
{
|
||||||
|
fclose(G_log_file);
|
||||||
|
G_log_file = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置日志级别
|
||||||
|
void log_set_level(LogLevel level)
|
||||||
|
{
|
||||||
|
G_log_level = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调试日志
|
||||||
|
void log_debug(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
log_write(LOG_LEVEL_DEBUG, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 信息日志
|
||||||
|
void log_info(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
log_write(LOG_LEVEL_INFO, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 警告日志
|
||||||
|
void log_warn(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
log_write(LOG_LEVEL_WARN, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 错误日志
|
||||||
|
void log_error(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
log_write(LOG_LEVEL_ERROR, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
#include "utils/safe_string.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// 安全字符串复制
|
||||||
|
char *safe_strcpy(char *dst, size_t dst_size, const char *src)
|
||||||
|
{
|
||||||
|
if (!dst || !src || dst_size == 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
size_t len = strlen(src);
|
||||||
|
if (len >= dst_size)
|
||||||
|
{
|
||||||
|
memcpy(dst, src, dst_size - 1);
|
||||||
|
dst[dst_size - 1] = '\0';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
strcpy(dst, src);
|
||||||
|
}
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 安全字符串拼接
|
||||||
|
char *safe_strcat(char *dst, size_t dst_size, const char *src)
|
||||||
|
{
|
||||||
|
if (!dst || !src || dst_size == 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
size_t dst_len = strlen(dst);
|
||||||
|
if (dst_len >= dst_size)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return safe_strcpy(dst + dst_len, dst_size - dst_len, src);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 安全字符串复制(返回新分配的内存)
|
||||||
|
char *safe_strdup(const char *src)
|
||||||
|
{
|
||||||
|
if (!src)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
size_t len = strlen(src);
|
||||||
|
char *result = (char *)malloc(len + 1);
|
||||||
|
if (result)
|
||||||
|
strcpy(result, src);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user