LocalProcess.h

Zbigniew Rebacz, 07/19/2014 01:27 PM

Download (2.68 KB)

 
1
struct AProcess : NoCopy {
2
public:
3
        virtual void Kill() = 0;
4
        virtual bool IsRunning() = 0;
5
        virtual void Write(String s) = 0;
6
        virtual bool Read(String& s) = 0;
7
        virtual bool Read2(String& os, String& es) { NEVER(); return false; }
8
        virtual int  GetExitCode() = 0;
9
        virtual String GetExitMessage() { return String(); }
10
        virtual void CloseRead()        {}
11
        virtual void CloseWrite()       {}
12
        virtual void Detach()           {}
13
        virtual void WaitForExit()      {}
14
        
15
        String  Get()                  { String x; if(Read(x)) return x; return String::GetVoid(); }
16

    
17
        AProcess() {}
18
        virtual ~AProcess() {}
19
};
20

    
21
class LocalProcess : public AProcess {
22
public:
23
        virtual void Kill();
24
        virtual bool IsRunning();
25
        virtual void Write(String s);
26
        virtual bool Read(String& s);
27
        virtual bool Read2(String& os, String &es);
28
        virtual String GetExitMessage();
29
        virtual int  GetExitCode();
30
        virtual void CloseRead();
31
        virtual void CloseWrite();
32
        virtual void Detach();
33
        virtual void WaitForExit();
34

    
35
private:
36
        void         Init();
37
        void         Free();
38
        void         CloseInputAndOutput();
39
#ifdef PLATFORM_POSIX
40
        bool         DecodeExitCode(int code);
41
#endif
42

    
43
private:
44
        bool         convertcharset;
45

    
46
#ifdef PLATFORM_WIN32
47
        HANDLE       hProcess;
48
        HANDLE       hOutputRead;
49
        HANDLE       hInputWrite;
50
#endif
51
#ifdef PLATFORM_POSIX
52
        Buffer<char> cmd_buf;
53
        Vector<char *> args;
54
        pid_t        pid;
55
        int          rpipe[2], wpipe[2], epipe[2];
56
        String       exit_string;
57
#endif
58
        int          exit_code;
59

    
60
        typedef LocalProcess CLASSNAME;
61

    
62
        bool DoStart(const char *cmdline, bool spliterr, const char *envptr = NULL);
63

    
64
public:
65
        bool Start(const char *cmdline, const char *envptr = NULL)        { return DoStart(cmdline, false, envptr); }
66
        bool Start2(const char *cmdline, const char *envptr = NULL)       { return DoStart(cmdline, true, envptr); }
67
        
68
#ifdef PLATFORM_POSIX
69
        int  GetPid()  const                                              { return pid; }
70
#endif
71

    
72
#ifdef PLATFORM_WIN32
73
        HANDLE  GetProcessHandle()  const                                    { return hProcess; }
74
#endif
75
                
76
        LocalProcess& ConvertCharset(bool b = true)                       { convertcharset = b; return *this; }
77
        LocalProcess& NoConvertCharset()                                  { return ConvertCharset(false); }
78

    
79
        LocalProcess()                                                    { Init(); }
80
        LocalProcess(const char *cmdline, const char *envptr = NULL)      { Init(); Start(cmdline, envptr); }
81
        virtual ~LocalProcess()                                           { Kill(); }
82
};
83

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