Guest User

Untitled

a guest
Jul 18th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.31 KB | Source Code | 0 0
  1. #ifndef F_O_H
  2. #define F_O_H
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <Windows.h>
  7. #include <random>
  8. #include <filesystem>
  9. #include <fstream>
  10. #include <memory>
  11. #include <vector>
  12. #include <thread>
  13. #include <mutex>
  14. #include <spdlog/spdlog.h>
  15. #include <spdlog/sinks/rotating_file_sink.h>
  16. #include <atomic>
  17. #include <tclap/CmdLine.h>
  18. #include <ncurses.h>
  19. #include <thread>
  20. #include <future>
  21.  
  22. class Ex : public std::exception {
  23. public:
  24.     Ex(const std::string& msg) : m(msg) {}
  25.     const char* what() const noexcept override { return m.c_str(); }
  26. private:
  27.     std::string m;
  28. };
  29.  
  30. class Cfg {
  31. public:
  32.     static const int A = 3;
  33.     static const int B = 4;
  34.     static const int C = 10;
  35.     static const std::string D;
  36.     static const int E;
  37.     static const int F;
  38. };
  39.  
  40. const std::string Cfg::D = "pl.txt";
  41. const int Cfg::E = 1024 * 1024;
  42. const int Cfg::F = 5;
  43.  
  44. class F_O {
  45. public:
  46.     static std::vector<std::filesystem::path> F_F(const std::filesystem::path& dp, const std::wstring& e) {
  47.         std::vector<std::filesystem::path> f;
  48.         try {
  49.             for (const auto& x : std::filesystem::directory_iterator(dp)) {
  50.                 if (x.path().extension() == e) {
  51.                     f.push_back(x.path());
  52.                 }
  53.             }
  54.         } catch (const std::filesystem::filesystem_error& e) {
  55.             throw Ex("Error accessing dir " + dp.string() + ": " + e.what());
  56.         }
  57.         return f;
  58.     }
  59.  
  60.     static bool D_F(const std::wstring& fp) {
  61.         for (int i = 0; i < Cfg::A; ++i) {
  62.             if (::DeleteFileW(fp.c_str()) != 0) {
  63.                 return true;
  64.             }
  65.             spdlog::warn("Retrying del of file: {}", std::string(fp.begin(), fp.end()));
  66.             std::this_thread::sleep_for(std::chrono::milliseconds(100));
  67.         }
  68.         throw Ex("Failed to del file after " + std::to_string(Cfg::A) + " retries: " + std::string(fp.begin(), fp.end()));
  69.     }
  70.  
  71.     static bool R_F(const std::filesystem::path& fp, const std::filesystem::path& nfp) {
  72.         try {
  73.             std::filesystem::rename(fp, nfp);
  74.             return true;
  75.         } catch (const std::filesystem::filesystem_error& e) {
  76.             throw Ex("Failed to rename file " + fp.string() + ": " + e.what());
  77.         }
  78.     }
  79.  
  80.     static bool R_E(const std::filesystem::path& fp) {
  81.         std::filesystem::path nfp = fp;
  82.         nfp.replace_extension();
  83.         return R_F(fp, nfp);
  84.     }
  85. };
  86.  
  87. class P_M {
  88. public:
  89.     static bool C_H_P(const std::wstring& ep) {
  90.         STARTUPINFOW si{};
  91.         PROCESS_INFORMATION pi{};
  92.  
  93.         si.cb = sizeof(si);
  94.         si.dwFlags = STARTF_USESHOWWINDOW;
  95.         si.wShowWindow = SW_HIDE;
  96.  
  97.         for (int i = 0; i < Cfg::A; ++i) {
  98.             if (CreateProcessW(ep.c_str(), NULL, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
  99.                 CloseHandle(pi.hProcess);
  100.                 CloseHandle(pi.hThread);
  101.                 return true;
  102.             }
  103.             spdlog::warn("Retrying creation of proc for: {}", std::string(ep.begin(), ep.end()));
  104.             std::this_thread::sleep_for(std::chrono::milliseconds(100));
  105.         }
  106.         throw Ex("Failed to create proc after " + std::to_string(Cfg::A) + " retries: " + std::string(ep.begin(), ep.end()));
  107.     }
  108. };
  109.  
  110. void H_C_W() {
  111.     HWND h = GetConsoleWindow();
  112.     if (h != NULL) {
  113.         ShowWindow(h, SW_HIDE);
  114.     } else {
  115.         throw Ex("Failed to get console window handle");
  116.     }
  117. }
  118.  
  119. bool is_v_c(int c, int m, int x) {
  120.     return c >= m && c <= x;
  121. }
  122.  
  123. void S_P(std::atomic<int>& p, int t) {
  124.     initscr();
  125.     cbreak();
  126.     noecho();
  127.     nodelay(stdscr, TRUE);
  128.     curs_set(0);
  129.  
  130.     while (p < t) {
  131.         clear();
  132.         int cp = p.load();
  133.         int pc = (cp * 100) / t;
  134.         mvprintw(0, 0, "Progress: %d%% (%d/%d)", pc, cp, t);
  135.         refresh();
  136.         std::this_thread::sleep_for(std::chrono::milliseconds(100));
  137.     }
  138.  
  139.     endwin();
  140. }
  141.  
  142. class Prg {
  143. public:
  144.     Prg(int ac, char* av[]) : cmd("F O Prg", ' ', "1.0") {
  145.         s_a();
  146.         p_a(ac, av);
  147.     }
  148.  
  149.     void r() {
  150.         s_l();
  151.         H_C_W();
  152.  
  153.         std::vector<std::filesystem::path> f = g_f();
  154.         p_f(f);
  155.  
  156.         spdlog::info("Prg completed successfully");
  157.     }
  158.  
  159. private:
  160.     TCLAP::CmdLine cmd;
  161.     TCLAP::ValueArg<int> a_a;
  162.     TCLAP::ValueArg<int> f_a;
  163.     TCLAP::ValueArg<std::string> d_a;
  164.     TCLAP::ValueArg<std::string> n_a;
  165.     TCLAP::ValueArg<std::string> e_a;
  166.  
  167.     void s_a() {
  168.         a_a = TCLAP::ValueArg<int>("a", "action", "A choice (1: Rename, 2: Remove Ext, 3: Exec and Del)", true, 1, "int");
  169.         f_a = TCLAP::ValueArg<int>("f", "fileChoice", "F choice (1: Single, 2: All in dir)", true, 1, "int");
  170.         d_a = TCLAP::ValueArg<std::string>("d", "dir", "Dir path", true, "", "string");
  171.         n_a = TCLAP::ValueArg<std::string>("n", "name", "F name (req if f_a is 1)", false, "", "string");
  172.         e_a = TCLAP::ValueArg<std::string>("e", "ext", "F ext (e.g., .exe)", true, "", "string");
  173.  
  174.         cmd.add(a_a);
  175.         cmd.add(f_a);
  176.         cmd.add(d_a);
  177.         cmd.add(n_a);
  178.         cmd.add(e_a);
  179.     }
  180.  
  181.     void p_a(int ac, char* av[]) {
  182.         try {
  183.             cmd.parse(ac, av);
  184.         } catch (TCLAP::ArgException &e) {
  185.             throw Ex("Cmd-line arg error: " + std::string(e.error()) + " for arg " + e.argId());
  186.         }
  187.     }
  188.  
  189.     void s_l() {
  190.         auto rl = spdlog::rotating_logger_mt("file_logger", Cfg::D, Cfg::E, Cfg::F);
  191.         spdlog::set_default_logger(rl);
  192.         spdlog::set_level(spdlog::level::info);
  193.         spdlog::info("Prg started");
  194.     }
  195.  
  196.     std::vector<std::filesystem::path> g_f() {
  197.         std::filesystem::path dp(d_a.getValue());
  198.         if (!std::filesystem::exists(dp)) {
  199.             throw Ex("Specified dir does not exist: " + d_a.getValue());
  200.         }
  201.  
  202.         if (f_a.getValue() == 1) {
  203.             return {dp / n_a.getValue()};
  204.         } else {
  205.             return F_O::F_F(dp, std::wstring(e_a.getValue().begin(), e_a.getValue().end()));
  206.         }
  207.     }
  208.  
  209.     void p_f(const std::vector<std::filesystem::path>& f) {
  210.         std::atomic<int> p(0);
  211.         int t = f.size();
  212.  
  213.         std::thread pt(S_P, std::ref(p), t);
  214.  
  215.         std::vector<std::future<void>> fs;
  216.         for (int i = 0; i < t; i += Cfg::C) {
  217.             int e = std::min(i + Cfg::C, t);
  218.             fs.push_back(std::async(std::launch::async, [this, &f, &p, i, e]() {
  219.                 for (int j = i; j < e; ++j) {
  220.                     p_f(f[j]);
  221.                     ++p;
  222.                 }
  223.             }));
  224.         }
  225.  
  226.         for (auto& fu : fs) {
  227.             fu.wait();
  228.         }
  229.  
  230.         pt.join();
  231.     }
  232.  
  233.     void p_f(const std::filesystem::path& f) {
  234.         try {
  235.             if (a_a.getValue() == 1) {
  236.                 r_f(f);
  237.             } else if (a_a.getValue() == 2) {
  238.                 r_e(f);
  239.             } else if (a_a.getValue() == 3) {
  240.                 e_d(f);
  241.             }
  242.         } catch (const Ex& e) {
  243.             spdlog::error("Error processing f {}: {}", f.string(), e.what());
  244.         }
  245.     }
  246.  
  247.     void r_f(const std::filesystem::path& f) {
  248.         std::wstring nfn;
  249.         {
  250.             std::lock_guard<std::mutex> g(m);
  251.             std::wcout << L"Enter new name for f " << f.wstring() << L": ";
  252.             std::wcin >> nfn;
  253.         }
  254.         std::filesystem::path nfp = f.parent_path() / nfn;
  255.         if (F_O::R_F(f, nfp)) {
  256.             spdlog::info("Renamed f {} to {}", f.string(), nfp.string());
  257.         }
  258.     }
  259.  
  260.     void r_e(const std::filesystem::path& f) {
  261.         if (F_O::R_E(f)) {
  262.             spdlog::info("Removed ext from f {}", f.string());
  263.         }
  264.     }
  265.  
  266.     void e_d(const std::filesystem::path& f) {
  267.         if (P_M::C_H_P(f.wstring())) {
  268.             spdlog::info("Proc created for: {}", f.string());
  269.             if (F_O::D_F(f.wstring())) {
  270.                 spdlog::info("Deleted f: {}", f.string());
  271.             }
  272.         }
  273.     }
  274.  
  275.     std::mutex m;
  276. };
  277.  
  278. int main(int ac, char* av[]) {
  279.     try {
  280.         Prg prg(ac, av);
  281.         prg.r();
  282.     } catch (const Ex& e) {
  283.         spdlog::error("An error occurred: {}", e.what());
  284.         return 1;
  285.     } catch (const std::exception& e) {
  286.         spdlog::error("An unexpected error occurred: {}", e.what());
  287.         return 1;
  288.     }
  289.  
  290.     return 0;
  291. }
  292.  
  293. #endif // F_O_H
  294.  
Advertisement
Add Comment
Please, Sign In to add comment