Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
leandromoh committed Oct 9, 2023
1 parent f84ea21 commit c750830
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 20 deletions.
4 changes: 2 additions & 2 deletions RecordParser/Builders/Reader/VariableLengthReaderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface IVariableLengthReaderBuilder<T>
/// <summary>
/// Creates the reader object using the registered mappings.
/// </summary>
/// <param name="separator">The text (usually a character) that delimits collumns and separate values.</param>
/// <param name="separator">The text (usually a character) that delimits columns and separate values.</param>
/// <param name="cultureInfo">Culture that will be used in the library internal default parsers functions.</param>
/// <param name="factory">Function that generates an instance of <typeparamref name="T"/>.</param>
/// <remarks>
Expand Down Expand Up @@ -86,7 +86,7 @@ public IVariableLengthReaderBuilder<T> DefaultTypeConvert<R>(FuncSpanT<R> ex)
/// <summary>
/// Creates the reader object using the registered mappings.
/// </summary>
/// <param name="separator">The text (usually a character) that delimits collumns and separate values.</param>
/// <param name="separator">The text (usually a character) that delimits columns and separate values.</param>
/// <param name="cultureInfo">Culture that will be used in the library internal default parsers functions.</param>
/// <param name="factory">Function that generates an instance of <typeparamref name="T"/>.</param>
/// <remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IVariableLengthReaderSequentialBuilder<T>
/// <summary>
/// Creates the reader object using the registered mappings.
/// </summary>
/// <param name="separator">The text (usually a character) that delimits collumns and separate values.</param>
/// <param name="separator">The text (usually a character) that delimits columns and separate values.</param>
/// <param name="cultureInfo">Culture that will be used in the library internal default parsers functions.</param>
/// <param name="factory">Function that generates an instance of <typeparamref name="T"/>.</param>
/// <remarks>
Expand Down Expand Up @@ -98,7 +98,7 @@ public IVariableLengthReaderSequentialBuilder<T> DefaultTypeConvert<R>(FuncSpanT
/// <summary>
/// Creates the reader object using the registered mappings.
/// </summary>
/// <param name="separator">The text (usually a character) that delimits collumns and separate values.</param>
/// <param name="separator">The text (usually a character) that delimits columns and separate values.</param>
/// <param name="cultureInfo">Culture that will be used in the library internal default parsers functions.</param>
/// <param name="factory">Function that generates an instance of <typeparamref name="T"/>.</param>
/// <remarks>
Expand Down
4 changes: 2 additions & 2 deletions RecordParser/Builders/Writer/VariableLengthWriterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface IVariableLengthWriterBuilder<T>
/// <summary>
/// Creates the writer object using the registered mappings.
/// </summary>
/// <param name="separator">The text (usually a character) that delimits collumns and separate values.</param>
/// <param name="separator">The text (usually a character) that delimits columns and separate values.</param>
/// <param name="cultureInfo">Culture that will be used in the library internal default parsers functions.</param>
/// <remarks>
/// Culture passed will not be applied in custom parser functions registered by the user (neither for member or type).
Expand Down Expand Up @@ -134,7 +134,7 @@ public IVariableLengthWriterBuilder<T> DefaultTypeConvert<R>(FuncSpanTIntBool<R>
/// <summary>
/// Creates the writer object using the registered mappings.
/// </summary>
/// <param name="separator">The text (usually a character) that delimits collumns and separate values.</param>
/// <param name="separator">The text (usually a character) that delimits columns and separate values.</param>
/// <param name="cultureInfo">Culture that will be used in the library internal default parsers functions.</param>
/// <remarks>
/// Culture passed will not be applied in custom parser functions registered by the user (neither for member or type).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IVariableLengthWriterSequentialBuilder<T>
/// <summary>
/// Creates the writer object using the registered mappings.
/// </summary>
/// <param name="separator">The text (usually a character) that delimits collumns and separate values.</param>
/// <param name="separator">The text (usually a character) that delimits columns and separate values.</param>
/// <param name="cultureInfo">Culture that will be used in the library internal default parsers functions.</param>
/// <remarks>
/// Culture passed will not be applied in custom parser functions registered by the user (neither for member or type).
Expand Down Expand Up @@ -74,7 +74,7 @@ public class VariableLengthWriterSequentialBuilder<T> : IVariableLengthWriterSeq
/// <summary>
/// Creates the writer object using the registered mappings.
/// </summary>
/// <param name="separator">The text (usually a character) that delimits collumns and separate values.</param>
/// <param name="separator">The text (usually a character) that delimits columns and separate values.</param>
/// <param name="cultureInfo">Culture that will be used in the library internal default parsers functions.</param>
/// <remarks>
/// Culture passed will not be applied in custom parser functions registered by the user (neither for member or type).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,30 @@ namespace RecordParser.Extensions.FileReader
{
public class FixedLengthReaderOptions<T>
{
/// <summary>
/// Options to configure parallel processing
/// </summary>
public ParallelOptions ParallelOptions { get; set; }
/// <summary>
/// Parse function which transforms text to object
/// </summary>
public FuncSpanT<T> Parser { get; set; }
}

public static class FixedLengthReaderExtensions
{
private const bool HasHeader = false;


/// <summary>
/// Reads the records (i.e., lines) from a fixed length file then parses each record
/// from text to object.
/// </summary>
/// <typeparam name="T">type of objects read from file</typeparam>
/// <param name="reader">fixed length file</param>
/// <param name="options">options to configure the parsing</param>
/// <returns>
/// Sequence of records.
/// </returns>
public static IEnumerable<T> GetRecords<T>(this TextReader reader, FixedLengthReaderOptions<T> options)
{
var func = () => new RowByLine(reader, Length);
Expand All @@ -28,6 +44,19 @@ public static IEnumerable<T> GetRecords<T>(this TextReader reader, FixedLengthRe
: GetRecordsSequential(parser, func, HasHeader);
}

/// <summary>
/// Reads the records (i.e., lines) from a fixed length file.
/// The records are returned in the order they are in the file
/// </summary>
/// <param name="reader">fixed length file</param>
/// <returns>
/// Sequence of records.
/// </returns>
/// <remarks>
/// The ReadOnlyMemory instances representing the records points to regions of the internal buffer used to read the file.
/// Store ReadOnlyMemory values will not hold record's values since the content of the buffer changes
/// as it goes forward through the file
/// </remarks>
public static IEnumerable<ReadOnlyMemory<char>> GetRecords(this TextReader reader)
{
return GetRecordsSequential((memory, i) => memory, () => new RowByLine(reader, Length), HasHeader);
Expand Down
10 changes: 7 additions & 3 deletions RecordParser/Extensions/FileReader/ReaderCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@ public class ParallelOptions
{
/// <summary>
/// Indicates if the processing should be performed
/// in a parallel instead of sequential.
/// in parallel instead of sequential.
/// Default value is true.
/// </summary>
public bool Enabled { get; set; } = true;

/// <summary>
/// Indicates if the original ordering of records must be maintained.
/// Default value is true.
/// </summary>
public bool EnsureOriginalOrdering { get; set; } = true;

/// <summary>
/// Maximum number of concurrently executing tasks
/// that will be used to process the records.
/// Default value is null.
/// </summary>
public int? MaxDegreeOfParallelism { get; set; }
public int? MaxDegreeOfParallelism { get; set; } = null;

/// <summary>
/// The CancellationToken to associate with the parallel processing.
/// Default value is null.
/// </summary>
public CancellationToken? CancellationToken { get; set; }
public CancellationToken? CancellationToken { get; set; } = null;
}

internal static class ReaderCommon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,36 @@ namespace RecordParser.Extensions.FileReader
{
public class VariableLengthReaderOptions
{
public bool HasHeader { get; set; }
public bool ContainsQuotedFields { get; set; }
/// <summary>
/// Indicates if there is a header record present in the reader's content.
/// If true, the first record (the header) will be skipped.
/// Default value is false, so nothing is skipped by default.
/// </summary>
public bool HasHeader { get; set; } = false;
/// <summary>
/// Indicates if there are any quoted field in the reader's content.
/// Default value is true.
/// </summary>
public bool ContainsQuotedFields { get; set; } = true;
/// <summary>
/// Options to configure parallel processing
/// </summary>
public ParallelOptions ParallelOptions { get; set; }
// TODO create ParallelOptionsSafe
}

public static class VariableLengthReaderExtensions
{
/// <summary>
/// Reads the records from a variable length file then parses each record
/// from text to object
/// </summary>
/// <typeparam name="T">type of objects read from file</typeparam>
/// <param name="stream">variable length file</param>
/// <param name="reader">parse reader</param>
/// <param name="options">options to configure the parsing</param>
/// <returns>
/// Sequence of records from the file
/// </returns>
public static IEnumerable<T> GetRecords<T>(this TextReader stream, IVariableLengthReader<T> reader, VariableLengthReaderOptions options)
{
Func<IFL> func = options.ContainsQuotedFields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,43 @@ namespace RecordParser.Extensions.FileReader

public class VariableLengthReaderRawOptions
{
public bool HasHeader { get; set; }
public bool ContainsQuotedFields { get; set; }
public bool Trim { get; set; }

/// <summary>
/// Indicates if there is a header record present in the reader's content.
/// If true, the first record (the header) will be skipped.
/// Default value is false, so nothing is skipped by default.
/// </summary>
public bool HasHeader { get; set; } = false;

/// <summary>
/// Indicates if there are any quoted field in the reader's content.
/// Default value is true.
/// </summary>
public bool ContainsQuotedFields { get; set; } = true;

/// <summary>
/// Indicates if field's values should be trimmed.
/// Default value is false.
/// </summary>
public bool Trim { get; set; } = false;

/// <summary>
/// Indicates how many columns each record has.
/// </summary>
public int ColumnCount { get; set; }
// TODO change to char

/// <summary>
/// The character that delimits columns and separate values.
/// </summary>
public char Separator { get; set; }

/// <summary>
/// Options to configure parallel processing
/// </summary>
public ParallelOptions ParallelOptions { get; set; }

/// <summary>
/// Factory for string pool instances.
/// </summary>
public Func<StringPool> StringPoolFactory { get; set; }
}

Expand Down Expand Up @@ -62,6 +91,17 @@ private static Get BuildRaw(int collumnCount, bool hasTransform, bool trim)
return final.Compile();
}

/// <summary>
/// Reads the records from a variable length file then parses each record
/// to object by accessing each field's value by index.
/// </summary>
/// <typeparam name="T">type of objects read from file</typeparam>
/// <param name="stream">variable length file</param>
/// <param name="options">options to configure the parsing</param>
/// <param name="reader">parser that receives a function that returns field's value by index</param>
/// <returns>
/// Sequence of records from the file
/// </returns>
public static IEnumerable<T> GetRecordsRaw<T>(this TextReader stream, VariableLengthReaderRawOptions options, Func<Func<int, string>, T> reader)
{
var get = BuildRaw(options.ColumnCount, options.StringPoolFactory != null, options.Trim);
Expand Down
26 changes: 26 additions & 0 deletions RecordParser/Extensions/FileWriter/WriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,43 @@ namespace RecordParser.Extensions.FileWriter
{
using RecordParser.Extensions.FileReader;

/// <summary>
/// Delegate representing object to text convert method.
/// </summary>
/// <typeparam name="T">Instance type</typeparam>
/// <param name="instance">Instance that will be turn into text</param>
/// <param name="destination">Destination buffer</param>
/// <param name="charsWritten">Count of chars written</param>
/// <returns>
/// True if the writting was succeeded, otherwise false.
/// </returns>
public delegate bool TryFormat<T>(T instance, Span<char> destination, out int charsWritten);

public static class WriterExtensions
{
private const int initialPow = 10;

/// <summary>
/// Writes the elements of a sequence into the <paramref name="textWriter"/>.
/// </summary>
/// <typeparam name="T">Type of items in the sequence.</typeparam>
/// <param name="textWriter">The TextWriter where the items will be written into.</param>
/// <param name="items">Sequence of the elements.</param>
/// <param name="tryFormat">Delegate that parses element into text.</param>
public static void Write<T>(this TextWriter textWriter, IEnumerable<T> items, TryFormat<T> tryFormat)
{
Write(textWriter, items, tryFormat, new ParallelOptions());
}


/// <summary>
/// Writes the elements of a sequence into the <paramref name="textWriter"/>.
/// </summary>
/// <typeparam name="T">Type of items in the sequence.</typeparam>
/// <param name="textWriter">The TextWriter where the items will be written into.</param>
/// <param name="items">Sequence of the elements.</param>
/// <param name="tryFormat">Delegate that parses element into text.</param>
/// <param name="options">Options to configure parallel processing.</param>
public static void Write<T>(this TextWriter textWriter, IEnumerable<T> items, TryFormat<T> tryFormat, ParallelOptions options)
{
if (options.Enabled)
Expand Down

0 comments on commit c750830

Please sign in to comment.