/// <summary> /// Reads the lines from the reader. /// </summary> /// <param name="reader">The text reader to read the data from.</param> /// <param name="options">The optional options to use when reading.</param> public static IEnumerable<ICsvLine> Read(TextReader reader, CsvOptions options = null) { if (reader == null) throw new ArgumentNullException(nameof(reader)); return ReadImpl(reader, options); }
/// <summary> /// Reads the lines from the stream. /// </summary> /// <param name="stream">The stream to read the data from.</param> /// <param name="options">The optional options to use when reading.</param> public static IEnumerable<ICsvLine> ReadFromStream(Stream stream, CsvOptions options = null) { if (stream == null) throw new ArgumentNullException(nameof(stream)); return ReadFromStreamImpl(stream, options); }
/// <summary> /// Reads the lines from the csv string. /// </summary> /// <param name="csv">The csv string to read the data from.</param> /// <param name="options">The optional options to use when reading.</param> public static IEnumerable<ICsvLine> ReadFromText(string csv, CsvOptions options = null) { if (csv == null) throw new ArgumentNullException(nameof(csv)); return ReadFromTextImpl(csv, options); }
private static string[] CreateDefaultHeaders(string line, CsvOptions options) { var columnCount = options.Splitter.Matches(line); var headers = new string[columnCount.Count]; for (var i = 0; i < headers.Length; i++) headers[i] = $"Column{i + 1}"; return headers; }
private static string[] GetHeaders(string line, CsvOptions options) { return SplitLine(line, options); }
private CsvCell(object value, bool inner, CsvOptions options) { _options = options; _value = value; _inner = inner; }
internal static string Serialize(T obj, CsvOptions options) { return(new CsvRow <T>(obj, options).Serialize()); }
private static string[] GetHeaders(string line, CsvOptions options) { return(SplitLine(line, options)); }
public static IEnumerable <T> Read <T>(TextReader reader, CsvOptions options, CsvFilter filter = null) where T : class, new() { return(new CsvReader <T>(reader, options, filter).Read()); }
private static IEnumerable<ICsvLine> ReadFromTextImpl(string csv, CsvOptions options) { using (var reader = new StringReader(csv)) { foreach (var line in ReadImpl(reader, options)) yield return line; } }
private static IEnumerable<ICsvLine> ReadImpl(TextReader reader, CsvOptions options) { if (options == null) options = new CsvOptions(); string line; var index = 0; string[] headers = null; Dictionary<string, int> headerLookup = null; var initalized = false; while ((line = reader.ReadLine()) != null) { index++; if (index <= options.RowsToSkip || options.SkipRow?.Invoke(line, index) == true) continue; if (!initalized) { InitalizeOptions(line, options); var skipInitialLine = options.HeaderMode == HeaderMode.HeaderPresent; headers = skipInitialLine ? GetHeaders(line, options) : CreateDefaultHeaders(line, options); headerLookup = headers.Select((h, idx) => Tuple.Create(h, idx)).ToDictionary(h => h.Item1, h => h.Item2, options.Comparer); initalized = true; if (skipInitialLine) continue; } yield return new ReadLine(headers, headerLookup, index, line, options); } }
private static IEnumerable <ICsvLine> ReadImpl(TextReader reader, CsvOptions options) { if (options == null) { options = new CsvOptions(); } string line; var index = 0; string[] headers = null; Dictionary <string, int> headerLookup = null; var initalized = false; while ((line = reader.ReadLine()) != null) { index++; if (index <= options.RowsToSkip || options.SkipRow?.Invoke(line, index) == true) { continue; } if (!initalized) { InitializeOptions(line, options); var skipInitialLine = options.HeaderMode == HeaderMode.HeaderPresent; headers = skipInitialLine ? GetHeaders(line, options) : CreateDefaultHeaders(line, options); try { headerLookup = headers.Select((h, idx) => Tuple.Create(h, idx)).ToDictionary(h => h.Item1, h => h.Item2, options.Comparer); } catch (ArgumentException) { throw new InvalidOperationException("Duplicate headers detected in HeaderPresent mode. If you don't have a header you can set the HeaderMode to HeaderAbsent."); } var aliases = options.Aliases; if (aliases != null) { // NOTE: For each group we need at most 1 match (i.e. SingleOrDefault) foreach (var aliasGroup in aliases) { var groupIndex = -1; foreach (var alias in aliasGroup) { if (headerLookup.TryGetValue(alias, out var aliasIndex)) { if (groupIndex != -1) { throw new InvalidOperationException("Found multiple matches within alias group: " + string.Join(";", aliasGroup)); } groupIndex = aliasIndex; } } if (groupIndex != -1) { foreach (var alias in aliasGroup) { headerLookup[alias] = groupIndex; } } } } initalized = true; if (skipInitialLine) { continue; } } var record = new ReadLine(headers, headerLookup, index, line, options); if (options.AllowNewLineInEnclosedFieldValues) { while (record.RawSplitLine.Any(f => IsUnterminatedQuotedValue(f, options))) { var nextLine = reader.ReadLine(); if (nextLine == null) { break; } line += options.NewLine + nextLine; record = new ReadLine(headers, headerLookup, index, line, options); } } yield return(record); } }
private static IEnumerable<ICsvLine> ReadFromStreamImpl(Stream stream, CsvOptions options) { using (var reader = new StreamReader(stream)) { foreach (var line in ReadImpl(reader, options)) yield return line; } }
internal static T Deserialize(string row, PropertyInfo[] headers, CsvOptions options) { return(new CsvRow <T>(options).Deserialize(row, headers)); }
internal static PropertyInfo[] DeserializeHeaders(string[] headerNames, CsvOptions options) { return(new CsvRow <T>(options).DeserializeHeaders(headerNames)); }
internal static PropertyInfo[] DeserializeHeaders(string row, CsvOptions options) { return(new CsvRow <T>(options).DeserializeHeader(row)); }
private static void InitalizeOptions(string line, CsvOptions options) { if (options.Separator == '\0') options.Separator = AutoDetectSeparator(line); Regex splitter; lock (syncRoot) { if (!splitterCache.TryGetValue(options.Separator, out splitter)) splitterCache[options.Separator] = splitter = new Regex(string.Format(@"(?>(?(IQ)(?(ESC).(?<-ESC>)|\\(?<ESC>))|(?!))|(?(IQ)\k<QUOTE>(?<-IQ>)|(?<QUOTE>"")(?<IQ>))|(?(IQ).|[^{0}]))+|^(?={0})|(?<={0})(?={0})|(?<={0})$", Regex.Escape(options.Separator.ToString())), (RegexOptions)8); } options.Splitter = splitter; }
private static string[] SplitLine(string line, CsvOptions options) { var matches = options.Splitter.Matches(line); var parts = new string[matches.Count]; for (var i = 0; i < matches.Count; i++) { var str = matches[i].Value; if (options.TrimData) str = str.Trim(); if (str.StartsWith("\"") && str.EndsWith("\"")) str = str.Substring(1, str.Length - 2); parts[i] = str; } return parts; }
private static IEnumerable <ICsvLine> ReadImpl(TextReader reader, CsvOptions options) { if (options == null) { options = new CsvOptions(); } string line; var index = 0; string[] headers = null; Dictionary <string, int> headerLookup = null; var initalized = false; while ((line = reader.ReadLine()) != null) { index++; if (index <= options.RowsToSkip || options.SkipRow?.Invoke(line, index) == true) { continue; } if (!initalized) { InitializeOptions(line, options); var skipInitialLine = options.HeaderMode == HeaderMode.HeaderPresent; headers = skipInitialLine ? GetHeaders(line, options) : CreateDefaultHeaders(line, options); headerLookup = headers.Select((h, idx) => Tuple.Create(h, idx)).ToDictionary(h => h.Item1, h => h.Item2, options.Comparer); var aliases = options.Aliases; if (aliases != null) { // NOTE: For each group we need at most 1 match (i.e. SingleOrDefault) foreach (var aliasGroup in aliases) { var groupIndex = -1; foreach (var alias in aliasGroup) { if (headerLookup.TryGetValue(alias, out var aliasIndex)) { if (groupIndex != -1) { throw new InvalidOperationException("Found multiple matches within alias group: " + string.Join(";", aliasGroup)); } groupIndex = aliasIndex; } } if (groupIndex != -1) { foreach (var alias in aliasGroup) { headerLookup[alias] = groupIndex; } } } } initalized = true; if (skipInitialLine) { continue; } } // TODO: #11 might need to read another line if this one isn't ended yet (i.e. open quoted value) yield return(new ReadLine(headers, headerLookup, index, line, options)); } }
public ReadLine(string[] headers, Dictionary<string, int> headerLookup, int index, string raw, CsvOptions options) { this.headerLookup = headerLookup; this.options = options; Headers = headers; Raw = raw; Index = index; }
public ReadLine(string[] headers, Dictionary <string, int> headerLookup, int index, string raw, CsvOptions options) { this.headerLookup = headerLookup; this.options = options; Headers = headers; Raw = raw; Index = index; }
internal static string Serialize(object value, CsvOptions options) { return(Serialize(value, false, options)); }
public static IEnumerable <T> Read <T>(TextReader reader, CsvFilter filter = null) where T : class, new() { return(Read <T>(reader, CsvOptions.CreateDefault(), filter)); }
private CsvRow(T obj, CsvOptions options) { _options = options; Obj = obj; }
private CsvCell(CsvOptions options) { _options = options; _inner = false; }
internal static string SerializeHeader(CsvOptions options) { return(new CsvRow <T>(options).SerializeHeader()); }
private static string Serialize(object value, bool inner, CsvOptions options) { return(new CsvCell(value, inner, options).Serialize()); }
private CsvRow(CsvOptions options) { _options = options; Obj = new T(); //Not null for deserialization }