#include "wTurtleAPI.h" #include "Utils.h" using namespace pai::turtle; wTurtleAPI::wTurtleAPI() { } wTurtleAPI::~wTurtleAPI() { } void wTurtleAPI::Sleep(unsigned long long time) { ::Sleep(time); } void wTurtleAPI::SysCall(const std::string& oper, const std::string& name, const std::string& param, const std::string& path, int cmd) { // ::ShellExecute(NULL, oper.c_str(), name.c_str(), param.c_str(), NULL, // cmd); } unsigned int wTurtleAPI::CreateThread(LPTHREAD_START_ROUTINE pFunction, void * param) { DWORD dwThreadID; ::CreateThread(NULL, 0, pFunction, param, 0, &dwThreadID); return dwThreadID; } bool wTurtleAPI::DeletePath(std::string &path) { if( -1 == _rmdir(path.c_str())) return false; else return true; } bool wTurtleAPI::CheckPath(std::string& path) { //TODO return true; } bool wTurtleAPI::MkDir(std::string &path) { if( -1 == mkdir(path.c_str())) return false; else return true; } bool wTurtleAPI::ScanDir(const std::string &path,std::vector &filelist) { //TODO HANDLE file; WIN32_FIND_DATA fileData; char line[1024]; wchar_t fn[1000]; mbstowcs(fn,path.c_str(),999); file = FindFirstFile(fn, &fileData); FindNextFile(file, &fileData); while(FindNextFile(file, &fileData)){ wcstombs(line,(const wchar_t*)fileData.cFileName,259); filelist.push_back(line); } return true; } bool wTurtleAPI::Readline(FILE *file,std::string &str) { char buffer[1024]; fgets(buffer, 1024, file); str = buffer; return true; } unsigned long long wTurtleAPI::GetSystemTime() { struct timeval tv; GetTimeOfDay(&tv, NULL); unsigned long long t = tv.tv_sec; t *=1000*1000; t +=tv.tv_usec; return t; } int wTurtleAPI::GetTimeOfDay(struct timeval *tv, void *tz) { time_t clock; struct tm tm; SYSTEMTIME wtm; GetLocalTime(&wtm); tm.tm_year = wtm.wYear - 1900; tm.tm_mon = wtm.wMonth - 1; tm.tm_mday = wtm.wDay; tm.tm_hour = wtm.wHour; tm.tm_min = wtm.wMinute; tm.tm_sec = wtm.wSecond; tm. tm_isdst = -1; clock = mktime(&tm); tv->tv_sec = clock; tv->tv_usec = wtm.wMilliseconds * 1000; return 0; } bool wTurtleAPI::GetNSTime(long long &time) { LARGE_INTEGER t1,tc; if (0 == QueryPerformanceFrequency(&tc)) return false; QueryPerformanceCounter(&t1); time = static_cast(t1.QuadPart); return true; } long long wTurtleAPI::StringToLong(const std::string &str) { return _atoi64(str.c_str()); } int wTurtleAPI::SetEnv(std::string &strEnvName,std::string &strEnvValue) { std::string strTemp = strEnvName + "=" + strEnvValue; int re = putenv(strTemp.c_str()); return re; } void wTurtleAPI::SetValue(pthread_t& pt, int value){ pt.x = value; } bool wTurtleAPI::IsValid(pthread_t& pt){ if (pt.x > 0) { return true; } else { return false; } } char* optarg = NULL; int optind = 1; int opterr = 1; int wTurtleAPI::GetOpt(int argc, char *argv[], char *opstring){ static char *pIndexPosition = NULL; char *pArgString = NULL; char *pOptString; if (pIndexPosition != NULL) { if (*(++pIndexPosition)) { pArgString = pIndexPosition; } } if (pArgString == NULL) { if (optind >= argc) { pIndexPosition = NULL; return -1; } pArgString = argv[optind++]; if ((_T('/') != *pArgString) && (_T('-') != *pArgString)) { --optind; optarg = NULL; pIndexPosition = NULL; return -1; } if ((strcmp(pArgString, "-") == 0) || (strcmp(pArgString,"--") == 0)) { optarg = NULL; pIndexPosition = NULL; return -1; } pArgString++; } if (_T(':') == *pArgString) { return (opterr ? (int)_T('?') : (int)_T(':')); } else if ((pOptString = strchr(opstring, *pArgString)) == 0) { optarg = NULL; pIndexPosition = NULL; return (opterr ? (int)_T('?') : (int)*pArgString); } else { if (_T(':') == (char)(*(pOptString+1))) { if (_T('\0') != (char)(*(pArgString+1))) { optarg = &pArgString[1]; } else { if (optind < argc) optarg = argv[optind++]; else { optarg = NULL; return (opterr ? (int)_T('?') : (int)*pArgString); } } pIndexPosition = NULL; } else { optarg = NULL; pIndexPosition = pArgString; } return (int)*pArgString; } } std::wstring StringToWString(const std::string &str) { std::wstring wstr(str.length(),L' '); std::copy(str.begin(), str.end(), wstr.begin()); return wstr; } void * wTurtleAPI::OpenLibrary(const std::string &strlibrary) { char *paihome; paihome = (char*)pai::utils::CUtils::GetPaiHome().c_str();//getenv( "PAI_HOME" ); // std::string modulepath(paihome); std::string libname=modulepath+"\\module\\"+strlibrary; return LoadLibrary(StringToWString(libname).c_str()); //todo aiya 重构模块路径 } long wTurtleAPI::CloseLibrary(void *library) { return FreeLibrary(HMODULE(library)); } std::string wTurtleAPI::ErrorLibrary() { DWORD errnox=GetLastError(); char buff[64]; std::sprintf(buff, "%lld", errnox); return "the library cannot load ,pls check it ,the error code is "+std::string(buff); } int wTurtleAPI::GetPid() { return static_cast(GetCurrentProcessId()); } //http://blog.csdn.net/sunmenggmail/article/details/7853081 int wTurtleAPI::isNAN(double x) { return _isnan(x); } int wTurtleAPI::isINF(double x) { return !_finite(x); } std::string wTurtleAPI::GetLibraryFileExt() { return ".dll"; }