public RowEnumerator(ReadOnlySpan <char> str, ParseRowFunc <T> parseRowFunc, char rowSeparator = '\n', char columnSeparator = ',') { rowsEnumerator = str.FastSplit(rowSeparator); this.parseRowFunc = parseRowFunc; this.columnSeparator = columnSeparator; Current = default; rowIndex = 0; }
public void Execute(ParseRowFunc parserFunc) { // validate parameters if (String.IsNullOrWhiteSpace(this.Shoulder)) throw new NullReferenceException("Shoulder must not be null"); if (String.IsNullOrWhiteSpace(this.CsvFilePath)) throw new NullReferenceException("CSVFilePath must not be null"); if (this.Authentication == null) throw new NullReferenceException("Authentication must not be null"); // open csv file for reading using (var csvFileStream = File.Open(this.CsvFilePath, FileMode.Open, FileAccess.Read)) { var csvFileReader = new StreamReader(csvFileStream, UTF8Encoding.UTF8, true); // open result file for writing var resultFile = this.ResultFile; if(resultFile == null) resultFile = String.Format("{0}.result.csv", Path.GetFileNameWithoutExtension(this.CsvFilePath)); using (var resultFileStream = File.Open(resultFile, FileMode.Append, FileAccess.Write)) { var resultFileWriter = new StreamWriter(resultFileStream, UTF8Encoding.UTF8); // read all lines for until line limit it hit int count = 0; while (!csvFileReader.EndOfStream && (count < this.Limit || this.Limit == LimitAll)) { string line = null; try { // parse the line line = csvFileReader.ReadLine(); var metadata = parserFunc(line); // make the request var request = new MintRequest(this.Shoulder, metadata, this.Authentication); var response = request.ExecuteRequest(); // ensure the response is valid if (String.IsNullOrWhiteSpace(response)) throw new Exception("No identifier returned"); // write response to result file resultFileWriter.WriteLine(String.Format("{0},{1}", line, response)); } catch (Exception e) { // something happened, track it in result file if (line == null) { resultFileWriter.WriteLine(String.Format(e.Message)); } else { resultFileWriter.WriteLine(String.Format("{0},{1}", line, e.Message)); } } finally { count++; } } csvFileReader.Close(); resultFileWriter.Close(); } } }
public static RowEnumerator <T> ParseCsv <T>(this ReadOnlySpan <char> csvSpan, ParseRowFunc <T> parseRowAction, char rowSeparator = '\n', char columnSeparator = ',') where T : new() { return(new RowEnumerator <T>(csvSpan, parseRowAction, rowSeparator, columnSeparator)); }
public static RowEnumerator <T> ParseCsv <T>(this string csvString, ParseRowFunc <T> parseRowAction, char rowSeparator = '\n', char columnSeparator = ',') where T : new() { return(new RowEnumerator <T>(csvString, parseRowAction, rowSeparator, columnSeparator)); }