Skip to content

Commit

Permalink
Add TryOpenStream/Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-visionaid committed Nov 17, 2024
1 parent 6feff0f commit 17626c3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 22 deletions.
31 changes: 17 additions & 14 deletions OpenMcdf.Tests/StorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ public sealed class StorageTests
[DataRow("MultipleStorage2.cfs", 1)]
[DataRow("MultipleStorage3.cfs", 1)]
[DataRow("MultipleStorage4.cfs", 1)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0063:Use simple 'using' statement", Justification = "Conditional build")]
public void Read(string fileName, long storageCount)
public void EnumerateEntries(string fileName, long storageCount)
{
using (var rootStorage = RootStorage.OpenRead(fileName))
{
IEnumerable<EntryInfo> storageEntries = rootStorage.EnumerateEntries();
Assert.AreEqual(storageCount, storageEntries.Count());
}
using var rootStorage = RootStorage.OpenRead(fileName);
IEnumerable<EntryInfo> storageEntries = rootStorage.EnumerateEntries();
Assert.AreEqual(storageCount, storageEntries.Count());
}

#if WINDOWS
using (var rootStorage = StructuredStorage.Storage.Open(fileName))
{
IEnumerable<string> entries = rootStorage.EnumerateEntries();
Assert.AreEqual(storageCount, entries.Count());
}
#endif
[TestMethod]
[DataRow("MultipleStorage.cfs")]
public void OpenStorage(string fileName)
{
using var rootStorage = RootStorage.OpenRead(fileName);
Assert.IsTrue(rootStorage.TryOpenStorage("MyStorage", out Storage? _));
Assert.IsFalse(rootStorage.TryOpenStorage("", out Storage? _));

Assert.ThrowsException<DirectoryNotFoundException>(() => rootStorage.OpenStorage(""));

Storage storage = rootStorage.OpenStorage("MyStorage");
Assert.AreEqual("MyStorage", storage.EntryInfo.Name);
}

[TestMethod]
Expand Down
14 changes: 14 additions & 0 deletions OpenMcdf.Tests/StreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
[TestClass]
public sealed class StreamTests
{
[TestMethod]
[DataRow("TestStream_v3_0.cfs")]
public void OpenStream(string fileName)
{
using var rootStorage = RootStorage.OpenRead(fileName);
Assert.IsTrue(rootStorage.TryOpenStream("TestStream", out CfbStream? _));
Assert.IsFalse(rootStorage.TryOpenStream("", out CfbStream? _));

Assert.ThrowsException<FileNotFoundException>(() => rootStorage.OpenStream(""));

CfbStream stream = rootStorage.OpenStream("TestStream");
Assert.AreEqual("TestStream", stream.EntryInfo.Name);
}

[TestMethod]
[DataRow(Version.V3, 0)]
[DataRow(Version.V3, 63)]
Expand Down
36 changes: 28 additions & 8 deletions OpenMcdf/Storage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OpenMcdf;
using System.Diagnostics.CodeAnalysis;

namespace OpenMcdf;

/// <summary>
/// An object in a compound file that is analogous to a file system directory.
Expand Down Expand Up @@ -114,28 +116,46 @@ public CfbStream CreateStream(string name)
}

public Storage OpenStorage(string name)
=> TryOpenStorage(name, out Storage? storage)
? storage!
: throw new DirectoryNotFoundException($"Storage not found: {name}.");

public bool TryOpenStorage(string name, [MaybeNullWhen(false)] out Storage? storage)
{
ThrowHelper.ThrowIfNameIsInvalid(name);

this.ThrowIfDisposed(Context.IsDisposed);

directoryTree.TryGetDirectoryEntry(name, out DirectoryEntry? entry);
directoryTree.TryGetDirectoryEntry(name, out DirectoryEntry? entry);
if (entry is null || entry.Type is not StorageType.Storage)
throw new DirectoryNotFoundException($"Storage not found: {name}.");
return new Storage(ContextSite, entry, this);
{
storage = null;
return false;
}

storage = new Storage(ContextSite, entry, this);
return true;
}

public CfbStream OpenStream(string name)
=> TryOpenStream(name, out CfbStream? stream)
? stream!
: throw new FileNotFoundException($"Stream not found: {name}.", name);

public bool TryOpenStream(string name, [MaybeNullWhen(false)] out CfbStream? stream)
{
ThrowHelper.ThrowIfNameIsInvalid(name);

this.ThrowIfDisposed(Context.IsDisposed);

directoryTree.TryGetDirectoryEntry(name, out DirectoryEntry? entry);

if (entry is null || entry.Type is not StorageType.Stream)
throw new FileNotFoundException($"Stream not found: {name}.", name);
{
stream = null;
return false;
}

return new CfbStream(ContextSite, entry, this);
stream = new CfbStream(ContextSite, entry, this);
return true;
}

public void CopyTo(Storage destination)
Expand Down

0 comments on commit 17626c3

Please sign in to comment.