forked from jackburton79/ocs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSupport.cpp
134 lines (102 loc) · 2.34 KB
/
Support.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Support.cpp
*
* Created on: 11/lug/2013
* Author: Stefano Ceccherini
* Code for popen_streambuf found here:
* http://stackoverflow.com/questions/1683051/file-and-istream-connect-the-two
*/
#include "Support.h"
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <streambuf>
#include <string>
CommandStreamBuffer::CommandStreamBuffer()
:
fFile(NULL),
fBuffer(NULL)
{
}
CommandStreamBuffer::CommandStreamBuffer(const char* fileName, const char* mode)
:
fFile(NULL),
fBuffer(NULL)
{
CommandStreamBuffer* buf = open(fileName, mode);
if (buf == NULL)
throw std::runtime_error("CommandStreamBuffer: cannot open file");
}
CommandStreamBuffer::~CommandStreamBuffer()
{
close();
}
CommandStreamBuffer*
CommandStreamBuffer::open(const char* fileName, const char* mode)
{
fFile = popen(fileName, mode);
if (fFile == NULL)
return NULL;
fBuffer = new char[512];
if (fBuffer == NULL)
return NULL;
setg(fBuffer, fBuffer, fBuffer);
return this;
}
void
CommandStreamBuffer::close()
{
if (fFile != NULL) {
pclose(fFile);
fFile = NULL;
}
delete[] fBuffer;
}
std::streamsize
CommandStreamBuffer::xsgetn(char_type* ptr, std::streamsize num)
{
std::streamsize howMany = showmanyc();
if (num < howMany) {
memcpy(ptr, gptr(), num * sizeof(char_type));
gbump(num);
return num;
}
memcpy(ptr, gptr(), howMany * sizeof(char_type));
gbump(howMany);
if (traits_type::eof() == underflow())
return howMany;
return howMany + xsgetn(ptr + howMany, num - howMany);
}
CommandStreamBuffer::traits_type::int_type
CommandStreamBuffer::underflow()
{
if (gptr() == 0)
return traits_type::eof();
if (gptr() < egptr())
return traits_type::to_int_type(*gptr());
size_t len = fread(eback(), sizeof(char_type), 512, fFile);
setg(eback(), eback(), eback() + sizeof(char_type) * len);
if (len == 0)
return traits_type::eof();
return traits_type::to_int_type(*gptr());
}
std::streamsize
CommandStreamBuffer::showmanyc()
{
if (gptr() == 0)
return 0;
if (gptr() < egptr())
return egptr() - gptr();
return 0;
}
bool
CommandExists(const char* command)
{
std::string fullCommand;
fullCommand.append("type ").append(command).append(" > /dev/null 2>&1");
int result = -1;
int systemStatus = system(fullCommand.c_str());
if (systemStatus == 0)
result = WEXITSTATUS(systemStatus);
return result == 0;
}