forked from jackburton79/inventory-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupport.cpp
More file actions
133 lines (101 loc) · 2.21 KB
/
Support.cpp
File metadata and controls
133 lines (101 loc) · 2.21 KB
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
/*
* 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 <iostream>
#include <streambuf>
#include <string>
#include <string.h>
popen_streambuf::popen_streambuf()
:
fFile(NULL),
fBuffer(NULL)
{
}
popen_streambuf::popen_streambuf(const char* fileName, const char* mode)
:
fFile(NULL),
fBuffer(NULL)
{
popen_streambuf* buf = open(fileName, mode);
if (buf == NULL)
throw -1;
}
popen_streambuf::~popen_streambuf()
{
close();
}
popen_streambuf*
popen_streambuf::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
popen_streambuf::close()
{
if (fFile != NULL) {
pclose(fFile);
fFile = NULL;
}
delete[] fBuffer;
}
std::streamsize
popen_streambuf::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);
}
popen_streambuf::traits_type::int_type
popen_streambuf::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
popen_streambuf::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;
}