Skip to content

Commit

Permalink
Created file/directory I/O abstraction and desktop implementation. Is…
Browse files Browse the repository at this point in the history
  • Loading branch information
anders9ustafsson committed Aug 31, 2015
1 parent ad174d2 commit 90e2300
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 2 deletions.
7 changes: 6 additions & 1 deletion DICOM/DICOM.csproj
Expand Up @@ -86,6 +86,11 @@
<Compile Include="Imaging\Mathematics\Histogram.cs" />
<Compile Include="Imaging\Mathematics\MovingAverage.cs" />
<Compile Include="Imaging\Render\ImageGraphic.cs" />
<Compile Include="IO\DesktopDirectoryReference.cs" />
<Compile Include="IO\DesktopIOManager.cs" />
<Compile Include="IO\IDirectoryReference.cs" />
<Compile Include="IO\IFileReference.cs" />
<Compile Include="IO\IOManager.cs" />
<Compile Include="IO\MemoryMappedFileByteSource.cs" />
<Compile Include="Log\ConsoleLogger.cs" />
<Compile Include="Log\Logger.cs" />
Expand Down Expand Up @@ -313,7 +318,7 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
<UserProperties BuildVersion_BuildVersioningStyle="None.None.Increment.YearDayOfYear" BuildVersion_UseGlobalSettings="True" />
<UserProperties BuildVersion_UseGlobalSettings="True" BuildVersion_BuildVersioningStyle="None.None.Increment.YearDayOfYear" />
</VisualStudio>
</ProjectExtensions>
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
Expand Down
78 changes: 78 additions & 0 deletions DICOM/IO/DesktopDirectoryReference.cs
@@ -0,0 +1,78 @@
// Copyright (c) 2012-2015 fo-dicom contributors.
// Licensed under the Microsoft Public License (MS-PL).

namespace Dicom.IO
{
using System.Collections.Generic;
using System.IO;
using System.Linq;

/// <summary>
/// .NET/Windows Desktop implementation of the <see cref="IDirectoryReference"/> interface.
/// </summary>
public class DesktopDirectoryReference : IDirectoryReference
{
#region FIELDS

private readonly DirectoryInfo directoryInfo;

#endregion

#region CONSTRUCTORS

/// <summary>
/// Initializes a <see cref="DesktopDirectoryReference"/> object.
/// </summary>
/// <param name="directoryName">Name of the directory.</param>
public DesktopDirectoryReference(string directoryName)
{
this.directoryInfo = new DirectoryInfo(directoryName);
}

#endregion

#region METHODS

/// <summary>
/// Path name of the current directory.
/// </summary>
public string Name
{
get
{
return this.directoryInfo.FullName;
}
}

/// <summary>
/// Create the directory path if it does not already exist.
/// </summary>
public void Create()
{
if (!this.directoryInfo.Exists) this.directoryInfo.Create();
}

/// <summary>
/// Gets the file names of the files in the current directory.
/// </summary>
/// <param name="searchPattern">File search pattern; if null or empty all files in the directory should be returned.</param>
/// <returns>File names of the files in the current directory.</returns>
public IEnumerable<string> EnumerateFileNames(string searchPattern = null)
{
return string.IsNullOrWhiteSpace(searchPattern)
? this.directoryInfo.GetFiles().Select(fi => fi.FullName)
: this.directoryInfo.GetFiles(searchPattern).Select(fi => fi.FullName);
}

/// <summary>
/// Gets the names of the sub-directories in the current directory.
/// </summary>
/// <returns>Names of the sub-directories in the current directory.</returns>
public IEnumerable<string> EnumerateDirectoryNames()
{
return this.directoryInfo.EnumerateDirectories().Select(di => di.FullName);
}

#endregion
}
}
64 changes: 64 additions & 0 deletions DICOM/IO/DesktopIOManager.cs
@@ -0,0 +1,64 @@
// Copyright (c) 2012-2015 fo-dicom contributors.
// Licensed under the Microsoft Public License (MS-PL).

namespace Dicom.IO
{
/// <summary>
/// .NET/Windows Desktop implementation of the I/O manager.
/// </summary>
public sealed class DesktopIOManager : IOManager
{
#region FIELDS

/// <summary>
/// Single instance of the desktop I/O manager.
/// </summary>
public static readonly IOManager Instance;

#endregion

#region CONSTRUCTORS

/// <summary>
/// Initializes the static fields of <see cref="DesktopIOManager"/>
/// </summary>
static DesktopIOManager()
{
Instance = new DesktopIOManager();
}

/// <summary>
/// Initializes a <see cref="DesktopIOManager"/> object.
/// </summary>
private DesktopIOManager()
{
}

#endregion

#region METHODS

/// <summary>
/// Create a file reference.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="isTempFile">Indicates whether the file should be handled as a temporary file or not.</param>
/// <returns>A file reference object.</returns>
public override IFileReference CreateFileReference(string fileName, bool isTempFile = false)
{
return new FileReference(fileName, isTempFile);
}

/// <summary>
/// Create a directory reference.
/// </summary>
/// <param name="directoryName">Name of the directory.</param>
/// <returns>A directory reference object.</returns>
public override IDirectoryReference CreateDirectoryReference(string directoryName)
{
return new DesktopDirectoryReference(directoryName);
}

#endregion
}
}
2 changes: 1 addition & 1 deletion DICOM/IO/FileReference.cs
Expand Up @@ -6,7 +6,7 @@

namespace Dicom.IO
{
public sealed class FileReference
public sealed class FileReference : IFileReference
{
public FileReference(string fileName, bool isTempFile = false)
{
Expand Down
44 changes: 44 additions & 0 deletions DICOM/IO/IDirectoryReference.cs
@@ -0,0 +1,44 @@
// Copyright (c) 2012-2015 fo-dicom contributors.
// Licensed under the Microsoft Public License (MS-PL).

namespace Dicom.IO
{
using System.Collections.Generic;

/// <summary>
/// Interface representing reference to a single directory.
/// </summary>
public interface IDirectoryReference
{
#region PROPERTIES

/// <summary>
/// Path name of the current directory.
/// </summary>
string Name { get; }

#endregion

#region METHODS

/// <summary>
/// Create the directory path if it does not already exist.
/// </summary>
void Create();

/// <summary>
/// Gets the file names of the files in the current directory.
/// </summary>
/// <param name="searchPattern">File search pattern; if null or empty all files in the directory should be returned.</param>
/// <returns>File names of the files in the current directory.</returns>
IEnumerable<string> EnumerateFileNames(string searchPattern = null);

/// <summary>
/// Gets the names of the sub-directories in the current directory.
/// </summary>
/// <returns>Names of the sub-directories in the current directory.</returns>
IEnumerable<string> EnumerateDirectoryNames();

#endregion
}
}
54 changes: 54 additions & 0 deletions DICOM/IO/IFileReference.cs
@@ -0,0 +1,54 @@
// Copyright (c) 2012-2015 fo-dicom contributors.
// Licensed under the Microsoft Public License (MS-PL).

namespace Dicom.IO
{
using System.IO;

/// <summary>
/// Interface representing reference to a single file.
/// </summary>
public interface IFileReference
{
/// <summary>
/// Gets the file name.
/// </summary>
string Name { get; }

/// <summary>Gets whether the file is temporary or not.</summary>
bool IsTempFile { get; }

/// <summary>
/// Open a file stream for reading.
/// </summary>
/// <returns></returns>
Stream OpenRead();

/// <summary>
/// Open a file stream for writing.
/// </summary>
/// <returns></returns>
Stream OpenWrite();

/// <summary>
/// Delete the file.
/// </summary>
void Delete();

/// <summary>
/// Moves file and updates internal reference.
/// Calling this method will also set the <see cref="FileReference.IsTempFile"/> property to <c>False</c>.
/// </summary>
/// <param name="dstFileName">Full name of the moved file.</param>
/// <param name="overwrite">True if already existing file should be overwritten, false otherwise.</param>
void Move(string dstFileName, bool overwrite = false);

/// <summary>
///
/// </summary>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <returns></returns>
byte[] GetByteRange(int offset, int count);
}
}
39 changes: 39 additions & 0 deletions DICOM/IO/IOManager.cs
@@ -0,0 +1,39 @@
// Copyright (c) 2012-2015 fo-dicom contributors.
// Licensed under the Microsoft Public License (MS-PL).

namespace Dicom.IO
{
/// <summary>
/// Abstract manager class for file and directory based I/O.
/// </summary>
public abstract class IOManager
{
#region PROPERTIES

/// <summary>
/// Gets or sets the IOManager implementation type.
/// </summary>
public static IOManager Default { get; set; }

#endregion

#region METHODS

/// <summary>
/// Create a file reference.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="isTempFile">Indicates whether the file should be handled as a temporary file or not.</param>
/// <returns>A file reference object.</returns>
public abstract IFileReference CreateFileReference(string fileName, bool isTempFile = false);

/// <summary>
/// Create a directory reference.
/// </summary>
/// <param name="directoryName">Name of the directory.</param>
/// <returns>A directory reference object.</returns>
public abstract IDirectoryReference CreateDirectoryReference(string directoryName);

#endregion
}
}

0 comments on commit 90e2300

Please sign in to comment.