2023-12-03 19:43:41 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
2023-09-17 22:13:42 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <time.h>
|
2023-12-03 19:43:41 +03:00
|
|
|
#include <chrono>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-09-17 22:13:42 +03:00
|
|
|
#include "String.h"
|
2023-12-03 19:43:41 +03:00
|
|
|
|
2023-09-17 22:13:42 +03:00
|
|
|
class Utils {
|
|
|
|
|
public:
|
2023-12-03 19:43:41 +03:00
|
|
|
static int max(int a, int b) {
|
|
|
|
|
return (a > b) ? a : b;
|
|
|
|
|
}
|
|
|
|
|
static int min(int a, int b) {
|
|
|
|
|
return (a > b) ? b : a;
|
2023-09-17 22:13:42 +03:00
|
|
|
}
|
2023-12-03 19:43:41 +03:00
|
|
|
static void Mkdir(const String& path) {
|
|
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
|
std::filesystem::create_directory(path.getCharArray());
|
|
|
|
|
#else
|
2023-09-17 22:13:42 +03:00
|
|
|
mkdir(path.getCharArray(), 0777);
|
2023-12-03 19:43:41 +03:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-17 22:13:42 +03:00
|
|
|
//https://stackoverflow.com/questions/4568681/using-chmod-in-a-c-program
|
2023-12-03 19:43:41 +03:00
|
|
|
static void Chmod(const String& path) {
|
2023-12-05 16:03:42 +03:00
|
|
|
#if __cplusplus >= 201703L
|
2023-12-04 18:42:20 +03:00
|
|
|
std::filesystem::permissions(path.getCharArray(), std::filesystem::perms::all);
|
2023-12-05 16:03:42 +03:00
|
|
|
#else
|
2023-12-04 18:42:20 +03:00
|
|
|
String command = "chmod 777 " + String::DQuotes(path);
|
|
|
|
|
int i=system(command.getCharArray());
|
|
|
|
|
printf("chmod 777 '%s' return code = %d\n", path.getCharArray(), i);
|
2023-12-05 16:03:42 +03:00
|
|
|
#endif
|
2023-12-03 19:43:41 +03:00
|
|
|
}
|
|
|
|
|
|
2023-09-17 22:13:42 +03:00
|
|
|
//https://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c
|
2023-12-03 19:43:41 +03:00
|
|
|
static bool Exists(const String& path) {
|
2023-12-05 16:03:42 +03:00
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
|
return std::filesystem::exists(path.getCharArray());
|
|
|
|
|
#else
|
2023-12-03 19:43:41 +03:00
|
|
|
struct stat buffer;
|
|
|
|
|
return (stat(path.getCharArray(), &buffer) == 0);
|
2023-12-05 16:03:42 +03:00
|
|
|
#endif
|
2023-12-03 19:43:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//in seconds
|
|
|
|
|
static void Sleep(int s) {
|
|
|
|
|
std::chrono::seconds timespan(s);
|
|
|
|
|
std::this_thread::sleep_for(timespan);
|
|
|
|
|
}
|
|
|
|
|
static void Copy(const String& src, const String& dst) {
|
2023-12-05 16:03:42 +03:00
|
|
|
#if __cplusplus >= 201703L
|
2023-12-04 18:42:20 +03:00
|
|
|
std::filesystem::copy(src.getCharArray(), dst.getCharArray());
|
2023-12-05 16:03:42 +03:00
|
|
|
#else
|
2023-12-04 18:42:20 +03:00
|
|
|
String command = "cp " + String::DQuotes(src) + " " + String::DQuotes(dst);
|
|
|
|
|
int i = system(command.getCharArray());
|
|
|
|
|
printf("cp '%s' return code = %d\n",src.getCharArray(), i);
|
2023-12-05 16:03:42 +03:00
|
|
|
#endif
|
2023-12-03 19:43:41 +03:00
|
|
|
}
|
2023-12-04 18:42:20 +03:00
|
|
|
static void CopyDirectory(const String& src, const String& dst) {
|
2023-12-05 16:03:42 +03:00
|
|
|
#if __cplusplus >= 201703L
|
2023-12-04 18:42:20 +03:00
|
|
|
std::filesystem::copy((src+ "/.").getCharArray(), dst.getCharArray(), std::filesystem::copy_options::recursive);
|
2023-12-05 16:03:42 +03:00
|
|
|
#else
|
2023-12-04 18:42:20 +03:00
|
|
|
String command = "cp -r " + String::DQuotes(src + "/.") + " " + String::DQuotes(dst);
|
|
|
|
|
int i = system(command.getCharArray());
|
|
|
|
|
printf("cp -r '%s' return code = %d\n",src.getCharArray(),i);
|
2023-12-05 16:03:42 +03:00
|
|
|
#endif
|
2023-12-04 18:42:20 +03:00
|
|
|
}
|
2023-12-03 19:43:41 +03:00
|
|
|
static time_t getAbsoluteTime() {
|
|
|
|
|
return time(NULL);
|
|
|
|
|
}
|
|
|
|
|
static String getDate() {
|
|
|
|
|
auto ttime = time(NULL);
|
|
|
|
|
String res(ctime(&ttime));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
};
|