From 0b13e436af07a14b255bed82254fad1ea8fe19ac Mon Sep 17 00:00:00 2001 From: Matthew Hamilton Date: Fri, 10 Aug 2018 23:51:50 -0500 Subject: [PATCH 1/2] Expose TextReader and TextWriter methods for reading and writing files. --- src/FlatFile.Core/Base/FlatFileEngine.cs | 26 +++++++++++++++++-- src/FlatFile.Core/IFlatFileEngine.cs | 16 ++++++++++++ src/FlatFile.Core/IFlatFileMultiEngine.cs | 8 ++++++ .../DelimetedFileMultiEngine.cs | 11 +++++++- .../FixedLengthFileMultiEngine.cs | 12 ++++----- 5 files changed, 64 insertions(+), 9 deletions(-) diff --git a/src/FlatFile.Core/Base/FlatFileEngine.cs b/src/FlatFile.Core/Base/FlatFileEngine.cs index 22d18a3..a3488b9 100644 --- a/src/FlatFile.Core/Base/FlatFileEngine.cs +++ b/src/FlatFile.Core/Base/FlatFileEngine.cs @@ -57,6 +57,18 @@ protected FlatFileEngine(Func handleEntryReadError = nu public virtual IEnumerable Read(Stream stream) where TEntity : class, new() { var reader = new StreamReader(stream); + return Read(reader); + } + + /// + /// Reads the specified text reader. + /// + /// The type of the t entity. + /// The reader. + /// IEnumerable<TEntity>. + /// Impossible to parse line + public virtual IEnumerable Read(TextReader reader) where TEntity : class, new() + { string line; int lineNumber = 0; @@ -68,7 +80,7 @@ protected FlatFileEngine(Func handleEntryReadError = nu while ((line = reader.ReadLine()) != null) { if (string.IsNullOrEmpty(line) || string.IsNullOrEmpty(line.Trim())) continue; - + bool ignoreEntry = false; var entry = new TEntity(); try @@ -104,7 +116,7 @@ protected FlatFileEngine(Func handleEntryReadError = nu /// Processes the header. /// /// The reader. - protected virtual void ProcessHeader(StreamReader reader) + protected virtual void ProcessHeader(TextReader reader) { reader.ReadLine(); } @@ -147,7 +159,17 @@ protected virtual void WriteEntry(TextWriter writer, int lineNumber, TE public virtual void Write(Stream stream, IEnumerable entries) where TEntity : class, new() { TextWriter writer = new StreamWriter(stream); + Write(writer, entries); + } + /// + /// Writes to the specified text writer. + /// + /// The type of the t entity. + /// The text writer. + /// The entries. + public void Write(TextWriter writer, IEnumerable entries) where TEntity : class, new() + { this.WriteHeader(writer); int lineNumber = 0; diff --git a/src/FlatFile.Core/IFlatFileEngine.cs b/src/FlatFile.Core/IFlatFileEngine.cs index 05a6d0f..b0b8a48 100644 --- a/src/FlatFile.Core/IFlatFileEngine.cs +++ b/src/FlatFile.Core/IFlatFileEngine.cs @@ -16,6 +16,14 @@ public interface IFlatFileEngine /// IEnumerable<TEntity>. IEnumerable Read(Stream stream) where TEntity : class, new(); + /// + /// Reads from the specified text reader. + /// + /// The type of the t entity. + /// The text reader. + /// IEnumerable<TEntity>. + IEnumerable Read(TextReader reader) where TEntity : class, new(); + /// /// Writes to the specified stream. /// @@ -23,5 +31,13 @@ public interface IFlatFileEngine /// The stream. /// The entries. void Write(Stream stream, IEnumerable entries) where TEntity : class, new(); + + /// + /// Writes to the specified text writer. + /// + /// The type of the t entity. + /// The text writer. + /// The entries. + void Write(TextWriter writer, IEnumerable entries) where TEntity : class, new(); } } \ No newline at end of file diff --git a/src/FlatFile.Core/IFlatFileMultiEngine.cs b/src/FlatFile.Core/IFlatFileMultiEngine.cs index a47b36d..c33299d 100644 --- a/src/FlatFile.Core/IFlatFileMultiEngine.cs +++ b/src/FlatFile.Core/IFlatFileMultiEngine.cs @@ -13,12 +13,20 @@ public interface IFlatFileMultiEngine : IFlatFileEngine /// /// The stream. void Read(Stream stream); + + /// + /// Reads the specified text reader. + /// + /// The text reader. + void Read(TextReader reader); + /// /// Gets any records of type read by . /// /// /// IEnumerable<T>. IEnumerable GetRecords() where T : class, new(); + /// /// Gets or sets a value indicating whether this instance has a file header. /// diff --git a/src/FlatFile.Delimited/Implementation/DelimetedFileMultiEngine.cs b/src/FlatFile.Delimited/Implementation/DelimetedFileMultiEngine.cs index 215d974..5145be4 100644 --- a/src/FlatFile.Delimited/Implementation/DelimetedFileMultiEngine.cs +++ b/src/FlatFile.Delimited/Implementation/DelimetedFileMultiEngine.cs @@ -137,7 +137,16 @@ protected override bool TryParseLine(string line, int lineNumber, ref T /// Impossible to parse line public void Read(Stream stream) { - var reader = new StreamReader(stream); + Read(new StreamReader(stream)); + } + + /// + /// Reads the specified text reader. + /// + /// The text reader. + /// Impossible to parse line + public void Read(TextReader reader) + { string line; var lineNumber = 0; diff --git a/src/FlatFile.FixedLength/Implementation/FixedLengthFileMultiEngine.cs b/src/FlatFile.FixedLength/Implementation/FixedLengthFileMultiEngine.cs index 9226d09..9021c69 100644 --- a/src/FlatFile.FixedLength/Implementation/FixedLengthFileMultiEngine.cs +++ b/src/FlatFile.FixedLength/Implementation/FixedLengthFileMultiEngine.cs @@ -142,22 +142,22 @@ public void Read(Stream stream) } /// - /// Reads the specified streamReader. + /// Reads the specified text reader. /// - /// The stream reader configured as the user wants. + /// The text reader configured as the user wants. /// Impossible to parse line - public void Read(StreamReader reader) + public void Read(TextReader reader) { ReadInternal(reader); } /// - /// Internal method (private) to read from streamreader instead of stream + /// Internal method (private) to read from a text reader instead of stream /// This way the client code have a way to specify encoding. /// - /// The stream reader to read. + /// The text reader to read. /// Impossible to parse line - private void ReadInternal(StreamReader reader) + private void ReadInternal(TextReader reader) { string line; var lineNumber = 0; From 8211a14249911e2b8b0925ac83acc461f69c40f4 Mon Sep 17 00:00:00 2001 From: Matthew Hamilton Date: Sat, 11 Aug 2018 00:06:33 -0500 Subject: [PATCH 2/2] Update comments. --- .../Implementation/DelimetedFileMultiEngine.cs | 2 +- .../Implementation/FixedLengthFileMultiEngine.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FlatFile.Delimited/Implementation/DelimetedFileMultiEngine.cs b/src/FlatFile.Delimited/Implementation/DelimetedFileMultiEngine.cs index 5145be4..e5b45db 100644 --- a/src/FlatFile.Delimited/Implementation/DelimetedFileMultiEngine.cs +++ b/src/FlatFile.Delimited/Implementation/DelimetedFileMultiEngine.cs @@ -141,7 +141,7 @@ public void Read(Stream stream) } /// - /// Reads the specified text reader. + /// Reads from the specified text reader. /// /// The text reader. /// Impossible to parse line diff --git a/src/FlatFile.FixedLength/Implementation/FixedLengthFileMultiEngine.cs b/src/FlatFile.FixedLength/Implementation/FixedLengthFileMultiEngine.cs index 9021c69..499bfe5 100644 --- a/src/FlatFile.FixedLength/Implementation/FixedLengthFileMultiEngine.cs +++ b/src/FlatFile.FixedLength/Implementation/FixedLengthFileMultiEngine.cs @@ -142,7 +142,7 @@ public void Read(Stream stream) } /// - /// Reads the specified text reader. + /// Reads from the specified text reader. /// /// The text reader configured as the user wants. /// Impossible to parse line