Example #1
0
        /// <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);
        }
Example #2
0
        /// <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);
        }
Example #3
0
        /// <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);
        }
Example #4
0
        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;
        }
Example #5
0
 private static string[] GetHeaders(string line, CsvOptions options)
 {
     return SplitLine(line, options);
 }
Example #6
0
 private CsvCell(object value, bool inner, CsvOptions options)
 {
     _options = options;
     _value   = value;
     _inner   = inner;
 }
Example #7
0
 internal static string Serialize(T obj, CsvOptions options)
 {
     return(new CsvRow <T>(obj, options).Serialize());
 }
Example #8
0
 private static string[] GetHeaders(string line, CsvOptions options)
 {
     return(SplitLine(line, options));
 }
Example #9
0
 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());
 }
Example #10
0
 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;
     }
 }
Example #11
0
        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);
            }
        }
Example #12
0
        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);
            }
        }
Example #13
0
 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;
     }
 }
Example #14
0
 internal static T Deserialize(string row, PropertyInfo[] headers, CsvOptions options)
 {
     return(new CsvRow <T>(options).Deserialize(row, headers));
 }
Example #15
0
 internal static PropertyInfo[] DeserializeHeaders(string[] headerNames, CsvOptions options)
 {
     return(new CsvRow <T>(options).DeserializeHeaders(headerNames));
 }
Example #16
0
 internal static PropertyInfo[] DeserializeHeaders(string row, CsvOptions options)
 {
     return(new CsvRow <T>(options).DeserializeHeader(row));
 }
Example #17
0
        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;
        }
Example #18
0
        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;
        }
Example #19
0
        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));
            }
        }
Example #20
0
 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;
 }
Example #21
0
 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;
 }
Example #22
0
 internal static string Serialize(object value, CsvOptions options)
 {
     return(Serialize(value, false, options));
 }
Example #23
0
 public static IEnumerable <T> Read <T>(TextReader reader, CsvFilter filter = null) where T : class, new()
 {
     return(Read <T>(reader, CsvOptions.CreateDefault(), filter));
 }
Example #24
0
 private CsvRow(T obj, CsvOptions options)
 {
     _options = options;
     Obj      = obj;
 }
Example #25
0
 private CsvCell(CsvOptions options)
 {
     _options = options;
     _inner   = false;
 }
Example #26
0
 internal static string SerializeHeader(CsvOptions options)
 {
     return(new CsvRow <T>(options).SerializeHeader());
 }
Example #27
0
 private static string Serialize(object value, bool inner, CsvOptions options)
 {
     return(new CsvCell(value, inner, options).Serialize());
 }
Example #28
0
 private CsvRow(CsvOptions options)
 {
     _options = options;
     Obj      = new T();        //Not null for deserialization
 }