Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
UppHub
Status & Roadmap
FAQ
Authors & License
Forums
Funding U++
Search on this site











SourceForge.net Logo

SourceForge.net Logo

GitHub Logo

Discord Logo

AProcess

 

struct AProcess : public NoCopy

This is an abstract class that represent a controlled child process. Client code can communicate with such process by supplying standard input and reading standard output. Note that while LocalProcess implements AProcess for host OS, some other derived class might choose to e.g. represent a process on remote system.

 

 

Public Method List

 

virtual void Kill() = 0

Terminates running process.

 


 

virtual bool IsRunning() = 0

Returns true if process is running.

 


 

virtual void Write(String s) = 0

Sends a text to standard input of process.

 


 

virtual bool Read(String& s) = 0

Reads data from standard output combined. Returns true if the process is running or there are more data to be read (even after the process terminated).

 


 

virtual bool Read2(String& os, String& es)

Reads data separately from standard output and from standard error output. Returns true if the process is running or there are more data to be read  (even after the process terminated). Usually, implementing class has to be in special mode for this to work (e.g. LocalProcess must be started with Start2).

 


 

virtual int GetExitCode() = 0

Returns an exit code of terminated process.

 


 

virtual String GetExitMessage()

In case of error, might return its text description.

 


 

virtual void CloseRead()

Closes handle to process output.

 


 

virtual void CloseWrite()

Closes handle to process input. This is useful e.g.  in case that process waits for the end of input data before processing them and providing output.

 


 

virtual void Detach()

Disconnects AProcess instance from running child process. Process continues to run while AProcess instance can be destructed. Please notice the importance to provide a mechanism to avoid zombies in POSIX here (e.g. use DoubleFork or appropriate SIGCHLD handler).

 


 

String Get()

This utility inline function calls Read and returns the standard output of process. If anything went wrong, returns String::GetVoid().

 

 

 

 

LocalProcess

 

class LocalProcess : public AProcess

This class implements a child process of host operating system.

 

 

Public Method List

 

bool Start(const char *cmdline, const char *envptr = NULL, const char *cd = NULL)

Starts a new process defined by cmdline, envptr can provide a new environment for the process, if NULL, then the new process inherits caller's environment. cd can be used to specify the new current directory for the process.

 


 

bool Start2(const char *cmdline, const char *envptr = NULL, const char *cd = NULL)

Starts a new process defined by cmdline, envptr can provide a new environment for the process, if NULL, then the new process inherits caller's environment. This variant activates mode when standard output and standard error output are read separately using Read2 method. cd can be used to specify the new current directory for the process.

 


 

bool Start(const char *cmd, const Vector<String>& arg, const char *envptr = NULL, const char *cd = NULL)

Starts a new process defined by cmd, arg. envptr can provide a new environment for the process, if NULL, then the new process inherits caller's environment. This variant passes individual arguments instead of whole commandline, this has advantage that arguments are in POSIX passed directly to execv, without parsing the commandline. cd can be used to specify the new current directory for the process.

 


 

bool Start2(const char *cmd, const Vector<String>& arg, const char *envptr = NULL, const char *cd = NULL)

Starts a new process defined by cmd, arg. envptr can provide a new environment for the process, if NULL, then the new process inherits caller's environment. This variant activates mode when standard output and standard error output are read separately using Read2 method. This variant passes individual arguments instead of whole commandline, this has advantage that arguments are in POSIX passed directly to execv, without parsing the commandline. cd can be used to specify the new current directory for the process.

 


 

int Finish(String& out)

Runs the started process until it exits. Returns standard output in out and exit code as return value.

 


 

LocalProcess& DoubleFork(bool b = true)

[POSIX]

Activates double-fork mode to avoid zombies. Use this in POSIX if you want to Detach the process.

 


 

LocalProcess& ConvertCharset(bool b = true)

LocalProcess& NoConvertCharset()

Determines LocalProcess should convert encoding from system to application one. Default setting is true.

 

Constructor Detail

 

LocalProcess()

Default constructor.

 


 

LocalProcess(const char *cmdline, const char *envptr = NULL)

Equivalent of default constructor and then invoking Start(cmdline, envptr).

 


 

LocalProcess(const char *cmd, const Vector<String>& arg, const char *envptr = NULL)

Equivalent of default constructor and then invoking Start(cmd, arg, envptr).

 

Global functions related to LocalProcess

 

int Sys(const char *cmd, String& out, bool convertcharset = true)

Runs process defined by cmd command line, returns its standard output in out.and its exit code as return value. If there was error invoking cmd, returns -1. If convertcharset is true, output is converted from system character encoding to application encoding.


 

String Sys(const char *cmd, bool convertcharset = true)

Runs process defined by cmd command line. If cmd was executed successfully and returned zero exit code, returns its standard output, otherwise returns String::GetVoid(). If convertcharset is true, output is converted from system character encoding to application encoding.

 


 

int Sys(const char *cmd, const Vector<String>& arg, String& out, bool convertcharset = true)

Runs process defined by cmd arg command line, returns its standard output in output.and its exit code as return value. If there was error invoking cmd, returns -1. If convertcharset is true, output is converted from system character encoding to application encoding.

 


 

String Sys(const char *cmd, const Vector<String>& arg, bool convertcharset = true)

Runs process defined by cmd arg command line. If cmd was executed successfully and returned zero exit code, returns its standard output, otherwise returns String::GetVoid(). If convertcharset is true, output is converted from system character encoding to application encoding.

 

 

Do you want to contribute?