Refactor: replace C-style code with modern C++ idioms

This commit is contained in:
Changhua 2025-01-29 18:32:20 +08:00
parent 554d62efcf
commit 15267632a1

View File

@ -8,13 +8,13 @@
#include "exec_sql.h" #include "exec_sql.h"
#include "funcs.h" #include "funcs.h"
#include "log.hpp" #include "log.hpp"
#include "receive_msg.h"
#include "spy_types.h" #include "spy_types.h"
#include "util.h" #include "util.h"
using namespace std; using namespace std;
namespace fs = std::filesystem; namespace fs = std::filesystem;
extern bool gIsListeningPyq;
extern QWORD g_WeChatWinDllAddr; extern QWORD g_WeChatWinDllAddr;
#define HEADER_PNG1 0x89 #define HEADER_PNG1 0x89
@ -111,32 +111,25 @@ string DecryptImage(string src, string dir)
string dst = ""; string dst = "";
try { if (dir.empty()) {
if (dir.empty()) { dst = fs::path(src).replace_extension(ext).string();
dst = fs::path(src).replace_extension(ext).string(); } else {
} else { dst = (dir.back() == '\\' || dir.back() == '/') ? dir : (dir + "/");
dst = (dir.back() == '\\' || dir.back() == '/') ? dir : (dir + "/");
// 判断dir文件夹是否存在若不存在则创建否则将无法创建出文件
// 判断dir文件夹是否存在若不存在则创建否则将无法创建出文件 if (!fs::exists(dst)) { // 判断该文件夹是否存在
if (!fs::exists(dst)) {//判断该文件夹是否存在 bool success = fs::create_directories(dst); // Windows创建文件夹
bool success = fs::create_directories(dst); //Windows创建文件夹 if (!success) { // 创建失败
if (!success) { //创建失败 LOG_ERROR("Failed to mkdir:{}", dst);
LOG_ERROR("Failed to mkdir:{}", dst); return "";
return "";
}
} }
dst += fs::path(src).stem().string() + ext;
} }
replace(dst.begin(), dst.end(), '\\', '/'); dst += fs::path(src).stem().string() + ext;
} catch (const std::exception &e) {
LOG_ERROR(GB2312ToUtf8(e.what()));
} catch (...) {
LOG_ERROR("Unknow exception.");
return "";
} }
replace(dst.begin(), dst.end(), '\\', '/');
ofstream out(dst.c_str(), ios::binary); ofstream out(dst.c_str(), ios::binary);
if (!out.is_open()) { if (!out.is_open()) {
LOG_ERROR("Failed to write file {}", dst); LOG_ERROR("Failed to write file {}", dst);
@ -178,7 +171,8 @@ static int GetNextPage(QWORD id)
int RefreshPyq(QWORD id) int RefreshPyq(QWORD id)
{ {
if (!gIsListeningPyq) { auto &msgHandler = MessageHandler::getInstance();
if (!msgHandler.isPyqListening()) {
LOG_ERROR("没有启动朋友圈消息接收参考enable_receiving_msg"); LOG_ERROR("没有启动朋友圈消息接收参考enable_receiving_msg");
return -1; return -1;
} }
@ -319,7 +313,7 @@ string GetPCMAudio(uint64_t id, string dir, int32_t sr)
SilkDecode(silk, pcm, sr); SilkDecode(silk, pcm, sr);
errno_t err; errno_t err;
FILE* fPCM; FILE *fPCM;
err = fopen_s(&fPCM, pcmpath.c_str(), "wb"); err = fopen_s(&fPCM, pcmpath.c_str(), "wb");
if (err != 0) { if (err != 0) {
printf("Error: could not open input file %s\n", pcmpath.c_str()); printf("Error: could not open input file %s\n", pcmpath.c_str());
@ -332,7 +326,6 @@ string GetPCMAudio(uint64_t id, string dir, int32_t sr)
return pcmpath; return pcmpath;
} }
OcrResult_t GetOcrResult(string path) OcrResult_t GetOcrResult(string path)
{ {
OcrResult_t ret = { -1, "" }; OcrResult_t ret = { -1, "" };