예제 #1
0
        public static T ReadLine <T>(string csvLine, CultureInfo culture = null, CsvReadOptions <T> options = null)
            where T : class, new()
        {
            if (options == null)
            {
                options = new CsvReadOptions <T>();
            }

            culture = culture ?? DefaultCulture ?? CultureInfo.CurrentCulture;

            Regex regex = GetRegex(culture, options.RegexTimeout);

            Match m = regex.Match(csvLine);

            var columns = ColumnInfoCache <T> .Columns;

            return(ReadObject <T>(m,
                                  columns.Select(c => c.MemberEntry).ToList(),
                                  columns.Select(c => GetParser(culture, c, options.ParserFactory)).ToList()));
        }
예제 #2
0
 public static List <T> ReadBytes <T>(byte[] data, Encoding encoding = null, CultureInfo culture = null, int skipLines = 1, CsvReadOptions <T> options = null) where T : class, new()
 {
     using (MemoryStream ms = new MemoryStream(data))
         return(ReadStream <T>(ms, encoding, culture, skipLines, options).ToList());
 }
예제 #3
0
        public static IEnumerable <T> ReadStream <T>(Stream stream, Encoding encoding = null, CultureInfo culture = null, int skipLines = 1, CsvReadOptions <T> options = null) where T : class, new()
        {
            encoding = encoding ?? DefaultEncoding;
            culture  = culture ?? DefaultCulture ?? CultureInfo.CurrentCulture;
            if (options == null)
            {
                options = new CsvReadOptions <T>();
            }

            var columns = ColumnInfoCache <T> .Columns;
            var members = columns.Select(c => c.MemberEntry).ToList();
            var parsers = columns.Select(c => GetParser(culture, c, options.ParserFactory)).ToList();

            Regex regex = GetRegex(culture, options.RegexTimeout);

            if (options.AsumeSingleLine)
            {
                using (StreamReader sr = new StreamReader(stream, encoding))
                {
                    for (int i = 0; i < skipLines; i++)
                    {
                        sr.ReadLine();
                    }

                    var line = skipLines;
                    while (true)
                    {
                        string csvLine = sr.ReadLine();

                        if (csvLine == null)
                        {
                            yield break;
                        }

                        Match m = null;
                        T     t = null;
                        try
                        {
                            m = regex.Match(csvLine);
                            if (m.Length > 0)
                            {
                                t = ReadObject <T>(m, members, parsers);
                            }
                        }
                        catch (Exception e)
                        {
                            e.Data["row"] = line;

                            if (options.SkipError == null || !options.SkipError(e, m))
                            {
                                throw new ParseCsvException(e);
                            }
                        }

                        if (t != null)
                        {
                            yield return(t);
                        }
                    }
                }
            }
            else
            {
                using (StreamReader sr = new StreamReader(stream, encoding))
                {
                    string str = sr.ReadToEnd();

                    var matches = regex.Matches(str).Cast <Match>();

                    if (skipLines > 0)
                    {
                        matches = matches.Skip(skipLines);
                    }

                    int line = skipLines;
                    foreach (var m in matches)
                    {
                        if (m.Length > 0)
                        {
                            T t = null;
                            try
                            {
                                t = ReadObject <T>(m, members, parsers);
                            }
                            catch (Exception e)
                            {
                                e.Data["row"] = line;

                                if (options.SkipError == null || !options.SkipError(e, m))
                                {
                                    throw new ParseCsvException(e);
                                }
                            }
                            if (t != null)
                            {
                                yield return(t);
                            }
                        }
                        line++;
                    }
                }
            }
        }
예제 #4
0
        public static List <T> ReadFile <T>(string fileName, Encoding encoding = null, CultureInfo culture = null, int skipLines = 1, CsvReadOptions <T> options = null) where T : class, new()
        {
            encoding = encoding ?? DefaultEncoding;
            culture  = culture ?? DefaultCulture ?? CultureInfo.CurrentCulture;

            using (FileStream fs = File.OpenRead(fileName))
                return(ReadStream <T>(fs, encoding, culture, skipLines, options).ToList());
        }