Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef F_O_H
- #define F_O_H
- #include <iostream>
- #include <string>
- #include <Windows.h>
- #include <random>
- #include <filesystem>
- #include <fstream>
- #include <memory>
- #include <vector>
- #include <thread>
- #include <mutex>
- #include <spdlog/spdlog.h>
- #include <spdlog/sinks/rotating_file_sink.h>
- #include <atomic>
- #include <tclap/CmdLine.h>
- #include <ncurses.h>
- #include <thread>
- #include <future>
- class Ex : public std::exception {
- public:
- Ex(const std::string& msg) : m(msg) {}
- const char* what() const noexcept override { return m.c_str(); }
- private:
- std::string m;
- };
- class Cfg {
- public:
- static const int A = 3;
- static const int B = 4;
- static const int C = 10;
- static const std::string D;
- static const int E;
- static const int F;
- };
- const std::string Cfg::D = "pl.txt";
- const int Cfg::E = 1024 * 1024;
- const int Cfg::F = 5;
- class F_O {
- public:
- static std::vector<std::filesystem::path> F_F(const std::filesystem::path& dp, const std::wstring& e) {
- std::vector<std::filesystem::path> f;
- try {
- for (const auto& x : std::filesystem::directory_iterator(dp)) {
- if (x.path().extension() == e) {
- f.push_back(x.path());
- }
- }
- } catch (const std::filesystem::filesystem_error& e) {
- throw Ex("Error accessing dir " + dp.string() + ": " + e.what());
- }
- return f;
- }
- static bool D_F(const std::wstring& fp) {
- for (int i = 0; i < Cfg::A; ++i) {
- if (::DeleteFileW(fp.c_str()) != 0) {
- return true;
- }
- spdlog::warn("Retrying del of file: {}", std::string(fp.begin(), fp.end()));
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- throw Ex("Failed to del file after " + std::to_string(Cfg::A) + " retries: " + std::string(fp.begin(), fp.end()));
- }
- static bool R_F(const std::filesystem::path& fp, const std::filesystem::path& nfp) {
- try {
- std::filesystem::rename(fp, nfp);
- return true;
- } catch (const std::filesystem::filesystem_error& e) {
- throw Ex("Failed to rename file " + fp.string() + ": " + e.what());
- }
- }
- static bool R_E(const std::filesystem::path& fp) {
- std::filesystem::path nfp = fp;
- nfp.replace_extension();
- return R_F(fp, nfp);
- }
- };
- class P_M {
- public:
- static bool C_H_P(const std::wstring& ep) {
- STARTUPINFOW si{};
- PROCESS_INFORMATION pi{};
- si.cb = sizeof(si);
- si.dwFlags = STARTF_USESHOWWINDOW;
- si.wShowWindow = SW_HIDE;
- for (int i = 0; i < Cfg::A; ++i) {
- if (CreateProcessW(ep.c_str(), NULL, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
- CloseHandle(pi.hProcess);
- CloseHandle(pi.hThread);
- return true;
- }
- spdlog::warn("Retrying creation of proc for: {}", std::string(ep.begin(), ep.end()));
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- throw Ex("Failed to create proc after " + std::to_string(Cfg::A) + " retries: " + std::string(ep.begin(), ep.end()));
- }
- };
- void H_C_W() {
- HWND h = GetConsoleWindow();
- if (h != NULL) {
- ShowWindow(h, SW_HIDE);
- } else {
- throw Ex("Failed to get console window handle");
- }
- }
- bool is_v_c(int c, int m, int x) {
- return c >= m && c <= x;
- }
- void S_P(std::atomic<int>& p, int t) {
- initscr();
- cbreak();
- noecho();
- nodelay(stdscr, TRUE);
- curs_set(0);
- while (p < t) {
- clear();
- int cp = p.load();
- int pc = (cp * 100) / t;
- mvprintw(0, 0, "Progress: %d%% (%d/%d)", pc, cp, t);
- refresh();
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- endwin();
- }
- class Prg {
- public:
- Prg(int ac, char* av[]) : cmd("F O Prg", ' ', "1.0") {
- s_a();
- p_a(ac, av);
- }
- void r() {
- s_l();
- H_C_W();
- std::vector<std::filesystem::path> f = g_f();
- p_f(f);
- spdlog::info("Prg completed successfully");
- }
- private:
- TCLAP::CmdLine cmd;
- TCLAP::ValueArg<int> a_a;
- TCLAP::ValueArg<int> f_a;
- TCLAP::ValueArg<std::string> d_a;
- TCLAP::ValueArg<std::string> n_a;
- TCLAP::ValueArg<std::string> e_a;
- void s_a() {
- a_a = TCLAP::ValueArg<int>("a", "action", "A choice (1: Rename, 2: Remove Ext, 3: Exec and Del)", true, 1, "int");
- f_a = TCLAP::ValueArg<int>("f", "fileChoice", "F choice (1: Single, 2: All in dir)", true, 1, "int");
- d_a = TCLAP::ValueArg<std::string>("d", "dir", "Dir path", true, "", "string");
- n_a = TCLAP::ValueArg<std::string>("n", "name", "F name (req if f_a is 1)", false, "", "string");
- e_a = TCLAP::ValueArg<std::string>("e", "ext", "F ext (e.g., .exe)", true, "", "string");
- cmd.add(a_a);
- cmd.add(f_a);
- cmd.add(d_a);
- cmd.add(n_a);
- cmd.add(e_a);
- }
- void p_a(int ac, char* av[]) {
- try {
- cmd.parse(ac, av);
- } catch (TCLAP::ArgException &e) {
- throw Ex("Cmd-line arg error: " + std::string(e.error()) + " for arg " + e.argId());
- }
- }
- void s_l() {
- auto rl = spdlog::rotating_logger_mt("file_logger", Cfg::D, Cfg::E, Cfg::F);
- spdlog::set_default_logger(rl);
- spdlog::set_level(spdlog::level::info);
- spdlog::info("Prg started");
- }
- std::vector<std::filesystem::path> g_f() {
- std::filesystem::path dp(d_a.getValue());
- if (!std::filesystem::exists(dp)) {
- throw Ex("Specified dir does not exist: " + d_a.getValue());
- }
- if (f_a.getValue() == 1) {
- return {dp / n_a.getValue()};
- } else {
- return F_O::F_F(dp, std::wstring(e_a.getValue().begin(), e_a.getValue().end()));
- }
- }
- void p_f(const std::vector<std::filesystem::path>& f) {
- std::atomic<int> p(0);
- int t = f.size();
- std::thread pt(S_P, std::ref(p), t);
- std::vector<std::future<void>> fs;
- for (int i = 0; i < t; i += Cfg::C) {
- int e = std::min(i + Cfg::C, t);
- fs.push_back(std::async(std::launch::async, [this, &f, &p, i, e]() {
- for (int j = i; j < e; ++j) {
- p_f(f[j]);
- ++p;
- }
- }));
- }
- for (auto& fu : fs) {
- fu.wait();
- }
- pt.join();
- }
- void p_f(const std::filesystem::path& f) {
- try {
- if (a_a.getValue() == 1) {
- r_f(f);
- } else if (a_a.getValue() == 2) {
- r_e(f);
- } else if (a_a.getValue() == 3) {
- e_d(f);
- }
- } catch (const Ex& e) {
- spdlog::error("Error processing f {}: {}", f.string(), e.what());
- }
- }
- void r_f(const std::filesystem::path& f) {
- std::wstring nfn;
- {
- std::lock_guard<std::mutex> g(m);
- std::wcout << L"Enter new name for f " << f.wstring() << L": ";
- std::wcin >> nfn;
- }
- std::filesystem::path nfp = f.parent_path() / nfn;
- if (F_O::R_F(f, nfp)) {
- spdlog::info("Renamed f {} to {}", f.string(), nfp.string());
- }
- }
- void r_e(const std::filesystem::path& f) {
- if (F_O::R_E(f)) {
- spdlog::info("Removed ext from f {}", f.string());
- }
- }
- void e_d(const std::filesystem::path& f) {
- if (P_M::C_H_P(f.wstring())) {
- spdlog::info("Proc created for: {}", f.string());
- if (F_O::D_F(f.wstring())) {
- spdlog::info("Deleted f: {}", f.string());
- }
- }
- }
- std::mutex m;
- };
- int main(int ac, char* av[]) {
- try {
- Prg prg(ac, av);
- prg.r();
- } catch (const Ex& e) {
- spdlog::error("An error occurred: {}", e.what());
- return 1;
- } catch (const std::exception& e) {
- spdlog::error("An unexpected error occurred: {}", e.what());
- return 1;
- }
- return 0;
- }
- #endif // F_O_H
Advertisement
Add Comment
Please, Sign In to add comment