-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCrystalScriptStream.cpp
156 lines (142 loc) · 4.63 KB
/
CrystalScriptStream.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
#include "stdafx.h"
#include "CrystalScriptStream.h"
// CCrystalScriptStream
CCrystalScriptStream::CCrystalScriptStream(CScriptStreamLimiter *pBuffer)
{
_pBuffer = pBuffer;
}
void CScriptStreamLimiter::GetMoreData(int &nChar, int &nLine, int &nLength, PCSTR &pszLine)
{
while (nChar > nLength)
{
// Need to move to the next line
int cLines = GetLineCount();
bool fAtLimitedEnd = true;
while (nLine < cLines)
{
nLine++;
if (nLine < cLines)
{
fAtLimitedEnd = false; // Indicate this definitely isn't a limited end.
nLength = GetLineLength(nLine);
pszLine = GetLineChars(nLine);
ASSERT(pszLine != NULL);
nChar = 0;
break; // done
}
}
if (nLine == cLines)
{
bool fGenerateEOF = true;
// We ran out of data.
if (fAtLimitedEnd)
{
if (_hACDone && !_fCancel)
{
fGenerateEOF = false;
// We're at a "limited end". Let's tell the client and block.
SetEvent(_hACDone); // Tell the client
// Wait for future instructions
if (WAIT_OBJECT_0 == WaitForSingleObject(_hDoAC, INFINITE))
{
fGenerateEOF = _fCancel;
if (!_fCancel)
{
nLine--;
// 1) Pretend we're back where we were when we entered.
// nLength should be unchanged, nLine should be --, nChar should be unchanged.
// We should refetch nLength (since it may have changed) (don't think we need a new pszLine???)
pszLine = GetLineChars(nLine); // REVIEW: is this even necessary?
nLength = GetLineLength(nLine);
// And then go back to teh beginning of the top level loop.
}
}
else
{
ASSERT(FALSE);
}
}
else if (_pCallback)
{
_pCallback->Done();
}
}
if (fGenerateEOF)
{
pszLine = "\0"; // EOF
nLength = 1;
nChar = 0;
}
}
// else we're all good.
}
}
CCrystalScriptStream::const_iterator::const_iterator(CScriptStreamLimiter *pBuffer, LineCol dwPos) : _pBuffer(pBuffer), _pidStream(pBuffer->GetIdPtr())
{
_nLine = dwPos.Line();
_nChar = dwPos.Column();
if (_nLine < _pBuffer->GetLineCount())
{
_nLength = _pBuffer->GetLineLength(_nLine);
_pszLine = _pBuffer->GetLineChars(_nLine);
}
else
{
_nLength = 0;
}
_id = *_pidStream;
}
char CCrystalScriptStream::const_iterator::operator*()
{
if (_id == *_pidStream)
{
return (_nChar == _nLength) ? '\n' : _pszLine[_nChar];
}
else
{
_pszLine = _pBuffer->GetLineChars(_nLine);
_id = *_pidStream;
return (_nChar == _nLength) ? '\n' : _pszLine[_nChar];
}
}
CCrystalScriptStream::const_iterator& CCrystalScriptStream::const_iterator::operator++()
{
ASSERT(*_pszLine != 0); // EOF
_nChar++;
if ((_nChar > _nLength) ||
((_nChar == _nLength) && (_nLine == (_pBuffer->GetLineCount() - 1)))) // for == we use '\n', unless this is the last line
{
_pBuffer->GetMoreData(_nChar, _nLine, _nLength, _pszLine);
}
return *this;
}
bool CCrystalScriptStream::const_iterator::operator<(const const_iterator& _Right) const
{
return (_nLine < _Right._nLine) || ((_nLine == _Right._nLine) && ((_nChar < _Right._nChar)));
}
std::string CCrystalScriptStream::const_iterator::tostring() const
{
char sz[50];
sprintf(sz, "Line %d, column %d", _nLine + 1, _nChar);
return sz;
}
bool CCrystalScriptStream::const_iterator::operator==(const const_iterator& value) const
{
return (_nLine == value._nLine) && (_nChar == value._nChar);
}
bool CCrystalScriptStream::const_iterator::operator!=(const const_iterator& value) const
{
return (_nChar != value._nChar) || (_nLine != value._nLine);
}
LineCol CCrystalScriptStream::const_iterator::GetPosition() const
{
return LineCol(_nLine, _nChar);
}
int CCrystalScriptStream::const_iterator::GetLineNumber() const
{
return _nLine;
}
int CCrystalScriptStream::const_iterator::GetColumnNumber() const
{
return _nChar;
}