#include "Toolbox.h" #include #include #include #include #include #include #include #include #include #include #include #include "magic.h" namespace TREPS { namespace Common { //list of available char for string generation static const char alphanum[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"; string genRandomStr(int len) { string s = ""; for (int i = 0; i < len; ++i) s += alphanum[rand() % (sizeof(alphanum) - 1)]; return s; } long int getCrtTime(void) { //time in UTC return static_cast(time(NULL)); } string getCrtTimeStr(void) { time_t t = time(NULL); stringstream strm; strm << t; return strm.str(); } void tmToTime(struct tm tm, t_Time &t) { t.ho = tm.tm_hour; t.mi = tm.tm_min; t.se = tm.tm_sec; t.ye = tm.tm_year + 1900; t.mo = tm.tm_mon+1; t.da = tm.tm_mday; t.ml = 0; } void setDOY(t_Time &t, long int doy) { struct tm tm; memset(&tm,0,sizeof(struct tm)); tm.tm_year = t.ye-1900; tm.tm_mday = doy; mktime(&tm); t.mo = tm.tm_mon+1; t.da = tm.tm_mday; } long int getDOY(t_Time t) { struct tm tm; memset(&tm,0,sizeof(struct tm)); tm.tm_year = t.ye-1900; tm.tm_mon = t.mo-1; tm.tm_mday = t.da; mktime(&tm); return tm.tm_yday+1; } bool isLeapYear(long int year) { return ((year & 3) == 0)&&(((year % 100) != 0)||((year % 400)==0)); } string getFileNameStr(const char *file_path) { string str = file_path; return str.substr( str.find_last_of("/\\") + 1 ); } string getTRIMStr(const char *s) { string str = s; str.erase (str.find_last_not_of (" \n\r\t") + 1); str.erase (0, str.find_first_not_of (" \n\r\t")); return str; } string intToStr(const int n) { stringstream ss; ss << n; return ss.str(); } string longToStr(const long int n) { stringstream ss; ss << n; return ss.str(); } string longlongToStr(const long long int n) { stringstream ss; ss << n; return ss.str(); } string doubleToStr(const double d) { char tmp[1024]; memset(tmp,0,sizeof(char)*1024); sprintf(tmp,"%.10f",d); string s = tmp; return s; } string replaceInStr(const char *s, const char *f, const char *t) { //replace f by t in s string str = s; size_t pos; pos = str.find(f); while (pos != string::npos) { str.replace(pos, strlen(f), t); pos = str.find(f); } return str; } string getFileMIMEType(const char *file_path) { magic_t myt = magic_open(MAGIC_CONTINUE|MAGIC_ERROR|MAGIC_MIME); if(magic_errno(myt) > 0) return ""; magic_load(myt,NULL); if(magic_errno(myt) > 0) return ""; const char *r = magic_file(myt,file_path); string res = ""; if (r != NULL) res = r; magic_close(myt); return res; } string getPathCorrection(const char *file_path) { string str = file_path; if (str.length() > 0) if (str[str.length()-1] != '/') str += "/"; return str; } string getExecPath(void) { //get the application path string fullFileName = ""; string path = ""; //get application pid pid_t pid = getpid(); char buf[20] = {0}; sprintf(buf,"%d",pid); // string _link = "/proc/"; _link.append(buf); _link.append( "/exe"); char proc[512]; int ch = readlink(_link.c_str(),proc,512); if (ch != -1) { proc[ch] = 0; path = proc; string::size_type t = path.find_last_of("/"); path = path.substr(0,t); } fullFileName = path + string("/"); return fullFileName; } bool isDir(const char *dir_path) { struct stat s_buf; if (stat(dir_path, &s_buf)) return false; return (S_ISDIR(s_buf.st_mode) > 0); } bool haveDirWritePermission(const char *dir_path) { //test write permission for a directory if (!isDir(dir_path)) return false; struct stat s_buf; if (stat(dir_path, &s_buf)) return false; return (s_buf.st_mode & S_IWUSR); } bool createDir(const char *dir_path, mode_t mode) { mode_t process_mask = umask(0); bool res = (mkdir(dir_path, mode) != -1); umask(process_mask); return res; } bool deleteDir(const char *dir_path) { //delete dir recursively DIR* dp; struct dirent* ep; string crtPath; if (!isDir(dir_path)) return false; dp = opendir(dir_path); while ((ep = readdir(dp)) != NULL) { crtPath = dir_path; crtPath = getPathCorrection(crtPath.c_str()); crtPath += ep->d_name; if (isDir(crtPath.c_str())) { if (crtPath.compare(".") || crtPath.compare("..")) continue; if (!deleteDir(crtPath.c_str())) { closedir(dp); return false; } } else if (unlink(crtPath.c_str()) == -1) { closedir(dp); return false; } } closedir(dp); return (rmdir(dir_path) != -1); } bool isFileExist(const char *file_path) { ifstream f(file_path); if (f.good()) { f.close(); return true; } f.close(); return false; } bool deleteFile(const char *file_path) { if (!isFileExist(file_path)) return false; return (unlink(file_path) != -1); } bool haveFileWritePermission(const char *file_path) { //test file write permission if (!isFileExist(file_path)) return false; struct stat s_buf; if (stat(file_path, &s_buf)) return false; return (s_buf.st_mode & S_IWUSR); } int getFileSize(const char *file_path) { struct stat s_buf; if (stat(file_path, &s_buf)) return -1; return s_buf.st_size; } #define TREPS_SYSCMDERR_POPEN -100 #define TREPS_SYSCMDERR_PCLOSE -101 #define TREPS_SYSCMDERR_SIGNALED -102 #define TREPS_SYSCMDERR_COREDUMP -103 #define TREPS_SYSCMDERR_STOPPED -104 #define TREPS_SYSCMDERR_CONTINUED -105 #define TREPS_SYSCMDERR_UNKNOWN -106 int executeSystemCommand(const char *cmd, string &output) { output = ""; signal(SIGCHLD, SIG_DFL); FILE* pipe = popen(cmd, "r"); if (!pipe) return TREPS_SYSCMDERR_POPEN; const int max_buffer = 2048; char buffer[max_buffer]; while (fgets(buffer, max_buffer, pipe) != NULL) output += buffer; int status; if ((status = pclose(pipe)) == -1) return TREPS_SYSCMDERR_PCLOSE; if (WIFEXITED(status)) return WEXITSTATUS(status); if (WIFSIGNALED(status)) { if (WCOREDUMP(status)) return TREPS_SYSCMDERR_COREDUMP; return TREPS_SYSCMDERR_SIGNALED; } if (WIFSTOPPED(status)) return TREPS_SYSCMDERR_STOPPED; if (WIFCONTINUED(status)) return TREPS_SYSCMDERR_CONTINUED; return TREPS_SYSCMDERR_UNKNOWN; } bool sysCmdError(int status) { return ((status == TREPS_SYSCMDERR_POPEN) || (status == TREPS_SYSCMDERR_PCLOSE) || (status == TREPS_SYSCMDERR_SIGNALED) || (status == TREPS_SYSCMDERR_COREDUMP) || (status == TREPS_SYSCMDERR_STOPPED) || (status == TREPS_SYSCMDERR_CONTINUED) || (status == TREPS_SYSCMDERR_UNKNOWN)); } int getCrtProcId(void) { return (int)getpid(); } bool procIsRunning(int procId) { //test if a process is running string procDir = "/proc/"; procDir += intToStr(procId); return isDir(procDir.c_str()); } void doSleep(long int sec) { sleep(sec); } string fieldTypeToStr(t_FieldType type) { switch (type) { case FT_TIME : return "time"; case FT_FLOAT : return "float"; case FT_DOUBLE : return "double"; case FT_SHORT : return "short"; case FT_INT : return "int"; case FT_LONG : return "long"; case FT_UNKNOWN : return ""; } return ""; } } }