private static void RunBufferedReaderTest(string[] args) { using (var fs = new FileStream(@"o:\tmp\test.log", FileMode.Open, FileAccess.Read)) { using (var of = new StreamWriter(@"o:\tmp\tmp.txt", false, Encoding.GetEncoding(1254))) { var lr = new BufferedLineReader(fs); var line = string.Empty; int nl = 0; int cnt = 0; while ((line = lr.ReadLine(Encoding.GetEncoding(1254), ref nl)) != null) { Console.Write("\b\b\b\b\b\b\b\b\b\b{0:D10}", ++cnt); of.WriteLine(line); of.Flush(); } } } }
private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e) { try { L.Log(LogType.FILE, LogLevel.INFORM, "Begin Processing LastFile(" + lastFile + ") LastRecord(" + lastPosition + ")"); if (!InitializeInstance()) return; if (!GetLastFile(false)) return; PrepareEncoding(ref encoding); var recordSent = 0; Dictionary<string, int> header = null; var svc = GetInstanceService(usingRegistry ? "Security Manager Sender" : "Security Manager Remote Recorder"); while (recordSent < max_record_send) { var fInfo = new FileInfo(Path.Combine(Location, lastFile)); if (fInfo.Exists) { using ( var inp = new FileStream(fInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) ) { long position = 0; var headerInfo = GetHeaderInfo(inp, ref header, ref position); if (headerInfo == null) { L.Log(LogType.FILE, LogLevel.WARN, fInfo.FullName + " has no header"); } else { WarnForMissingHeaders(fInfo, headerInfo, header); if (position > lastPosition) { SetReg(Id, position.ToString(CultureInfo.InvariantCulture), string.Empty, fInfo.Name, string.Empty); lastPosition = position; } inp.Seek(lastPosition, SeekOrigin.Begin); var line = string.Empty; var reader = new BufferedLineReader(inp); var nl = 0; var rec = new RecWrapper(); while (recordSent < max_record_send && (line = reader.ReadLine(encoding, ref nl)) != null) { if (ProcessLine(headerInfo, line, rec)) { rec.LogName = "DHCPRecorder"; rec.CustomStr9 = headerInfo.Name; svc.SetData(_dal, _virtualHost, rec.rec); recordSent++; } svc.SetReg(Id, reader.Position.ToString(CultureInfo.InvariantCulture), string.Empty, fInfo.Name, string.Empty, rec.Datetime); lastPosition = reader.Position; } if (recordSent == max_record_send) return; } } } if (isLastFileInLocation) return; if (!GetLastFile(true)) return; } } catch (Exception ex) { L.Log(LogType.FILE, LogLevel.ERROR, "Error in timer handler:" + ex); } finally { timer1.Enabled = true; } }
private DataMappingInfo GetHeaderInfo(FileStream inp, ref Dictionary<string, int> header, ref long endOfHeader) { var offset = -1L; try { offset = inp.Position; var reader = new BufferedLineReader(inp); var line = string.Empty; var lineParts = new string[2][]; var headerPos = new long[] { 0, 0 }; var curr = 0; var cnt = 0; var nl = 0; while ((line = reader.ReadLine(encoding, ref nl)) != null) { lineParts[curr] = line.Split(','); if (lineParts[curr].Length > 2) //Line must have at least 2 commas { headerPos[curr] = reader.Position; curr ^= 1; if (++cnt == 2) { if (lineParts[curr].Length <= lineParts[curr ^ 1].Length) { cnt = 0; if (header == null) header = new Dictionary<string, int>(); else header.Clear(); while (cnt < lineParts[curr].Length) { header[lineParts[curr][cnt].Trim()] = cnt; cnt++; } endOfHeader = headerPos[curr]; return RecordFields2Info(header); } cnt = 1; } } else cnt = 0; } return null; } finally { if (offset >= 0) inp.Seek(offset, SeekOrigin.Begin); } }
private DataMappingInfo GetHeaderInfo(FileStream inp, ref Dictionary<string, int> header, ref long endOfHeader) { var offset = -1L; try { offset = inp.Position; var reader = new BufferedLineReader(inp); string line; var nl = 0; while ((line = reader.ReadLine(Encoding, ref nl)) != null) { if (line.StartsWith("#Fields: ")) { if (header == null) header = new Dictionary<string, int>(); else header.Clear(); var lineParts = line.Substring(8).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var cnt = 0; while (cnt < lineParts.Length) { header[lineParts[cnt]] = cnt; cnt++; } endOfHeader = reader.Position; return RecordFields2Info(header); } } return null; } finally { if (offset >= 0) inp.Seek(offset, SeekOrigin.Begin); } }