public RvProgram Parse(string source) { Logger.Info("Parse file {file}", source); var program = new RvProgram(); ParseContent concreteParse = null; // Check if not null... if (source == null) { throw new ArgumentNullException("source"); } // Check if file exists.. var fileExists = File.Exists(source); if (!fileExists) { throw new FileNotFoundException("File " + source + " cannot be found!"); } try { var fi = new FileInfo(source); var extension = fi.Extension; bool isText = extension.Equals(".hex") || extension.Equals(".bin"); if (isText) { ParseText(source, program, extension); } else { ParseBinary(source, program, extension); } } catch (Exception ex) { Logger.Error(ex, "Exception detected"); throw; } return(program); }
private void ParseText(string source, RvProgram program, string extension) { ParseContent concreteParse = null; if (extension.Equals(".hex")) { concreteParse = ParseHex; } if (extension.Equals(".bin")) { concreteParse = ParseBin; } // Something is not as expected. if (concreteParse == null) { throw new ParserException("Could not read rv format. Please use a valid extension (.hex, .bin,.e) "); } // *.hex and *.bin are using the text format // Parse it now.. using (var textStream = File.OpenText(source)) { // Read each line and convert it to Opcodes while (!textStream.EndOfStream) { var line = textStream.ReadLine(); Logger.Debug("Reading {line}", line); // Convert concreteParse(line, program); } } }