forked from config4star/config4cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.cpp
More file actions
240 lines (209 loc) · 4.92 KB
/
platform.cpp
File metadata and controls
240 lines (209 loc) · 4.92 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//-----------------------------------------------------------------------
// Copyright 2011 Ciaran McHale.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions.
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//----------------------------------------------------------------------
#include "platform.h"
#include <assert.h>
#include <stdlib.h>
#include <config4cpp/StringBuffer.h>
#ifdef P_STDIO_HAS_LIMITED_FDS
# ifdef WIN32
//--------
// Windows does NOT suffer from this problem. However, we can
// pretend it does so we can compile and test both versions of
// the BufferedFileReader class on Windows.
//--------
# include <io.h>
# include <fcntl.h>
# else
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# endif
#endif
namespace CONFIG4CPP_NAMESPACE {
bool
execCmd(const char * cmd, StringBuffer & output)
{
StringBuffer modifiedCmd;
FILE * pipe;
int ch;
int len;
int pcloseStatus;
output.empty();
//--------
// Execute the command with its stderr and stdout merged together.
// TO DO: the "modifiedCmd" below works on Linux and in a cmd
// shell on Windows, but sometimes it does not work in a Cygwin
// shell.
//--------
modifiedCmd << cmd << " 2>&1";
//modifiedCmd << cmd;
pipe = CONFIG4CPP_POPEN(modifiedCmd.c_str(), "r");
if (!pipe) {
output << "cannot execute '" << cmd << "': popen() failed";
return false;
}
//--------
// Read from the pipe and delete the final '\n', if any.
//--------
while ((ch = fgetc(pipe)) != EOF) {
if (ch != '\r') {
output.append((char)ch);
}
}
len = output.length();
if (len > 0 && output[len-1] == '\n') {
output.deleteLastChar();
}
//--------
// We're done. Return success (true) if the exit status of
// the command was 0.
//--------
pcloseStatus = CONFIG4CPP_PCLOSE(pipe);
return (pcloseStatus == 0);
}
#ifdef WIN32
//--------
// Windows version.
//--------
#include <windows.h>
bool
isCmdInDir(const char * cmd, const char * dir)
{
StringBuffer fileName;
static const char * extensions[] = { "", ".exe", ".bat", 0 };
int i;
DWORD fileAttr;
for (i = 0; extensions[i] != 0; i++) {
fileName = "";
fileName << dir << "\\" << cmd << extensions[i];
fileAttr = GetFileAttributes(fileName.c_str());
if (fileAttr != 0xFFFFFFFF) {
return true;
}
}
return false;
}
#else
//--------
// UNIX version.
//--------
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
bool
isCmdInDir(const char * cmd, const char * dir)
{
StringBuffer fileName;
struct stat sb;
fileName << dir << "/" << cmd;
if (stat(fileName.c_str(), &sb) == 0) {
return true;
}
return false;
}
#endif
#ifdef P_STDIO_HAS_LIMITED_FDS
BufferedFileReader::BufferedFileReader()
{
m_fd = -1;
}
BufferedFileReader::~BufferedFileReader()
{
if (m_fd != -1) {
close();
}
}
bool
BufferedFileReader::open(const char * fileName)
{
assert(m_fd == -1);
m_fd = ::open(fileName, O_RDONLY);
m_bufIndex = 0;
m_bufLen = 0;
return (m_fd != -1);
}
bool
BufferedFileReader::close()
{
int result;
assert(m_fd != -1);
result = ::close(m_fd);
if (result != -1) {
m_fd = -1;
}
return (result != -1);
}
int
BufferedFileReader::getChar()
{
int size;
int result;
assert(m_fd != -1);
if (m_bufIndex == m_bufLen) {
size = ::read(m_fd, m_buf, BUFFERED_FILE_READER_BUF_SIZE);
m_bufIndex = 0;
m_bufLen = size;
if (size <= 0) {
m_bufLen = 0;
return EOF;
}
}
assert(m_bufIndex < m_bufLen);
result = m_buf[m_bufIndex];
m_bufIndex++;
return result;
}
#else
BufferedFileReader::BufferedFileReader()
{
m_file = 0;
}
BufferedFileReader::~BufferedFileReader()
{
if (m_file != 0) {
fclose(m_file);
}
}
bool
BufferedFileReader::open(const char * fileName)
{
assert(m_file == 0);
m_file = fopen(fileName, "r");
return (m_file != 0);
}
bool
BufferedFileReader::close()
{
int status;
status = fclose(m_file);
return (status != -1);
}
int
BufferedFileReader::getChar()
{
assert(m_file != 0);
return fgetc(m_file);
}
#endif
}; // namespace CONFIG4CPP_NAMESPACE