Skip to content

Instantly share code, notes, and snippets.

@ealvesss
Created September 6, 2022 19:41
Show Gist options
  • Save ealvesss/dc25be40b7296d67c4d36a37dd2ba416 to your computer and use it in GitHub Desktop.
Save ealvesss/dc25be40b7296d67c4d36a37dd2ba416 to your computer and use it in GitHub Desktop.
asdasd
using System;
using System.Buffers;
using System.ComponentModel.Design;
using System.IO;
using System.IO.Pipelines;
using System.Text;
using System.Threading.Tasks;
using iot.project.domain.entities;
namespace iot.project.service.Util
{
public abstract class CsvParseUtil<T> where T : class, ICsvParseUtil<T>
{
private readonly byte[] _header;
private readonly byte _separator = (byte)',';
public virtual async Task<T[]> Parse(string filePath)
{
var csvPool = ArrayPool<T>.Shared;
var csv = csvPool.Rent(500);
int position = 0;
await using (var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
var reader = PipeReader.Create(fileStream);
while (true)
{
var data = await reader.ReadAsync();
var dataBuffer = data.Buffer;
var actualPosition = ParseLine(dataBuffer, position, csv);
reader.AdvanceTo(actualPosition, dataBuffer.End);
if (data.IsCompleted)
break;
}
await reader.CompleteAsync();
}
csvPool.Return(csv);
return csv;
}
public virtual SequencePosition ParseLine(ReadOnlySequence<byte> dataBuffer, int position, T[] csvs)
{
var reader = new SequenceReader<byte>(dataBuffer);
while(reader.TryReadTo(out ReadOnlySpan<byte> line, (byte) '\n'))
{
T csv = GetDataFromCsv(line);
if (csv != null)
{
csvs[position] = csv;
position++;
}
}
return reader.Position;
}
private T GetDataFromCsv(ReadOnlySpan<byte> line)
{
if (line.IndexOf(_header) >= 0)
return null;
var record = new <typed object>();
for (int i = 0; i < 1; i++)
{
var index = line.IndexOf(_separator);
if (index < 0)
{
index = line.Length;
}
switch (i)
{
case 0:
record.Region = Encoding.UTF8.GetString(line.Slice(0, index));
break;
case 1:
record.Country = Encoding.UTF8.GetString(line.Slice(0, index));
break;
line = line.Slice(index + 1);
}
return record;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment