Blame view

server/kernel/src/Common/Toolbox.h 2.57 KB
346b85c6   Benjamin Renard   First commit with...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#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