]> git.tdb.fi Git - libs/core.git/blob - source/core/process.cpp
Use nullptr instead of 0 for pointers
[libs/core.git] / source / core / process.cpp
1 #include <msp/io/console.h>
2 #include "process.h"
3 #include "process_private.h"
4
5 using namespace std;
6
7 namespace Msp {
8
9 Process *Process::_self = nullptr;
10
11 Process::Process(const Private &p):
12         priv(new Private(p))
13 { }
14
15 Process::Process():
16         priv(new Private)
17 { }
18
19 Process &Process::self()
20 {
21         if(!_self)
22         {
23                 Private _priv;
24                 platform_get_self_info(_priv);
25                 _self = new Process(_priv);
26         }
27         return *_self;
28 }
29
30 void Process::set_working_directory(const FS::Path &d)
31 {
32         work_dir = d;
33 }
34
35 void Process::redirect_cin(IO::Base &io)
36 {
37         do_redirect(cin, io);
38 }
39
40 void Process::redirect_cout(IO::Base &io)
41 {
42         do_redirect(cout, io);
43 }
44
45 void Process::redirect_cerr(IO::Base &io)
46 {
47         do_redirect(cerr, io);
48 }
49
50 void Process::do_redirect(IO::Base *&ptr, IO::Base &io)
51 {
52         if(this==_self)
53         {
54                 if(&ptr==&cin)
55                         IO::cin.redirect(io);
56                 else if(&ptr==&cout)
57                         IO::cout.redirect(io);
58                 else if(&ptr==&cerr)
59                         IO::cerr.redirect(io);
60         }
61         else
62         {
63                 redirect = true;
64                 ptr = &io;
65         }
66 }
67
68 void Process::execute(const string &command, const Arguments &args)
69 {
70         execute(command, true, args);
71 }
72
73 void Process::execute(const FS::Path &command, const Arguments &args)
74 {
75         execute(command.str(), false, args);
76 }
77
78 unsigned Process::get_exit_code() const
79 {
80         if(!finished)
81                 throw logic_error("not finished");
82         return exit_code;
83 }
84
85 } // namespace Msp