Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions MorseCode.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9163BD1D-B2B0-4FA6-B81F-37ED66C0E86A}"
ProjectSection(SolutionItems) = preProject
ReadMe.txt = ReadMe.txt
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MorseCodeClass", "MorseCodeClass\MorseCodeClass.csproj", "{E1869FCB-3DB9-4996-A249-FE30111AD3BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MorseCodeDesktop", "MorseCodeDesktop\MorseCodeDesktop.csproj", "{3EA21E6C-4EFF-46E2-BE06-8C2CDB04F5F8}"
ProjectSection(ProjectDependencies) = postProject
{E1869FCB-3DB9-4996-A249-FE30111AD3BE} = {E1869FCB-3DB9-4996-A249-FE30111AD3BE}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E1869FCB-3DB9-4996-A249-FE30111AD3BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E1869FCB-3DB9-4996-A249-FE30111AD3BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E1869FCB-3DB9-4996-A249-FE30111AD3BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E1869FCB-3DB9-4996-A249-FE30111AD3BE}.Release|Any CPU.Build.0 = Release|Any CPU
{3EA21E6C-4EFF-46E2-BE06-8C2CDB04F5F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3EA21E6C-4EFF-46E2-BE06-8C2CDB04F5F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EA21E6C-4EFF-46E2-BE06-8C2CDB04F5F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EA21E6C-4EFF-46E2-BE06-8C2CDB04F5F8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BAD0103D-8F8B-4B47-99F2-7AA8147419B8}
EndGlobalSection
EndGlobal
48 changes: 48 additions & 0 deletions MorseCodeClass/MorseCodeClass.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E1869FCB-3DB9-4996-A249-FE30111AD3BE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MorseCodeClass</RootNamespace>
<AssemblyName>MorseCodeClass</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MorseCodeConverter.cs" />
<Compile Include="MorseCodeFileConverter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
205 changes: 205 additions & 0 deletions MorseCodeClass/MorseCodeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
using System;
using System.Collections.Generic;

namespace MorseCodeClass
{
/// <summary>
/// Provides basic operations to convert to//from Morse Code
/// </summary>
public class MorseCodeConverter
{
/// <summary>
/// Lookup table for text to morse code
/// </summary>
private IDictionary<string, string> MorseCodeTable = new Dictionary<string, string>();

private string sNewLine = "\r\n"; // Default EOL as CR/LF

public MorseCodeConverter()
{
Build_MorseCodeLookupTable();
}

/// <summary>
/// string to use for new line.
/// </summary>
public string NewLine
{
get { return sNewLine; }
set
{
if (sNewLine.Contains(".") || sNewLine.Contains("-") || sNewLine.Contains("|"))
throw new Exception("NewLine cannont contain '.','-' or '|'");
else
sNewLine = value;
}
}

/// <summary>
/// Convert a string in Morse Code to a string to plain text.
/// </summary>
/// <param name="MorseCode">String in Morse Code to convert</param>
/// <returns>Converted Plain text</returns>
public string MorseCode_ToText(string MorseCode)
{
string PlainText = "";

// Whatever newline character(s) are, change to CR only to split into seperate lines.
string[] Lines = MorseCode.Replace(sNewLine,"\r").Split('\r');

for (int iIndex = 0; iIndex< Lines.Length;iIndex++)
{
// Can only use split with charcacters, not strings so change every break notaed by "||" to "|" first
string[] Letters = Lines[iIndex].Replace("||", "|").Split('|');

foreach (string SingleLetter in Letters)
{
string ConvertedMorseCode = "";

if (MorseCodeTable.TryGetValue(SingleLetter, out ConvertedMorseCode))
PlainText += ConvertedMorseCode;
else
PlainText += "?"; // Could not convert this text. Not A-Z or 0-9
}

if (iIndex != Lines.Length-1) PlainText += sNewLine; // Done with line. Add new line
}

return PlainText;
}

/// <summary>
/// Convert a string in plain text to a string of Morse Code.
/// </summary>
/// <param name="Text">String of Plain Textto convert</param>
/// <returns>Converted Morse Coe</returns>
public string Text_ToMorseCode(string Text)
{
string MorseCode = "";

// Whatever newline character(s) are, change to CR only to split into seperate lines.
// If EOL was always single character, would not need to split into lines like this.
string[] Lines = Text.Replace(sNewLine, "\r").Split('\r');

// ForEach would be nice here, but then have to deal with not putting a trailing CR/LF
for (int iLineNum = 0; iLineNum < Lines.Length; iLineNum++)
{
for (int iIndex = 0; iIndex < Lines[iLineNum].Length; iIndex++)
{
MorseCode += TextToMorse(Lines[iLineNum].Substring(iIndex, 1));
if (iIndex != Lines[iLineNum].Length - 1) MorseCode += "||"; // Break between each letter or space.
}

if (iLineNum != Lines.Length - 1) MorseCode += sNewLine; // Done with line. Add new line
}

return MorseCode;
}

/// <summary>
/// Populates a dictionary of Morse Code to use to conver letters/numbers to Morse Code.
/// </summary>
private void Build_MorseCodeLookupTable()
{
MorseCodeTable.Clear();
MorseCodeTable.Add(".-", "A");
MorseCodeTable.Add("-...", "B");
MorseCodeTable.Add("-.-.", "C");
MorseCodeTable.Add("-..", "D");
MorseCodeTable.Add(".","E");
MorseCodeTable.Add("..-.", "F");
MorseCodeTable.Add("--.", "G");
MorseCodeTable.Add("....", "H");
MorseCodeTable.Add("..", "I");
MorseCodeTable.Add(".---", "J");
MorseCodeTable.Add("-.-", "K");
MorseCodeTable.Add(".-..", "L");
MorseCodeTable.Add("--", "M");
MorseCodeTable.Add("-.", "N");
MorseCodeTable.Add("---", "O");
MorseCodeTable.Add(".--.", "P");
MorseCodeTable.Add("--.-", "Q");
MorseCodeTable.Add(".-.", "R");
MorseCodeTable.Add("...", "S");
MorseCodeTable.Add("-", "T");
MorseCodeTable.Add("..-", "U");
MorseCodeTable.Add("...-", "V");
MorseCodeTable.Add(".--", "W");
MorseCodeTable.Add("-..-", "X");
MorseCodeTable.Add("-.--", "Y");
MorseCodeTable.Add("--..", "Z");

MorseCodeTable.Add(".----", "1");
MorseCodeTable.Add("..---", "2");
MorseCodeTable.Add("...--", "3");
MorseCodeTable.Add("....-", "4");
MorseCodeTable.Add(".....", "5");
MorseCodeTable.Add("-....", "6");
MorseCodeTable.Add("--...", "7");
MorseCodeTable.Add("---..", "8");
MorseCodeTable.Add("----.", "9");
MorseCodeTable.Add("-----", "0");

MorseCodeTable.Add("", " "); // Two breaks in a row create this...which is a space between words

}

/// <summary>
/// Converts one single character to the corrosponding morse code
/// </summary>
/// <param name="text">character to convert to Morse Code</param>
/// <returns>Morse Code representation</returns>
private string TextToMorse(string text)
{
if (text.Length != 1) throw new Exception("TextToMorse accepts only a single character!");

switch(text.ToUpper())
{
case "A": return ".-";
case "B": return "-...";
case "C":return "-.-.";
case "D": return "-..";
case "E": return ".";
case "F": return "..-.";
case "G": return "--.";
case "H": return "....";
case "I": return "..";
case "J": return ".---";
case "K": return "-.-";
case "L": return ".-..";
case "M": return "--";
case "N": return "-.";
case "O": return "---";
case "P":return ".--.";
case "Q": return "--.-";
case "R": return ".-.";
case "S": return "...";
case "T": return "-";
case "U": return "..-";
case "V": return "...-";
case "W": return ".--";
case "X": return "-..-";
case "Y": return "-.--";
case "Z": return "--..";

case "1":return ".----";
case "2": return "..---";
case "3": return "...--";
case "4": return "....-";
case "5": return ".....";
case "6": return "-....";
case "7": return "--...";
case "8": return "---..";
case "9":return "----.";
case "0": return "-----";

case " ": return "||";

default: return "?";

};

}

}
}
68 changes: 68 additions & 0 deletions MorseCodeClass/MorseCodeFileConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.IO;
using System.Collections.Generic;

namespace MorseCodeClass
{
/// <summary>
/// Will open and convert a file from either plain text or Morse Code to a string
/// </summary>
public class MorseCodeFileConverter : MorseCodeConverter
{

/// <summary>
/// Open a file, read it and interpret its contents as Morse Code (dots, dashes, breaks)
/// </summary>
/// <param name="FileName">Fully qualified path of file to open</param>
/// <returns>Morse Code file contents converted to plain text</returns>
public string MorseCodeFile_ToText(string FileName)
{


if (!File.Exists(FileName)) throw new Exception("File does not exist:" + FileName);

try
{

using (StreamReader reader = File.OpenText(FileName))
{
string MorseCodeFile = reader.ReadToEnd();

return MorseCode_ToText(MorseCodeFile); // Using will ensure file is closed
}

}

catch (Exception Ex) // Always capture exceptions when doing file I/O. Lots can go wrong.
{
// Capture filename we had an issue with and throw to client
throw new Exception("Could not read file: " + FileName, Ex);
}

}

public string TextFile_ToMorseCode(String FileName)
{

if (!File.Exists(FileName)) throw new Exception("File does not exist:" + FileName);

try
{

using (StreamReader reader = File.OpenText(FileName))
{
string PlainTextFile = reader.ReadToEnd();

return Text_ToMorseCode (PlainTextFile); // Using will ensure file is closed
}

}

catch (Exception Ex) // Always capture exceptions when doing file I/O. Lots can go wrong.
{
// Capture filename we had an issue with and throw to client
throw new Exception("Could not read file: " + FileName, Ex);
}
}
}
}
Loading