164 lines
3.5 KiB
C++
164 lines
3.5 KiB
C++
|
|
#include "uTurtleAPI.h"
|
||
|
|
#include <cstdlib>
|
||
|
|
#include <execinfo.h>
|
||
|
|
#include <dirent.h>
|
||
|
|
#include <cmath>
|
||
|
|
#include <sys/time.h>
|
||
|
|
#include <sys/stat.h>
|
||
|
|
#include <dlfcn.h>
|
||
|
|
#include <QtGlobal>
|
||
|
|
using namespace pai::turtle;
|
||
|
|
|
||
|
|
|
||
|
|
uTurtleAPI::uTurtleAPI() {
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
//uTurtleAPI::~uTurtleAPI() {
|
||
|
|
//
|
||
|
|
//}
|
||
|
|
|
||
|
|
void uTurtleAPI::Sleep(unsigned long long time) {
|
||
|
|
usleep(1000 * time);
|
||
|
|
}
|
||
|
|
|
||
|
|
void uTurtleAPI::SysCall(const std::string& oper, const std::string& name,
|
||
|
|
const std::string& param, const std::string& path, int cmd) {
|
||
|
|
system(oper.c_str());
|
||
|
|
}
|
||
|
|
|
||
|
|
unsigned int uTurtleAPI::CreateThread(LPTHREAD_START_ROUTINE pFunction,
|
||
|
|
void * param) {
|
||
|
|
pthread_t handle;
|
||
|
|
pthread_attr_t attr;
|
||
|
|
pthread_attr_init(&attr);
|
||
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||
|
|
int ret = pthread_create(&handle, &attr,
|
||
|
|
(LPTHREAD_START_ROUTINE) pFunction, (void *) param);
|
||
|
|
pthread_attr_destroy(&attr);
|
||
|
|
return ret;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool uTurtleAPI::DeletePath(std::string &path) {
|
||
|
|
if(remove(path.c_str()) == 0)
|
||
|
|
return true;
|
||
|
|
else
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool uTurtleAPI::CheckPath(std::string& path) {
|
||
|
|
//TODO
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool uTurtleAPI::MkDir(std::string &path) {
|
||
|
|
char const *b = path.c_str();
|
||
|
|
int status = mkdir(b, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||
|
|
if (0 == status)
|
||
|
|
return true;
|
||
|
|
else
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool uTurtleAPI::ScanDir(const std::string &path,std::vector<std::string> &filelist) {
|
||
|
|
struct dirent **namelist;
|
||
|
|
char const *b = path.c_str();
|
||
|
|
std::string listFsPath = "";
|
||
|
|
int n = scandir(b, &namelist, 0, alphasort);
|
||
|
|
if(n < 0)
|
||
|
|
return false;
|
||
|
|
else {
|
||
|
|
for(int i=0;i<n;i++)
|
||
|
|
{
|
||
|
|
listFsPath = namelist[i]->d_name;
|
||
|
|
filelist.push_back(listFsPath);
|
||
|
|
free(namelist[i]);
|
||
|
|
}
|
||
|
|
free(namelist);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool uTurtleAPI::Readline(FILE *file,std::string &str) {
|
||
|
|
char *line = NULL;
|
||
|
|
size_t len = 0;
|
||
|
|
ssize_t items_read = getline(&line, &len, file);
|
||
|
|
str = line;
|
||
|
|
free(line);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
unsigned long long uTurtleAPI::GetSystemTime() {
|
||
|
|
struct timeval tv;
|
||
|
|
gettimeofday(&tv, NULL);
|
||
|
|
unsigned long long t = tv.tv_sec;
|
||
|
|
t *=1000*1000;
|
||
|
|
t +=tv.tv_usec;
|
||
|
|
return t;
|
||
|
|
}
|
||
|
|
|
||
|
|
int uTurtleAPI::GetTimeOfDay(struct timeval *tv, void *tz) {
|
||
|
|
int re = gettimeofday(tv, NULL);
|
||
|
|
return re;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool uTurtleAPI::GetNSTime(long long &time) {
|
||
|
|
|
||
|
|
timespec ts;
|
||
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||
|
|
time = static_cast<long long>(ts.tv_sec) * 1000000000LL + static_cast<long long>(ts.tv_nsec);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
long long uTurtleAPI::StringToLong(const std::string &str) {
|
||
|
|
return std::atoll(str.c_str());
|
||
|
|
}
|
||
|
|
|
||
|
|
int uTurtleAPI::SetEnv(std::string &strEnvName,std::string &strEnvValue) {
|
||
|
|
return setenv(strEnvName.c_str(), strEnvValue.c_str(), 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
void uTurtleAPI::SetValue(pthread_t& pt, int value){
|
||
|
|
pt = value;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool uTurtleAPI::IsValid(pthread_t& pt){
|
||
|
|
return pt>0 ? true : false;
|
||
|
|
}
|
||
|
|
|
||
|
|
int uTurtleAPI::GetOpt(int argc, char **argv, char *opstring) {
|
||
|
|
return ::getopt(argc, argv, opstring);
|
||
|
|
}
|
||
|
|
|
||
|
|
void * uTurtleAPI::OpenLibrary(const std::string &strlibrary)
|
||
|
|
{
|
||
|
|
return dlopen(strlibrary.c_str(), RTLD_LAZY);
|
||
|
|
}
|
||
|
|
|
||
|
|
long uTurtleAPI::CloseLibrary(void *library)
|
||
|
|
{
|
||
|
|
return dlclose(library);
|
||
|
|
}
|
||
|
|
std::string uTurtleAPI::ErrorLibrary()
|
||
|
|
{
|
||
|
|
return dlerror();
|
||
|
|
}
|
||
|
|
|
||
|
|
int uTurtleAPI::GetPid() {
|
||
|
|
return static_cast<int>(getpid());
|
||
|
|
}
|
||
|
|
|
||
|
|
int uTurtleAPI::isNAN(double x)
|
||
|
|
{
|
||
|
|
return qIsNaN(x);
|
||
|
|
}
|
||
|
|
|
||
|
|
int uTurtleAPI::isINF(double x)
|
||
|
|
{
|
||
|
|
return qIsInf(x);
|
||
|
|
}
|
||
|
|
std::string uTurtleAPI::GetLibraryFileExt()
|
||
|
|
{
|
||
|
|
return ".so";
|
||
|
|
}
|