From e78f5656189fe33a31757d0caa23c853a3ed39dd Mon Sep 17 00:00:00 2001 From: burntheroad Date: Sun, 4 Oct 2015 23:42:13 +0300 Subject: [PATCH] Finished FileReader mechanism. Added Open file functionality --- .../ASCIIFileReader.cs | 12 +++-- TextEditor/FileManager/DefaultFileReader.cs | 41 ++++++++++++++++ .../FileReaderStrategy.cs | 4 +- .../UTF8FileReader.cs | 5 +- TextEditor/MainWindow.xaml | 2 +- TextEditor/MainWindow.xaml.cs | 48 +++++++++++++++++-- TextEditor/TextEditor.csproj | 9 ++-- 7 files changed, 106 insertions(+), 15 deletions(-) rename TextEditor/{FileSaveLoad => FileManager}/ASCIIFileReader.cs (64%) create mode 100644 TextEditor/FileManager/DefaultFileReader.cs rename TextEditor/{FileSaveLoad => FileManager}/FileReaderStrategy.cs (92%) rename TextEditor/{FileSaveLoad => FileManager}/UTF8FileReader.cs (88%) diff --git a/TextEditor/FileSaveLoad/ASCIIFileReader.cs b/TextEditor/FileManager/ASCIIFileReader.cs similarity index 64% rename from TextEditor/FileSaveLoad/ASCIIFileReader.cs rename to TextEditor/FileManager/ASCIIFileReader.cs index 2116aa7..a3cea5c 100644 --- a/TextEditor/FileSaveLoad/ASCIIFileReader.cs +++ b/TextEditor/FileManager/ASCIIFileReader.cs @@ -4,22 +4,26 @@ using System.Text; using System.Threading.Tasks; -namespace TextEditor +namespace TextEditor.FileManager { /// - /// Provides agorythm to reading ASCII encoded file + /// Provides agorythm to reading ASCII encoded file. /// public class ASCIIFileReader : FileReaderStrategy { + /// + /// Initializes a new instance of the class. + /// + /// Path to read file. public ASCIIFileReader(string fileName) { this.FileName = fileName; } - public override string[] ReadFile() + public override string[] Read() { List result = new List(); - using (System.IO.StreamReader streamReader = new System.IO.StreamReader(this.FileName, Encoding.ASCII)) + using (System.IO.StreamReader streamReader = new System.IO.StreamReader(this.FileName, ASCIIEncoding.Default)) { while (!streamReader.EndOfStream) { diff --git a/TextEditor/FileManager/DefaultFileReader.cs b/TextEditor/FileManager/DefaultFileReader.cs new file mode 100644 index 0000000..0e180ac --- /dev/null +++ b/TextEditor/FileManager/DefaultFileReader.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TextEditor.FileManager +{ + /// + /// Provides algorithm to reading ASCII encoded file. + /// + public class DefaultFileReader : FileReaderStrategy + { + /// + /// Initializes a new instance of the class. + /// + /// Path to read file. + public DefaultFileReader(string fileName) + { + this.FileName = fileName; + } + + /// + /// Reads data from file. + /// + /// Array of read strings. + public override string[] Read() + { + List result = new List(); + using (System.IO.StreamReader streamReader = new System.IO.StreamReader(this.FileName)) + { + while (!streamReader.EndOfStream) + { + result.Add(streamReader.ReadLine()); + } + } + + return result.ToArray(); + } + } +} diff --git a/TextEditor/FileSaveLoad/FileReaderStrategy.cs b/TextEditor/FileManager/FileReaderStrategy.cs similarity index 92% rename from TextEditor/FileSaveLoad/FileReaderStrategy.cs rename to TextEditor/FileManager/FileReaderStrategy.cs index 47d2164..884c3d5 100644 --- a/TextEditor/FileSaveLoad/FileReaderStrategy.cs +++ b/TextEditor/FileManager/FileReaderStrategy.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace TextEditor +namespace TextEditor.FileManager { /// /// Represents an abstract strategy class for reading from file @@ -35,6 +35,6 @@ public string FileName /// Reads data from file. /// /// Array of read strings. - public abstract string[] ReadFile(); + public abstract string[] Read(); } } diff --git a/TextEditor/FileSaveLoad/UTF8FileReader.cs b/TextEditor/FileManager/UTF8FileReader.cs similarity index 88% rename from TextEditor/FileSaveLoad/UTF8FileReader.cs rename to TextEditor/FileManager/UTF8FileReader.cs index 8ac5c31..ccd8288 100644 --- a/TextEditor/FileSaveLoad/UTF8FileReader.cs +++ b/TextEditor/FileManager/UTF8FileReader.cs @@ -3,8 +3,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using TextEditor.FileManager; -namespace TextEditor +namespace TextEditor.FileManager { /// /// Provides agorythm to reading UTF-8 encoded file @@ -16,7 +17,7 @@ public UTF8FileReader(string fileName) this.FileName = fileName; } - public override string[] ReadFile() + public override string[] Read() { List result = new List(); using (System.IO.StreamReader streamReader = new System.IO.StreamReader(this.FileName, UTF8Encoding.UTF8)) diff --git a/TextEditor/MainWindow.xaml b/TextEditor/MainWindow.xaml index 7b653db..21b7acf 100644 --- a/TextEditor/MainWindow.xaml +++ b/TextEditor/MainWindow.xaml @@ -49,7 +49,7 @@ - + diff --git a/TextEditor/MainWindow.xaml.cs b/TextEditor/MainWindow.xaml.cs index 78312a9..1ffadb9 100644 --- a/TextEditor/MainWindow.xaml.cs +++ b/TextEditor/MainWindow.xaml.cs @@ -1,6 +1,10 @@ using System; +using System.IO; +using System.Text; using System.Windows; +using System.Windows.Documents; using System.Windows.Input; +using TextEditor.FileManager; namespace TextEditor { @@ -17,6 +21,24 @@ public MainWindow() this.InitializeComponent(); } + private FlowDocument Document + { + get { return this.TextBox.Document; } + } + + private void DisplayText(string[] text, bool clearWindow = true) + { + if (clearWindow) + { + this.Document.Blocks.Clear(); + } + + foreach (string line in text) + { + this.Document.Blocks.Add(new Paragraph(new Run(line))); + } + } + private void NewFileMenuItem_Click(object sender, RoutedEventArgs e) { } @@ -25,11 +47,31 @@ private void OpenFileMenuItem_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); - if (ofd.ShowDialog() == true) + if (ofd.ShowDialog() == false) { - string filename = ofd.FileName; - Console.WriteLine(filename); + return; } + + FileReaderStrategy fileReader; + string filename = ofd.FileName; + using (StreamReader streamReader = new StreamReader(filename)) + { + streamReader.Peek(); + if (streamReader.CurrentEncoding == UTF8Encoding.Default) + { + fileReader = new UTF8FileReader(filename); + } + else if (streamReader.CurrentEncoding == ASCIIEncoding.Default) + { + fileReader = new ASCIIFileReader(filename); + } + else + { + fileReader = new DefaultFileReader(filename); + } + } + + this.DisplayText(fileReader.Read()); } private void SaveFileMenuItem_Click(object sender, RoutedEventArgs e) diff --git a/TextEditor/TextEditor.csproj b/TextEditor/TextEditor.csproj index 0931dc8..9fd8f08 100644 --- a/TextEditor/TextEditor.csproj +++ b/TextEditor/TextEditor.csproj @@ -87,8 +87,11 @@ MSBuild:Compile Designer - - + + True + + + True @@ -103,7 +106,7 @@ Code True - + MainWindow.xaml Code