Files
VisualSapfor/Planner/Utils.h

66 lines
1.6 KiB
C
Raw Normal View History

#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 15:31:50 +03:00
#include <chrono>
#include <thread>
#if __cplusplus >= 201703L
#include <filesystem>
#endif
2023-09-17 22:13:42 +03:00
#include "String.h"
2023-09-17 22:13:42 +03:00
class Utils {
public:
static int max(int a, int b) {
return (a > b) ? a : b;
2023-09-17 22:13:42 +03:00
}
static int min(int a, int b) {
return (a > b) ? b : a;
}
static void Mkdir(const String& path) {
2023-12-03 15:31:50 +03:00
#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 15:31:50 +03:00
#endif
}
2023-12-03 15:31:50 +03:00
2023-09-17 22:13:42 +03:00
//https://stackoverflow.com/questions/4568681/using-chmod-in-a-c-program
static void Chmod(const String& path) {
String command = "chmod 777 " + String::DQuotes(path);
2023-09-17 22:13:42 +03:00
system(command.getCharArray());
}
2023-12-03 15:31:50 +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
static bool Exists(const String& path) {
struct stat buffer;
return (stat(path.getCharArray(), &buffer) == 0);
}
2023-12-03 15:31:50 +03:00
//in seconds
static void Sleep(int s) {
2023-12-03 15:31:50 +03:00
std::chrono::seconds timespan(s);
std::this_thread::sleep_for(timespan);
2023-09-17 22:13:42 +03:00
}
static void Copy(const String& src, const String& dst) {
String command = "cp " + String::DQuotes(src) + " " + String::DQuotes(dst);
system(command.getCharArray());
2023-09-17 22:13:42 +03:00
}
2023-12-03 15:31:50 +03:00
static time_t getAbsoluteTime() {
return time(NULL);
2023-09-17 22:13:42 +03:00
}
static String getDate() {
2023-12-03 15:31:50 +03:00
auto ttime = time(NULL);
String res(ctime(&ttime));
return res;
2023-09-17 22:13:42 +03:00
}
static void ZipFolder(const String& src, const String& dst) {
String command = "zip -r " + String::DQuotes(dst) + " " + String::DQuotes(src);
2023-09-17 22:13:42 +03:00
system(command.getCharArray());
}
};