static void Main(string[] args) { // TODO: Consider using map approach and create Mathcer property inside a File. var matchersMap = new Dictionary<string,IHeaderMatcher> { { UnknownHeaderMatcher.FullName, new FirstLine3IntegersTypedMatcher() }, { UnknownHeaderMatcher.FullName, new FirstLineJustIntegersMatcher() }, { UnknownHeaderMatcher.FullName, new JustVowelsMatcher() }, { UnknownHeaderMatcher.FullName, new UnknownHeaderMatcher() } }; var matchers = new IHeaderMatcher[] { new FirstLine3IntegersTypedMatcher(), new FirstLineJustIntegersMatcher(), new JustVowelsMatcher(), new UnknownHeaderMatcher() }; var processor = new DirectoryProcessor(new FileMatcher(matchers)); foreach (var path in args) { System.Console.WriteLine("Start processing '{0}'..", path); processor.Process(path); System.Console.WriteLine("Directory processing finished."); System.Console.WriteLine(); } System.Console.WriteLine("Press any key to exit.."); System.Console.Read(); }
static void Main(string[] args) { // TODO: Consider using map approach and create Mathcer property inside a File. var matchersMap = new Dictionary <string, IHeaderMatcher> { { UnknownHeaderMatcher.FullName, new FirstLine3IntegersTypedMatcher() }, { UnknownHeaderMatcher.FullName, new FirstLineJustIntegersMatcher() }, { UnknownHeaderMatcher.FullName, new JustVowelsMatcher() }, { UnknownHeaderMatcher.FullName, new UnknownHeaderMatcher() } }; var matchers = new IHeaderMatcher[] { new FirstLine3IntegersTypedMatcher(), new FirstLineJustIntegersMatcher(), new JustVowelsMatcher(), new UnknownHeaderMatcher() }; var processor = new DirectoryProcessor(new FileMatcher(matchers)); foreach (var path in args) { System.Console.WriteLine("Start processing '{0}'..", path); processor.Process(path); System.Console.WriteLine("Directory processing finished."); System.Console.WriteLine(); } System.Console.WriteLine("Press any key to exit.."); System.Console.Read(); }
public static async Task ParseStream( Stream inputStream, IHeaderMatcher headerMatcher, Func <List <MessageInfo>, Task <bool> > messagesSink, Action <double> progressHandler = null, Flags flags = Flags.None ) { inputStream.Position = 0; var totalLen = inputStream.Length; if (totalLen == 0) { progressHandler = null; } int rawBufferSize = 1024 * 512; int bufferUnderflowThreshold = 1024 * 4; byte[] rawBytesBuffer = new byte[rawBufferSize]; char[] rawCharsBuffer = new char[rawBufferSize]; var messages = new List <MessageInfo>(5000); var buffer = new SlidingBuffer(); int currentMessageIndex = 0; if ((flags & Flags.SkipDoubleBytePeamble) != 0) { await inputStream.ReadAsync(rawBytesBuffer, 0, 2); } for (; ;) { int bytesRead = await inputStream.ReadAsync(rawBytesBuffer, 0, rawBufferSize); if (bytesRead == 0) { break; } int charsCount = ((flags & Flags.UCS2) == 0) ? GetChars(rawBytesBuffer, bytesRead, rawCharsBuffer) : Encoding.Unicode.GetChars(rawBytesBuffer, 0, bytesRead, rawCharsBuffer, 0); buffer.Push(rawCharsBuffer, charsCount); int endOfProcessedTextPosition = HandleBufferContent(headerMatcher, buffer, messages, bufferUnderflowThreshold, ref currentMessageIndex, flags); buffer.Pop(endOfProcessedTextPosition); if (!await messagesSink(messages)) { break; } messages.Clear(); if (progressHandler != null) { progressHandler((double)inputStream.Position / (double)totalLen); } } buffer.Pop(HandleBufferContent(headerMatcher, buffer, messages, 0, ref currentMessageIndex, flags)); YieldMessage(buffer, buffer.CurrentContent.Length, messages, ref currentMessageIndex); if (messages.Count != 0) { await messagesSink(messages); } }
public void SetReplyHeaderNames(params string[] replyHeaderNames) { if (replyHeaderNames == null) { throw new ArgumentNullException(nameof(replyHeaderNames)); } ReplyHeaderMatcher = CreateHeaderMatcher(replyHeaderNames); }
protected AbstractHeaderMapper(string standardHeaderPrefix, List <string> requestHeaderNames, List <string> replyHeaderNames) { StandardHeaderPrefix = standardHeaderPrefix; RequestHeaderNames = requestHeaderNames; ReplyHeaderNames = replyHeaderNames; #pragma warning disable S1699 // Constructors should only call non-overridable methods RequestHeaderMatcher = CreateDefaultHeaderMatcher(StandardHeaderPrefix, RequestHeaderNames); ReplyHeaderMatcher = CreateDefaultHeaderMatcher(StandardHeaderPrefix, ReplyHeaderNames); #pragma warning restore S1699 // Constructors should only call non-overridable methods }
unsafe static int HandleBufferContent(IHeaderMatcher headerMatcher, SlidingBuffer buffer, List <MessageInfo> messages, int bufferUnderflowThreshold, ref int currentMessageIndex, Flags flags) { string content = buffer.CurrentContent; int contentLen = content.Length; fixed(char *contentPtr = content) { for (int currentPosition = 0; ;) { if (contentLen - currentPosition < bufferUnderflowThreshold) { return(currentPosition); } var startIdx = currentMessageIndex > 0 ? FindNewLine(contentPtr, contentLen, currentPosition) : currentPosition; var match = headerMatcher.Match(contentPtr, contentLen, startIdx, content); if (match == null) { return(currentPosition); } YieldMessage(buffer, contentPtr, match.Index, messages, ref currentMessageIndex); var h = buffer.AllocatedMatchHeader; h.StreamPosition = buffer.ContentStreamPosition + match.Index; h.EndOfHeaderPosition = match.Index + match.Length; h.Match2 = match; h.Buffer = content; buffer.CurrentMessageHeader = h; if ((flags & Flags.UCS2) != 0) { int DefaultAlignmentBlockSize = 32 * 1024; var realStreamPos = h.StreamPosition * 2; if ((flags & Flags.SkipDoubleBytePeamble) != 0) { realStreamPos += 2; } h.StreamPosition = (realStreamPos / DefaultAlignmentBlockSize) * DefaultAlignmentBlockSize + (realStreamPos % DefaultAlignmentBlockSize) / 2; } currentPosition = match.Index + match.Length; } } }
public MatchedPair(IHeaderMatcher matcher, File file) { Matcher = matcher; File = file; }