Skip to content

Commit

Permalink
Minor change to ReadLines method
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandPheasant committed Nov 22, 2015
1 parent 658c789 commit 0981307
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
14 changes: 9 additions & 5 deletions Source/TailBlazer.Domain/FileHandling/FileInfoEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public static IObservable<LineMatches> Match(this IObservable<FileNotification>
}).Switch();
}


public static IEnumerable<Line> ReadLines(this FileInfo source, int[] lines, Func<int, bool> isEndOfTail = null)
{
using (var stream = File.Open(source.FullName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
Expand All @@ -145,18 +144,23 @@ public static IEnumerable<Line> ReadLines(this FileInfo source, int[] lines, Fun
}
}

public static IEnumerable<T> ReadLines<T>(this FileInfo source, IEnumerable<LineIndex> lines, Func<LineIndex,string, T> selector)
public static IEnumerable<T> ReadLines<T>(this FileInfo source, IEnumerable<LineIndex> lines, Func<LineIndex,string, T> selector, Encoding encoding=null)
{
encoding = encoding ?? Encoding.Default;

//TODO: This assumes a windows file whereas Unix / max have length=1.
//This is accounted for on the LineIndexer but we need to pass the length and current encoding here ()
const int delimiterLength = 2;

using (var stream = File.Open(source.FullName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
{

foreach (var lineIndex in lines.OrderBy(l=>l.Index))
{
var data = new byte[lineIndex.Size-2];
var data = new byte[lineIndex.Size- delimiterLength];
stream.Seek(lineIndex.Start, SeekOrigin.Begin);
stream.Read(data, 0, data.Length);

var result = Encoding.Default.GetString(data);
var result = encoding.GetString(data);
yield return selector(lineIndex, result);
}
}
Expand Down
2 changes: 2 additions & 0 deletions Source/TailBlazer.Domain/FileHandling/LineIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public IEnumerable<int> ReadToEnd()
_postion = _postion + line.Length + _delimiterSize;
yield return _postion;
}


}

public void Dispose()
Expand Down
13 changes: 6 additions & 7 deletions Source/TailBlazer.Domain/FileHandling/LineMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public class LineMatcher: IDisposable
private readonly StreamReader _reader;

public Encoding Encoding { get; }

public int TotalCount => _index;

public int Count => _matches;

private int _index = -1;
Expand All @@ -29,6 +27,9 @@ public LineMatcher(FileInfo info, Func<string,bool> predicate, Encoding encoding

_stream = File.Open(info.FullName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite);
_reader = new StreamReader(_stream, Encoding, true);

//TODO 1: Get current encoding from _reader.CurrentEncoding and expose so cusumers can read the lines with the same encoding
//TODO 2: Expose line delimiter length so it can be correctly removed from when re-reading a line
}

public IEnumerable<int> ScanToEnd()
Expand All @@ -40,11 +41,9 @@ public IEnumerable<int> ScanToEnd()
while ((line = _reader.ReadLine()) != null)
{
_index++;
if (_predicate(line))
{
_matches++;
yield return _index;
}
if (!_predicate(line)) continue;
_matches++;
yield return _index;
}
}

Expand Down
5 changes: 5 additions & 0 deletions Source/TailBlazer/Views/VirtualScrollPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

namespace TailBlazer.Views
{
/// <summary>
/// This is adapted (butchered!) from VirtualWrapPanel in https://github.com/samueldjack/VirtualCollection
///
/// See http://blog.hibernatingrhinos.com/12515/implementing-a-virtualizingwrappanel
/// </summary>
public class VirtualScrollPanel : VirtualizingPanel, IScrollInfo
{
private const double ScrollLineAmount = 16.0;
Expand Down

0 comments on commit 0981307

Please sign in to comment.