コード例 #1
0
        public static IEnumerable <T> ReadStream <T>(Stream stream, Encoding encoding = null, int skipLines = 1, CultureInfo culture = null, TsvReadOptions <T> options = null) where T : class, new()
        {
            encoding = encoding ?? DefaultEncoding;
            if (options == null)
            {
                options = new TsvReadOptions <T>();
            }

            var defCulture = culture ?? DefaultCulture;

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


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

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

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

                    T t = null;
                    try
                    {
                        t = ReadObject <T>(tsvLine, members, parsers);
                    }
                    catch (Exception e)
                    {
                        e.Data["row"] = line;

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

                    if (t != null)
                    {
                        yield return(t);
                    }
                }
            }
        }
コード例 #2
0
        public static T ReadLine <T>(string tsvLine, TsvReadOptions <T> options = null)
            where T : class, new()
        {
            if (options == null)
            {
                options = new TsvReadOptions <T>();
            }

            var columns = ColumnInfoCache <T> .Columns;

            return(ReadObject <T>(tsvLine,
                                  columns.Select(c => c.MemberEntry).ToList(),
                                  columns.Select(c => GetParser(c, options.ParserFactory)).ToList()));
        }
コード例 #3
0
 public static List <T> ReadBytes <T>(byte[] data, Encoding encoding = null, int skipLines = 1, TsvReadOptions <T> options = null) where T : class, new()
 {
     using (MemoryStream ms = new MemoryStream(data))
         return(ReadStream <T>(ms, encoding, skipLines, options).ToList());
 }
コード例 #4
0
        public static List <T> ReadFile <T>(string fileName, Encoding encoding = null, int skipLines = 1, TsvReadOptions <T> options = null) where T : class, new()
        {
            encoding = encoding ?? DefaultEncoding;

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