forked from fo-dicom/fo-dicom
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created file/directory I/O abstraction and desktop implementation. Is…
…sue fo-dicom#69
- Loading branch information
1 parent
ad174d2
commit 90e2300
Showing
7 changed files
with
286 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |