text: //
  // Created by HominSu on 2021/10/3.
  //
  
  #include <iostream>
  #include <string>
  #include <list>
  #include <chrono>
  #include <memory>
  #include <filesystem>
  
  #include "x_thread_pool/x_thread_pool.h"
  #include "task/replace_task.h"
  
  /**
   * @brief 帮助信息
   * @param _name argv[0]
   */
  void help(const char *_name) {
    std::string exec_file_name = _name;
    size_t lastPos = exec_file_name.find_last_of("/\\");
    exec_file_name = exec_file_name.substr(lastPos + 1);
  
    // 显示帮助
    std::cout << std::endl;
    std::cout << "This tool replaces the file path of the image in the XML file in the dataset" << std::endl << std::endl;
    std::cout << "Usage: " << std::endl;
  
  #if WIN32
    std::cout << "\t.\\" << exec_file_name << " [DataSet Path] [Replace Path]" << std::endl << std::endl;
  #else
    std::cout << "\t./" << exec_file_name << " [DataSet Path] [Replace Path]" << std::endl << std::endl;
  #endif
  
    std::cout << "For example: " << std::endl;
  
  #if WIN32
    std::cout << "\t.\\" << exec_file_name << R"( C:\Users\17740\Desktop\DataSet\ D:\xyolo\images\train\)"
              << std::endl << std::endl;
  #else
    std::cout << "\t./" << exec_file_name << R"( /home/hominsu/DataSet/ /home/hominsu/xyolo/images/train/)"
              << std::endl << std::endl;
  #endif
  }
  
  int main(int argc, char *argv[]) {
    // 显示帮助
    if (argc == 1 || (argc == 2 && (std::string(argv[1]) == "--help" || std::string(argv[1]) == "-h"))) {
      help(argv[0]);
      return 0;
    }
  
    std::string data_set_path = argv[1]; // 数据集路径
    std::string replace_path = argv[2];  // 替换的文件路径
    int data_set_path_length = static_cast<int>(data_set_path.length());  // 数据集路径的长度
  
    // 检查目录是否存在
    if (!std::filesystem::exists(data_set_path)) {
      std::cerr << "Error: dataSet path not exists!" << std::endl;
      return EXIT_FAILURE;
    }
  
    // 规范化替换字符串
    size_t last = replace_path.find_last_of("/\\");
    if (last != replace_path.length() - 1) {
      replace_path.push_back(replace_path.at(last));
    } else if (last == std::string::npos) {
      replace_path.push_back('/');
    }
  
    // 初始化线程池
    XThreadPool::Get()->Init();
  
    auto memory_resource = std::make_shared<std::pmr::synchronized_pool_resource>();  // 创建线程安全的内存池
  
    std::list<std::shared_ptr<ReplaceTask>> replace_task_list;  // 替换任务列表
  
    // 开始计时
    auto start_time_point = std::chrono::system_clock::now();
  
    // 遍历输入目录
    for (auto &it: std::filesystem::directory_iterator(data_set_path)) {
      // 只处理文件以及 xml 文件
      if (!it.is_regular_file() || !(it.path().extension().string() == ".xml")) {
        continue;
      }
  
      // 创建替换任务
      auto replace_task = std::make_shared<ReplaceTask>(it.path().string(), replace_path);
  
      // 设置内存资源
      replace_task->set_memory_resource(memory_resource);
  
      // 添加替换任务到线程池
      XThreadPool::Get()->AddTask(replace_task);
  
      // 将替换任务添加到替换任务列表
      replace_task_list.push_back(replace_task);
  
      // /data/ai/yolov5/data/SSM/
    }
  
    // 等待任务执行完成
    size_t replace_task_list_size = replace_task_list.size();
    size_t index = 0;
    for (auto &replace_task: replace_task_list) {
      std::cout << "[" << ++index << "/" << replace_task_list_size << "]: " << std::left
                << std::setw(data_set_path_length + 15) << replace_task->file_path_ + "," << "status: " << std::boolalpha
                << replace_task->get_return() << std::endl;
  //    replace_task->get_return();
    }
  
    // 结束计时
    std::chrono::duration<double> diff = std::chrono::system_clock::now() - start_time_point;
    std::cout << std::endl << "Used: " << diff.count() << " Second" << std::endl;
  
    // 停止线程池
    XThreadPool::Get()->Stop();
  
    return EXIT_SUCCESS;
  }
  
  // g++ --static main.cpp replace_.cpp -o main
  