Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CsvReader.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30621.155
# Visual Studio Version 18
VisualStudioVersion = 18.3.11520.95
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D23ED3F3-7347-4369-B8AA-4E622924D01E}"
ProjectSection(SolutionItems) = preProject
build.fsx = build.fsx
License.md = License.md
nuget\LumenWorksCsvReader2.nuspec = nuget\LumenWorksCsvReader2.nuspec
packet.dependencies = packet.dependencies
README.md = README.md
RELEASE_NOTES.md = RELEASE_NOTES.md
EndProjectSection
Expand Down
5 changes: 5 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#### 4.5.0 (2026-03-01)
* Removed support of .NET 6.0 and .NET 7.0
* Added support of .NET 9.0 and 10.0
* Exception thrown with duplicated colum names

#### 4.4.0 (2024-09-12)
* Added MapDataToDto<T> method to map CSV file to an IEnumerable<T> where T is a type of an entity/DTO
* Removed support of .NET Framework 4.7.2, .NET Core 3.1 and .NET 5.0. Added support of .NET 7.0 and 8.0
Expand Down
18 changes: 17 additions & 1 deletion code/CsvReader.Benchmarks/CsvReader.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net48;net6.0-windows;net7.0-windows;net8.0-windows</TargetFrameworks>
<TargetFrameworks>net48;net8.0-windows;net9.0-windows;net10.0-windows</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<OutputPath>$(SolutionDir)build\CsvReader.Benchmarks\$(Configuration)</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net9.0-windows|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net9.0-windows|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net10.0-windows|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net10.0-windows|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\SolutionInfo.cs" Link="SolutionInfo.cs" />
</ItemGroup>
Expand Down
18 changes: 17 additions & 1 deletion code/CsvReader.UnitTests/CsvReader.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48;net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net48;net8.0;net9.0;net10.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<OutputPath>$(SolutionDir)build\CsvReader.UnitTests\$(Configuration)</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net9.0|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net9.0|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net10.0|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net10.0|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\SolutionInfo.cs" Link="SolutionInfo.cs" />
</ItemGroup>
Expand Down
34 changes: 34 additions & 0 deletions code/CsvReader.UnitTests/CsvReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,40 @@ public void HasHeader_NullHeader()
}
}

[Test]
public void HasHeader_DuplicateHeader()
{
try
{
using (CsvReader csvReader = new CsvReader(new StringReader("Header,Header\r\nValue1,Value2"), true))
{
csvReader.ReadNextRecord();
}
Assert.Fail("Expected DuplicateHeaderException");
}
catch (DuplicateHeaderException ex)
{
Assert.AreEqual("Header", ex.HeaderName);
Assert.AreEqual(1, ex.ColumnIndex);
}
catch (Exception)
{
Assert.Fail("Expected DuplicateHeaderException");
}
}

[Test]
public void HasHeader_DuplicateHeader_Override()
{
using (CsvReader csvReader = new CsvReader(new StringReader("Header,Header\r\nValue1,Value2"), true))
{
csvReader.DuplicateHeaderEncountered += (s, e) => e.HeaderName = $"{e.HeaderName}_{e.Index}";
csvReader.ReadNextRecord();
Assert.AreEqual(csvReader.Columns[0].Name, "Header");
Assert.AreEqual(csvReader.Columns[1].Name, "Header_1");
}
}

[Test]
public void HasHeader_HeaderExists()
{
Expand Down
18 changes: 17 additions & 1 deletion code/CsvReader.WebDemo/CsvReader.WebDemo.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<OutputPath>$(SolutionDir)build\CsvReader.WebDemo\$(Configuration)</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net9.0|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net9.0|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net10.0|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net10.0|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\SolutionInfo.cs" Link="SolutionInfo.cs" />
</ItemGroup>
Expand Down
18 changes: 17 additions & 1 deletion code/CsvReader.WpfDemo/CsvReader.WpfDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net48;net6.0-windows;net7.0-windows;net8.0-windows</TargetFrameworks>
<TargetFrameworks>net48;net8.0-windows;net9.0-windows;net10.0-windows</TargetFrameworks>
<UseWPF>true</UseWPF>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<OutputPath>$(SolutionDir)build\CsvReader.WpfDemo\$(Configuration)</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net9.0-windows|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net9.0-windows|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net10.0-windows|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net10.0-windows|AnyCPU'">
<WarningLevel>8</WarningLevel>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\SolutionInfo.cs" Link="SolutionInfo.cs" />
</ItemGroup>
Expand Down
4 changes: 1 addition & 3 deletions code/CsvReader.WpfDemo/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public DataView DataView
{
get
{
#if NET472
return new DataView(_data);
#elif NET48
#if NET48
return new DataView(_data);
#else
return _data.AsDataView();
Expand Down
27 changes: 25 additions & 2 deletions code/CsvReader/CsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,13 @@ protected virtual void OnParseError(ParseErrorEventArgs e)
handler(this, e);
}

/// <summary>
/// Occurs when HasHeaders is true and a duplicate Column Header Name is encountered.
/// Setting the HeaderName property on this column will prevent the library from throwing a duplicate key exception
/// </summary>
public event EventHandler<DuplicateHeaderEventArgs> DuplicateHeaderEncountered;


/// <summary>
/// Gets the comment character indicating that a line is commented out.
/// </summary>
Expand Down Expand Up @@ -1745,10 +1752,24 @@ protected virtual bool ReadNextRecord(bool onlyReadHeaders, bool skipToNextLine)
Type = typeof(string)
};

int existingIndex;
if (_fieldHeaderIndexes.TryGetValue(headerName, out existingIndex))
{
if (DuplicateHeaderEncountered == null)
throw new DuplicateHeaderException(headerName, i);

DuplicateHeaderEventArgs args = new DuplicateHeaderEventArgs(headerName, i, existingIndex);
DuplicateHeaderEncountered(this, args);
col.Name = args.HeaderName;
}

_fieldHeaderIndexes.Add(col.Name, i);

// Should be correct as we are going in ascending order.
Columns.Add(col);
}
_fieldHeaderIndexes.Add(headerName, i);
else
_fieldHeaderIndexes.Add(headerName, i);
}

// Proceed to first record
Expand Down Expand Up @@ -2360,10 +2381,12 @@ string IDataRecord.GetName(int i)
EnsureInitialize();
ValidateDataReader(DataReaderValidations.IsNotClosed);

if (i < 0 || i >= Columns.Count)
if (i < 0 || i >= FieldCount)
throw new ArgumentOutOfRangeException("i", i,
string.Format(CultureInfo.InvariantCulture, ExceptionMessage.FieldIndexOutOfRange, i));

if (i >= Columns.Count) return null;

return Columns[i].Name;
}

Expand Down
12 changes: 11 additions & 1 deletion code/CsvReader/CsvReader.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net48;net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net48;net8.0;net9.0;net10.0</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>LumenWorks.Framework.snk</AssemblyOriginatorKeyFile>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
Expand All @@ -14,12 +14,22 @@
<RepositoryType>github</RepositoryType>
<PackageTags>CSV cvs-reader</PackageTags>
<PackageReleaseNotes>Added an additional CsvReader constructor parameter to specify a custom 'new line' character</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<Version>4.5.0</Version>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\SolutionInfo.cs" Link="SolutionInfo.cs" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<Compile Update="Resources\ExceptionMessage.Designer.cs">
<DependentUpon>ExceptionMessage.resx</DependentUpon>
Expand Down
71 changes: 71 additions & 0 deletions code/CsvReader/Events/DuplicateHeaderEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// LumenWorks.Framework.IO.CSV.ParseErrorEventArgs
// Copyright (c) 2006 S�bastien Lorion
//
// MIT license (http://en.wikipedia.org/wiki/MIT_License)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;

namespace CsvReader.Events
{
/// <summary>
/// Provides data for the <see cref="M:CsvReader.OnDuplicateHeader"/> event.
/// </summary>
public class DuplicateHeaderEventArgs
: EventArgs
{
#region Constructors

/// <summary>
/// Initializes a new instance of the DuplicateHeaderEventArgs class.
/// </summary>
/// <param name="headerName">The name of the duplicate header.</param>
/// <param name="index">The index of the duplicate header being added.</param>
/// <param name="existingDuplicateIndex">The index of the duplicate header that is already in the Column collection.</param>
public DuplicateHeaderEventArgs(string headerName, int index, int existingDuplicateIndex)
{
HeaderName = headerName;
Index = index;
ExistingDuplicateIndex = existingDuplicateIndex;
}

#endregion

#region Properties

/// <summary>
/// Name of the header that is a duplicate.
/// </summary>
/// <value>The header name.</value>
public string HeaderName { get; set; }

/// <summary>
/// Index of the duplicate header being added
/// </summary>
/// <value>The column index</value>
public int Index { get; }

/// <summary>
/// Index of the duplicate header that has already been added to the Column collection
/// </summary>
/// <value>The column index</value>
public int ExistingDuplicateIndex { get; }

#endregion
}
}
Loading