zhao
2021-06-24 02ca96debc6056275d58e55d97f7885a195542d0
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
using System;
using System.Collections.Generic;
using System.IO;
 
namespace HH.WMS.Utils.ExcelLibrary.CompoundDocumentFormat
{
    public class ShortSectorAllocation
    {
        CompoundDocument Document;
 
        List<Int32> ShortSectorAllocationTable;
 
        public ShortSectorAllocation(CompoundDocument document)
        {
            this.Document = document;
            ShortSectorAllocationTable = document.GetStreamDataAsIntegers(document.Header.FirstSectorIDofShortSectorAllocationTable);
            //ShortSectorAllocationTable.RemoveRange(document.Header.NumberOfShortSectors, ShortSectorAllocationTable.Count - document.Header.NumberOfShortSectors);
            while (ShortSectorAllocationTable.Count > 0 && ShortSectorAllocationTable[ShortSectorAllocationTable.Count - 1] == SID.Free)
            {
                ShortSectorAllocationTable.RemoveAt(ShortSectorAllocationTable.Count - 1);
            }
        }
 
        public int AllocateSector()
        {
            int newSectorID = ShortSectorAllocationTable.Count;
            LinkSectorID(newSectorID, SID.EOC);
            Document.AllocateNewShortSector();
            Document.Header.NumberOfShortSectors++;
            return newSectorID;
        }
 
        public int AllocateSectorAfter(int sectorID)
        {
            int newSectorID = this.AllocateSector();
            this.LinkSectorID(sectorID, newSectorID);
            return newSectorID;
        }
 
        public void LinkSectorID(int sectorID, int newSectorID)
        {
            if (sectorID < ShortSectorAllocationTable.Count)
            {
                ShortSectorAllocationTable[sectorID] = newSectorID;
            }
            else if (sectorID == ShortSectorAllocationTable.Count)
            {
                ShortSectorAllocationTable.Add(newSectorID);
            }
            else
            {
                throw new ArgumentOutOfRangeException("sectorID");
            }
        }
 
        public int GetNextSectorID(int sectorID)
        {
            if (sectorID < ShortSectorAllocationTable.Count)
            {
                return ShortSectorAllocationTable[sectorID];
            }
            else
            {
                return SID.EOC;
            }
        }
 
        public List<int> GetSIDChain(int StartSID)
        {
            List<int> chain = new List<int>();
            int sid = StartSID;
            while (sid != SID.EOC)
            {
                chain.Add(sid);
                sid = GetNextSectorID(sid);
            }
            return chain;
        }
 
        public void Save()
        {
            if (ShortSectorAllocationTable.Count > 0)
            {
                if (Document.Header.FirstSectorIDofShortSectorAllocationTable == SID.EOC)
                {
                    int SecIDCapacity = Document.SectorSize / 4;
                    int[] sids = new Int32[SecIDCapacity];
                    for (int i = 0; i < sids.Length; i++)
                    {
                        sids[i] = SID.Free;
                    }
                    Document.Header.FirstSectorIDofShortSectorAllocationTable = Document.AllocateDataSector();
                    Document.WriteInSector(Document.Header.FirstSectorIDofShortSectorAllocationTable, 0, sids);
                }
                MemoryStream satStream = new MemoryStream(ShortSectorAllocationTable.Count * 4);
                CompoundDocument.WriteArrayOfInt32(new BinaryWriter(satStream), ShortSectorAllocationTable.ToArray());
                Document.WriteStreamData(Document.Header.FirstSectorIDofShortSectorAllocationTable, satStream.ToArray());
            }
        }
    }
}