#include "filesystem.hpp"
|
|
#include <stdlib.h>
|
#include <unistd.h>
|
#include <sys/stat.h>
|
#include <string.h>
|
|
extern "C"{
|
#include "../thirdparty/whereami/whereami.h"
|
}
|
|
#include "../ffmpeg/log/log.hpp"
|
using namespace logif;
|
|
namespace common{
|
|
bool getExecutablePath(std::string &exec_path){
|
char* path = NULL;
|
int length, dirname_length;
|
int i;
|
|
length = wai_getExecutablePath(NULL, 0, &dirname_length);
|
if (length > 0)
|
{
|
path = (char*)malloc(length + 1);
|
if (!path)
|
return false;
|
wai_getExecutablePath(path, length, &dirname_length);
|
path[length] = '\0';
|
exec_path = path;
|
|
// printf("executable path: %s\n", path);
|
// path[dirname_length] = '\0';
|
// printf(" dirname: %s\n", path);
|
// printf(" basename: %s\n", path + dirname_length + 1);
|
free(path);
|
}
|
return true;
|
}
|
|
bool getExecutableDirectory(std::string &exec_dir){
|
char* path = NULL;
|
int length, dirname_length;
|
int i;
|
|
length = wai_getExecutablePath(NULL, 0, &dirname_length);
|
if (length > 0)
|
{
|
path = (char*)malloc(length + 1);
|
if (!path)
|
return false;
|
wai_getExecutablePath(path, length, &dirname_length);
|
path[length] = '\0';
|
|
// printf("executable path: %s\n", path);
|
path[dirname_length] = '\0';
|
// printf(" dirname: %s\n", path);
|
// printf(" basename: %s\n", path + dirname_length + 1);
|
|
exec_dir = path;
|
|
free(path);
|
}
|
return true;
|
}
|
|
bool makeDirectory(std::string &dir){
|
if(dir == "." || dir == "./" || dir == ".." || dir == "../"){
|
return true;
|
}
|
std::string str_dir(dir);
|
if(str_dir.find("/") == std::string::npos){
|
std::string temp;
|
const bool ret = getExecutableDirectory(temp);
|
if(ret){
|
str_dir = temp + "/" + dir;
|
}
|
}
|
|
char DirName[256];
|
strcpy(DirName, str_dir.c_str());
|
int i = 0;
|
int len = strlen(DirName);
|
if(DirName[len-1]!='/'){
|
strcat(DirName, "/");
|
|
len = strlen(DirName);
|
}
|
|
|
for(i=1; i<len; i++)
|
{
|
if(DirName[i]=='/')
|
{
|
DirName[i] = 0;
|
if( access(DirName, NULL)!=0 )
|
{
|
if(mkdir(DirName, 0777)==-1)
|
{
|
logIt("record mkdir error\n");
|
return false;
|
}
|
}
|
DirName[i] = '/';
|
}
|
}
|
|
return true;
|
}
|
|
std::string getTimeStampString(){
|
time_t now;
|
struct tm *now_time;
|
time(&now);
|
now_time = localtime(&now);
|
char timestamp[64];
|
snprintf(timestamp, 64, "%d%02d%02d%02d%02d%02d",
|
now_time->tm_year+1900, now_time->tm_mon+1,
|
now_time->tm_mday, now_time->tm_hour,
|
now_time->tm_min,now_time->tm_sec);
|
return timestamp;
|
}
|
|
}
|