-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathclass_LineReader.ahk
257 lines (235 loc) · 7.97 KB
/
class_LineReader.ahk
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Line reader classes
;; Copyright (C) 2012 Adrian Hawryluk
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; LINEREADER CLASSES:
;
; The stringLineReader class is a way of buffering a stream on line
; boundaries. The stream in this case is a string, however, by extending this
; class, you can buffer any stream.
;
; The member functions that need to be overridden are:
;
; 1. source()
;
; 2. updateBuffer(gettingLineContinuation)
;
; source()
;
; This is used by the diagnositicInfo() member function to state the source
; of the data. It returns a string with no whitespce at the begining or end
; of it.
;
; updateBuffer()
; This updated the internal member variables to the new
; values required to get the next line. It take one parameter,
; gettingLineContinuation, which if true, must keep the current buffer
; content intact by appending to it. If false, the data upto but excluding
; this.s_pos must be removed, this.s_pos must be set to 1 and
; this.contextStartsAtLine must be set to this.atLine. In either case
; this.s_len must reflect the number of characters that are in the buffer.
;
; The buffer contents is a string referenced by this.buffer. Changing this
; buffer's location is not a problem.
;
; USAGE:
;
; 1. Instantiate either stringLineReader() with a string to buffer as its 1st
; parameter, or fileLineReader() with the name of the file to buffer as its
; 1st parameter.
;
; 2. When you need a line, call the GetLine() member function. If there is a
; line to get, it will return 1, otherwise it will return 0. The parameters
; startPos and endPos are to be used on the member variable buffer using
; string manipulations functions like instr(), substr(), RegExMatch(), etc.
; This is done this way to minimize string copies.
;
; The parameter gettingLineContinuation is a flag to tell the lineReader to
; not clear the buffer, but rather append to it. This allows for more useful
; diagnostic information by allowing the information to persist and could
; potentially be used to not generate more strings then are necessary by
; keeping the buffered data alive.
;
; 3. Diagnostic info can be retreived using the member function
; diagnositicInfo(). This reports where the stream is coming from (the
; source), and a listing of the currently buffered lines (the context)
; with a pointer showing the start and end of the lines in use.
;
; The source, where the infomation is coming from and the context can be
; retrieved seperatly by the member functions source(), whatAndWhereAmI()
; and getContext() respectivly.
class stringLineReader
{
atLine := 0
startLine := 0
s_pos := 1
;buffer := ""
;s_len := StrLen(this.s_pos)
contextStartsAtLine := 1
__New(buffer)
{
this.buffer := buffer
this.s_len := strlen(buffer)
}
; Line is where the line is returned to.
; Returns 1 if found more data to read.
; Returns 0 if no more data to read.
GetLine(byref startPos, byref endPos, gettingLineContinuation=0)
{
if (gettingLineContinuation)
++this.atLine
else
this.startLine := ++this.atLine
cont := gettingLineContinuation
, newPos := -1
while (((newPos := RegExMatch(this.buffer, "P)\n\r?|\r\n?", NL_Len, this.s_pos)) == 0 or newPos == this.s_len)
&& cont := this.updateBuffer(cont))
{ ; intentionally left blank
}
if (newPos or this.s_pos <= this.s_len)
{
if (newPos)
{
; line with NL
startPos := this.s_pos
; Do not include `n in the endPos.
endPos := newPos - 1 ; -1 points at the character prior to the NL in the buffer
this.s_pos := newPos + NL_Len
return 1
}
else
{
; last line without terminating `n
; no more to get, use what you have
startPos := this.s_pos
this.s_pos := (endPos := this.s_len) + 1
--this.atLine
return 1
}
}
else
; no more to get
return 0
}
diagnositicInfo()
{
return this.whatAndWhereAmI() "`n`n" this.getContext()
}
; override this function if you can retreive more data from somwhere else.
;
; If gettingLineContinuation is non-zero, append to this.buffer and
; update this.s_len approprately.
;
; If gettingLineContinuation is zero, remove leading characters upto but
; excluding this.s_pos, update this.contextStartsAtLine, set this.s_pos to
; 1 and this.s_len to new length of buffer.
updateBuffer(gettingLineContinuation)
{
return 0
}
; states the source and at what line the last line that was read from
whatAndWhereAmI()
{
return this.source() (this.startLine == this.atLine ? " at line " this.atLine : " between lines " this.startLine " and " this.atLine)
}
; returns a string that shows each line that has been cached prefixed with line numbers and the
; previous line with a -> in front of it.
getContext()
{
; loop parse variable needs to be NOT a class member
buffer := this.buffer
oldFmt := A_FormatFloat
; context is to show previous line analysed.
atLine := this.atLine
startLine := this.startLine
lineCount := 0
pos := 1
while pos := RegExMatch(buffer, "P)\r\n?|\n\r?", len, pos) + len
++lineCount
; right justify line number
SetFormat, FloatFast, % "0" strlen(this.contextStartsAtLine + lineCount) ".0"
pos := 1
lineNumber := this.contextStartsAtLine + 0.0
while newPos := RegExMatch(buffer, "P)\r\n?|\n\r?", len, pos) + len
{
len := newPos - pos - len
line := substr(buffer, pos, len)
if (atLine != startLine)
stringWithLineNumbers .= (lineNumber = startLine ? "-\ " : lineNumber = atLine ? "-/ " : " ") lineNumber ": " line "`n"
else
stringWithLineNumbers .= (lineNumber = atLine ? "=> " : " ") lineNumber ": " line "`n"
++lineNumber
pos := newPos ? newPos : pos
}
line := substr(buffer, pos)
if (atLine != startLine)
stringWithLineNumbers .= (lineNumber = startLine ? "-\ " : lineNumber = atLine ? "-/ " : " ") lineNumber ": " line "`n"
else
stringWithLineNumbers .= (lineNumber = atLine ? "=> " : " ") lineNumber ": " line "`n"
; restore to old format
SetFormat, FloatFast, %oldFmt%
return stringWithLineNumbers
}
; Override this function to say where the class is getting its text stream from.
source()
{
return "string"
}
}
class fileLineReader extends stringLineReader
{
__New(filename)
{
this.filename := filename
this.file := FileOpen(filename, "r")
base.__New("")
}
; override this function if you can retreive more data from somwhere else.
;
; If gettingLineContinuation is non-zero, append to this.buffer and
; update this.s_len approprately.
;
; If gettingLineContinuation is zero, remove leading characters upto but
; excluding this.s_pos, update this.contextStartsAtLine, set this.s_pos to
; 1 and this.s_len to new length of buffer.
updateBuffer(gettingLineContinuation)
{
static bufferSize := 4096
if (this.file.AtEOF())
{
return 0
}
else if (gettingLineContinuation)
{
this.s_len := strlen(this.buffer .= this.file.Read(bufferSize))
}
else
{
this.s_len := strlen(this.buffer := substr(this.buffer, this.s_pos) this.file.Read(bufferSize))
this.s_pos := 1
this.contextStartsAtLine := this.atLine
}
return 1
}
source()
{
return "file '" this.filename "'"
}
__Delete()
{
this.file.Close()
}
}