This repository was archived by the owner on May 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathStorage.cs
More file actions
69 lines (60 loc) · 2.35 KB
/
Storage.cs
File metadata and controls
69 lines (60 loc) · 2.35 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
using System;
using System.Collections.Generic;
using System.IO;
namespace DBFilesClient.NET
{
public class Storage<T> : Dictionary<int, T>, IStorage where T : class, new()
{
#region Header
public Type RecordType => typeof(T);
public int Signature { get; set; }
public bool HasIndexTable { get; set; }
public bool HasStringTable { get; set; }
public ushort IndexField { get; set; }
#endregion
public Storage(Stream fileStream, bool readOnly = true)
{
FromStream(fileStream);
}
private void FromStream(Stream dataStream)
{
using (var binaryReader = new BinaryReader(dataStream))
{
Signature = binaryReader.ReadInt32();
Reader<T> fileReader;
switch (Signature)
{
case 0x36424457:
fileReader = new WDB6.Reader<T>(dataStream);
break;
case 0x35424457:
fileReader = new WDB5.Reader<T>(dataStream);
break;
case 0x32424457:
fileReader = new WDB2.Reader<T>(dataStream);
break;
case 0x43424457:
fileReader = new WDBC.Reader<T>(dataStream);
break;
default:
throw new ArgumentOutOfRangeException(Signature.ToString("X"));
}
fileReader.OnRecordLoaded += (index, record) => this[index] = (T)record;
fileReader.Load();
HasIndexTable = fileReader.FileHeader.HasIndexTable;
HasStringTable = fileReader.FileHeader.HasStringTable;
IndexField = fileReader.FileHeader.IndexField;
}
}
public Storage(string fileName, bool readOnly = true)
{
using (var fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var fileBytes = new byte[fileStream.Length];
fileStream.Read(fileBytes, 0, fileBytes.Length);
using (var memoryStream = new MemoryStream(fileBytes, 0, fileBytes.Length, true, true))
FromStream(memoryStream);
}
}
}
}