Toolbox.h 2.57 KB
#ifndef TOOLBOX_H
#define TOOLBOX_H

#include <string>

#include <sys/types.h>

#include "TREPSTypes.h"

using namespace std;

namespace TREPS
{
	namespace Common
	{
		//get a random string
		string genRandomStr(int len);

		//get the current timestamp
		long int getCrtTime(void);
		
		//get the current timestamp in a string
		string getCrtTimeStr(void);

		//struct tm to TREPS time struct
		void tmToTime(struct tm tm, t_Time &t);

		//set month and day in time struct from day of year. (t.ye must be defined!)
		void setDOY(t_Time &t, long int doy);
		
		//get day of year from time struct
		long int getDOY(t_Time t);

		//test if the year is a leap year
		bool isLeapYear(long int year);

		//extract the file name to a path
		string getFileNameStr(const char *file_path);

		//trim a string
		string getTRIMStr(const char *s);

		//convert integer to string
		string intToStr(const int n);

		//convert long int to string
		string longToStr(const long int n);

		//convert long long int to string
		string longlongToStr(const long long int n);
		
		//convert double to string
		string doubleToStr(const double d);

		//replace f string by t string in s string, and return result
		string replaceInStr(const char *s, const char *f, const char *t);
		
		//apply a correction to a path (ie, add "/" at the end of the path if not exist)
		string getPathCorrection(const char *file_path);

		//get current executable path
		string getExecPath(void);
		
		//create a directory with specific rights set to mode
		bool createDir(const char *dir_path, mode_t mode);

		//delete recursively a directory
		bool deleteDir(const char *dir_path);

		//is it a directory?
		bool isDir(const char *dir_path);

		//check if we have the write permission for dir
		bool haveDirWritePermission(const char *dir_path);

		//is file exist
		bool isFileExist(const char *file_path);

		//check if we have write permission to a given file
		bool haveFileWritePermission(const char *file_path);

		//delete a file
		bool deleteFile(const char *file_path);

		//get the size in octets of a file
		int getFileSize(const char *file_path);
		
		//execute a system command
		int executeSystemCommand(const char *cmd, string &output);

		//test system command error
		bool sysCmdError(int status);

		//get the current process id
		int getCrtProcId(void);

		//check if a process is running
		bool procIsRunning(int procId);

		//sleepfor the specified number of seconds
		void doSleep(long int sec);
		
		//get a file MIME type
		string getFileMIMEType(const char *file_path);

		//field type to string
		string fieldTypeToStr(t_FieldType type);
	}
}

#endif