public void WriteResultTo(Stream stream, Encoding encoding) { StreamWriterProvider writerProvider = new StreamWriterProvider(stream, encoding, true); InternalWriteResultTo(writerProvider); }
private void InternalWriteResultTo(StreamWriterProvider writerProvider) { var children = new List<ILookupDictionary>(); CombineTxt m = this; while (m.Parent != null) { var matchDictionary = LookupDictionaryFactory.CreateLookupDictionary(); using (StreamReader sr = _readerProvider.GetStreamReader()) { string line = null; while ((line = sr.ReadLine()) != null) { var filtered = m._forEachFunc(line); var key = m.KeyInfo.KeyDefinition(filtered); matchDictionary.Add(key, filtered); } sr.Close(); } children.Add(matchDictionary); m = m.Parent; } StreamWriter sw = writerProvider.GetStreamWriter(); using (StreamReader sr = m._readerProvider.GetStreamReader()) { string line = null; string key = null; string previousKey = null; while ((line = sr.ReadLine()) != null) { var filtered = m._forEachFunc(line); if (m._newRecordFunc(filtered)) { key = m.KeyInfo.KeyDefinition(filtered); } if (previousKey != null && previousKey != key) { WriteChildren(sw, previousKey, children); } sw.WriteLine(filtered); previousKey = key; } WriteChildren(sw, previousKey, children); // StreamWriters assume they own the stream so they // close the stream when they are disposed of. sw.Flush(); if (!writerProvider.KeepStreamOpen) { sw.Close(); sw.Dispose(); } } m = this; int i = 0; while (m.Parent != null) { List<string> unMatchedItems = new List<string>(); foreach (var matchItem in children[i++]) { unMatchedItems.AddRange(matchItem.Value); } if (m._unMatchedFunc != null) { m._unMatchedFunc(unMatchedItems); } m = m.Parent; } }
public void WriteResultTo(string fileName, Encoding encoding) { StreamWriterProvider writerProvider = new StreamWriterProvider(fileName, encoding); InternalWriteResultTo(writerProvider); }