Replace CsvIndexer with CsvFieldIndexer.
This commit is contained in:
@@ -1,133 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace CsvLib
|
||||
{
|
||||
public class CsvIndexer
|
||||
{
|
||||
private bool _insideString;
|
||||
|
||||
private readonly char _separator;
|
||||
private readonly char _quoteChar;
|
||||
private readonly char _escapeChar;
|
||||
|
||||
public CsvIndexer(char separator = ',', char quoteChar = '"', char escapeChar = '\\')
|
||||
{
|
||||
_separator = separator;
|
||||
_quoteChar = quoteChar;
|
||||
_escapeChar = escapeChar;
|
||||
}
|
||||
|
||||
private List<long> _index = new();
|
||||
|
||||
public List<long> Index { get { return _index; } }
|
||||
|
||||
private void DummyParser(string line)
|
||||
{
|
||||
for (int i = 0; i < line.Length; i++)
|
||||
{
|
||||
char c = line[i];
|
||||
if (c == _separator && _insideString == false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (c == _quoteChar && _insideString == false)
|
||||
{
|
||||
_insideString = true;
|
||||
continue;
|
||||
}
|
||||
if (c == _quoteChar && _insideString)
|
||||
{
|
||||
_insideString = false;
|
||||
continue;
|
||||
}
|
||||
if (c == _escapeChar && _insideString)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void GenerateIndex(string file)
|
||||
{
|
||||
_insideString = false;
|
||||
_index.Clear();
|
||||
_index.Add(0);
|
||||
using FileStream stream = new(file, FileMode.Open);
|
||||
using StreamReader streamReader = new(stream, Encoding.Default, true, 4096);
|
||||
using TrackingTextReader reader = new(streamReader);
|
||||
|
||||
string currentLine;
|
||||
while ((currentLine = reader.ReadLine()) != null)
|
||||
{
|
||||
DummyParser(currentLine);
|
||||
if (_insideString == false)
|
||||
{
|
||||
_index.Add(reader.Position);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Index_SaveFile(string indexFile)
|
||||
{
|
||||
if (indexFile == null) { return; }
|
||||
if (File.Exists(indexFile))
|
||||
{
|
||||
File.Delete(indexFile);
|
||||
}
|
||||
Stream streamOut = File.Open(indexFile, FileMode.Create);
|
||||
using (BinaryWriter binWriter = new(streamOut))
|
||||
{
|
||||
binWriter.Write(_index.Count);
|
||||
for (int i = 0; i < _index.Count; i++)
|
||||
{
|
||||
binWriter.Write(_index[i]);
|
||||
}
|
||||
}
|
||||
streamOut.Close();
|
||||
}
|
||||
|
||||
private static List<long> Index_LoadFile(string indexFile)
|
||||
{
|
||||
List<long> tempIndex = new();
|
||||
|
||||
Stream streamIn = File.Open(indexFile, FileMode.Open);
|
||||
using (BinaryReader binReader = new(streamIn))
|
||||
{
|
||||
int numRegs = binReader.ReadInt32();
|
||||
for (int i = 0; i < numRegs; i++)
|
||||
{
|
||||
long value = binReader.ReadInt64();
|
||||
tempIndex.Add(value);
|
||||
}
|
||||
}
|
||||
streamIn.Close();
|
||||
return tempIndex;
|
||||
}
|
||||
|
||||
public void LoadIndexOfFile(string file)
|
||||
{
|
||||
DateTime dtFile = File.GetCreationTime(file);
|
||||
string indexFile = $"{file}.idx";
|
||||
if (File.Exists(indexFile) && File.GetCreationTime(indexFile) > dtFile)
|
||||
{
|
||||
_index = Index_LoadFile(indexFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Generate index
|
||||
DateTime dtNow = DateTime.UtcNow;
|
||||
GenerateIndex(file);
|
||||
TimeSpan tsGenIndex = DateTime.UtcNow - dtNow;
|
||||
|
||||
// Save Index if expensive generation
|
||||
if (tsGenIndex.TotalSeconds > 2)
|
||||
{
|
||||
Index_SaveFile(indexFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace CsvLib
|
||||
{
|
||||
public class TrackingTextReader : TextReader
|
||||
{
|
||||
private readonly TextReader _baseReader;
|
||||
private int _position;
|
||||
private readonly Encoding _currentEncoding = Encoding.Default;
|
||||
|
||||
public TrackingTextReader(TextReader baseReader)
|
||||
{
|
||||
_baseReader = baseReader;
|
||||
if (baseReader is StreamReader streamReader)
|
||||
{
|
||||
_currentEncoding = streamReader.CurrentEncoding;
|
||||
}
|
||||
}
|
||||
|
||||
public override int Read()
|
||||
{
|
||||
int read = _baseReader.Read();
|
||||
if (read > 127)
|
||||
{
|
||||
int count = _currentEncoding.GetByteCount(((char)read).ToString());
|
||||
_position += count;
|
||||
}
|
||||
else
|
||||
{
|
||||
_position++;
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
public override int Peek()
|
||||
{
|
||||
return _baseReader.Peek();
|
||||
}
|
||||
|
||||
public int Position
|
||||
{
|
||||
get { return _position; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,7 +74,7 @@ public partial class MainWindow : Window
|
||||
_loadedFile = fileName;
|
||||
TxtFileName.Text = fileName;
|
||||
|
||||
CsvIndexer csvIndexer = new();
|
||||
CsvFieldIndexer csvIndexer = new();
|
||||
csvIndexer.LoadIndexOfFile(_loadedFile);
|
||||
_index = csvIndexer.Index;
|
||||
_totalRegs = _index.Count - 1;
|
||||
|
||||
Reference in New Issue
Block a user