Skip to content

Commit

Permalink
Simplify the reading of DictionaryProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
Numpsy committed Nov 16, 2024
1 parent 6feff0f commit 2bd9aa6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 62 deletions.
57 changes: 0 additions & 57 deletions OpenMcdf.Ole/DictionaryEntry.cs

This file was deleted.

32 changes: 27 additions & 5 deletions OpenMcdf.Ole/DictionaryProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OpenMcdf.Ole;

public class DictionaryProperty : IProperty
public sealed class DictionaryProperty : IProperty
{
private readonly int codePage;
private Dictionary<uint, string>? entries = new();
Expand All @@ -28,10 +28,7 @@ public void Read(BinaryReader br)

for (uint i = 0; i < numEntries; i++)
{
DictionaryEntry de = new(codePage);

de.Read(br);
entries!.Add(de.PropertyIdentifier, de.Name);
ReadEntry(br);
}

int m = (int)(br.BaseStream.Position - curPos) % 4;
Expand All @@ -45,6 +42,31 @@ public void Read(BinaryReader br)
}
}

// Read a single dictionary entry
private void ReadEntry(BinaryReader br)
{
uint propertyIdentifier = br.ReadUInt32();
int length = br.ReadInt32();

byte[] nameBytes;

if (codePage == CodePages.WinUnicode)
{
nameBytes = br.ReadBytes(length << 1);

int m = length * 2 % 4;
if (m > 0)
br.ReadBytes(m);
}
else
{
nameBytes = br.ReadBytes(length);
}

string entryName = Encoding.GetEncoding(codePage).GetString(nameBytes).Trim('\0');
entries!.Add(propertyIdentifier, entryName);
}

/// <summary>
/// Write the dictionary and all its values into the specified <see cref="BinaryWriter"/>.
/// </summary>
Expand Down

0 comments on commit 2bd9aa6

Please sign in to comment.