diff --git a/.gitignore b/.gitignore index 8ef8374..acabd84 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,12 @@ ITI.Run/bin/ ITI.NetMorseCode/bin/ ITI.NetMorseCode/obj/ ITI.Run/obj/ +NetFileProcessor.v12.suo +ITI.NetFileProcessor.Tests/bin/* +ITI.NetFileProcessor.Tests/obj/* +packages/NUnitTestAdapter.1.0/* +.nuget/* +packages/* +TestResults/c7563c40-b522-4d48-ba11-3a9a7aaa85d8/Nementon_NEMENTON-PC 2014-03-12 15_41_29.coverage +NetFileProcessor.v12.suo +NetFileProcessor.v12.suo diff --git a/ITI.NetFileProcessor.Tests/FileProcessorTests.cs b/ITI.NetFileProcessor.Tests/FileProcessorTests.cs new file mode 100644 index 0000000..1ee426e --- /dev/null +++ b/ITI.NetFileProcessor.Tests/FileProcessorTests.cs @@ -0,0 +1,56 @@ +using NUnit.Framework; +using System; +using System.IO; + +namespace ITI.NetFileProcessor.Tests +{ + [TestFixture] + public class FileProcessorTests + { + static string GenerateUniqPath() + { + return Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString()); + } + + static void CreateDirectory(string rootPath, int folderCount, int filesCount, int withHiddenFileCount) + { + int hiddenFileIdx = 0; + + for(var i=0; i + + + Debug + AnyCPU + {33703B7F-21DB-4A01-98F7-99708B2EB3B4} + Library + Properties + ITI.NetFileProcessor.Tests + ITI.NetFileProcessor.Tests + v4.5 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + ..\ + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\NUnitTestAdapter.1.0\lib\nunit.core.dll + False + + + ..\packages\NUnitTestAdapter.1.0\lib\nunit.core.interfaces.dll + False + + + ..\packages\NUnit.2.6.3\lib\nunit.framework.dll + + + ..\packages\NUnitTestAdapter.1.0\lib\nunit.util.dll + False + + + ..\packages\NUnitTestAdapter.1.0\lib\NUnit.VisualStudio.TestAdapter.dll + False + + + + + + + + + + + + + + + + + + + + + + + + + {c2c98f7c-5d72-4c14-9ecc-3e599ec71e4e} + ITI.NetFileProcessor + + + + + + + False + + + False + + + False + + + False + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + \ No newline at end of file diff --git a/ITI.NetFileProcessor.Tests/ITI.NetFileProcessor.Tests.csproj.user b/ITI.NetFileProcessor.Tests/ITI.NetFileProcessor.Tests.csproj.user new file mode 100644 index 0000000..13396e3 --- /dev/null +++ b/ITI.NetFileProcessor.Tests/ITI.NetFileProcessor.Tests.csproj.user @@ -0,0 +1,10 @@ + + + + Program + H:\Clouds\SkyDrive\Dev\C#\netfileprocessor\packages\NUnit.Runners.2.6.3\tools\nunit.exe + "ITI.NetFileProcessor.Tests.dll" + false + false + + \ No newline at end of file diff --git a/ITI.NetFileProcessor.Tests/Properties/AssemblyInfo.cs b/ITI.NetFileProcessor.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..1da0942 --- /dev/null +++ b/ITI.NetFileProcessor.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ITI.NetFileProcessor.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ITI.NetFileProcessor.Tests")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("affe2400-b9e9-4c6a-b362-81ecee40bc50")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ITI.NetFileProcessor.Tests/packages.config b/ITI.NetFileProcessor.Tests/packages.config new file mode 100644 index 0000000..5465fd2 --- /dev/null +++ b/ITI.NetFileProcessor.Tests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/ITI.NetFileProcessor/ConsoleFileProcessorRenderer.cs b/ITI.NetFileProcessor/ConsoleFileProcessorRenderer.cs index ea07fd6..4cde24e 100644 --- a/ITI.NetFileProcessor/ConsoleFileProcessorRenderer.cs +++ b/ITI.NetFileProcessor/ConsoleFileProcessorRenderer.cs @@ -9,8 +9,8 @@ public class ConsoleFileProcessorRenderer : IFileProcessorRenderer { public void render(FileProcessor processor) { - Console.WriteLine("Found {0} files(s) with {1} hidden and {2} innacessible", processor.FileCount, processor.HiddenFileCount, processor.InaccesibleFileCount); - Console.WriteLine("Found {0} folder(s) with {1} hidden and {2} innacessible", processor.DirectoryCount, processor.HiddenDirectoryCount, processor.InaccesibleDirectoryCount); + Console.WriteLine("Found {0} files(s) with {1} hidden and {2} inacessible", processor.Result.FileCount, processor.Result.HiddenFileCount, processor.Result.InaccesibleFileCount); + Console.WriteLine("Found {0} folder(s) with {1} hidden and {2} inacessible", processor.Result.DirectoryCount, processor.Result.HiddenDirectoryCount, processor.Result.InaccesibleDirectoryCount); } } } diff --git a/ITI.NetFileProcessor/ConsoleMorseFileProcessorRenderer.cs b/ITI.NetFileProcessor/ConsoleMorseFileProcessorRenderer.cs index 6b3e0c8..64b8ccf 100644 --- a/ITI.NetFileProcessor/ConsoleMorseFileProcessorRenderer.cs +++ b/ITI.NetFileProcessor/ConsoleMorseFileProcessorRenderer.cs @@ -10,11 +10,11 @@ public class ConsoleMorseFileProcessorRenderer : IFileProcessorRenderer { public void render(FileProcessor processor) { - MorsePlayer player = new MorsePlayer(new MorseProcessor(15)); + MorsePlayer player = new MorsePlayer(new MorseProcessor()); StringBuilder pResult = new StringBuilder(256); - pResult.AppendFormat("Found {0} files(s) with {1} hidden and {2} innacessible", processor.FileCount, processor.HiddenFileCount, processor.InaccesibleFileCount); - pResult.AppendFormat(" and found {0} folder(s) with {1} hidden and {2} innacessible", processor.FileCount, processor.HiddenFileCount, processor.InaccesibleFileCount); + pResult.AppendFormat("Found {0} files(s) with {1} hidden and {2} inacessible", processor.Result.FileCount, processor.Result.HiddenFileCount, processor.Result.InaccesibleFileCount); + pResult.AppendFormat(" and found {0} folder(s) with {1} hidden and {2} inacessible", processor.Result.DirectoryCount, processor.Result.HiddenDirectoryCount, processor.Result.InaccesibleDirectoryCount); player.Text = pResult.ToString().Trim(); player.Play(); diff --git a/ITI.NetFileProcessor/DirectoryResult.cs b/ITI.NetFileProcessor/DirectoryResult.cs new file mode 100644 index 0000000..72b4d9f --- /dev/null +++ b/ITI.NetFileProcessor/DirectoryResult.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ITI.NetFileProcessor +{ + public class DirectoryResult + { + internal DirectoryResult() { } + public int FileCount { get; internal set; } + public int HiddenFileCount { get; internal set; } + public int InaccesibleFileCount { get; internal set; } + public int DirectoryCount { get; internal set; } + public int HiddenDirectoryCount { get; internal set; } + public int InaccesibleDirectoryCount { get; internal set; } + public DirectoryInfo RootDirectory { get; internal set; } + } +} diff --git a/ITI.NetFileProcessor/FileProcessor.cs b/ITI.NetFileProcessor/FileProcessor.cs index 8920d40..7c67e2d 100644 --- a/ITI.NetFileProcessor/FileProcessor.cs +++ b/ITI.NetFileProcessor/FileProcessor.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; namespace ITI.NetFileProcessor @@ -6,15 +7,9 @@ namespace ITI.NetFileProcessor public class FileProcessor { IFileProcessorRenderer _rendererProvider; - - #region attributes - public int FileCount { get; private set; } - public int HiddenFileCount { get; private set; } - public int InaccesibleFileCount { get; private set; } - public int DirectoryCount { get; private set; } - public int HiddenDirectoryCount { get; private set; } - public int InaccesibleDirectoryCount { get; private set; } - #endregion + DirectoryResult _directoryResult; + + public DirectoryResult Result { get { return _directoryResult; } } #region constructor public FileProcessor() : this(new ConsoleFileProcessorRenderer()) { } @@ -22,6 +17,7 @@ public FileProcessor() : this(new ConsoleFileProcessorRenderer()) { } public FileProcessor(IFileProcessorRenderer rendererProvider) { _rendererProvider = rendererProvider; + _directoryResult = new DirectoryResult(); } #endregion @@ -30,65 +26,83 @@ public void ShowResults() _rendererProvider.render(this); } - public void Process(string path) + public DirectoryResult Process(string path) + { + _directoryResult.RootDirectory = new DirectoryInfo(path); + if (_directoryResult.RootDirectory.Exists) + { + Process(path, isHidden(_directoryResult.RootDirectory)); + } + return _directoryResult; + } + + #region private methods + private void Process(string path, bool hasParentHidden) { if (File.Exists(path)) { - ProcessFile(new FileInfo(path)); + ProcessFile(path, hasParentHidden); } else if (Directory.Exists(path)) { - string[] directories = Directory.GetDirectories(path, "*", SearchOption.AllDirectories); - string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories); + hasParentHidden = hasParentHidden ? hasParentHidden : isHidden(path); - foreach (string file in files) + foreach (string file in SafeFileEnumerator.EnumerateFiles(path)) { - ProcessFile(new FileInfo(file), new DirectoryInfo(path)); + ProcessFile(file, hasParentHidden); } - foreach (string directory in directories) + foreach (string directory in SafeFileEnumerator.EnumerateDirectories(path)) { - ProcessDirectory(new DirectoryInfo(directory)); - Process(directory); + ProcessDirectory(directory, hasParentHidden); } } } - #region private methods - private void ProcessFile(FileInfo file, DirectoryInfo parent) + private void ProcessFile(string path, bool hasParentHidden) { - if (isHidden(parent)) + FileInfo file = new FileInfo(path); + + if (isHidden(file)) { - InaccesibleFileCount += 1; + _directoryResult.HiddenFileCount += 1; } - ProcessFile(file); - } - private void ProcessFile(FileInfo file) - { - FileCount += 1; - if (isHidden(file)) + if (isHidden(file) || hasParentHidden) { - HiddenFileCount += 1; + _directoryResult.InaccesibleFileCount += 1; } + _directoryResult.FileCount += 1; } - private void ProcessDirectory(DirectoryInfo directory) + private void ProcessDirectory(string directoryPath, bool hasParentHidden) { - DirectoryCount += 1; + DirectoryInfo directory = new DirectoryInfo(directoryPath); + if (isHidden(directory)) { - HiddenDirectoryCount += 1; + _directoryResult.HiddenDirectoryCount += 1; } - if (null != directory.Parent && isHidden(directory.Parent)) + + if (isHidden(directory) || hasParentHidden) { - InaccesibleDirectoryCount += 1; + _directoryResult.InaccesibleDirectoryCount += 1; } + + _directoryResult.DirectoryCount += 1; + Process(directory.FullName, hasParentHidden); + } + + private bool isHidden(string directoryPath) + { + return isHidden(new DirectoryInfo(directoryPath)); } private bool isHidden(FileSystemInfo file) { - return file.Attributes.HasFlag(FileAttributes.Hidden); + //return file.Attributes.HasFlag(FileAttributes.Hidden); + var mask = FileAttributes.Hidden; + return (file.Attributes & mask) != 0; } #endregion } diff --git a/ITI.NetFileProcessor/ITI.NetFileProcessor.csproj b/ITI.NetFileProcessor/ITI.NetFileProcessor.csproj index 172f10d..53bb026 100644 --- a/ITI.NetFileProcessor/ITI.NetFileProcessor.csproj +++ b/ITI.NetFileProcessor/ITI.NetFileProcessor.csproj @@ -41,9 +41,11 @@ + + diff --git a/ITI.NetFileProcessor/SafeFileEnumerator.cs b/ITI.NetFileProcessor/SafeFileEnumerator.cs new file mode 100644 index 0000000..7f145ac --- /dev/null +++ b/ITI.NetFileProcessor/SafeFileEnumerator.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ITI.NetFileProcessor +{ + class SafeFileEnumerator + { + public static IEnumerable EnumerateDirectories(string parentDirectory, string searchPattern = "*", SearchOption searchOpt = SearchOption.TopDirectoryOnly) + { + try + { + var directories = Enumerable.Empty(); + if (searchOpt == SearchOption.AllDirectories) + { + directories = Directory.EnumerateDirectories(parentDirectory) + .SelectMany(x => EnumerateDirectories(x, searchPattern, searchOpt)); + } + return directories.Concat(Directory.EnumerateDirectories(parentDirectory, searchPattern)); + } + catch + { + return Enumerable.Empty(); + } + } + + public static IEnumerable EnumerateFiles(string path, string searchPattern = "*", SearchOption searchOpt = SearchOption.TopDirectoryOnly) + { + try + { + var dirFiles = Enumerable.Empty(); + if (searchOpt == SearchOption.AllDirectories) + { + dirFiles = Directory.EnumerateDirectories(path) + .SelectMany(x => EnumerateFiles(x, searchPattern, searchOpt)); + } + return dirFiles.Concat(Directory.EnumerateFiles(path, searchPattern)); + } + catch + { + return Enumerable.Empty(); + } + } + } +} diff --git a/ITI.NetMorseCode/MorsePlayer.cs b/ITI.NetMorseCode/MorsePlayer.cs index 645b0ff..5286fd4 100644 --- a/ITI.NetMorseCode/MorsePlayer.cs +++ b/ITI.NetMorseCode/MorsePlayer.cs @@ -8,14 +8,14 @@ namespace ITI.NetMorseCode { public class MorsePlayer { - MorseProcessor _translator; + MorseProcessor _processor; string Morse { get; set; } public string Text { get; set; } - public MorsePlayer(MorseProcessor translator) + public MorsePlayer(MorseProcessor processor) { - _translator = translator; + _processor = processor; Text = "sos"; } @@ -24,7 +24,7 @@ public void Play() Morse = MorseProcessor.ConvertToMorseCode(Text); int idx = 0; - foreach (MorsePulsation pulse in _translator.GetPulseFromMorse(Morse)) + foreach (MorsePulsation pulse in _processor.GetPulseFromMorse(Morse)) { if (PulsationType.PAUSE == pulse.Type) { @@ -41,7 +41,6 @@ public void Play() Console.Beep(700, pulse.DurationInMSeconds); ++idx; } - } } } diff --git a/ITI.NetMorseCode/MorseProcessor.cs b/ITI.NetMorseCode/MorseProcessor.cs index 1a1ebef..7b99901 100644 --- a/ITI.NetMorseCode/MorseProcessor.cs +++ b/ITI.NetMorseCode/MorseProcessor.cs @@ -91,7 +91,6 @@ public static string ConvertFromMorseCode(string morse) { { char charVal; reverseMorseTable.TryGetValue(morseVal, out charVal); - if ('\0' != charVal) { textBuilder.Append(charVal); @@ -101,13 +100,13 @@ public static string ConvertFromMorseCode(string morse) { } #endregion - private int pulseDurationInSeconde { get; set; } + private int pulseDurationInMSeconds { get; set; } public MorseProcessor() : this(13) { } public MorseProcessor(int wordPerMinutes) : this(wordPerMinutes, MorseStandard.PARIS) { } public MorseProcessor(int wordPerMinutes, MorseStandard standard) { if (wordPerMinutes <= 0) { throw new ArgumentOutOfRangeException("wordPerMinutes", "Should be superior at 0"); } - pulseDurationInSeconde = 60000 / ((int)standard * wordPerMinutes); + pulseDurationInMSeconds = 60000 / ((int)standard * wordPerMinutes); } public List GetPulseFromText(string text) @@ -131,30 +130,30 @@ public List GetPulseFromMorse(string morse) switch (c) { case '.': - pulsations.Add(new MorsePulsation(PulsationToSecond(1), PulsationType.EMISSION)); + pulsations.Add(new MorsePulsation(PulsationToMSeconds(1), PulsationType.EMISSION)); break; case '-': if (lastChar == '.') { - pulsations.Add(new MorsePulsation(PulsationToSecond(1), PulsationType.PAUSE)); + pulsations.Add(new MorsePulsation(PulsationToMSeconds(1), PulsationType.PAUSE)); } - pulsations.Add(new MorsePulsation(PulsationToSecond(3), PulsationType.EMISSION)); + pulsations.Add(new MorsePulsation(PulsationToMSeconds(3), PulsationType.EMISSION)); break; } lastChar = c; } - pulsations.Add(new MorsePulsation(PulsationToSecond(3), PulsationType.PAUSE)); + pulsations.Add(new MorsePulsation(PulsationToMSeconds(3), PulsationType.PAUSE)); } // TODO Fix - // NXT Should be 7 units at the end of an word, but till we use an ugly thead.sleep in the morse player, the silent beat is too slow - pulsations.Add(new MorsePulsation(PulsationToSecond(1), PulsationType.PAUSE)); + // NXT Should be 7 instead of 1 pulse units at the end of an word, but till we use an ugly thread.sleep in the morse player, the pause beat is too slow + pulsations.Add(new MorsePulsation(PulsationToMSeconds(7), PulsationType.PAUSE)); } return pulsations; } - private int PulsationToSecond(int pCount) + private int PulsationToMSeconds(int pCount) { - return pCount * pulseDurationInSeconde; + return pCount * pulseDurationInMSeconds; } } } diff --git a/ITI.Run/Program.cs b/ITI.Run/Program.cs index 013144a..0c2a4b8 100644 --- a/ITI.Run/Program.cs +++ b/ITI.Run/Program.cs @@ -9,7 +9,6 @@ namespace EatMyShort { - class Program { static ConsoleColor getRandomColor() diff --git a/NetFileProcessor.sln b/NetFileProcessor.sln index 3c28260..8930311 100644 --- a/NetFileProcessor.sln +++ b/NetFileProcessor.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2013 -VisualStudioVersion = 12.0.30110.0 +VisualStudioVersion = 12.0.21005.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ITI.Run", "ITI.Run\ITI.Run.csproj", "{501E8FF1-8093-4E84-BA7A-18BC179DD196}" EndProject @@ -9,6 +9,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ITI.NetFileProcessor", "ITI EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ITI.NetMorseCode", "ITI.NetMorseCode\ITI.NetMorseCode.csproj", "{8AE9337C-BA2A-4D2D-BE1F-AE2210450919}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ITI.NetFileProcessor.Tests", "ITI.NetFileProcessor.Tests\ITI.NetFileProcessor.Tests.csproj", "{33703B7F-21DB-4A01-98F7-99708B2EB3B4}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{B6B58618-47EA-46A8-B359-82BEC806CB79}" + ProjectSection(SolutionItems) = preProject + .nuget\NuGet.Config = .nuget\NuGet.Config + .nuget\NuGet.exe = .nuget\NuGet.exe + .nuget\NuGet.targets = .nuget\NuGet.targets + .nuget\packages.config = .nuget\packages.config + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +37,10 @@ Global {8AE9337C-BA2A-4D2D-BE1F-AE2210450919}.Debug|Any CPU.Build.0 = Debug|Any CPU {8AE9337C-BA2A-4D2D-BE1F-AE2210450919}.Release|Any CPU.ActiveCfg = Release|Any CPU {8AE9337C-BA2A-4D2D-BE1F-AE2210450919}.Release|Any CPU.Build.0 = Release|Any CPU + {33703B7F-21DB-4A01-98F7-99708B2EB3B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {33703B7F-21DB-4A01-98F7-99708B2EB3B4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {33703B7F-21DB-4A01-98F7-99708B2EB3B4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {33703B7F-21DB-4A01-98F7-99708B2EB3B4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/NetFileProcessor.v12.suo b/NetFileProcessor.v12.suo index 0c2b8e2..58c7300 100644 Binary files a/NetFileProcessor.v12.suo and b/NetFileProcessor.v12.suo differ diff --git a/README.md b/README.md index cbc5ced..559d250 100644 --- a/README.md +++ b/README.md @@ -1 +1,6 @@ -# This is my README +# NetFileProcessor with integrated morse code player ! + +Do some basic files system stats and sing the result in morse code. + + +![ScreenShot](http://wiki.guildwars.com/images/thumb/6/60/User_Jette_awesome.svg/2048px-User_Jette_awesome.svg.png)