forked from bkaradzic/bx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.cpp
168 lines (136 loc) · 3.69 KB
/
process.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
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
/*
* Copyright 2010-2024 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bx/blob/master/LICENSE
*/
#include <bx/process.h>
#include <stdio.h>
#ifndef BX_CONFIG_CRT_PROCESS
# define BX_CONFIG_CRT_PROCESS !(0 \
|| BX_CRT_NONE \
|| BX_PLATFORM_EMSCRIPTEN \
|| BX_PLATFORM_PS4 \
|| BX_PLATFORM_WINRT \
|| BX_PLATFORM_XBOXONE \
)
#endif // BX_CONFIG_CRT_PROCESS
namespace bx
{
#if BX_CONFIG_CRT_PROCESS
#if BX_CRT_MSVC
# define popen _popen
# define pclose _pclose
#endif // BX_CRT_MSVC
ProcessReader::ProcessReader()
: m_file(NULL)
{
}
ProcessReader::~ProcessReader()
{
BX_ASSERT(NULL == m_file, "Process not closed!");
}
bool ProcessReader::open(const FilePath& _filePath, const StringView& _args, Error* _err)
{
BX_ASSERT(NULL != _err, "Reader/Writer interface calling functions must handle errors.");
if (NULL != m_file)
{
BX_ERROR_SET(_err, kErrorReaderWriterAlreadyOpen, "ProcessReader: File is already open.");
return false;
}
char tmp[kMaxFilePath*2] = "\"";
strCat(tmp, BX_COUNTOF(tmp), _filePath);
strCat(tmp, BX_COUNTOF(tmp), "\" ");
strCat(tmp, BX_COUNTOF(tmp), _args);
m_file = popen(tmp, "r");
if (NULL == m_file)
{
BX_ERROR_SET(_err, kErrorReaderWriterOpen, "ProcessReader: Failed to open process.");
return false;
}
return true;
}
void ProcessReader::close()
{
BX_ASSERT(NULL != m_file, "Process not open!");
FILE* file = (FILE*)m_file;
m_exitCode = pclose(file);
m_file = NULL;
}
int32_t ProcessReader::read(void* _data, int32_t _size, Error* _err)
{
BX_ASSERT(NULL != _err, "Reader/Writer interface calling functions must handle errors."); BX_UNUSED(_err);
FILE* file = (FILE*)m_file;
int32_t size = (int32_t)fread(_data, 1, _size, file);
if (size != _size)
{
if (0 != feof(file) )
{
BX_ERROR_SET(_err, kErrorReaderWriterEof, "ProcessReader: EOF.");
}
else if (0 != ferror(file) )
{
BX_ERROR_SET(_err, kErrorReaderWriterRead, "ProcessReader: read error.");
}
return size >= 0 ? size : 0;
}
return size;
}
int32_t ProcessReader::getExitCode() const
{
return m_exitCode;
}
ProcessWriter::ProcessWriter()
: m_file(NULL)
{
}
ProcessWriter::~ProcessWriter()
{
BX_ASSERT(NULL == m_file, "Process not closed!");
}
bool ProcessWriter::open(const FilePath& _filePath, const StringView& _args, Error* _err)
{
BX_ASSERT(NULL != _err, "Reader/Writer interface calling functions must handle errors.");
if (NULL != m_file)
{
BX_ERROR_SET(_err, kErrorReaderWriterAlreadyOpen, "ProcessWriter: File is already open.");
return false;
}
char tmp[kMaxFilePath*2] = "\"";
strCat(tmp, BX_COUNTOF(tmp), _filePath);
strCat(tmp, BX_COUNTOF(tmp), "\" ");
strCat(tmp, BX_COUNTOF(tmp), _args);
m_file = popen(tmp, "w");
if (NULL == m_file)
{
BX_ERROR_SET(_err, kErrorReaderWriterOpen, "ProcessWriter: Failed to open process.");
return false;
}
return true;
}
void ProcessWriter::close()
{
BX_ASSERT(NULL != m_file, "Process not open!");
FILE* file = (FILE*)m_file;
m_exitCode = pclose(file);
m_file = NULL;
}
int32_t ProcessWriter::write(const void* _data, int32_t _size, Error* _err)
{
BX_ASSERT(NULL != _err, "Reader/Writer interface calling functions must handle errors."); BX_UNUSED(_err);
FILE* file = (FILE*)m_file;
int32_t size = (int32_t)fwrite(_data, 1, _size, file);
if (size != _size)
{
if (0 != ferror(file) )
{
BX_ERROR_SET(_err, kErrorReaderWriterWrite, "ProcessWriter: write error.");
}
return size >= 0 ? size : 0;
}
return size;
}
int32_t ProcessWriter::getExitCode() const
{
return m_exitCode;
}
#endif // BX_CONFIG_CRT_PROCESS
} // namespace bx