Skip to content
Merged
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
27 changes: 19 additions & 8 deletions src/DbfDataReader/DbfTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ public class DbfTable : Disposable

public DbfTable(string path, Encoding encoding = null, StringTrimmingOption stringTrimming = StringTrimmingOption.Trim, bool readFloatsAsDecimals = false)
{
if (!File.Exists(path)) throw new FileNotFoundException();
// resolve a relative path once: each File.Exists or FileStream open on a
// relative path looks up the current directory again, which costs far more
// than the check itself
var fullPath = System.IO.Path.GetFullPath(path);
if (!File.Exists(fullPath)) throw new FileNotFoundException();

Path = path;
CurrentEncoding = encoding;
StringTrimming = stringTrimming;
ReadFloatsAsDecimals = readFloatsAsDecimals;

Stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

Init();

var memoPath = MemoPath();
var memoPath = FindMemoPath(fullPath);
if (!string.IsNullOrEmpty(memoPath)) Memo = CreateMemo(memoPath);
}

Expand Down Expand Up @@ -85,7 +89,9 @@ private void Init()

public void Close()
{
Dispose(true);
// through Dispose() so that the finalizer is suppressed; otherwise every
// closed table is still finalized and survives into the next GC generation
Dispose();
}

protected override void Dispose(bool disposing)
Expand All @@ -104,13 +110,18 @@ protected override void Dispose(bool disposing)
}

public string MemoPath()
{
return FindMemoPath(Path);
}

private static string FindMemoPath(string tablePath)
{
var paths = new[]
{
System.IO.Path.ChangeExtension(Path, "fpt"),
System.IO.Path.ChangeExtension(Path, "FPT"),
System.IO.Path.ChangeExtension(Path, "dbt"),
System.IO.Path.ChangeExtension(Path, "DBT")
System.IO.Path.ChangeExtension(tablePath, "fpt"),
System.IO.Path.ChangeExtension(tablePath, "FPT"),
System.IO.Path.ChangeExtension(tablePath, "dbt"),
System.IO.Path.ChangeExtension(tablePath, "DBT")
};

foreach (var path in paths)
Expand Down