Skip to content

Commit

Permalink
Finished FileReader mechanism. Added Open file functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
arthur-here committed Oct 4, 2015
1 parent bf92706 commit e78f565
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@
using System.Text;
using System.Threading.Tasks;

namespace TextEditor
namespace TextEditor.FileManager
{
/// <summary>
/// Provides agorythm to reading ASCII encoded file
/// Provides agorythm to reading ASCII encoded file.
/// </summary>
public class ASCIIFileReader : FileReaderStrategy
{
/// <summary>
/// Initializes a new instance of the <see cref="ASCIIFileReader"/> class.
/// </summary>
/// <param name="fileName">Path to read file.</param>
public ASCIIFileReader(string fileName)
{
this.FileName = fileName;
}

public override string[] ReadFile()
public override string[] Read()
{
List<string> result = new List<string>();
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)
{
Expand Down
41 changes: 41 additions & 0 deletions TextEditor/FileManager/DefaultFileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TextEditor.FileManager
{
/// <summary>
/// Provides algorithm to reading ASCII encoded file.
/// </summary>
public class DefaultFileReader : FileReaderStrategy
{
/// <summary>
/// Initializes a new instance of the <see cref="DefaultFileReader"/> class.
/// </summary>
/// <param name="fileName">Path to read file.</param>
public DefaultFileReader(string fileName)
{
this.FileName = fileName;
}

/// <summary>
/// Reads data from file.
/// </summary>
/// <returns>Array of read strings.</returns>
public override string[] Read()
{
List<string> result = new List<string>();
using (System.IO.StreamReader streamReader = new System.IO.StreamReader(this.FileName))
{
while (!streamReader.EndOfStream)
{
result.Add(streamReader.ReadLine());
}
}

return result.ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace TextEditor
namespace TextEditor.FileManager
{
/// <summary>
/// Represents an abstract strategy class for reading from file
Expand Down Expand Up @@ -35,6 +35,6 @@ public string FileName
/// Reads data from file.
/// </summary>
/// <returns>Array of read strings.</returns>
public abstract string[] ReadFile();
public abstract string[] Read();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TextEditor.FileManager;

namespace TextEditor
namespace TextEditor.FileManager
{
/// <summary>
/// Provides agorythm to reading UTF-8 encoded file
Expand All @@ -16,7 +17,7 @@ public UTF8FileReader(string fileName)
this.FileName = fileName;
}

public override string[] ReadFile()
public override string[] Read()
{
List<string> result = new List<string>();
using (System.IO.StreamReader streamReader = new System.IO.StreamReader(this.FileName, UTF8Encoding.UTF8))
Expand Down
2 changes: 1 addition & 1 deletion TextEditor/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<Button x:Name="ExitButton" Margin="0,11,11,0" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="12" VerticalAlignment="Top" HorizontalAlignment="Right" Width="12" IsManipulationEnabled="True" Style="{DynamicResource IconButtonStyle}" Background="{x:Null}" Click="ExitButton_Click" Grid.Column="1">
<Image Source="pack://siteoforigin:,,,/Resources/exit_icon.png" />
</Button>
<Rectangle Fill="#FF535A6F" Margin="0,63,0,324" Grid.ColumnSpan="2"/>
<Rectangle Fill="#FF535A6F" Margin="0,63,0,0" Grid.ColumnSpan="2" Height="23" VerticalAlignment="Top"/>
<Grid.OpacityMask>
<VisualBrush Visual="{Binding ElementName=mask}"/>
</Grid.OpacityMask>
Expand Down
48 changes: 45 additions & 3 deletions TextEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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)
{
}
Expand All @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions TextEditor/TextEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="FileSaveLoad\ASCIIFileReader.cs" />
<Compile Include="FileSaveLoad\UTF8FileReader.cs">
<Compile Include="FileManager\ASCIIFileReader.cs">
<ExcludeFromStyleCop>True</ExcludeFromStyleCop>
</Compile>
<Compile Include="FileManager\DefaultFileReader.cs" />
<Compile Include="FileManager\UTF8FileReader.cs">
<ExcludeFromStyleCop>True</ExcludeFromStyleCop>
</Compile>
<Page Include="MainWindow.xaml">
Expand All @@ -103,7 +106,7 @@
<SubType>Code</SubType>
<ExcludeFromStyleCop>True</ExcludeFromStyleCop>
</Compile>
<Compile Include="FileSaveLoad\FileReaderStrategy.cs" />
<Compile Include="FileManager\FileReaderStrategy.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
Expand Down

0 comments on commit e78f565

Please sign in to comment.