mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-05-10 02:09:46 +08:00
refactor(core): 统一使用ErrorCode并添加错误日志记录
- 将path_manager和import_export模块的返回值从int/void改为ErrorCode - 在关键操作中添加日志记录,便于调试和错误追踪 - 更新调用代码以检查错误码并记录错误 - 修改运行命令以管理员权限启动程序
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
#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"
|
||||||
|
|
||||||
@@ -11,9 +12,9 @@ typedef struct {
|
|||||||
} ExportData;
|
} ExportData;
|
||||||
|
|
||||||
// 导出 PATH 到文件
|
// 导出 PATH 到文件
|
||||||
int export_paths_to_file(const ExportData *data, const char *filepath);
|
ErrorCode export_paths_to_file(const ExportData *data, const char *filepath);
|
||||||
|
|
||||||
// 从文件导入 PATH (返回是否包含全部格式)
|
// 从文件导入 PATH
|
||||||
int import_paths_from_file(const char *filepath, ExportData *data);
|
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
|
||||||
|
|||||||
@@ -158,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);
|
||||||
|
|
||||||
@@ -179,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);
|
||||||
@@ -198,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);
|
||||||
@@ -219,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);
|
||||||
@@ -391,7 +405,8 @@ int btn_import_cb(Ihandle *self)
|
|||||||
if (filepath)
|
if (filepath)
|
||||||
{
|
{
|
||||||
ExportData imported;
|
ExportData imported;
|
||||||
if (import_paths_from_file(filepath, &imported) == 0)
|
ErrorCode import_result = import_paths_from_file(filepath, &imported);
|
||||||
|
if (import_result == ERR_OK)
|
||||||
{
|
{
|
||||||
int has_system = imported.system.count > 0;
|
int has_system = imported.system.count > 0;
|
||||||
int has_user = imported.user.count > 0;
|
int has_user = imported.user.count > 0;
|
||||||
@@ -453,6 +468,7 @@ int btn_import_cb(Ihandle *self)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
log_error("Import failed: error code %d", import_result);
|
||||||
IupMessage("错误", "导入失败,请检查文件格式是否正确!");
|
IupMessage("错误", "导入失败,请检查文件格式是否正确!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -502,7 +518,8 @@ int btn_export_cb(Ihandle *self)
|
|||||||
}
|
}
|
||||||
filepath = final_path;
|
filepath = final_path;
|
||||||
|
|
||||||
if (export_paths_to_file(&data, filepath) == 0)
|
ErrorCode export_result = export_paths_to_file(&data, filepath);
|
||||||
|
if (export_result == ERR_OK)
|
||||||
{
|
{
|
||||||
char msg[512];
|
char msg[512];
|
||||||
snprintf(msg, sizeof(msg), "成功导出!\n系统变量: %d 个\n用户变量: %d 个\n\n保存位置: %s",
|
snprintf(msg, sizeof(msg), "成功导出!\n系统变量: %d 个\n用户变量: %d 个\n\n保存位置: %s",
|
||||||
@@ -511,6 +528,7 @@ int btn_export_cb(Ihandle *self)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
log_error("Export failed: error code %d", export_result);
|
||||||
IupMessage("错误", "导出失败!");
|
IupMessage("错误", "导出失败!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-11
@@ -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>
|
||||||
@@ -59,14 +61,17 @@ static char *escape_json_string(const char *str)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 导出路径数据到 JSON 文件
|
// 导出路径数据到 JSON 文件
|
||||||
int export_paths_to_file(const ExportData *data, const char *filepath)
|
ErrorCode export_paths_to_file(const ExportData *data, const char *filepath)
|
||||||
{
|
{
|
||||||
if (!data || !filepath)
|
if (!data || !filepath)
|
||||||
return -1;
|
return ERR_NULL_PTR;
|
||||||
|
|
||||||
FILE *fp = fopen(filepath, "w");
|
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");
|
fprintf(fp, "\xEF\xBB\xBF");
|
||||||
|
|
||||||
@@ -111,7 +116,9 @@ int export_paths_to_file(const ExportData *data, const char *filepath)
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 移除字符串首尾的空格、制表符、换行符和回车符
|
// 移除字符串首尾的空格、制表符、换行符和回车符
|
||||||
@@ -148,11 +155,11 @@ static int is_json_file(const char *filepath)
|
|||||||
return ext && strcasecmp(ext, ".json") == 0;
|
return ext && strcasecmp(ext, ".json") == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从文件导入 PATH (返回是否包含全部格式)
|
// 从文件导入 PATH
|
||||||
int import_paths_from_file(const char *filepath, ExportData *data)
|
ErrorCode import_paths_from_file(const char *filepath, ExportData *data)
|
||||||
{
|
{
|
||||||
if (!filepath || !data)
|
if (!filepath || !data)
|
||||||
return -1;
|
return ERR_NULL_PTR;
|
||||||
|
|
||||||
init_string_list(&data->system);
|
init_string_list(&data->system);
|
||||||
init_string_list(&data->user);
|
init_string_list(&data->user);
|
||||||
@@ -161,7 +168,10 @@ int import_paths_from_file(const char *filepath, ExportData *data)
|
|||||||
{
|
{
|
||||||
FILE *fp = fopen(filepath, "rb");
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
StringList list;
|
StringList list;
|
||||||
init_string_list(&list);
|
init_string_list(&list);
|
||||||
@@ -178,12 +188,16 @@ int import_paths_from_file(const char *filepath, ExportData *data)
|
|||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
data->system = list;
|
data->system = list;
|
||||||
return 0;
|
log_info("Imported paths from TXT file: %d paths, file=%s", list.count, filepath);
|
||||||
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *fp = fopen(filepath, "rb");
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
char buffer[8192];
|
char buffer[8192];
|
||||||
int in_system = 0;
|
int in_system = 0;
|
||||||
@@ -292,5 +306,7 @@ int import_paths_from_file(const char *filepath, ExportData *data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
+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;
|
||||||
}
|
}
|
||||||
+1
-1
@@ -18,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 消息常量
|
||||||
|
|||||||
Reference in New Issue
Block a user