Jpp
Classes | Typedefs | Functions | Variables
JSYSTEM Namespace Reference

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

Classes

struct  JDateAndTime
 Auxililary class to get date and time. 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 class to list files. More...
 

Typedefs

typedef long long int localtime_t
 Type definition of local time. More...
 

Functions

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

Variables

static JDateAndTime getDateAndTime
 Function object to get ASCII formatted date and time. More...
 
static const JCompareEndian compareEndian
 Function object operator. More...
 
static JStat getFileStatus
 Function object for file status. More...
 
static const char *const SH = "sh"
 Shell names. More...
 
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. More...
 

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 ASCII formatted date.

Returns
date

Definition at line 23 of file JDate.hh.

24  {
25  static time_t ts;
26  static const int MAX_SIZE = 256;
27  static char buffer[MAX_SIZE];
28 
29  time(&ts);
30 
31  strftime(buffer, MAX_SIZE, "%x", localtime(&ts));
32 
33  return buffer;
34  }

◆ getTime()

const char* JSYSTEM::getTime ( )
inline

Get ASCII formatted time.

Returns
time

Definition at line 42 of file JDate.hh.

43  {
44  static time_t ts;
45  static const int MAX_SIZE = 256;
46  static char buffer[MAX_SIZE];
47 
48  time(&ts);
49 
50  strftime(buffer, MAX_SIZE, "%X", localtime(&ts));
51 
52  return buffer;
53  }

◆ 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  }

◆ 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.");
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  }

◆ 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  }

◆ 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  }

◆ 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  }

◆ 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 
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->sa_family == AF_INET ||
232  p->ifa_addr->sa_family == AF_INET6) {
233 
234  if (getnameinfo( p->ifa_addr,
235  (p->ifa_addr->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6),
236  buffer, MAXIMUM_LENGTH,
237  NULL, 0, NI_NUMERICHOST) == 0) {
238 
239  result.push_back(buffer);
240  }
241  }
242  }
243  }
244 
245  freeifaddrs(p);
246 
247  return result;
248  }

◆ 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 50 of file JSystemToolkit.hh.

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

◆ 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 71 of file JSystemToolkit.hh.

72  {
73  return getMemoryUsage(shell, getpid());
74  }

◆ getMemoryUsage() [3/3]

float JSYSTEM::getMemoryUsage ( )
inline

Get memory usage in percent of this process.

Returns
memory usage [%]

Definition at line 82 of file JSystemToolkit.hh.

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

◆ 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 95 of file JSystemToolkit.hh.

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

◆ 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 116 of file JSystemToolkit.hh.

117  {
118  return getCpuUsage(shell, getpid());
119  }

◆ getCpuUsage() [3/3]

float JSYSTEM::getCpuUsage ( )
inline

Get cpu usage in percent of this process.

Returns
cpu usage [%]

Definition at line 127 of file JSystemToolkit.hh.

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

◆ 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 140 of file JSystemToolkit.hh.

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

◆ getPID() [2/2]

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

Get process identifier.

Parameters
processprocess name
Returns
process identifier

Definition at line 161 of file JSystemToolkit.hh.

162  {
163  return getPID(JShell::getInstance(), process);
164  }

◆ 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 174 of file JSystemToolkit.hh.

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

◆ 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 193 of file JSystemToolkit.hh.

194  {
195  return getParentID(JShell::getInstance(), getpid());
196  }

◆ getShell() [1/2]

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

Get shell name.

Parameters
shellshell interface
Returns
shell name

Definition at line 205 of file JSystemToolkit.hh.

206  {
207  using namespace std;
208 
209  static string value = "";
210 
211  if (value == "") {
212 
213  pid_t pid = getppid();
214 
215  shell << "ps -o ppid= -o args= -p " << pid << endl;
216 
217  if (shell >> pid >> value) {
218 
219  shell.flush();
220 
221  if (!value.empty() && value[0] == '-') {
222  value = value.substr(1);
223  }
224 
225  } else {
226 
227  static_cast<istream&>(shell).clear();
228  shell.flush();
229 
230  THROW(JSystemException, "No shell");
231  }
232  }
233 
234  return value;
235  }

◆ getShell() [2/2]

std::string JSYSTEM::getShell ( )
inline

Get shell name of this process.

Returns
shell name

Definition at line 243 of file JSystemToolkit.hh.

244  {
245  return getShell(JShell::getInstance());
246  }

◆ getRAM()

unsigned long long int JSYSTEM::getRAM ( )
inline

Get RAM of this CPU.

Returns
number of bytes

Definition at line 254 of file JSystemToolkit.hh.

255  {
256  const JSysinfo info;
257 
258  return info.getTotalRAM();
259  }

◆ 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 269 of file JSystemToolkit.hh.

270  {
271  using namespace std;
272 
273  string buffer;
274 
275  shell << "which " << process << endl;
276 
277  shell.getline(buffer);
278 
279  shell.flush();
280 
281  return buffer;
282  }

◆ which() [2/2]

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

Get process path.

Parameters
processprocess name
Returns
path

Definition at line 291 of file JSystemToolkit.hh.

292  {
293  return which(JShell::getInstance(), process);
294  }

◆ gprint()

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

Print method.

Parameters
messagemessage

Definition at line 356 of file JSystemToolkit.hh.

357  {
358  using namespace std;
359 
360  istringstream is(message);
361 
362  for (string buffer; getline(is, buffer); ) {
363 
364  if (!buffer.empty()) {
365 
366  for (string::size_type i = 0; i != buffer.size(); ++i) {
367  if (SPECIAL_CHARACTERS.find(buffer[i]) != string::npos) {
368  buffer.insert(i++, "\\");
369  }
370  }
371 
372  cout << "echo " << buffer << ";" << endl;
373  }
374  }
375  }

◆ gexit()

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

Exit method.

Parameters
statusexit status
messageoptional message

Definition at line 384 of file JSystemToolkit.hh.

385  {
386  using namespace std;
387 
388  gprint(message);
389 
390  cout << "exit " << status << ";" << endl;
391 
392  return status;
393  }

◆ 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 404 of file JSystemToolkit.hh.

405  {
406  using namespace std;
407  using namespace JSYSTEM;
408 
409  static const string shell = getShell();
410 
411  string buffer(value);
412 
413  if (buffer.find(' ') != string::npos) {
414  buffer = "\"" + buffer + "\"";
415  }
416 
417  if (shell.find(ZSH) != string::npos ||
418  shell.find(KSH) != string::npos ||
419  shell.find(BASH) != string::npos) {
420 
421  cout << "export " << name << "=" << buffer << ";" << endl;
422 
423  } else if (shell.find(CSH) != string::npos ||
424  shell.find(TCSH) != string::npos) {
425 
426  cout << "setenv " << name << " " << buffer << ";" << endl;
427 
428  } else {
429 
430  THROW(JSystemException, "unknown shell " << shell);
431  }
432  }

Variable Documentation

◆ getDateAndTime

JDateAndTime JSYSTEM::getDateAndTime
static

Function object to get ASCII formatted date and time.

Definition at line 195 of file JDate.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 32 of file JSystemToolkit.hh.

◆ ZSH

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

Definition at line 33 of file JSystemToolkit.hh.

◆ KSH

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

Definition at line 34 of file JSystemToolkit.hh.

◆ BASH

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

Definition at line 35 of file JSystemToolkit.hh.

◆ CSH

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

Definition at line 36 of file JSystemToolkit.hh.

◆ TCSH

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

Definition at line 37 of file JSystemToolkit.hh.

◆ SPECIAL_CHARACTERS

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

Definition at line 39 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.

JSYSTEM::CSH
static const char *const CSH
Definition: JSystemToolkit.hh:36
JSYSTEM::KSH
static const char *const KSH
Definition: JSystemToolkit.hh:34
JSYSTEM::getIPnumber
int getIPnumber()
Get IP number.
Definition: JNetwork.hh:142
JSYSTEM::getHostname
std::string getHostname(const int ip)
Get host name.
Definition: JNetwork.hh:96
JSYSTEM::gprint
void gprint(const std::string &message)
Print method.
Definition: JSystemToolkit.hh:356
JSYSTEM::TCSH
static const char *const TCSH
Definition: JSystemToolkit.hh:37
JLANG::getInstance
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:73
JSYSTEM::getParentID
pid_t getParentID(const pid_t pid)
Get parent process identifier.
Definition: JSystemToolkit.hh:193
std::vector
Definition: JSTDTypes.hh:12
JSYSTEM::which
const std::string which(const char *process)
Get process path.
Definition: JSystemToolkit.hh:291
JSYSTEM::getMemoryUsage
float getMemoryUsage()
Get memory usage in percent of this process.
Definition: JSystemToolkit.hh:82
JSYSTEM::ZSH
static const char *const ZSH
Definition: JSystemToolkit.hh:33
JSYSTEM::getIPaddress
std::string getIPaddress()
Get IP address (decimal-dot notation).
Definition: JNetwork.hh:178
JTOOLS::result
return result
Definition: JPolint.hh:695
JSYSTEM
Auxiliary classes and methods for operating system calls.
Definition: JDate.hh:13
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:669
JSYSTEM::JSysinfo::getTotalRAM
unsigned long long int getTotalRAM() const
Get total RAM.
Definition: JSysinfo.hh:38
JSYSTEM::JShell::get
bool get(T &value)
Get value.
Definition: JShell.hh:106
JSYSTEM::compareEndian
static const JCompareEndian compareEndian
Function object operator.
Definition: JNetwork.hh:66
JSYSTEM::JShell::flush
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
JSYSTEM::getCpuUsage
float getCpuUsage()
Get cpu usage in percent of this process.
Definition: JSystemToolkit.hh:127
JSYSTEM::getPID
pid_t getPID(const char *process)
Get process identifier.
Definition: JSystemToolkit.hh:161
JSYSTEM::JShell::getline
bool getline(std::string &buffer, const char eol='\n')
Get line of text.
Definition: JShell.hh:137
std
Definition: jaanetDictionary.h:36
JSYSTEM::JSysinfo
Auxiliary class for system information.
Definition: JSysinfo.hh:21
JSYSTEM::SPECIAL_CHARACTERS
static const std::string SPECIAL_CHARACTERS
Definition: JSystemToolkit.hh:39
JSYSTEM::BASH
static const char *const BASH
Definition: JSystemToolkit.hh:35
JSYSTEM::getSubaddress
unsigned short getSubaddress()
Get host identifier within network.
Definition: JNetwork.hh:205
JLANG::getline
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:468
JSYSTEM::getShell
std::string getShell()
Get shell name of this process.
Definition: JSystemToolkit.hh:243