Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JSYSTEM Namespace Reference

Auxiliary classes and methods for operating system calls. More...

Classes

struct  JDateAndTime
 Auxiliary class for date and time. More...
 
struct  JGlob
 Auxiliary class to list files. More...
 
class  JKeypress
 Enable unbuffered terminal input. More...
 
struct  JLocalTime
 Auxililary class to local get time in micro seconds. More...
 
class  JProcess
 Streaming of input and output from Linux command. More...
 
class  JShell
 The JShell clas can be used to interact with the shell via I/O streams. More...
 
struct  JStat
 Auxiliary class for file status. More...
 
struct  JSysinfo
 Auxiliary class for system information. More...
 
struct  JUTSName
 Auxiliary class for operating system information. More...
 
struct  ls
 Auxiliary data structure to list files in directory. More...
 

Typedefs

typedef long long int localtime_t
 Type definition of local time.
 

Functions

const char * getDate ()
 Get current local date conform ISO-8601 standard.
 
const char * getTime ()
 Get current local time conform ISO-8601 standard.
 
int rename (const std::string &inputFile, const std::string &outputFile)
 Rename file across file systems.
 
std::string getHostname ()
 Get host name.
 
std::string getHostname (const int ip)
 Get host name.
 
int getIPnumber (const std::string &host_name)
 Get IP number.
 
int getIPnumber ()
 Get IP number.
 
std::string getIPaddress (const int ip)
 Get IP address (decimal-dot notation).
 
std::string getIPaddress ()
 Get IP address (decimal-dot notation).
 
unsigned short getSubaddress (const int ip)
 Get host identifier within network.
 
unsigned short getSubaddress ()
 Get host identifier within network.
 
std::vector< std::string > getListOfIPaddresses ()
 Get list of IP address (decimal-dot notation).
 
float getMemoryUsage (JShell &shell, const pid_t pid)
 Get memory usage in percent of given process identifier.
 
float getMemoryUsage (JShell &shell)
 Get memory usage in percent of this process.
 
float getMemoryUsage ()
 Get memory usage in percent of this process.
 
float getCpuUsage (JShell &shell, const pid_t pid)
 Get cpu usage in percent of given process identifier.
 
float getCpuUsage (JShell &shell)
 Get cpu usage in percent of this process.
 
float getCpuUsage ()
 Get cpu usage in percent of this process.
 
pid_t getPID (JShell &shell, const char *process)
 Get process identifier.
 
pid_t getPID (const char *process)
 Get process identifier.
 
pid_t getParentID (JShell &shell, pid_t pid)
 Get parent process identifier.
 
pid_t getParentID (const pid_t pid)
 Get parent process identifier.
 
std::string getShell (JShell &shell)
 Get shell name.
 
std::string getShell ()
 Get shell name of this process.
 
unsigned long long int getRAM ()
 Get RAM of this CPU.
 
const std::string which (JShell &shell, const char *process)
 Get process path.
 
const std::string which (const char *process)
 Get process path.
 
void gprint (const std::string &message)
 Print method.
 
int gexit (int status, const std::string &message="")
 Exit method.
 
void set_variable (const std::string &name, const std::string &value)
 Set environment variable.
 

Variables

static JDateAndTime getDateAndTime
 Function object to get current date and time.
 
static JGlob getFilenames
 Function object to get list of files for given pattern.
 
static const JCompareEndian compareEndian
 Function object operator.
 
static JStat getFileStatus
 Function object for file status.
 
static const char *const SH = "sh"
 Shell names.
 
static const char *const ZSH = "zsh"
 
static const char *const KSH = "ksh"
 
static const char *const BASH = "bash"
 
static const char *const CSH = "csh"
 
static const char *const TCSH = "tcsh"
 
static const std::string SPECIAL_CHARACTERS = "\"' <>[](){};$#"
 
static const JLocalTime getLocalTime
 Function object to get local time in micro seconds.
 

Detailed Description

Auxiliary classes and methods for operating system calls.

Author
mdejong

Typedef Documentation

◆ localtime_t

typedef long long int JSYSTEM::localtime_t

Type definition of local time.

Definition at line 21 of file JSystem/JTime.hh.

Function Documentation

◆ getDate()

const char * JSYSTEM::getDate ( )
inline

Get current local date conform ISO-8601 standard.

Returns
date

Definition at line 36 of file JDateAndTime.hh.

37 {
38 static time_t ts;
39 static const size_t MAX_SIZE = 256;
40 static char buffer[MAX_SIZE];
41
42 time(&ts);
43
44 strftime(buffer, MAX_SIZE, "%F", localtime(&ts));
45
46 return buffer;
47 }

◆ getTime()

const char * JSYSTEM::getTime ( )
inline

Get current local time conform ISO-8601 standard.

Returns
time

Definition at line 55 of file JDateAndTime.hh.

56 {
57 static time_t ts;
58 static const size_t MAX_SIZE = 256;
59 static char buffer[MAX_SIZE];
60
61 time(&ts);
62
63 strftime(buffer, MAX_SIZE, "%T%z", localtime(&ts));
64
65 return buffer;
66 }

◆ rename()

int JSYSTEM::rename ( const std::string & inputFile,
const std::string & outputFile )
inline

Rename file across file systems.

Parameters
inputFileinput file
outputFileinput file
Returns
zero upon success; else non-zero

Definition at line 49 of file JFilesystem.hh.

51 {
52 using namespace std;
53
54 ifstream in (inputFile .c_str(), ios::binary);
55 ofstream out(outputFile.c_str(), ios::binary);
56
57 out << in.rdbuf();
58
59 if (out)
60 return remove(inputFile.c_str());
61 else
62 return -1;
63 }
string outputFile

◆ getHostname() [1/2]

std::string JSYSTEM::getHostname ( )
inline

Get host name.

Note that to obtain a host name including the domain name, JSYSTEM::getHostname(JSYSTEM::getIPnumber()) should be used.

Returns
host name

Definition at line 77 of file JNetwork.hh.

78 {
79 const size_t MAXIMUM_LENGTH = 256;
80 char buffer[MAXIMUM_LENGTH];
81
82 if (gethostname(buffer, MAXIMUM_LENGTH) == 0)
83 return std::string(buffer);
84 else
85 THROW(JSystemException, "Unknown host name.");
86 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Exception for system call.

◆ getHostname() [2/2]

std::string JSYSTEM::getHostname ( const int ip)
inline

Get host name.

Parameters
ipIP number
Returns
host name

Definition at line 96 of file JNetwork.hh.

97 {
98 in_addr buffer;
99
100 buffer.s_addr = ip;
101
102 hostent* p = gethostbyaddr(&buffer, sizeof(in_addr), AF_INET);
103
104 if (p != NULL)
105 return std::string(p->h_name);
106 else
107 THROW(JSystemException, "Unknown IP address " << ip);
108 }

◆ getIPnumber() [1/2]

int JSYSTEM::getIPnumber ( const std::string & host_name)
inline

Get IP number.

Parameters
host_namehost name
Returns
IP number

Definition at line 117 of file JNetwork.hh.

118 {
119 int ip = -1;
120
121 std::string buffer = host_name;
122
123 if (buffer == "" || buffer == "localhost") {
124 buffer = getHostname();
125 }
126
127 const hostent* hp = gethostbyname(buffer.c_str());
128
129 if (hp != NULL) {
130 memcpy((char *) &ip, hp->h_addr, sizeof(int));
131 }
132
133 return ip;
134 }
std::string getHostname()
Get host name.
Definition JNetwork.hh:77

◆ getIPnumber() [2/2]

int JSYSTEM::getIPnumber ( )
inline

Get IP number.

Returns
IP number

Definition at line 142 of file JNetwork.hh.

143 {
144 return getIPnumber(getHostname());
145 }
int getIPnumber(const std::string &host_name)
Get IP number.
Definition JNetwork.hh:117

◆ getIPaddress() [1/2]

std::string JSYSTEM::getIPaddress ( const int ip)
inline

Get IP address (decimal-dot notation).

Parameters
ipIP number
Returns
IP address

Definition at line 154 of file JNetwork.hh.

155 {
156 std::ostringstream os;
157
158 if (compareEndian())
159 os << ((ip >> 24) & 0xFF) << '.'
160 << ((ip >> 16) & 0xFF) << '.'
161 << ((ip >> 8) & 0xFF) << '.'
162 << ((ip >> 0) & 0xFF);
163 else
164 os << ((ip >> 0) & 0xFF) << '.'
165 << ((ip >> 8) & 0xFF) << '.'
166 << ((ip >> 16) & 0xFF) << '.'
167 << ((ip >> 24) & 0xFF);
168
169 return os.str();
170 }
static const JCompareEndian compareEndian
Function object operator.
Definition JNetwork.hh:66

◆ getIPaddress() [2/2]

std::string JSYSTEM::getIPaddress ( )
inline

Get IP address (decimal-dot notation).

Returns
IP address

Definition at line 178 of file JNetwork.hh.

179 {
180 return getIPaddress(getIPnumber());
181 }

◆ getSubaddress() [1/2]

unsigned short JSYSTEM::getSubaddress ( const int ip)
inline

Get host identifier within network.

Parameters
ipIP number
Returns
ID

Definition at line 190 of file JNetwork.hh.

191 {
192 if (compareEndian())
193 return (unsigned short) (ip & 0x0000FFFF);
194 else
195 return (unsigned short) (((ip & 0xFF000000) >> 24) |
196 ((ip & 0x00FF0000) >> 8) );
197 }

◆ getSubaddress() [2/2]

unsigned short JSYSTEM::getSubaddress ( )
inline

Get host identifier within network.

Returns
ID

Definition at line 205 of file JNetwork.hh.

206 {
207 return getSubaddress(getIPnumber());
208 }
unsigned short getSubaddress(const int ip)
Get host identifier within network.
Definition JNetwork.hh:190

◆ getListOfIPaddresses()

std::vector< std::string > JSYSTEM::getListOfIPaddresses ( )
inline

Get list of IP address (decimal-dot notation).

Returns
IP address

Definition at line 216 of file JNetwork.hh.

217 {
218 using namespace std;
219
220 const size_t MAXIMUM_LENGTH = 256;
221 char buffer[MAXIMUM_LENGTH];
222
223 vector<string> result;
224
225 struct ifaddrs *p = NULL;
226
227 if (getifaddrs(&p) == 0) {
228
229 for ( ; p != NULL; p = p->ifa_next) {
230
231 if (p->ifa_addr != NULL) {
232
233 if (p->ifa_addr->sa_family == AF_INET ||
234 p->ifa_addr->sa_family == AF_INET6) {
235
236 if (getnameinfo( p->ifa_addr,
237 (p->ifa_addr->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6),
238 buffer, MAXIMUM_LENGTH,
239 NULL, 0, NI_NUMERICHOST) == 0) {
240
241 result.push_back(buffer);
242 }
243 }
244 }
245 }
246
247 freeifaddrs(p);
248 }
249
250 return result;
251 }

◆ getMemoryUsage() [1/3]

float JSYSTEM::getMemoryUsage ( JShell & shell,
const pid_t pid )
inline

Get memory usage in percent of given process identifier.

Parameters
shellshell interface
pidprocess identifier
Returns
memory usage [%]

Definition at line 51 of file JSystemToolkit.hh.

52 {
53 using namespace std;
54
55 float value = 0.0;
56
57 shell << "ps -o %mem= -p " << pid << endl;
58
59 if (shell.get(value))
60 return value;
61 else
62 THROW(JSystemException, "No process data for PID " << pid);
63 }
bool get(T &value)
Get value.
Definition JShell.hh:106

◆ getMemoryUsage() [2/3]

float JSYSTEM::getMemoryUsage ( JShell & shell)
inline

Get memory usage in percent of this process.

Parameters
shellshell interface
Returns
memory usage [%]

Definition at line 72 of file JSystemToolkit.hh.

73 {
74 return getMemoryUsage(shell, getpid());
75 }
float getMemoryUsage(JShell &shell, const pid_t pid)
Get memory usage in percent of given process identifier.

◆ getMemoryUsage() [3/3]

float JSYSTEM::getMemoryUsage ( )
inline

Get memory usage in percent of this process.

Returns
memory usage [%]

Definition at line 83 of file JSystemToolkit.hh.

84 {
85 return getMemoryUsage(JShell::getInstance(), getpid());
86 }

◆ getCpuUsage() [1/3]

float JSYSTEM::getCpuUsage ( JShell & shell,
const pid_t pid )
inline

Get cpu usage in percent of given process identifier.

Parameters
shellshell interface
pidprocess identifier
Returns
cpu usage [%]

Definition at line 96 of file JSystemToolkit.hh.

97 {
98 using namespace std;
99
100 float value = 0.0;
101
102 shell << "ps -o %cpu= -p " << pid << endl;
103
104 if (shell.get(value))
105 return value;
106 else
107 THROW(JSystemException, "No process data for PID " << pid);
108 }

◆ getCpuUsage() [2/3]

float JSYSTEM::getCpuUsage ( JShell & shell)
inline

Get cpu usage in percent of this process.

Parameters
shellshell interface
Returns
cpu usage [%]

Definition at line 117 of file JSystemToolkit.hh.

118 {
119 return getCpuUsage(shell, getpid());
120 }
float getCpuUsage(JShell &shell, const pid_t pid)
Get cpu usage in percent of given process identifier.

◆ getCpuUsage() [3/3]

float JSYSTEM::getCpuUsage ( )
inline

Get cpu usage in percent of this process.

Returns
cpu usage [%]

Definition at line 128 of file JSystemToolkit.hh.

129 {
130 return getCpuUsage(JShell::getInstance(), getpid());
131 }

◆ getPID() [1/2]

pid_t JSYSTEM::getPID ( JShell & shell,
const char * process )
inline

Get process identifier.

Parameters
shellshell interface
processprocess name
Returns
process identifier

Definition at line 141 of file JSystemToolkit.hh.

142 {
143 using namespace std;
144
145 pid_t pid = -1;
146
147 shell << "ps -o pid= -C " << process << endl;
148
149 if (shell.get(pid))
150 return pid;
151 else
152 THROW(JSystemException, "No process " << process);
153 }

◆ getPID() [2/2]

pid_t JSYSTEM::getPID ( const char * process)
inline

Get process identifier.

Parameters
processprocess name
Returns
process identifier

Definition at line 162 of file JSystemToolkit.hh.

163 {
164 return getPID(JShell::getInstance(), process);
165 }
pid_t getPID(JShell &shell, const char *process)
Get process identifier.

◆ getParentID() [1/2]

pid_t JSYSTEM::getParentID ( JShell & shell,
pid_t pid )
inline

Get parent process identifier.

Parameters
shellshell interface
pidprocess identifier
Returns
parent identifier

Definition at line 175 of file JSystemToolkit.hh.

176 {
177 using namespace std;
178
179 shell << "ps -o ppid= -p " << pid << endl;
180
181 if (shell.get(pid))
182 return pid;
183 else
184 THROW(JSystemException, "No parent identifier " << pid);
185 }

◆ getParentID() [2/2]

pid_t JSYSTEM::getParentID ( const pid_t pid)
inline

Get parent process identifier.

Parameters
pidprocess identifier
Returns
parent identifier

Definition at line 194 of file JSystemToolkit.hh.

195 {
196 return getParentID(JShell::getInstance(), getpid());
197 }
pid_t getParentID(JShell &shell, pid_t pid)
Get parent process identifier.

◆ getShell() [1/2]

std::string JSYSTEM::getShell ( JShell & shell)
inline

Get shell name.

Parameters
shellshell interface
Returns
shell name

Definition at line 206 of file JSystemToolkit.hh.

207 {
208 using namespace std;
209
210 static string value = "";
211
212 if (value == "") {
213
214 pid_t pid = getppid();
215
216 shell << "ps -o ppid= -o args= -p " << pid << endl;
217
218 if (shell >> pid >> value) {
219
220 shell.flush();
221
222 if (!value.empty() && value[0] == '-') {
223 value = value.substr(1);
224 }
225
226 } else {
227
228 static_cast<istream&>(shell).clear();
229 shell.flush();
230
231 THROW(JSystemException, "No shell");
232 }
233 }
234
235 return value;
236 }
JShell & flush(std::ostream &out=null)
Extracts characters from this shell and flush them to the given output stream until the prompt is rea...
Definition JShell.hh:184

◆ getShell() [2/2]

std::string JSYSTEM::getShell ( )
inline

Get shell name of this process.

Returns
shell name

Definition at line 244 of file JSystemToolkit.hh.

245 {
246 return getShell(JShell::getInstance());
247 }
std::string getShell(JShell &shell)
Get shell name.

◆ getRAM()

unsigned long long int JSYSTEM::getRAM ( )
inline

Get RAM of this CPU.

Returns
number of bytes

Definition at line 255 of file JSystemToolkit.hh.

256 {
257 const JSysinfo info;
258
259 return info.getTotalRAM();
260 }
Auxiliary class for system information.
Definition JSysinfo.hh:38
unsigned long long int getTotalRAM() const
Get total RAM.
Definition JSysinfo.hh:64

◆ which() [1/2]

const std::string JSYSTEM::which ( JShell & shell,
const char * process )
inline

Get process path.

Parameters
shellshell interface
processprocess name
Returns
path

Definition at line 270 of file JSystemToolkit.hh.

271 {
272 using namespace std;
273
274 string buffer;
275
276 shell << "which " << process << endl;
277
278 shell.getline(buffer);
279
280 shell.flush();
281
282 return buffer;
283 }
bool getline(std::string &buffer, const char eol='\n')
Get line of text.
Definition JShell.hh:137

◆ which() [2/2]

const std::string JSYSTEM::which ( const char * process)
inline

Get process path.

Parameters
processprocess name
Returns
path

Definition at line 292 of file JSystemToolkit.hh.

293 {
294 return which(JShell::getInstance(), process);
295 }
const std::string which(JShell &shell, const char *process)
Get process path.

◆ gprint()

void JSYSTEM::gprint ( const std::string & message)
inline

Print method.

Parameters
messagemessage

Definition at line 303 of file JSystemToolkit.hh.

304 {
305 using namespace std;
306
307 istringstream is(message);
308
309 for (string buffer; getline(is, buffer); ) {
310
311 if (!buffer.empty()) {
312
313 for (string::size_type i = 0; i != buffer.size(); ++i) {
314 if (SPECIAL_CHARACTERS.find(buffer[i]) != string::npos) {
315 buffer.insert(i++, "\\");
316 }
317 }
318
319 cout << "echo " << buffer << ";" << endl;
320 }
321 }
322 }
static const std::string SPECIAL_CHARACTERS

◆ gexit()

int JSYSTEM::gexit ( int status,
const std::string & message = "" )
inline

Exit method.

Parameters
statusexit status
messageoptional message

Definition at line 331 of file JSystemToolkit.hh.

332 {
333 using namespace std;
334
335 gprint(message);
336
337 cout << "exit " << status << ";" << endl;
338
339 return status;
340 }
void gprint(const std::string &message)
Print method.

◆ set_variable()

void JSYSTEM::set_variable ( const std::string & name,
const std::string & value )
inline

Set environment variable.

This method prints the shell command to set the variable with the given name to the specified value.

Parameters
namevariable name
valuevariable value

Definition at line 351 of file JSystemToolkit.hh.

352 {
353 using namespace std;
354 using namespace JSYSTEM;
355
356 static const string shell = getShell();
357
358 string buffer(value);
359
360 if (buffer.find(' ') != string::npos) {
361 buffer = "\"" + buffer + "\"";
362 }
363
364 if (shell.find(ZSH) != string::npos ||
365 shell.find(KSH) != string::npos ||
366 shell.find(BASH) != string::npos) {
367
368 cout << "export " << name << "=" << buffer << ";" << endl;
369
370 } else if (shell.find(CSH) != string::npos ||
371 shell.find(TCSH) != string::npos) {
372
373 cout << "setenv " << name << " " << buffer << ";" << endl;
374
375 } else {
376
377 THROW(JSystemException, "unknown shell " << shell);
378 }
379 }
Auxiliary classes and methods for operating system calls.

Variable Documentation

◆ getDateAndTime

JDateAndTime JSYSTEM::getDateAndTime
static

Function object to get current date and time.

Definition at line 468 of file JDateAndTime.hh.

◆ getFilenames

JGlob JSYSTEM::getFilenames
static

Function object to get list of files for given pattern.

Definition at line 123 of file JGlob.hh.

◆ compareEndian

const JCompareEndian JSYSTEM::compareEndian
static

Function object operator.

Returns
true if network and system endianess same; else false

Definition at line 66 of file JNetwork.hh.

◆ getFileStatus

JStat JSYSTEM::getFileStatus
static

Function object for file status.

Definition at line 173 of file JStat.hh.

◆ SH

const char* const JSYSTEM::SH = "sh"
static

Shell names.

Definition at line 33 of file JSystemToolkit.hh.

◆ ZSH

const char* const JSYSTEM::ZSH = "zsh"
static

Definition at line 34 of file JSystemToolkit.hh.

◆ KSH

const char* const JSYSTEM::KSH = "ksh"
static

Definition at line 35 of file JSystemToolkit.hh.

◆ BASH

const char* const JSYSTEM::BASH = "bash"
static

Definition at line 36 of file JSystemToolkit.hh.

◆ CSH

const char* const JSYSTEM::CSH = "csh"
static

Definition at line 37 of file JSystemToolkit.hh.

◆ TCSH

const char* const JSYSTEM::TCSH = "tcsh"
static

Definition at line 38 of file JSystemToolkit.hh.

◆ SPECIAL_CHARACTERS

const std::string JSYSTEM::SPECIAL_CHARACTERS = "\"' <>[](){};$#"
static

Definition at line 40 of file JSystemToolkit.hh.

◆ getLocalTime

const JLocalTime JSYSTEM::getLocalTime
static

Function object to get local time in micro seconds.

Definition at line 61 of file JSystem/JTime.hh.