forked from ironfede/openmcdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFatChainEnumerator.cs
239 lines (199 loc) · 5.78 KB
/
FatChainEnumerator.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
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
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace OpenMcdf;
/// <summary>
/// Enumerates the <see cref="Sector"/>s in a FAT sector chain.
/// </summary>
internal sealed class FatChainEnumerator : IEnumerator<uint>
{
private readonly Fat fat;
private readonly FatEnumerator fatEnumerator;
private uint startId;
private bool start = true;
private uint index = uint.MaxValue;
private uint current = uint.MaxValue;
private long length = -1;
public FatChainEnumerator(Fat fat, uint startSectorId)
{
this.fat = fat;
startId = startSectorId;
fatEnumerator = new(fat);
}
/// <inheritdoc/>
public void Dispose()
{
fatEnumerator.Dispose();
}
public Sector CurrentSector => new(current, fat.Context.SectorSize);
/// <inheritdoc/>
public uint Current
{
get
{
if (index == uint.MaxValue)
throw new InvalidOperationException("Enumeration has not started. Call MoveNext.");
return current;
}
}
/// <inheritdoc/>
object IEnumerator.Current => Current;
public bool IsAt(uint index) => !start && index == this.index;
/// <inheritdoc/>
public bool MoveNext()
{
if (start)
{
if (startId is SectorType.EndOfChain or SectorType.Free)
{
index = uint.MaxValue;
current = uint.MaxValue;
return false;
}
index = 0;
current = startId;
start = false;
return true;
}
if (index == uint.MaxValue)
return false;
uint value = fat[current];
if (value is SectorType.EndOfChain)
{
index = uint.MaxValue;
current = uint.MaxValue;
return false;
}
index++;
if (index > SectorType.Maximum)
{
// If the index is greater than the maximum, then the chain must contain a loop
index = uint.MaxValue;
current = uint.MaxValue;
throw new IOException("FAT sector chain is corrupt.");
}
current = value;
return true;
}
/// <summary>
/// Moves to the specified index within the FAT sector chain.
/// </summary>
/// <param name="index"></param>
/// <returns>true if the enumerator was successfully advanced to the given index</returns>
public bool MoveTo(uint index)
{
if (index < this.index)
Reset();
while (start || this.index < index)
{
if (!MoveNext())
return false;
}
return true;
}
public long GetLength()
{
if (length == -1)
{
Reset();
length = 0;
while (MoveNext())
{
length++;
}
}
return length;
}
/// <summary>
/// Extends the chain by one
/// </summary>
/// <returns>The ID of the new sector</returns>
public uint Extend() => ExtendFrom(0);
/// <summary>
/// Returns the ID of the first sector in the chain.
/// </summary>
public uint Extend(uint requiredChainLength)
{
uint chainLength = (uint)GetLength();
if (chainLength >= requiredChainLength)
throw new ArgumentException("The chain is already longer than required.", nameof(requiredChainLength));
if (startId == StreamId.NoStream)
{
startId = fat.Add(fatEnumerator, 0);
chainLength = 1;
}
bool ok = MoveTo(chainLength - 1);
Debug.Assert(ok);
uint lastId = current;
ok = fatEnumerator.MoveTo(lastId);
Debug.Assert(ok);
while (chainLength < requiredChainLength)
{
uint id = fat.Add(fatEnumerator, lastId);
fat[lastId] = id;
lastId = id;
chainLength++;
}
length = requiredChainLength;
return startId;
}
public uint ExtendFrom(uint hintId)
{
if (startId == SectorType.EndOfChain)
{
startId = fat.Add(fatEnumerator, hintId);
return startId;
}
Reset();
uint lastId = startId;
while (MoveNext())
{
lastId = current;
}
uint id = fat.Add(fatEnumerator, hintId);
fat[lastId] = id;
return id;
}
public uint Shrink(uint requiredChainLength)
{
uint chainLength = (uint)GetLength();
if (chainLength <= requiredChainLength)
throw new ArgumentException("The chain is already shorter than required.", nameof(requiredChainLength));
Reset();
uint lastId = current;
while (MoveNext())
{
if (!SectorType.IsFreeOrEndOfChain(lastId))
{
if (index == requiredChainLength)
fat[lastId] = SectorType.EndOfChain;
else if (index > requiredChainLength)
fat[lastId] = SectorType.Free;
}
lastId = current;
}
fat[lastId] = SectorType.Free;
if (requiredChainLength == 0)
{
startId = StreamId.NoStream;
}
#if DEBUG
this.length = -1;
this.length = GetLength();
Debug.Assert(length == requiredChainLength);
#endif
length = requiredChainLength;
return startId;
}
/// <inheritdoc/>
public void Reset() => Reset(startId);
public void Reset(uint startSectorId)
{
startId = startSectorId;
start = true;
index = uint.MaxValue;
current = uint.MaxValue;
}
[ExcludeFromCodeCoverage]
public override string ToString() => $"Index: {index} Current: {current}";
}