From c7aef5ff22d90eb772e24bd1db50a3a903725975 Mon Sep 17 00:00:00 2001 From: WDShaffer <31552614+WDShaffer@users.noreply.github.com> Date: Wed, 6 Sep 2017 07:26:35 -0400 Subject: [PATCH] Add files via upload This is my solution to the Softwriters Morse Code challenge --- MorseCode.sln | 39 ++++ MorseCodeClass/MorseCodeClass.csproj | 48 ++++ MorseCodeClass/MorseCodeConverter.cs | 205 ++++++++++++++++++ MorseCodeClass/MorseCodeFileConverter.cs | 68 ++++++ MorseCodeClass/Properties/AssemblyInfo.cs | 36 +++ MorseCodeDesktop/App.config | 6 + MorseCodeDesktop/FrmMain.Designer.cs | 145 +++++++++++++ MorseCodeDesktop/FrmMain.cs | 161 ++++++++++++++ MorseCodeDesktop/FrmMain.resx | 132 +++++++++++ MorseCodeDesktop/MorseCodeDesktop.csproj | 88 ++++++++ MorseCodeDesktop/MorseCodeDesktop.csproj.user | 7 + MorseCodeDesktop/Program.cs | 22 ++ MorseCodeDesktop/Properties/AssemblyInfo.cs | 36 +++ .../Properties/Resources.Designer.cs | 63 ++++++ MorseCodeDesktop/Properties/Resources.resx | 117 ++++++++++ .../Properties/Settings.Designer.cs | 26 +++ MorseCodeDesktop/Properties/Settings.settings | 7 + ReadMe.txt | 23 ++ 18 files changed, 1229 insertions(+) create mode 100644 MorseCode.sln create mode 100644 MorseCodeClass/MorseCodeClass.csproj create mode 100644 MorseCodeClass/MorseCodeConverter.cs create mode 100644 MorseCodeClass/MorseCodeFileConverter.cs create mode 100644 MorseCodeClass/Properties/AssemblyInfo.cs create mode 100644 MorseCodeDesktop/App.config create mode 100644 MorseCodeDesktop/FrmMain.Designer.cs create mode 100644 MorseCodeDesktop/FrmMain.cs create mode 100644 MorseCodeDesktop/FrmMain.resx create mode 100644 MorseCodeDesktop/MorseCodeDesktop.csproj create mode 100644 MorseCodeDesktop/MorseCodeDesktop.csproj.user create mode 100644 MorseCodeDesktop/Program.cs create mode 100644 MorseCodeDesktop/Properties/AssemblyInfo.cs create mode 100644 MorseCodeDesktop/Properties/Resources.Designer.cs create mode 100644 MorseCodeDesktop/Properties/Resources.resx create mode 100644 MorseCodeDesktop/Properties/Settings.Designer.cs create mode 100644 MorseCodeDesktop/Properties/Settings.settings create mode 100644 ReadMe.txt diff --git a/MorseCode.sln b/MorseCode.sln new file mode 100644 index 0000000..3f5bf4d --- /dev/null +++ b/MorseCode.sln @@ -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 diff --git a/MorseCodeClass/MorseCodeClass.csproj b/MorseCodeClass/MorseCodeClass.csproj new file mode 100644 index 0000000..792d132 --- /dev/null +++ b/MorseCodeClass/MorseCodeClass.csproj @@ -0,0 +1,48 @@ + + + + + Debug + AnyCPU + {E1869FCB-3DB9-4996-A249-FE30111AD3BE} + Library + Properties + MorseCodeClass + MorseCodeClass + v4.6.1 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MorseCodeClass/MorseCodeConverter.cs b/MorseCodeClass/MorseCodeConverter.cs new file mode 100644 index 0000000..dee7f2e --- /dev/null +++ b/MorseCodeClass/MorseCodeConverter.cs @@ -0,0 +1,205 @@ +using System; +using System.Collections.Generic; + +namespace MorseCodeClass +{ + /// + /// Provides basic operations to convert to//from Morse Code + /// + public class MorseCodeConverter + { + /// + /// Lookup table for text to morse code + /// + private IDictionary MorseCodeTable = new Dictionary(); + + private string sNewLine = "\r\n"; // Default EOL as CR/LF + + public MorseCodeConverter() + { + Build_MorseCodeLookupTable(); + } + + /// + /// string to use for new line. + /// + public string NewLine + { + get { return sNewLine; } + set + { + if (sNewLine.Contains(".") || sNewLine.Contains("-") || sNewLine.Contains("|")) + throw new Exception("NewLine cannont contain '.','-' or '|'"); + else + sNewLine = value; + } + } + + /// + /// Convert a string in Morse Code to a string to plain text. + /// + /// String in Morse Code to convert + /// Converted Plain text + 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; + } + + /// + /// Convert a string in plain text to a string of Morse Code. + /// + /// String of Plain Textto convert + /// Converted Morse Coe + 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; + } + + /// + /// Populates a dictionary of Morse Code to use to conver letters/numbers to Morse Code. + /// + 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 + + } + + /// + /// Converts one single character to the corrosponding morse code + /// + /// character to convert to Morse Code + /// Morse Code representation + 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 "?"; + + }; + + } + + } +} diff --git a/MorseCodeClass/MorseCodeFileConverter.cs b/MorseCodeClass/MorseCodeFileConverter.cs new file mode 100644 index 0000000..6a5035b --- /dev/null +++ b/MorseCodeClass/MorseCodeFileConverter.cs @@ -0,0 +1,68 @@ +using System; +using System.IO; +using System.Collections.Generic; + +namespace MorseCodeClass +{ + /// + /// Will open and convert a file from either plain text or Morse Code to a string + /// + public class MorseCodeFileConverter : MorseCodeConverter + { + + /// + /// Open a file, read it and interpret its contents as Morse Code (dots, dashes, breaks) + /// + /// Fully qualified path of file to open + /// Morse Code file contents converted to plain text + 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); + } + } + } +} diff --git a/MorseCodeClass/Properties/AssemblyInfo.cs b/MorseCodeClass/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..96e929d --- /dev/null +++ b/MorseCodeClass/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("MorseCodeClass")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("MorseCodeClass")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[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("e1869fcb-3db9-4996-a249-fe30111ad3be")] + +// 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/MorseCodeDesktop/App.config b/MorseCodeDesktop/App.config new file mode 100644 index 0000000..4543795 --- /dev/null +++ b/MorseCodeDesktop/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/MorseCodeDesktop/FrmMain.Designer.cs b/MorseCodeDesktop/FrmMain.Designer.cs new file mode 100644 index 0000000..58bc44c --- /dev/null +++ b/MorseCodeDesktop/FrmMain.Designer.cs @@ -0,0 +1,145 @@ +namespace MorseCodeDesktop +{ + partial class MainForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.FileText = new System.Windows.Forms.TextBox(); + this.MyMenu = new System.Windows.Forms.MenuStrip(); + this.FileMenu = new System.Windows.Forms.ToolStripMenuItem(); + this.openPlainTextToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openMorseCodeFileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveFileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.MyMenu.SuspendLayout(); + this.SuspendLayout(); + // + // FileText + // + this.FileText.Location = new System.Drawing.Point(12, 27); + this.FileText.Multiline = true; + this.FileText.Name = "FileText"; + this.FileText.Size = new System.Drawing.Size(521, 274); + this.FileText.TabIndex = 0; + this.FileText.TextChanged += new System.EventHandler(this.FileText_TextChanged); + // + // MyMenu + // + this.MyMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.FileMenu}); + this.MyMenu.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.HorizontalStackWithOverflow; + this.MyMenu.Location = new System.Drawing.Point(0, 0); + this.MyMenu.Name = "MyMenu"; + this.MyMenu.Size = new System.Drawing.Size(545, 24); + this.MyMenu.TabIndex = 1; + // + // FileMenu + // + this.FileMenu.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.openPlainTextToolStripMenuItem, + this.openMorseCodeFileToolStripMenuItem, + this.saveFileToolStripMenuItem, + this.exitToolStripMenuItem}); + this.FileMenu.Name = "FileMenu"; + this.FileMenu.Size = new System.Drawing.Size(37, 20); + this.FileMenu.Text = "&File"; + // + // openPlainTextToolStripMenuItem + // + this.openPlainTextToolStripMenuItem.Name = "openPlainTextToolStripMenuItem"; + this.openPlainTextToolStripMenuItem.Size = new System.Drawing.Size(191, 22); + this.openPlainTextToolStripMenuItem.Text = "Open &Plain Text File"; + this.openPlainTextToolStripMenuItem.Click += new System.EventHandler(this.openPlainTextToolStripMenuItem_Click); + // + // openMorseCodeFileToolStripMenuItem + // + this.openMorseCodeFileToolStripMenuItem.Name = "openMorseCodeFileToolStripMenuItem"; + this.openMorseCodeFileToolStripMenuItem.Size = new System.Drawing.Size(191, 22); + this.openMorseCodeFileToolStripMenuItem.Text = "Open &Morse Code File"; + this.openMorseCodeFileToolStripMenuItem.Click += new System.EventHandler(this.openMorseCodeFileToolStripMenuItem_Click); + // + // saveFileToolStripMenuItem + // + this.saveFileToolStripMenuItem.Name = "saveFileToolStripMenuItem"; + this.saveFileToolStripMenuItem.Size = new System.Drawing.Size(191, 22); + this.saveFileToolStripMenuItem.Text = "&Save File"; + this.saveFileToolStripMenuItem.Click += new System.EventHandler(this.saveFileToolStripMenuItem_Click); + // + // exitToolStripMenuItem + // + this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; + this.exitToolStripMenuItem.Size = new System.Drawing.Size(191, 22); + this.exitToolStripMenuItem.Text = "E&xit"; + this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); + // + // openFileDialog + // + this.openFileDialog.DefaultExt = "txt"; + this.openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; + this.openFileDialog.FilterIndex = 0; + // + // saveFileDialog + // + this.saveFileDialog.DefaultExt = "txt"; + this.saveFileDialog.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*"; + this.saveFileDialog.Title = "Save File"; + // + // MainForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(545, 313); + this.Controls.Add(this.FileText); + this.Controls.Add(this.MyMenu); + this.MainMenuStrip = this.MyMenu; + this.Name = "MainForm"; + this.Text = "Morse Code Converter"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); + this.ResizeEnd += new System.EventHandler(this.MainForm_ResizeEnd); + this.MyMenu.ResumeLayout(false); + this.MyMenu.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox FileText; + private System.Windows.Forms.MenuStrip MyMenu; + private System.Windows.Forms.ToolStripMenuItem FileMenu; + private System.Windows.Forms.ToolStripMenuItem openPlainTextToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; + private System.Windows.Forms.OpenFileDialog openFileDialog; + private System.Windows.Forms.ToolStripMenuItem openMorseCodeFileToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem saveFileToolStripMenuItem; + private System.Windows.Forms.SaveFileDialog saveFileDialog; + } +} + diff --git a/MorseCodeDesktop/FrmMain.cs b/MorseCodeDesktop/FrmMain.cs new file mode 100644 index 0000000..42468c7 --- /dev/null +++ b/MorseCodeDesktop/FrmMain.cs @@ -0,0 +1,161 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Windows.Forms; + +namespace MorseCodeDesktop +{ + public partial class MainForm : Form + { + public MainForm() + { + InitializeComponent(); + } + + private MorseCodeClass.MorseCodeFileConverter MyMorseCode = new MorseCodeClass.MorseCodeFileConverter(); + + /// + /// Indicates if a converted file has been saved. + /// + private bool ChangesSaved = true; + + private void exitToolStripMenuItem_Click(object sender, EventArgs e) + { + if (CheckExit()) Application.Exit(); + } + + private void openMorseCodeFileToolStripMenuItem_Click(object sender, EventArgs e) + { + // Could check "ChangesSaved" in case user meant to save file, but didn't. Easy to re-convert, so won't check here. + // User can also hit "cancel" on File Dialog. + + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + string FileName = openFileDialog.FileName; + + try + { + string PlainText = MyMorseCode.MorseCodeFile_ToText(FileName); + FileText.Text = PlainText; + + if (PlainText.Contains("?")) MessageBox.Show("File contains invalid Morse Code sequences!\r\nInvalid sequences shown as \"?\"",Application.ProductName ); + } + + catch (Exception Ex) + { + GenericExceptionHandler(Ex); + } + } + } + + private void openPlainTextToolStripMenuItem_Click(object sender, EventArgs e) + { + // Could check "ChangesSaved" in case user meant to save file, but didn't. Easy to re-convert, so won't check here. + // User can also hit "cancel" on File Dialog. + + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + string FileName = openFileDialog.FileName; + + try + { + string MorseCode = MyMorseCode.TextFile_ToMorseCode(FileName); + + FileText.Text = MorseCode; + + if (MorseCode.Contains("?")) + { + MessageBox.Show("File contains characters that can't be converted to Morse Code!\r\nInvalid characters shown as \"?\"", Application.ProductName); + } + + } + + catch (Exception Ex) + { + GenericExceptionHandler(Ex); + } + + } + } + + private void MainForm_ResizeEnd(object sender, EventArgs e) + { + if (this.Height > 75) FileText.Height = this.Height - 75; + if (this.Width > 40) FileText.Width = this.Width - 40; + } + + private void saveFileToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + try + { + SaveResults(saveFileDialog.FileName); + + ChangesSaved = true; + + MessageBox.Show("File Saved!"); + } + catch (Exception Ex) + { + GenericExceptionHandler(Ex); + } + } + } + + /// + /// Save converted file results. Can be plain text or Morse Code + /// + /// Full path to save file as + private void SaveResults(string FileName) + { + using (TextWriter SaveFile = new StreamWriter(FileName)) + { + SaveFile.Write(FileText.Text); + SaveFile.Close(); + } + } + + /// + /// Common Exception handler + /// + /// Exception Object + private void GenericExceptionHandler(Exception Ex) + { + MessageBox.Show(Ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + + private void MainForm_FormClosing(object sender, FormClosingEventArgs e) + { + e.Cancel = !CheckExit(); + } + + /// + /// Checks if converted file (if any) has been saved. If not, prompt user. + /// + /// Returns true if OK to exit. Either because changes saved or user says its OK + private bool CheckExit() + { + if (!ChangesSaved) + { + if (MessageBox.Show("Converted File has not been saved. Exit anyway?" + , Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + ChangesSaved = true; // Don't know if we're being called by Form_Closing or by menu option + return true; + } + + else + return false; + } + else + return true; + } + + private void FileText_TextChanged(object sender, EventArgs e) + { + ChangesSaved = false; // Indicate some change in contents in case of exiting. + } + + } +} \ No newline at end of file diff --git a/MorseCodeDesktop/FrmMain.resx b/MorseCodeDesktop/FrmMain.resx new file mode 100644 index 0000000..7b9f331 --- /dev/null +++ b/MorseCodeDesktop/FrmMain.resx @@ -0,0 +1,132 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + + 100, 17 + + + 233, 17 + + + True + + \ No newline at end of file diff --git a/MorseCodeDesktop/MorseCodeDesktop.csproj b/MorseCodeDesktop/MorseCodeDesktop.csproj new file mode 100644 index 0000000..e81cbea --- /dev/null +++ b/MorseCodeDesktop/MorseCodeDesktop.csproj @@ -0,0 +1,88 @@ + + + + + Debug + AnyCPU + {3EA21E6C-4EFF-46E2-BE06-8C2CDB04F5F8} + WinExe + Morce_Code_Deskop + Morse Code Desktop + v4.6.1 + 512 + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\MorseCodeClass\bin\Debug\MorseCodeClass.dll + True + + + + + + + + + + + + + + + + Form + + + FrmMain.cs + + + + + FrmMain.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + True + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + \ No newline at end of file diff --git a/MorseCodeDesktop/MorseCodeDesktop.csproj.user b/MorseCodeDesktop/MorseCodeDesktop.csproj.user new file mode 100644 index 0000000..0c039e3 --- /dev/null +++ b/MorseCodeDesktop/MorseCodeDesktop.csproj.user @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/MorseCodeDesktop/Program.cs b/MorseCodeDesktop/Program.cs new file mode 100644 index 0000000..ed6be44 --- /dev/null +++ b/MorseCodeDesktop/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace MorseCodeDesktop +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new MainForm()); + } + } +} diff --git a/MorseCodeDesktop/Properties/AssemblyInfo.cs b/MorseCodeDesktop/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..eb6f004 --- /dev/null +++ b/MorseCodeDesktop/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("Morse Code Desktop")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Morse Code Desktop")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[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("3ea21e6c-4eff-46e2-be06-8c2cdb04f5f8")] + +// 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/MorseCodeDesktop/Properties/Resources.Designer.cs b/MorseCodeDesktop/Properties/Resources.Designer.cs new file mode 100644 index 0000000..85c8eca --- /dev/null +++ b/MorseCodeDesktop/Properties/Resources.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Morce_Code_Deskop.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Morce_Code_Deskop.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/MorseCodeDesktop/Properties/Resources.resx b/MorseCodeDesktop/Properties/Resources.resx new file mode 100644 index 0000000..ffecec8 --- /dev/null +++ b/MorseCodeDesktop/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/MorseCodeDesktop/Properties/Settings.Designer.cs b/MorseCodeDesktop/Properties/Settings.Designer.cs new file mode 100644 index 0000000..4d27548 --- /dev/null +++ b/MorseCodeDesktop/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Morce_Code_Deskop.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.3.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/MorseCodeDesktop/Properties/Settings.settings b/MorseCodeDesktop/Properties/Settings.settings new file mode 100644 index 0000000..abf36c5 --- /dev/null +++ b/MorseCodeDesktop/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/ReadMe.txt b/ReadMe.txt new file mode 100644 index 0000000..fbeba2e --- /dev/null +++ b/ReadMe.txt @@ -0,0 +1,23 @@ +This is my solution to the Softwriters Morse Code challenge. + +I found everything about this code challenge to be well within my wheel house. +I decided to implment in two parts. One as a library assembly that could used independantly of this particular solution. +The other, a "user interface" that would consume this assembly and allow the user to open and convert Morse Code files + +Originally, I planned to make the library in C# and the UI in Visual Basic. However, I don't have Visual Basic +installed on my machine and I think Softwriters typically writes in C#. So I proceeded to write the UI in C#. + +In keeping with the spirit of a solution that was part of a larger whole, I created a class that can convert from +plain text to morse code as well as morse code to text. Having both functions was useful to expitie testing. + +I tested the application using the following cases: + +Example provided in the code challenge + +"The quick brown dog jumped over the lazy dog's back." + +"Now is the time for all good men to come to the aid of their country." + +"abcdefghijklmnopqrstuvwxyz 0123456789" + +Thank you for reviewing this solution. I hope to hear from you soon. \ No newline at end of file