Add Folder check and Folder creation
This commit is contained in:
Symlink
+1
@@ -0,0 +1 @@
|
||||
../Makefile
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "Util.h"
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <filesystem/path.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
||||
namespace CameraRecorder {
|
||||
namespace Util {
|
||||
|
||||
std::string GetDateTimeString(bool omitSeparator)
|
||||
{
|
||||
std::array<char, 17> buffer;
|
||||
buffer.fill(0);
|
||||
std::time_t t = std::time(nullptr);
|
||||
std::tm* tm = std::localtime(&t);
|
||||
|
||||
if (omitSeparator)
|
||||
strftime(buffer.data(), sizeof(buffer), "%Y%m%d%H%M%S", tm);
|
||||
else
|
||||
strftime(buffer.data(), sizeof(buffer), "%Y%m%d-%H%M%S", tm);
|
||||
|
||||
return buffer.data();
|
||||
}
|
||||
|
||||
bool FolderExists(const std::string& path)
|
||||
{
|
||||
struct stat info;
|
||||
if (stat(path.c_str(), &info) != 0)
|
||||
return false;
|
||||
else if (info.st_mode & S_IFDIR)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void CreateFolder(const std::string& path)
|
||||
{
|
||||
int status = mkdir(path.c_str(), 0777);
|
||||
if ((status < 0) && (errno != EEXIST))
|
||||
throw std::runtime_error("Can't create folder.");
|
||||
}
|
||||
|
||||
} // namespace Util
|
||||
} // namespace CameraRecorder
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
#ifndef UTIL_UTIL_H
|
||||
#define UTIL_UTIL_H
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace CameraRecorder {
|
||||
namespace Util {
|
||||
|
||||
std::string GetDateTimeString(bool omitSeparator = false);
|
||||
|
||||
bool FolderExists(const std::string& path);
|
||||
void CreateFolder(const std::string& path);
|
||||
|
||||
} // namespace Util
|
||||
} // namespace CameraRecorder
|
||||
|
||||
#endif // UTIL_UTIL_H
|
||||
Reference in New Issue
Block a user