-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pipe.h
executable file
·112 lines (97 loc) · 2.74 KB
/
Pipe.h
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @file Pipe.h
* @ingroup DataManagement
* @author Dominique Vaufreydaz, Grenoble Alpes University, Inria
* @copyright All right reserved.
*/
#ifndef __PIPE_H__
#define __PIPE_H__
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#if !defined WIN32 && !defined WIN64
#if __cplusplus < 201103L
#ifndef nullptr
#define nullptr NULL
#endif
#endif
#endif
#if defined WIN32 || defined WIN64
// Define null device
#define NULL_OUTPUT "NUL"
#else
// Define null device
#define NULL_OUTPUT "/dev/null"
#endif
/**
* @class Pipe Pipe.cpp Pipe.h
* @brief Abtraction class for pipe functions under Windows/Linux/MacOSX.
*
* This classe is used to make consistancy calls for popen and pclose function accross
* operating systems.
* @author Dominique Vaufreydaz, Grenoble Alpes University, Inria
*/
class Pipe
{
public:
/** @brief Constructor, empty but defined. Values are set in c++11 way.
*/
Pipe();
/** @brief Virtual destructor always
*/
virtual ~Pipe();
/** @brief cast operator to permit access to underlying FILE structure
*/
operator FILE *()
{
return InternalFile;
}
/** @enum Pipe::PipeMode
* @brief Enum flags to select read *or* write pipe, i.e. if we want to pipe from (read) or to (write) an external program
*/
enum PipeMode {
UNK_MODE, /*!< Default value, mode is not known */
READ_MODE, /*!< Read from an external program */
WRITE_MODE /*!< Write to an exteran program */
};
/** @brief Open a pipe to a command
*
*/
bool Open(const char *Command, int eMode = READ_MODE );
/** @brief Close the Pipe (if opened).
*/
void Close();
private:
/** @brief Inline function to wrap the popen function. Not for external usage.
*
* @param Command [in] the command to be executed
* @param Type [in] the access type.
* @return Pointer to a FILE * structure
*/
inline FILE * popen(const char *Command, const char *Type)
{
#if defined WIN32 || defined WIN64
return ::_popen(Command, Type);
#else
return ::popen(Command, Type);
#endif
}
/** @brief inline function to wrap the popen function. Not for external usage.
*
* @param Command [in] the pipe stream to close
*/
inline int pclose(FILE *stream)
{
#if defined WIN32 || defined WIN64
return ::_pclose(stream);
#else
return ::pclose(stream);
#endif
}
protected:
FILE * InternalFile; /*!< A pointer to a FILE structure. In subclasses, this pointer can be re-used for usual files whenever there was no call for Pipe methods. */
public:
int Mode; /*!< Used to say if we want to open pip for reading or writing */
bool DebugMode; /*!< Boolean value to ask for Pipe debugging information. Default=false. */
};
#endif // __PIPE_H__