|
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace AlgorithmReuse
{
public struct DisplayItem
{
public string Info;
public string Message;
}
public class ProcessorBase
{
/// <summary>
/// Messages from processors to show on UI.
/// </summary>
protected List<DisplayItem> m_DisplayItems = new List<DisplayItem>();
/// <summary>
/// Gets the messages to show on UI.
/// </summary>
public virtual List<DisplayItem> DisplayItems
{
get { return m_DisplayItems; }
}
}
/// <summary>
/// Processor to display all directories and all files.
/// </summary>
public class DisplayAllProcessor : ProcessorBase
{
#region Delegate properties
public Iterators.DirectoryInfoHandler DisplayDirInfo
{
get { return MyDisplayDirInfo; }
}
public Iterators.FileInfoHandler DisplayFileInfo
{
get { return MyDisplayFileInfo; }
}
#endregion Delegate properties
#region Functions returned by delegate properties
private void MyDisplayDirInfo(DirectoryInfo Info)
{
DisplayItem di;
di.Info = "Dir";
di.Message = Info.FullName;
m_DisplayItems.Add(di);
}
private void MyDisplayFileInfo(FileInfo Info)
{
DisplayItem di;
di.Info = "File";
di.Message = Info.FullName;
m_DisplayItems.Add(di);
}
#endregion Functions returned by delegate properties
}
/// <summary>
/// Processor to find an average and maximum file sizes in a collection.
/// </summary>
public class FindAvgAndMaxProcessor : ProcessorBase
{
private long m_MaxFileSize = 0;
private FileInfo m_MaxFileInfo = null;
private long m_TotalFileSizes = 0;
private long m_TotalFiles = 0;
public override List<DisplayItem> DisplayItems
{
get
{
m_DisplayItems.Clear();
DisplayItem diAvg;
diAvg.Info = "Avg file size";
long lAverage = m_TotalFiles == 0 ? 0 : m_TotalFileSizes / m_TotalFiles;
diAvg.Message = string.Format("{0} bytes", lAverage.ToString());
DisplayItem diMax;
diMax.Info = "Max file size";
string strFileName = m_MaxFileInfo == null ? "None" : m_MaxFileInfo.FullName;
diMax.Message = string.Format("File name: {0}, size: {1} bytes", strFileName, m_MaxFileSize);
m_DisplayItems.Add(diAvg);
m_DisplayItems.Add(diMax);
return base.DisplayItems;
}
}
protected virtual void ClearStats()
{
m_MaxFileSize = 0;
m_MaxFileInfo = null;
m_TotalFileSizes = 0;
m_TotalFiles = 0;
}
#region Delegate properties
public Iterators.FileInfoHandler CountFileSizes
{
get { return MyCountFileSizes; }
}
#endregion Delegate properties
#region Functions returned by delegate properties
protected virtual void MyCountFileSizes(FileInfo Info)
{
// Keep track of the biggest file.
if (Info.Length > m_MaxFileSize)
{
m_MaxFileSize = Info.Length;
m_MaxFileInfo = Info;
}
// Keep track of file count and the sum of all file sizes.
++m_TotalFiles;
m_TotalFileSizes += Info.Length;
}
#endregion Functions returned by delegate properties
}
public class FindAvgAndMaxByExtProcessor : FindAvgAndMaxProcessor
{
private string m_Extension;
public FindAvgAndMaxByExtProcessor(string Extension)
{
m_Extension = Extension;
}
public string Extension
{
get { return m_Extension; }
set
{
m_Extension = value;
ClearStats();
}
}
#region Functions returned by delegate properties
protected override void MyCountFileSizes(FileInfo Info)
{
if (0 == string.Compare(Info.Extension, m_Extension, StringComparison.CurrentCultureIgnoreCase))
{
base.MyCountFileSizes(Info);
}
}
#endregion Functions returned by delegate properties
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.