forked from ironfede/openmcdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFatEnumerator.cs
95 lines (78 loc) · 1.86 KB
/
FatEnumerator.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
using System.Collections;
using System.Diagnostics.CodeAnalysis;
namespace OpenMcdf;
/// <summary>
/// Enumerates the <see cref="FatEntry"/> records in a <see cref="Fat"/>.
/// </summary>
internal class FatEnumerator : IEnumerator<FatEntry>
{
readonly Fat fat;
bool start = true;
uint index = uint.MaxValue;
uint value = uint.MaxValue;
public FatEnumerator(Fat fat)
{
this.fat = fat;
}
/// <inheritdoc/>
public void Dispose()
{
}
/// <inheritdoc/>
public FatEntry Current
{
get
{
if (index == uint.MaxValue)
throw new InvalidOperationException("Enumeration has not started. Call MoveNext.");
return new(index, value);
}
}
/// <inheritdoc/>
object IEnumerator.Current => Current;
/// <inheritdoc/>
public bool MoveNext()
{
if (start)
{
start = false;
return MoveTo(0);
}
if (index >= SectorType.Maximum)
return false;
uint next = index + 1;
return MoveTo(next);
}
public bool MoveTo(uint index)
{
ThrowHelper.ThrowIfSectorIdIsInvalid(index);
start = false;
if (this.index == index)
return true;
if (fat.TryGetValue(index, out value))
{
this.index = index;
return true;
}
this.index = uint.MaxValue;
return false;
}
public bool MoveNextFreeEntry()
{
while (MoveNext())
{
if (value == SectorType.Free)
return true;
}
return false;
}
/// <inheritdoc/>
public void Reset()
{
start = true;
index = uint.MaxValue;
value = uint.MaxValue;
}
[ExcludeFromCodeCoverage]
public override string ToString() => $"{Current}";
}