forked from ironfede/openmcdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDifatSectorEnumerator.cs
129 lines (107 loc) · 3.31 KB
/
DifatSectorEnumerator.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
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
using System.Collections;
namespace OpenMcdf;
/// <summary>
/// Enumerates the <see cref="Sector"/>s in a DIFAT chain.
/// </summary>
internal class DifatSectorEnumerator : ContextBase, IEnumerator<Sector>
{
bool start = true;
uint index = uint.MaxValue;
Sector current = Sector.EndOfChain;
private uint difatSectorId = SectorType.EndOfChain;
public DifatSectorEnumerator(RootContextSite rootContextSite)
: base(rootContextSite)
{
}
/// <inheritdoc/>
public void Dispose()
{
}
/// <inheritdoc/>
public Sector Current
{
get
{
if (current.Id == SectorType.EndOfChain)
throw new InvalidOperationException("Enumeration has not started. Call MoveNext.");
return current;
}
}
/// <inheritdoc/>
object IEnumerator.Current => Current;
/// <inheritdoc/>
public bool MoveNext()
{
if (start)
{
start = false;
index = uint.MaxValue;
difatSectorId = Context.Header.FirstDifatSectorId;
}
uint nextIndex = index + 1;
if (difatSectorId == SectorType.EndOfChain)
{
index = uint.MaxValue;
current = Sector.EndOfChain;
difatSectorId = SectorType.EndOfChain;
return false;
}
current = new(difatSectorId, Context.SectorSize);
index = nextIndex;
Context.Reader.Position = current.EndPosition - sizeof(uint);
difatSectorId = Context.Reader.ReadUInt32();
return true;
}
public bool MoveTo(uint index)
{
if (index >= Context.Header.DifatSectorCount)
return false;
if (start && !MoveNext())
return false;
if (index < this.index)
Reset();
while (start || this.index < index)
{
if (!MoveNext())
return false;
}
return true;
}
/// <inheritdoc/>
public void Reset()
{
start = true;
index = uint.MaxValue;
current = Sector.EndOfChain;
difatSectorId = SectorType.EndOfChain;
}
public void Add()
{
Sector newDifatSector = new(Context.SectorCount, Context.SectorSize);
Header header = Context.Header;
CfbBinaryWriter writer = Context.Writer;
if (header.FirstDifatSectorId == SectorType.EndOfChain)
{
header.FirstDifatSectorId = newDifatSector.Id;
}
else
{
bool ok = MoveTo(header.DifatSectorCount - 1);
if (!ok)
throw new FileFormatException("The DIFAT sector count is invalid.");
writer.Position = current.EndPosition - sizeof(uint);
writer.Write(newDifatSector.Id);
}
writer.Position = newDifatSector.Position;
writer.Write(SectorDataCache.GetFatEntryData(newDifatSector.Length));
writer.Position = newDifatSector.EndPosition - sizeof(uint);
writer.Write(SectorType.EndOfChain);
Context.ExtendStreamLength(newDifatSector.EndPosition);
header.DifatSectorCount++;
Context.Fat[newDifatSector.Id] = SectorType.Difat;
start = false;
index = header.DifatSectorCount - 1;
current = newDifatSector;
difatSectorId = SectorType.EndOfChain;
}
}