public static CsvDocument CreateFrom(TextReader reader, CsvParsingOptions options = null) { if (null == reader) { throw new ArgumentNullException(nameof(reader)); } var document = new CsvDocument(); using (var tokenizer = new CsvTokenizer(reader)) { try { var parser = new CsvParser(tokenizer); parser.Parse(document, options ?? DefaultOptions.Value); } catch (Exception e) { Console.WriteLine(e); throw; } } return(document); }
public void Parse(CsvDocument document, CsvParsingOptions options) { try { ParseInternal(document, options); } catch (Exception exception) { throw new CsvParsingException(exception.Message, exception); } }
internal void ParseInternal(CsvDocument document, CsvParsingOptions options) { var hasHeader = false; var addRow = new Action <ICollection <string> >(fields => { var row = document.CreateRow(); fields.ForEach(text => row.Fields.Add(document.CreateField(row, text))); document.Rows.Add(row); }); var state = ParserState.Begin; while (ParserState.Done != state) { switch (state) { case ParserState.Begin: { state = ParseLine(fields => { if (options.HasHeader && false == hasHeader) { hasHeader = true; fields.ForEach(field => document.Names.Add(field)); return; } addRow.Invoke(fields); }); break; } case ParserState.NewLine: { state = ParseLine(addRow); break; } default: { throw new CsvParsingException(); } } } }