private void ReadHeader(ILineReader lineReader) { // TODO: Cope with truncated headers string header1 = lineReader.ReadLine(); string[] fields1 = header1.Split(separator, StringSplitOptions.RemoveEmptyEntries); parameters.JNum = Int32.Parse(fields1[1]); parameters.IInc = Double.Parse(fields1[2]); parameters.JInc = Double.Parse(fields1[3]); string header2 = lineReader.ReadLine(); string[] fields2 = header2.Split(separator, StringSplitOptions.RemoveEmptyEntries); parameters.XOrigin = Double.Parse(fields2[0]); parameters.XMax = Double.Parse(fields2[1]); parameters.YOrigin = Double.Parse(fields2[2]); parameters.YMax = Double.Parse(fields2[3]); string header3 = lineReader.ReadLine(); string[] fields3 = header3.Split(separator, StringSplitOptions.RemoveEmptyEntries); parameters.INum = Int32.Parse(fields3[0]); parameters.Orientation = Double.Parse(fields3[1]); // Two other unused fields here lineReader.ReadLine(); // Header 4 - Always seems to contain seven zeros parameters.VerifyAndCompleteParameters(); parameters.Build(builder); }
private async Task GetBlocks(CancellationToken cancellationToken) { while (true) { if (cancellationToken.IsCancellationRequested) { return; } var line = await lineReader.ReadLine(); if (line == null) { await Task.Delay(50, cancellationToken); } else { var block = ProcessLine(line.TrimEnd()); if (block != null) { dispatcher.Dispatch(block); } } } }
// TODO: Factor this method out somhow - pass in grid // parameters. Modify grid parameters to contain // information on whether the grid is stored in // RowMajor or ColumnMajor order, and in order ot // increasing or decreasing I and J private void ReadRowMajorGrid(ILineReader lineReader) { Debug.Assert(parameters.INum.HasValue); string line; int counter = 0; while ((line = lineReader.ReadLine()) != null) { string[] fields = line.Split(separator, StringSplitOptions.RemoveEmptyEntries); //string[] fields = Patterns.Whitespace.Split(line.Trim()); foreach (string field in fields) { double z; if (double.TryParse(field, out z)) { if (z != parameters.ZNull) { int i = counter % parameters.INum.Value; int j = counter / parameters.INum.Value; builder[i, j] = -z; // Irap Classic is in depth } ++counter; } else { StringBuilder message = new StringBuilder(); message.AppendFormat("Bad grid data '{0}' at line {1}", field, lineReader.PhysicalLineNumber); throw new OpenException(message.ToString()); } } } }
private void ReadBody(ILineReader lineReader) { Debug.Assert(parameters.INum.HasValue); // Parse the body of the grid string line; int counter = 0; while ((line = lineReader.ReadLine()) != null) { string[] fields = SplitOnWhitespace(line); foreach (string field in fields) { double z; if (double.TryParse(field, out z)) { int i = counter / parameters.JNum.Value; int j = parameters.JNum.Value - (counter % parameters.JNum.Value); if (z != parameters.ZNull) { builder[i, j] = z; } ++counter; } else { StringBuilder message = new StringBuilder(); message.AppendFormat("Bad grid data '{0}' at line {1}", field, lineReader.PhysicalLineNumber); throw new OpenException(message.ToString()); } } } }
public void Test() { MockRepository mockRepository = new MockRepository(); ILineReader lineReader = mockRepository.DynamicMock<ILineReader>(); lineReader.Stub(r => r.ReadLine()).Callback(new ReadLineDelegate(ReadRecord)).Return(_count < _countOfLines); lineReader.Stub(r => r.CurrentLine()).Do(new CurrentStringDelegate(ReturnString)).Return(_currentLine); mockRepository.ReplayAll(); bool read1 = lineReader.ReadLine(); Assert.That(read1, Is.True); Assert.That(lineReader.CurrentLine(), Is.EqualTo("A")); bool read2 = lineReader.ReadLine(); Assert.That(read2, Is.True); Assert.That(lineReader.CurrentLine(), Is.EqualTo("B")); bool read3 = lineReader.ReadLine(); Assert.That(read3, Is.True); Assert.That(lineReader.CurrentLine(), Is.EqualTo("C")); bool read4 = lineReader.ReadLine(); Assert.That(read4, Is.False); Assert.That(lineReader.CurrentLine(), Is.Null); }
private static string DetermineType(ILineReader lineReader) { string line = lineReader.ReadLine(); if (line.StartsWith("@")) { string[] fields = line.Split(','); string type = fields[1].Trim(); return(type); } return("Unknown"); }
public bool EncryptMessage() { var sb = new StringBuilder(); var inputString = lineReader.ReadLine(); var isEmptyString = string.IsNullOrWhiteSpace(inputString); if (!isEmptyString) { foreach (byte b in GetHash(inputString)) { sb.Append(b.ToString("X2")); } lineWriter.WriteLine(sb.ToString()); } return(isEmptyString); }
public void GetDataContent(ILineReader dr) { int i = 0; //rowcounter to see where error occured try { var fileStopwatch = Stopwatch.StartNew(); #region Verbose output if (Option.Verbose) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Start reading {0}", DataPath); } #endregion using (dr) { string line; var rowKey = new StringBuilder(); var colKeys = Option.Keycolumns; var sepChar = Option.Fieldseparator; while ((line = dr.ReadLine()) != null) { i += 1; //Fields 4,6,7 makes the row unique according to rumours. Not that fieldArr is nollbased so it is: 3,5,6 BuildRowKey(ref rowKey, line, sepChar, colKeys); if (Option.FieldCompression) { line = StringCompressor.CompressString(line); } LineDictionary.Add(rowKey.ToString(), line); rowKey.Clear(); } } fileStopwatch.Stop(); #region Verbose output if (Option.Verbose) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("Done Reading {0} called file {3}. It took {1} ms and contained {2} rows", DataPath, fileStopwatch.Elapsed.Milliseconds, LineDictionary.Count, Name); Console.ForegroundColor = ConsoleColor.DarkRed; } #endregion } #region Catch if error catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("The file {0} could not be transformed to Dictionary structure error in line {1}:", DataPath, i); Console.WriteLine(e.Message); Console.ForegroundColor = ConsoleColor.White; } #endregion }
public LfdLine ReadLine() { while (true) { String line = reader.ReadLine(); if (line == null) { return(null); } lineNumber++; // // Identify line // for (int offset = 0; true; offset++) { // Empty Line if (offset >= line.Length) { break; } if (Char.IsWhiteSpace(line[offset])) { continue; } // // If the first character is a '#' then the line is a comment if (line[offset] == '#') { return(new LfdLine((context.Count > 0) ? context.Peek() : null, line, lineNumber)); } // // If the first character is '}' then the line must be a BLOCK_END line // if (line[offset] == '}') { // Verify whitespace till end of line while (true) { offset++; if (offset >= line.Length) { break; } if (!Char.IsWhiteSpace(line[offset])) { throw FormatError(line, "Line starting with '}' must be followed by only whitespace"); } } if (context.Count <= 0) { throw FormatError(line, "The block end '}' had no matching block begin"); } context.Pop(); break; } // // '{' and '"' are invalid characters to start a line // if (line[offset] == '{' || line[offset] == '"') { throw FormatError(line, "A line cannot start with '{0}'", line[offset]); } if (line[offset] == '\\') { // verify whitespace till end while (true) { offset++; if (offset >= line.Length) { line = reader.ReadLine(); if (line == null) { return(null); } offset = 0; lineNumber++; break; } if (!Char.IsWhiteSpace(line[offset])) { throw FormatError(line, "The '\\' character cannot have non whitespace after it"); } } continue; } // // Get the line id // Int32 saveOffset = offset; while (true) { offset++; if (offset >= line.Length) { return(new LfdLine((context.Count > 0) ? context.Peek() : null, line.Substring(saveOffset), null, lineNumber)); } if (line[offset] == '{') { String lineIdSpecialCase = line.Substring(saveOffset, offset - saveOffset); //verify whitespace till the end while (true) { offset++; if (offset >= line.Length) { break; } if (!Char.IsWhiteSpace(line[offset])) { throw FormatError(line, "The '{' character just after the line Id can only be followed by whitespace"); } } LfdLine newLine = new LfdLine((context.Count > 0) ? context.Peek() : null, lineIdSpecialCase, null, lineNumber); context.Push(newLine); return(newLine); } if (Char.IsWhiteSpace(line[offset])) { break; } } String lineId = line.Substring(saveOffset, offset - saveOffset); // Skip whitespace until you get to the first field (or end of line) while (true) { offset++; if (offset >= line.Length) { return(new LfdLine((context.Count > 0) ? context.Peek() : null, lineId, null, lineNumber)); } if (!Char.IsWhiteSpace(line[offset])) { break; } } // // Get all fields in the line // List <String> fields = new List <String>(); while (true) { Int32 fieldStart = offset; // // A quoted field // if (line[offset] == '"') { fieldStart++; Boolean continueLoop = false; while (true) { offset++; if (offset >= line.Length) { throw FormatError(line, "Found a quoted field without an ending quote"); } if (line[offset] == '"') { if (line[offset - 1] != '\\') { fields.Add(line.Substring(fieldStart, offset - fieldStart)); offset++; break; } else { continueLoop = true; break; } } } // // If there is an escaped quote in the string, then the rest of the characters // must be added to a new string to remove any extra backslashes // if (continueLoop) { StringBuilder builder = new StringBuilder(line.Substring(fieldStart, offset - 1 - fieldStart)); builder.Append('"'); while (true) { offset++; if (offset >= line.Length) { throw FormatError(line, "Found a quoted field without an ending quote"); } if (line[offset] == '"') { fields.Add(builder.ToString()); offset++; break; } if (line[offset] == '\\') { offset++; if (offset >= line.Length) { throw FormatError(line, "Line ended with '\\' but was in the middle of a quote"); } if (line[offset] == '\\') { builder.Append('\\'); } else if (line[offset] == '"') { builder.Append('"'); } else { throw FormatError(line, "Unrecognized escape character '{0}', expected '\"' or '\\'", line[offset]); } } else { builder.Append(line[offset]); } } } } // // An Open Brace field which must be the last field // else if (line[offset] == '{') { //verify whitespace till the end while (true) { offset++; if (offset >= line.Length) { break; } if (!Char.IsWhiteSpace(line[offset])) { throw FormatError(line, "A Field starting with the '{' character can only be followed by whitespace unless escaped with '\\{'"); } } LfdLine newLine = new LfdLine((context.Count > 0) ? context.Peek() : null, lineId, fields.ToArray(), lineNumber); context.Push(newLine); return(newLine); } // // An escape character '\' used for starting fields with the '{' character // and continuing the fields on the next line // else if (line[offset] == '\\') { offset++; if (offset >= line.Length) { line = reader.ReadLine(); if (line == null) { return(null); } offset = 0; lineNumber++; } else { if (line[offset] == '{') { fieldStart++; while (true) { offset++; if (offset >= line.Length) { fields.Add(line.Substring(fieldStart)); return(new LfdLine((context.Count > 0) ? context.Peek() : null, lineId, fields.ToArray(), lineNumber)); } if (Char.IsWhiteSpace(line[offset])) { fields.Add(line.Substring(fieldStart, offset - fieldStart)); break; } } } else { while (true) { offset++; if (offset >= line.Length) { line = reader.ReadLine(); if (line == null) { return(null); } offset = 0; lineNumber++; break; } if (!Char.IsWhiteSpace(line[offset])) { throw FormatError(line, "The '\\' character cannot have non whitespace after it"); } } } } } // // A normal field that ends when whitespace is encountered // else { while (true) { offset++; if (offset >= line.Length) { fields.Add(line.Substring(fieldStart)); return(new LfdLine((context.Count > 0) ? context.Peek() : null, lineId, fields.ToArray(), lineNumber)); } if (Char.IsWhiteSpace(line[offset])) { fields.Add(line.Substring(fieldStart, offset - fieldStart)); break; } } } // // Skip whitespace, or return a line if the end of line is found // while (true) { if (offset >= line.Length) { return(new LfdLine((context.Count > 0) ? context.Peek() : null, lineId, fields.ToArray(), lineNumber)); } if (!Char.IsWhiteSpace(line[offset])) { break; } offset++; } } } } }
private void ReadHeader(ILineReader lineReader) { // TODO: Parse failure exception handling // Header 1 : name, type, number of values per line string header1 = lineReader.ReadLine(); string[] fields1 = header1.Split(','); if (fields1.Length < 3) { StringBuilder message = new StringBuilder(); message.AppendFormat("Expected at least two comma separated fields at line {0}", lineReader.PhysicalLineNumber); throw new OpenException(message.ToString()); } Match header1Match = Regex.Match(fields1[0], @"\s*@\s*(.*)\s*HEADER\s*"); if (!header1Match.Success) { StringBuilder message = new StringBuilder(); message.AppendFormat("First line of header does not match expected pattern at line {0}", lineReader.PhysicalLineNumber); throw new OpenException(message.ToString()); } string headerName = header1Match.Groups[1].ToString(); string headerType = fields1[1].Trim(); int headerValuesPerLine = Int32.Parse(fields1[2]); // Header 2 : field width, Z null value, Z null text, decimal position, start position string header2 = lineReader.ReadLine(); string[] fields2 = header2.Split(','); // TODO Header2 error handling int fieldWidth = Int32.Parse(fields2[0].Trim()); parameters.ZNull = Double.Parse(fields2[1]); string nullText = fields2[2].Trim(); int decimalPosition = Int32.Parse(fields2[3]); int startPosition = Int32.Parse(fields2[4]); // Header 3: number of rows (j), number of columns (i), X min, X max, Y min, Y max string header3 = lineReader.ReadLine(); string[] fields3 = header3.Split(','); // TODO Header3 error handling parameters.JNum = Int32.Parse(fields3[0]); parameters.INum = Int32.Parse(fields3[1]); parameters.XOrigin = Double.Parse(fields3[2]); parameters.XMax = Double.Parse(fields3[3]); parameters.YOrigin = Double.Parse(fields3[4]); parameters.YMax = Double.Parse(fields3[5]); // Header 4: three floats. What do they mean? string header4 = lineReader.ReadLine(); // TODO: Parse this header // Header 5: We expect a line starting with @ to terminate the header string header5 = lineReader.ReadLine(); if (!header5.StartsWith("@")) { StringBuilder message = new StringBuilder(); message.AppendFormat("Malformed ZMap header at line {0}. Expected header termination", lineReader.PhysicalLineNumber); throw new OpenException(message.ToString()); } }