Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
RNoeldner committed Jun 2, 2024
1 parent 1c15aa5 commit f77c657
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Library/ClassLibraryCSV/ErrorInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static class ErrorInformation

private const char cOpenField = '[';
private const char cClosingField = ']';
private const char cAlternateColumnMessageSeparator = ':';
// private const char cAlternateColumnMessageSeparator = ':';

/// <summary>
/// Char to separate two column names
Expand Down
16 changes: 5 additions & 11 deletions Library/ClassLibraryCSV/FileReader/CsvFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class CsvFileReader : BaseFileReader
private const char cLf = (char) 0x0a;

/// <summary>
/// A non-breaking space..
/// A non-breaking space.
/// </summary>
private const char cNbsp = (char) 0xA0;

Expand Down Expand Up @@ -396,12 +396,6 @@ public override void Close()
m_NumWarningsNbspChar = 0;
}

/// <inheritdoc cref="IFileReader" />
public new void Dispose()
{
Dispose(true);
}

/// <inheritdoc cref="IFileReader" />
public new long GetBytes(int i, long fieldOffset, byte[]? buffer, int bufferOffset, int length)
{
Expand Down Expand Up @@ -767,7 +761,7 @@ private bool GetNextRecord()
string.IsNullOrEmpty(CurrentRowColumnText[columnNo]))
continue;

// Handle replacements and warnings etc,
// Handle replacements and warnings etc.
var adjustedValue = HandleTextSpecials(
CurrentRowColumnText[columnNo]
.ReplaceCaseInsensitive(m_NewLinePlaceholder, '\n')
Expand Down Expand Up @@ -1011,7 +1005,7 @@ private char ReadChar()
if (!postData)
{
// This is not 100% correct in case we have a misalignment of column that is corrected
// afterwards warning for NBP need to be issues before trimming as trimming would
// afterward warning for NBP need to be issues before trimming as trimming would
// remove the char
if (m_WarnNbsp && columnNo < FieldCount && !GetColumn(columnNo).Ignore &&
(m_NumWarning < 1 || m_NumWarningsNbspChar++ < m_NumWarning))
Expand Down Expand Up @@ -1080,7 +1074,7 @@ private char ReadChar()
{
// Store the white spaces if we do any kind of trimming
if (m_TrimmingOption == TrimmingOptionEnum.None)
// Values will be trimmed later but we need to find out, if the filed is quoted first
// Values will be trimmed later, but we need to find out, if the filed is quoted first
stringBuilder.Append(character);
continue;
}
Expand Down Expand Up @@ -1124,7 +1118,7 @@ private char ReadChar()
continue;
}

// a single " should be regarded as closing when its followed by the delimiter
// a single " should be regarded as closing when it's followed by the delimiter
if (m_ContextSensitiveQualifier &&
(peekNextChar == m_FieldDelimiter || peekNextChar == cCr || peekNextChar == cLf))
{
Expand Down
35 changes: 16 additions & 19 deletions Library/ClassLibraryCSV/FileReader/DataReaderWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public class DataReaderWrapper : DbDataReader, IFileReader
private readonly IFileReader? m_FileReader;
private readonly ReaderMapping m_ReaderMapping;
private readonly long m_RecordLimit;
private long m_NumberRowError;
private long m_NumberRowWarnings;
private string m_RowErrorInformation;

/// <summary>
/// Constructor for a DataReaderWrapper this wrapper adds artificial fields like Error,
Expand All @@ -64,7 +61,7 @@ public DataReaderWrapper(in IDataReader reader,
m_FileReader = reader as IFileReader;
if (reader.IsClosed)
throw new InvalidOperationException("Reader can not be closed");
m_RowErrorInformation = string.Empty;
RowErrorInformation = string.Empty;
m_ColumnErrorDictionary = new ColumnErrorDictionary(m_FileReader);
m_RecordLimit = recordLimit < 1 ? long.MaxValue : recordLimit;
var sourceColumns = new List<Column>();
Expand Down Expand Up @@ -118,12 +115,12 @@ public DataReaderWrapper(in IDataReader reader,
/// <summary>
/// Get the number of rows with errors (at least one row is missing)
/// </summary>
public long NumberRowError => m_NumberRowError;
public long NumberRowError { get; private set; }

/// <summary>
/// Get the number of rows with issues
/// </summary>
public long NumberRowWarnings => m_NumberRowWarnings;
public long NumberRowWarnings { get; private set; }

/// <inheritdoc />
public Func<Task>? OnOpenAsync { get; set; }
Expand Down Expand Up @@ -153,7 +150,7 @@ public IProgress<ProgressInfo> ReportProgress
/// <summary>
/// Gets the error information for the row, this could be filled by an error column or by a reader raising warnings
/// </summary>
public string RowErrorInformation => m_RowErrorInformation;
public string RowErrorInformation { get; private set; }

/// <inheritdoc />
public long StartLineNumber => m_FileReader?.StartLineNumber ?? RecordNumber;
Expand Down Expand Up @@ -291,7 +288,7 @@ public override DataTable GetSchemaTable()
// ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
public override string GetString(int ordinal)
=> (ordinal == m_ReaderMapping.ColNumErrorField)
? m_RowErrorInformation
? RowErrorInformation
: DataReader.GetString(m_ReaderMapping.ResultToSource(ordinal));

/// <inheritdoc />
Expand All @@ -304,7 +301,7 @@ public override object GetValue(int ordinal)
if (ordinal == m_ReaderMapping.ColNumRecNum)
return RecordNumber;
return ordinal == m_ReaderMapping.ColNumErrorField
? m_RowErrorInformation
? RowErrorInformation
: DataReader.GetValue(m_ReaderMapping.ResultToSource(ordinal));
}

Expand All @@ -329,7 +326,7 @@ public override bool IsDBNull(int ordinal)
ordinal == m_ReaderMapping.ColNumRecNum)
return false;
if (ordinal == m_ReaderMapping.ColNumErrorField)
return m_RowErrorInformation.Length == 0;
return RowErrorInformation.Length == 0;
return DataReader.IsDBNull(m_ReaderMapping.ResultToSource(ordinal));
}

Expand Down Expand Up @@ -389,9 +386,9 @@ public virtual void ResetPositionToFirstDataRow()
{
m_FileReader?.ResetPositionToFirstDataRow();
m_ColumnErrorDictionary.Clear();
m_RowErrorInformation = string.Empty;
RowErrorInformation = string.Empty;
RecordNumber = 0;
m_NumberRowWarnings = 0;
NumberRowWarnings = 0;
}

/// <summary>
Expand All @@ -402,26 +399,26 @@ private void FinishRead()
RecordNumber++;

if (m_ReaderMapping.ColNumErrorFieldSource != -1)
m_RowErrorInformation = DataReader.IsDBNull(m_ReaderMapping.ColNumErrorFieldSource)
RowErrorInformation = DataReader.IsDBNull(m_ReaderMapping.ColNumErrorFieldSource)
? string.Empty
: DataReader.GetValue(m_ReaderMapping.ColNumErrorFieldSource).ToString() ?? string.Empty;
else
{
m_RowErrorInformation = ErrorInformation.ReadErrorInformation(m_ColumnErrorDictionary,
RowErrorInformation = ErrorInformation.ReadErrorInformation(m_ColumnErrorDictionary,
i => i >= 0 ? m_ReaderMapping.ResultingColumns[i].Name : string.Empty);
m_ColumnErrorDictionary.Clear();
}

if (string.IsNullOrEmpty(m_RowErrorInformation))
if (string.IsNullOrEmpty(RowErrorInformation))
return;

if (m_RowErrorInformation.IsWarningMessage())
m_NumberRowWarnings++;
if (RowErrorInformation.IsWarningMessage())
NumberRowWarnings++;
else
m_NumberRowError++;
NumberRowError++;

Warning?.Invoke(this,
new WarningEventArgs(RecordNumber, 0, m_RowErrorInformation, StartLineNumber, EndLineNumber, string.Empty));
new WarningEventArgs(RecordNumber, 0, RowErrorInformation, StartLineNumber, EndLineNumber, string.Empty));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Library/ClassLibraryCSV/FileReader/IFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public interface IFileReader : IDataReader
new bool NextResult();

/// <summary>
/// Opens the text file and begins to read the meta data, like columns
/// Opens the text file and begins to read the metadata, like columns
/// </summary>
/// <param name="token">The cancellation token.</param>
/// <returns>Number of records in the file if known (use determineColumnSize), -1 otherwise</returns>
Expand Down
4 changes: 0 additions & 4 deletions Library/ClassLibraryCSV/FileReader/JsonFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -118,9 +117,6 @@ public override void Close()
m_Stream = null;
}

/// <inheritdoc cref="DbDataReader" />
public new void Dispose() => Dispose(true);


#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
/// <inheritdoc cref="DbDataReader" />
Expand Down
2 changes: 1 addition & 1 deletion Library/ClassLibraryCSV/FilterDataTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public FilterDataTable(in DataTable init)
/// <summary>
/// The type of Filter
/// </summary>
public FilterTypeEnum FilterType { get; private set; } = FilterTypeEnum.All;
private FilterTypeEnum FilterType { get; set; } = FilterTypeEnum.All;

/// <summary>
/// Sets the name of the unique field.
Expand Down
2 changes: 1 addition & 1 deletion Library/ClassLibraryCSV/Punctuation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static string Description(this char character)
'.' => "Dot .",
'|' => "Pipe |",
'"' => "Quotation marks \"",
'\'' => "Apostrophe \'",
'\'' => "Apostrophe '",
'&' => "Ampersand &",
'*' => "Asterisk *",
'`' => "Tick Mark `",
Expand Down

0 comments on commit f77c657

Please sign in to comment.