-
Notifications
You must be signed in to change notification settings - Fork 23
/
CTextStream.cpp
118 lines (98 loc) · 2.4 KB
/
CTextStream.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
#include "stdafx.h"
#include "CTextStream.h"
#define MAX_PIPE_SIZE (1024 * 1024)
#ifdef SPASM_NG_ENABLE_COM
HRESULT CTextStream::FinalConstruct()
{
HANDLE hRead, hWrite;
BOOL fResult = CreatePipe(&hRead, &hWrite, NULL, MAX_PIPE_SIZE);
if (fResult)
{
//DWORD dwMode = PIPE_READMODE_BYTE | PIPE_NOWAIT;
//fResult = SetNamedPipeHandleState(hRead, &dwMode, NULL, NULL);
fResult = SetStdHandle(STD_OUTPUT_HANDLE, hWrite);
m_hRead = hRead;
stdout->_file = _open_osfhandle((long) hWrite, _O_TEXT);
}
return S_OK;
}
STDMETHODIMP CTextStream::get_AtEndOfLine(VARIANT_BOOL *bEOL)
{
return E_NOTIMPL;
}
STDMETHODIMP CTextStream::get_AtEndOfStream(VARIANT_BOOL *bEOS)
{
fflush(stdout);
DWORD dwAvail;
BOOL fResult = PeekNamedPipe(m_hRead, NULL, 0, NULL, &dwAvail, NULL);
*bEOS = (dwAvail == 0) ? VARIANT_TRUE : VARIANT_FALSE;
return S_OK;
}
STDMETHODIMP CTextStream::Read(LONG cch, LPBSTR lpbstrResult)
{
fflush(stdout);
DWORD dwRead;
CComHeapPtr<TCHAR> szText;
int cbText = min(MAX_PIPE_SIZE, cch) * sizeof(TCHAR);
szText.Allocate(cbText);
BOOL fResult = ReadFile(m_hRead, szText, cbText, &dwRead, NULL);
if (fResult)
{
szText[dwRead / sizeof(TCHAR)] = _T('\0');
*lpbstrResult = SysAllocString(_bstr_t(szText));
return S_OK;
}
return E_FAIL;
}
STDMETHODIMP CTextStream::ReadLine(LPBSTR lpbstrLine)
{
fflush(stdout);
_bstr_t result;
TCHAR ch[2] = {_T('\0')};
DWORD dwRead;
BOOL fResult;
do
{
fResult = ReadFile(m_hRead, ch, sizeof(TCHAR), &dwRead, NULL);
if (fResult && dwRead == sizeof(TCHAR) && ch[0] != _T('\r') && ch[0] != _T('\n'))
{
result += ch;
}
}
while (ch[0] != _T('\n') && fResult && dwRead > 0);
*lpbstrLine = SysAllocString(result);
return S_OK;
}
STDMETHODIMP CTextStream::ReadAll(LPBSTR lpbstrResult)
{
return Read(LONG_MAX, lpbstrResult);
}
STDMETHODIMP CTextStream::Write(BSTR bstrText)
{
return E_NOTIMPL;
}
STDMETHODIMP CTextStream::WriteLine(BSTR bstrLine)
{
return E_NOTIMPL;
}
STDMETHODIMP CTextStream::WriteBlankLines(LONG nLines)
{
return E_NOTIMPL;
}
STDMETHODIMP CTextStream::Skip(LONG cch)
{
BSTR bstrLine = NULL;
HRESULT hr = Read(cch, &bstrLine);
return hr;
}
STDMETHODIMP CTextStream::SkipLine()
{
BSTR bstrLine = NULL;
HRESULT hr = ReadLine(&bstrLine);
return hr;
}
STDMETHODIMP CTextStream::Close()
{
return S_FALSE;
}
#endif