/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Text;
namespace Alphaleonis.Win32.Filesystem
{
/// Information about an alternate data stream.
///
public struct AlternateDataStreamInfo
{
#region Constructor
internal AlternateDataStreamInfo(string fullPath, NativeMethods.WIN32_FIND_STREAM_DATA findData) : this()
{
StreamName = ParseStreamName(findData.cStreamName);
Size = findData.StreamSize;
_fullPath = fullPath;
}
#endregion // Constructor
#region Public Properties
/// Gets the name of the alternate data stream.
/// This value is an empty string for the default stream (:$DATA), and for any other data stream it contains the name of the stream.
/// The name of the stream.
public string StreamName { get; private set; }
/// Gets the size of the stream.
public long Size { get; private set; }
private readonly string _fullPath;
/// Gets the full path to the stream.
///
/// This is a path in long path format that can be passed to to open the stream if
/// or
/// is specified.
///
/// The full path to the stream in long path format.
public string FullPath
{
get { return _fullPath + Path.StreamSeparator + StreamName + Path.StreamDataLabel; }
}
#endregion // Public Properties
#region Public Methods
/// Returns the hash code for this instance.
/// A 32-bit signed integer that is the hash code for this instance.
public override int GetHashCode()
{
return StreamName.GetHashCode();
}
/// Indicates whether this instance and a specified object are equal.
/// The object to compare with the current instance.
///
/// true if and this instance are the same type and represent the same value; otherwise, false.
///
public override bool Equals(object obj)
{
if (obj is AlternateDataStreamInfo)
{
AlternateDataStreamInfo other = (AlternateDataStreamInfo) obj;
return StreamName.Equals(other.StreamName, StringComparison.OrdinalIgnoreCase) && Size.Equals(other.Size);
}
return false;
}
/// Equality operator.
/// The first operand.
/// The second operand.
/// The result of the operation.
public static bool operator ==(AlternateDataStreamInfo first, AlternateDataStreamInfo second)
{
return first.Equals(second);
}
/// Inequality operator.
/// The first operand.
/// The second operand.
/// The result of the operation.
public static bool operator !=(AlternateDataStreamInfo first, AlternateDataStreamInfo second)
{
return !first.Equals(second);
}
#endregion // Public Methods
#region Private Methods
private static string ParseStreamName(string input)
{
if (input == null || input.Length < 2)
return string.Empty;
if (input[0] != Path.StreamSeparatorChar)
throw new ArgumentException(Resources.Invalid_Stream_Name);
var sb = new StringBuilder();
for (int i = 1; i < input.Length; i++)
{
if (input[i] == Path.StreamSeparatorChar)
break;
sb.Append(input[i]);
}
return sb.ToString();
}
#endregion // Private Methods
}
}