浏览代码

先建立文件队列,减少浪费资源

Hongwenjun 11 月之前
父节点
当前提交
703bcec780
共有 2 个文件被更改,包括 44 次插入60 次删除
  1. 2 2
      GuiApp/GuiMain.cpp
  2. 42 58
      GuiApp/GuiThumbnail.cpp

+ 2 - 2
GuiApp/GuiMain.cpp

@@ -27,7 +27,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdL
     if (IsFileExist(ConfigFile))
         LoadConfigFile();
 
-    hWindow = XWnd_CreateWindow(0, 0, 950, 600, L"Adobe AI EPS INDD PDF和CorelDRAW 缩略图工具  版权所有 2013-2021 Hongwenjun (蘭雅)"); // 创建窗口
+    hWindow = XWnd_CreateWindow(0, 0, 950, 600, L"Adobe AI EPS INDD PDF和CorelDRAW 缩略图工具  版权所有 2013-2024 Hongwenjun (蘭雅)"); // 创建窗口
     if (hWindow) {
         // 设置图标
         HICON logo_hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_ICON1);
@@ -115,7 +115,7 @@ void InitXC_Window(HWINDOW& hWindow)
     XList_AddColumn(hList, 100, L"文件版本", 2);
 
     // 创建静态文本
-    hStatic = XStatic_Create(1, 545, 650, 22, L"Adobe AI EPS INDD PDF和CorelDRAW 缩略图工具  版权所有 2013-2021 Hongwenjun (蘭雅)", hWindow);
+    hStatic = XStatic_Create(1, 545, 650, 22, L"Adobe AI EPS INDD PDF和CorelDRAW 缩略图工具  版权所有 2013-2024 Hongwenjun (蘭雅)", hWindow);
 
     XStatic_Create(780, 195, 160, 18, L"微信扫一扫打赏作者", hWindow);
 

+ 42 - 58
GuiApp/GuiThumbnail.cpp

@@ -1,94 +1,78 @@
 #include "GuiThumbnail.h"
 #include <string.h>
 #include <wchar.h>
-
 #include <thread>
-#include <vector>
-#include <algorithm>
-#include <mutex>
 
-std::mutex mtx;
 
-void processResult(int i)
+void processFile(const std::wstring& srcFile, const std::wstring& thumbFile)
 {
-    wchar_t src_filename[MAX_PATH] = {0};
-    wchar_t thumb_filename[MAX_PATH] = {0};
-
-    Everything_GetResultFullPathNameW(i, src_filename, MAX_PATH);
-
     // AI EPS INDD 文件导出缩略图
-    wcscpy(thumb_filename, Everything_GetResultFileName(i));
-    wcscat(thumb_filename, L"_T.jpg");
-    bool ret = AdobeThumbnail_W(src_filename, thumb_filename);
+    bool ret = AdobeThumbnail_W(srcFile.c_str(), thumbFile.c_str());
+    // CorelDRAW CDR 文件导出缩略图
+    if (!ret)
+        ret = CorelThumbnail_W(srcFile.c_str(), thumbFile.c_str());
 
-    if (!ret) { // CorelDRAW CDR 文件导出缩略图
-        wcscpy(thumb_filename, Everything_GetResultFileName(i));
-        wcscat(thumb_filename, L"_T.png");
-        ret = CorelThumbnail_W(src_filename, thumb_filename);
-    }
-
-    std::lock_guard<std::mutex> lock(mtx);
-    // 在这里可以对处理结果进行进一步的处理或记录
+    // 可以在此处添加处理成功或失败的逻辑
 }
 
 // 执行提取缩略图 主功能
-bool GuiThumbnail(const wchar_t *keyWord, const wchar_t *savePath)
+bool GuiThumbnail(const wchar_t* keyWord, const wchar_t* savePath)
 {
-    //    wchar_t src_filename[MAX_PATH] = {0};
-    //    wchar_t thumb_filename[MAX_PATH] = {0};
-    wchar_t wbuf[MAX_PATH] = {0};
+    wchar_t src_filename[MAX_PATH] = {0};
+    wchar_t thumb_filename[MAX_PATH] = {0};
 
     Everything_SetSearch(keyWord);
-    bool EQRet = Everything_Query(TRUE);
-
-    if (EQRet == false) {
+    if (false==Everything_Query(TRUE))
         return false;
+
+    std::vector<std::wstring> vec_files;
+    std::vector<std::wstring> vec_names;
+    const wchar_t* rs = L"(.+)(\\.(?:ai|AI|indd|INDD|Indd|eps|EPS|Eps|pdf|PDF|cdr|CDR|Cdr))";  // 正则字符串,exp开始的单词
+    std::wregex expression(rs);                   // 字符串传递给构造函数,建立正则表达式
+
+    for (int i = 0 ; i < Everything_GetNumResults(); i++) {
+        bool ret = std::regex_match((wchar_t*)Everything_GetResultFileName(i), expression);
+        if (ret) {
+            Everything_GetResultFullPathNameW(i, src_filename, MAX_PATH);
+            vec_files.push_back(src_filename);
+            vec_names.push_back((wchar_t*)Everything_GetResultFileName(i));
+        }
     }
 
     // 如果没有目录建立,进入目录
     _wmkdir(savePath);
     _wchdir(savePath);
 
-    //    // 文件导出缩略图   改成多线程
-    //    for (int i = 0 ; i < Everything_GetNumResults(); i++) {
-    //
-    //        Everything_GetResultFullPathNameW(i, src_filename , MAX_PATH);
-    //
-    //        // AI EPS INDD 文件导出缩略图
-    //        wcscpy(thumb_filename, Everything_GetResultFileName(i));
-    //        wcscat(thumb_filename, L"_T.jpg");
-    //        bool ret = AdobeThumbnail_W(src_filename , thumb_filename);
-    //
-    //        if (!ret) { // CorelDRAW CDR 文件导出缩略图
-    //            wcscpy(thumb_filename, Everything_GetResultFileName(i));
-    //            wcscat(thumb_filename, L"_T.png");
-    //            ret = CorelThumbnail_W(src_filename, thumb_filename);
-    //        }
-    //    }
+//    // 文件导出缩略图
+//    for (int i = 0 ; i != vec_files.size(); i++) {
+//        wcscpy(src_filename, vec_files[i].c_str());
+//        wcscpy(thumb_filename, vec_names[i].c_str());
+//        wcscat(thumb_filename, L"_T.jpg");
+//        // AI EPS INDD 文件导出缩略图
+//        bool ret = AdobeThumbnail_W(src_filename, thumb_filename);
+//        if (!ret)  // CorelDRAW CDR 文件导出缩略图
+//            ret = CorelThumbnail_W(src_filename, thumb_filename);
+//    }
 
-    int numResults = Everything_GetNumResults();
     std::vector<std::thread> threads;
-
-    for (int i = 0; i < numResults; i++) {
-        threads.emplace_back(std::thread(processResult, i));
+    for (int i = 0; i < vec_files.size(); i++) {
+        const std::wstring& srcFilename = vec_files[i];
+        const std::wstring& thumbFilename = vec_names[i] + L"_T.jpg";
+        threads.emplace_back(std::thread(processFile, srcFilename, thumbFilename));
     }
-
     // 等待所有线程执行完毕
-    std::for_each(threads.begin(), threads.end(), [](std::thread& t) {
-        t.join();
-    });
+    for (auto& thread : threads) {
+        thread.join();
+    }
 
     return true;
 }
 
 // 临时PNG显示文件
-bool Thumbnail_TempPng(const wchar_t *src_filename, const wchar_t *tmppng)
+bool Thumbnail_TempPng(const wchar_t* src_filename, const wchar_t* tmppng)
 {
     bool ret = AdobeThumbnail_W(src_filename, tmppng);
-
-    if (!ret) {
+    if (!ret)
         ret = CorelThumbnail_W(src_filename, tmppng);
-    }
-
     return ret;
 }