-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pipe.cpp
executable file
·85 lines (78 loc) · 1.54 KB
/
Pipe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* @file Pipe.cpp
* @ingroup DataManagement
* @author Dominique Vaufreydaz, Grenoble Alpes University, Inria
* @copyright All right reserved.
*/
#include "Pipe.h"
/** @brief Constructor, empty but defined. Values are set in c++11 way.
*/
Pipe::Pipe() :
DebugMode(false) // Do not show command line before executing them
{
// by default,
Mode = UNK_MODE;
InternalFile = nullptr;
}
/** @brief Virtual destructor always
*/
Pipe::~Pipe()
{
// Close everything
Close();
}
/** @brief Open a pipe to a command
*
*/
bool Pipe::Open(const char *Command, int eMode /* = READ_MODE */ )
{
#if defined WIN32 || defined WIN64
const char * ModeRead = "rb";
const char * ModeWrite = "wb";
#else
// Linux and MacOS does not support ReadMode
const char * ModeRead = "r";
const char * ModeWrite = "w";
#endif
// Read mode
if ( eMode == READ_MODE )
{
Mode = READ_MODE;
InternalFile = popen( Command, ModeRead );
}
// Or write mode
else if ( eMode == WRITE_MODE )
{
Mode = WRITE_MODE;
InternalFile = popen( Command, ModeWrite );
}
// Bad param, unkown mode
else
{
fprintf( stderr, "Unkown opening mode\n" );
Mode = UNK_MODE;
return false;
}
// Check if we manage to open pipe
if ( InternalFile == nullptr )
{
// no...
Mode = UNK_MODE;
return false;
}
else
{
// pipe is opened
return true;
}
}
/** @brief Close the Pipe (if opened).
*/
void Pipe::Close()
{
if ( InternalFile != nullptr )
{
pclose(InternalFile);
InternalFile = nullptr;
}
}