Skip to content

Commit

Permalink
Add RootStorage.OpenWrite and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-visionaid committed Nov 20, 2024
1 parent 03ce131 commit 8124b1c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion OpenMcdf.Ole.Tests/OlePropertiesExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void ModifyDocumentSummaryInformation()
public void ReadSummaryInformationUtf8()
{
// Regression test for #33
using var cf = RootStorage.Open("wstr_presets.doc", FileMode.Open);
using var cf = RootStorage.OpenRead("wstr_presets.doc");
using CfbStream stream = cf.OpenStream(PropertySetNames.SummaryInformation);
OlePropertiesContainer co = new(stream);

Expand Down
12 changes: 12 additions & 0 deletions OpenMcdf.Tests/RootStorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
[TestClass]
public sealed class RootStorageTests
{
[TestMethod]
[DoNotParallelize] // Test sharing
[DataRow("MultipleStorage.cfs")]
public void Open(string fileName)
{
using var rootStorage = RootStorage.OpenRead(fileName);
using var rootStorage2 = RootStorage.OpenRead(fileName);

Assert.ThrowsException<IOException>(() => RootStorage.Open(fileName, FileMode.Open));
Assert.ThrowsException<IOException>(() => RootStorage.OpenWrite(fileName));
}

[TestMethod]
[DataRow(Version.V3, 0)]
[DataRow(Version.V3, 1)]
Expand Down
22 changes: 15 additions & 7 deletions OpenMcdf/RootStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ public static RootStorage Open(string fileName, FileMode mode, StorageModeFlags
return Open(stream, flags);
}

public static RootStorage Open(Stream stream, StorageModeFlags flags = StorageModeFlags.None)
{
stream.ThrowIfNotSeekable();
stream.Position = 0;

IOContextFlags contextFlags = ToIOContextFlags(flags);
RootContextSite rootContextSite = new();
_ = new RootContext(rootContextSite, stream, Version.Unknown, contextFlags);
return new RootStorage(rootContextSite, flags);
}

public static RootStorage OpenRead(string fileName, StorageModeFlags flags = StorageModeFlags.None)
{
ThrowIfLeaveOpen(flags);
Expand All @@ -78,15 +89,12 @@ public static RootStorage OpenRead(string fileName, StorageModeFlags flags = Sto
return Open(stream, flags);
}

public static RootStorage Open(Stream stream, StorageModeFlags flags = StorageModeFlags.None)
public static RootStorage OpenWrite(string fileName, StorageModeFlags flags = StorageModeFlags.None)
{
stream.ThrowIfNotSeekable();
stream.Position = 0;
ThrowIfLeaveOpen(flags);

IOContextFlags contextFlags = ToIOContextFlags(flags);
RootContextSite rootContextSite = new();
_ = new RootContext(rootContextSite, stream, Version.Unknown, contextFlags);
return new RootStorage(rootContextSite, flags);
FileStream stream = File.OpenWrite(fileName);
return Open(stream, flags);
}

RootStorage(RootContextSite rootContextSite, StorageModeFlags storageModeFlags)
Expand Down
2 changes: 1 addition & 1 deletion StructuredStorageExplorer/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void LoadFile(string fileName)
cf = null;

// Load file
cf = RootStorage.Open(fileName, FileMode.Open);
cf = RootStorage.OpenWrite(fileName);

fileNameLabel.Text = fileName;
saveAsToolStripMenuItem.Enabled = true;
Expand Down

0 comments on commit 8124b1c

Please sign in to comment.