-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilteredTextFileReader.cs
More file actions
106 lines (88 loc) · 3.22 KB
/
FilteredTextFileReader.cs
File metadata and controls
106 lines (88 loc) · 3.22 KB
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
////////////////////////////////////////////////
// © https://github.com/badhitman - @fakegov
////////////////////////////////////////////////
using TextFileScannerLib.scan;
namespace TextFileScannerLib;
/// <summary>
/// Чтение файла через фильтр. Настроив фильтрующий сканер в последствии при чтении из этого файла фильтруемые данные не будут прочитаны (будут пропущены)
/// Фильтры могут быть трёх типов: строковой фильтр, фильтр регулярного выражения (regex) или фильтр данных (byte[])
/// </summary>
public class FilteredTextFileReader(string path, FileMode mode, FileAccess access) : IDisposable
{
readonly Stream TextFileStream = new FileStream(path, mode, access);
/// <summary>
/// Фильтрующий сканер. Данные, распознанные сканером, пропадут в выходном потоке [ReadByte()] так, как буд-то их не было вовсе
/// </summary>
public DataScanner Scanner { get; } = new DataScanner();
/// <summary>
/// Принудительное указание отключить фильтр
/// </summary>
public bool DisableFilters { get; set; } = false;
/// <summary>
/// TextFileStream.CanRead
/// </summary>
public bool CanRead => TextFileStream is not null && TextFileStream.CanRead;
/// <summary>
/// TextFileStream.Length
/// </summary>
public long Length => TextFileStream is null ? -1 : TextFileStream.Length;
/// <summary>
/// TextFileStream.Position
/// </summary>
public long Position
{
get => TextFileStream.Position;
set => TextFileStream.Position = value;
}
/// <summary>
/// FileStream.Name
/// </summary>
public string Name => TextFileStream is FileStream stream ? stream.Name : "";
/// <summary>
/// Read byte
/// </summary>
public int ReadByte()
{
int ret_val = TextFileStream.ReadByte();
if (DisableFilters || Scanner.MatchUnitsCount == 0)
return ret_val;
if (ret_val < 0)
{
if (Scanner.BufferBytes.Count == 0)//this.FileFilteredReadStream.Scanner.BufferBytes
return ret_val;
else
{
ret_val = Scanner.BufferBytes[0];
Scanner.BufferBytes.RemoveAt(0);
return ret_val;
}
}
Scanner.AddToBuffer(ret_val);
while (Scanner.BufferBytes.Count < Scanner.MaxDataLengthBytes)
{
ret_val = TextFileStream.ReadByte();
if (ret_val < 0)
break;
Scanner.AddToBuffer(ret_val);
}
ret_val = Scanner.BufferBytes[0];
Scanner.BufferBytes.RemoveAt(0);
return ret_val;
}
/// <summary>
/// Close
/// </summary>
public void Close()
{
TextFileStream.Close();
TextFileStream.Dispose();
}
/// <summary>
/// Dispose
/// </summary>
public void Dispose()
{
Close();
GC.SuppressFinalize(this);
}
}