TransferPathCsvReader
using Microsoft.VisualBasic.FileIO;
using Relativity.Transfer.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Relativity.Transfer
{
public class TransferPathCsvReader : ITransferPathReader, IDisposable
{
private readonly TransferPathCsvHeader header = new TransferPathCsvHeader();
private bool disposed;
private TextFieldParser parser;
private string csvFile;
public IList<string> Delimiters { get; }
public TransferPathCsvReader()
{
Delimiters = new List<string>(new string[1] {
","
});
disposed = false;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Open(string file)
{
CheckDisposed();
Close();
parser = new TextFieldParser(file) {
TextFieldType = FieldType.Delimited
};
parser.SetDelimiters(Delimiters.ToArray());
csvFile = file;
}
public void Close()
{
CheckDisposed();
header.Clear();
parser?.Dispose();
}
public TransferPathRecord GetRecord()
{
CheckDisposed();
CheckOpen();
string[] array = parser.ReadFields();
if (array != null && array.Length != 0) {
if (!header.ParsedHeaders && header.ParseHeaders(csvFile, array)) {
array = parser.ReadFields();
if (array == null || array.Length == 0)
return null;
}
TransferPathRecord transferPathRecord = new TransferPathRecord();
if (array.Length > header.SourcePathFieldIndex)
transferPathRecord.SourcePath = array[header.SourcePathFieldIndex];
if (array.Length > header.TargetPathFieldIndex)
transferPathRecord.TargetPath = array[header.TargetPathFieldIndex];
if (array.Length > header.TargetFileNameFieldIndex)
transferPathRecord.TargetFileName = array[header.TargetFileNameFieldIndex];
if (array.Length > header.TotalBytesFieldIndex) {
string text = array[header.TotalBytesFieldIndex];
if (!string.IsNullOrEmpty(text))
transferPathRecord.TotalBytes = long.Parse(text);
}
if (array.Length > header.TagFieldIndex)
transferPathRecord.Tag = array[header.TagFieldIndex];
if (array.Length > header.SourcePathIdFieldIndex) {
string text2 = array[header.SourcePathIdFieldIndex];
if (!string.IsNullOrEmpty(text2))
transferPathRecord.SourcePathId = int.Parse(text2);
}
return transferPathRecord;
}
return null;
}
public bool Read()
{
CheckDisposed();
CheckOpen();
return !parser.EndOfData;
}
private void CheckOpen()
{
if (parser == null || string.IsNullOrEmpty(csvFile))
throw new InvalidOperationException(CoreStrings.CsvFileNotOpenedExceptionMessage);
}
private void CheckDisposed()
{
if (disposed)
throw new ObjectDisposedException(CoreStrings.ObjectDisposedExceptionMessage);
}
private void Dispose(bool disposing)
{
if (!disposed) {
if (disposing && parser != null) {
parser.Dispose();
parser = null;
}
disposed = true;
}
}
}
}