This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageReader.cs
94 lines (76 loc) · 2.93 KB
/
PageReader.cs
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
using System.IO;
using System.Collections.Generic;
using static GruntWurk.QuickLog;
namespace GruntWurk {
/// <summary>
/// Reads a TextStream page-by-page (FF breaks)
/// </summary>
public class PageReader {
public static int UNCOUNTED = int.MaxValue;
public static char DC1_CHAR = '\x017';
public static char[] WHITESPACE = { ' ' };
private StreamReader ReportTextStream;
string Buf;
public int PageNo;
public int PageCount = UNCOUNTED;
public int LineCount;
public List<string> PageLines;
public bool AtLastPage;
public PageReader(string FullyQualifiedFilename) {
PageLines = new List<string>();
OpenStream(FullyQualifiedFilename);
}
private void OpenStream(string FullyQualifiedFilename) {
ReportTextStream = new StreamReader(FullyQualifiedFilename);
PageNo = 0;
AtLastPage = false;
}
/// <summary>
/// Loads the PageLines array with the contents of the next page.
/// IMPORTANT: This routine assumes that any FF that occurs in the file will be at the beginning of a line (i.e. that it is immediately preceeded by a linefeed).
/// </summary>
public void FetchNextPage() {
ClearPage();
// First, start with the line buffer that was left over from the last page fetch (if any)
if (PageNo > 0) {
LineCount = 1;
PageLines.Add(Buf);
Buf = "";
}
PageNo++;
while (ReportTextStream.Peek() != 0) {
Buf = ReportTextStream.ReadLine();
if (Buf == null) {
ClosePageReader();
return;
}
Buf = Buf.TrimEnd(null);
if (Buf.Length > 0 && (Buf[0] == '\f' || Buf[0] == DC1_CHAR)) {
Buf = Buf.Substring(1);
if (Buf.Length == 0 && ReportTextStream.Peek() == 0) {
// There was nothing after the last FF but a blank line
ClosePageReader();
return;
}
if (!(PageNo == 1 && LineCount == 0)) {
// This marks the end of the page (unless the FF is at the very begining of the file)
return;
}
}
LineCount++;
PageLines.Add(Buf);
}
ClosePageReader();
}
public void ClearPage() {
PageLines.Clear();
LineCount = 0;
}
private void ClosePageReader() {
ReportTextStream.Close();
AtLastPage = true;
}
// TODO CountPages() -- Read though the file ASAP and count the FF's.
// TODO Invoke CountPages() if there are any Page specs that use "N-nn".
}
}