Beispiel #1
0
        protected async Task <IList <string> > ReadHeaderAsync(XsvReader xsvReader)
        {
            var header = Settings.HeaderStrings ?? Enumerable.Empty <string>();

            if (Settings.HeaderExists)
            {
                header = await xsvReader.ReadXsvLineAsync(Settings.Delimiters);
            }
            return(header.ToList());
        }
Beispiel #2
0
        protected IList <string> ReadHeader(XsvReader xsvReader)
        {
            if (xsvReader == null)
            {
                throw new ArgumentNullException("xsvReader");
            }

            var header = Settings.HeaderStrings ?? new List <string>();

            if (Settings.HeaderExists)
            {
                header = xsvReader.ReadXsvLine(Settings.Delimiters).ToList();
            }
            return(header);
        }
 public void ReadXsvLineTest02()
 {
     var result = new List<IList<string>>();
     using (var reader = new XsvReader(new StringReader(testData02)))
     {
         while (!reader.EndOfData)
         {
             var row = reader.ReadXsvLine(new[] { " ", "\t" }).ToArray();
             result.Add(row);
         }
     }
     foreach (var row in result.Zip(expected02, (l, r) => new { expected = r, actual = l }))
     {
         Console.WriteLine(row.actual.ConcatWith("|") + "\\n");
         CollectionAssert.AreEqual((ICollection)row.expected, (ICollection)row.actual);
     }
 }
Beispiel #4
0
        public void Read(TextReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            using (var xsvReader = new XsvReader(reader, Settings.CommentToken))
            {
                var headers = ReadHeader(xsvReader);
                _columnHeaders = new XsvColumnHeaders(headers);
                var rows = xsvReader.ReadXsvToEnd(Settings.Delimiters);

                _items.Clear();
                foreach (var row in rows)
                {
                    _items.Add(CreateXsvRow(headers, row));
                }
            }
        }
Beispiel #5
0
        public async Task ReadAsync(TextReader reader, CancellationToken cancellationToken)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (cancellationToken == null)
            {
                throw new ArgumentNullException("cancellationToken");
            }

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            using (var xsvReader = new XsvReader(reader, Settings.CommentToken))
            {
                var headers = await ReadHeaderAsync(xsvReader);

                _columnHeaders = new XsvColumnHeaders(headers);

                _items.Clear();
                while (!xsvReader.EndOfData)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }

                    var row = await xsvReader.ReadXsvLineAsync(Settings.Delimiters);

                    _items.Add(CreateXsvRow(headers, row));
                }
            }
        }
Beispiel #6
0
 static Task ReadXsvAsync(XsvReader reader)
 {
     return Task.Factory.StartNew(() => { Console.WriteLine("Not Supported."); });
 }
Beispiel #7
0
        static IDisposable ReadXsvObservable(XsvReader reader)
        {
            var csv = new List<IList<string>>();

            var header = reader.ReadXsvLine(new[] { "," }).ToArray();
            csv.Add(header);

            return reader.ReadXsvAsObservable(new[] { "," })
                .Subscribe(row =>
                {
                    csv.Add(row);
                    Console.WriteLine("OnNext:" + System.Threading.Thread.CurrentThread.ManagedThreadId + " => " + row.ConcatWith(", "));
                },
            e => Console.WriteLine("OnError " + e.Message),
            () =>
            {
                Console.WriteLine("OnCompleted:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine("");
                foreach (var row in csv)
                {
                    Console.WriteLine(row.Select(s => s.MakeXsvField(new[] { "," })).ConcatWith(", "));
                }
            });

        }
Beispiel #8
0
 static async Task ReadXsvAsync(XsvReader reader)
 {
     var currentId = System.Threading.Thread.CurrentThread.ManagedThreadId;
     Console.WriteLine("start:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
     var result = await reader.ReadXsvToEndAsync(new[] { "," });
     Console.WriteLine("end:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
     Console.WriteLine("current:" + currentId);
     foreach (var row in result)
     {
         Console.WriteLine(row.Select(s => s.MakeXsvField(new[] { "," })).ConcatWith(", "));
     }
 }
Beispiel #9
0
        static void Main(string[] args)
        {

            while (true)
            {
                Console.WriteLine("Press any key.");
                Console.WriteLine("[r]- ReadXsvAsync");
                Console.WriteLine("[o]- ReadXsvObservable");
                Console.WriteLine("[a]- TextFieldParser");
                Console.WriteLine("[p]- Perfomance");
                Console.WriteLine("[q]- Perfomance2");
                Console.WriteLine("[e]- Exit");
                var key = Console.ReadKey(false);
                switch (key.KeyChar)
                {
                    case 'o':
                        using (var reader = new XsvReader(new StreamReader(@".\ModelShips.txt")))
                        //using (var reader = new XsvReader(new StringReader(Properties.Resources.ModelShips)))
                        {
                            var id = System.Threading.Thread.CurrentThread.ManagedThreadId;
                            Console.WriteLine("start:" + id);
                            var disposable = ReadXsvObservable(reader);
                            Console.ReadKey(false);
                            Console.WriteLine("current:" + id);
                            disposable.Dispose();
                            Console.WriteLine("Disposed");
                            
                        }
                        break;
                    case 'r':
                        //using (var reader = new XsvReader(new StreamReader(@".\ModelShips.txt")))
                        using (var reader = new XsvReader(new StringReader(Properties.Resources.ModelShips)))
                        {
                            ReadXsvAsync(reader).Wait();
                        }
                        break;
                    case 'a':
                        var data = "No., Name, Price, Comment" + Environment.NewLine
                                 + "001, りんご, 98円, 青森産" + Environment.NewLine
                                 + "002, バナナ, 120円, \"    とっても!\r\nお,い,し,い,よ!\"" + Environment.NewLine
                                 + "" + Environment.NewLine
                                 + "004, \"うまい棒\"\"めんたい\"\"\", 10円," + Environment.NewLine
                                 + "005, バナメイ海老, 800円, \"300g\r\n\r\nエビチリに\"";
                        Console.WriteLine("<< Source >>");
                        Console.WriteLine(data);
                        Console.WriteLine("");
                        Console.WriteLine("<< XsvReader.Parse() >>");
                        foreach (var row in Parse(data))
                        {
                            Console.WriteLine(row.ConcatWith(";") + "<n>");
                        }
                        Console.WriteLine("");                        
                        Console.WriteLine("<< TextFieldParser (TrimWhiteSpace=true) >>");
                        foreach (var row in ReadFields(data,trimWS:true))
                        {
                            Console.WriteLine(row.ConcatWith(";") + "<n>");
                        }
                        Console.WriteLine("");
                        Console.WriteLine("<< TextFieldParser (TrimWhiteSpace=false) >>");
                        foreach (var row in ReadFields(data,trimWS:false))
                        {
                            Console.WriteLine(row.ConcatWith(";") + "<n>");
                        }
                        break;
                    case 'p':
                        Console.WriteLine("");
                        var delimiters = new[] { "," };
                        var sw = System.Diagnostics.Stopwatch.StartNew();
                        for (int i = 0; i < 1000; i++)
                        {
                            using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(Properties.Resources.ModelShips)))
                            {
                                parser.Delimiters = delimiters;
                                parser.TrimWhiteSpace = true;
                                while (!parser.EndOfData)
                                {
                                    parser.ReadFields();
                                }
                            }
                        }
                        sw.Stop();
                        Console.WriteLine(string.Format("TextFieldParser: {0} ms", sw.ElapsedMilliseconds));
                        sw.Reset();
                        sw.Start();
                        for (int i = 0; i < 1000; i++)
                        {
                            using (var reader = new XsvReader(new StringReader(Properties.Resources.ModelShips)))
                            {
                                while (!reader.EndOfData)
                                {
                                    reader.ReadXsvLine(delimiters).ToArray();
                                }
                            }
                        }
                        sw.Stop();
                        Console.WriteLine(string.Format("XsvReader: {0} ms", sw.ElapsedMilliseconds));

                        break;
#if net45
                    case 'q':
                        ReadAsyncTest().Wait();
                        break;
#endif
                    case 'e':
                        return;
                    
                }
                Console.WriteLine("***-***-***-***");
            }
        }
Beispiel #10
0
        static async Task ReadAsyncTest() 
        {
            var sw = new System.Diagnostics.Stopwatch();
            var lines = String.Join(Environment.NewLine, Enumerable.Range(1, 10000)
                .Select(_ => Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString()));
            using (var reader = new XsvReader(new StringReader(lines)))
            {
                while (!reader.EndOfData)
                {
                    await reader.ReadLineAsync().ConfigureAwait(false);
                }
            }

            sw.Restart();
            using (var reader = new XsvReader(new StringReader(lines)))
            {
                while (!reader.EndOfData)
                {
                    await reader.ReadLineAsync().ConfigureAwait(false);
                }
            }
            sw.Stop();
            Console.WriteLine(string.Format("ReadlineAsync: {0} ms", sw.ElapsedMilliseconds));

            //sw.Restart();
            //using (var reader = new XsvReader(new StringReader(lines)))
            //{
            //    while (!reader.EndOfData)
            //    {
            //        await reader.ReadLineAsync2().ConfigureAwait(false);
            //    }
            //}
            //sw.Stop();
            //Console.WriteLine(string.Format("ReadlineAsync2: {0} ms", sw.ElapsedMilliseconds));
        }
        public void ReadXsvObservableTest()
        {
            var csv = new List<IList<string>>();

            using (var reader = new XsvReader(new StringReader(testData01)))
            {
                csv.Add(reader.ReadXsvLine(new[] { "," }).ToArray());
#if net40
                IDisposable disposable = reader.ReadXsvAsObservable(new[] { "," }, System.Reactive.Concurrency.ThreadPoolScheduler.Instance).Subscribe(row =>
#endif
#if net45
                IDisposable disposable = reader.ReadXsvAsObservable(new[] { "," }).Subscribe(row =>
#endif
                {
                    csv.Add(row);
                    Console.WriteLine("OnNext");
                    Console.WriteLine(row.ConcatWith(", "));
                },
                e => Console.WriteLine("OnError " + e.Message),
                () =>
                {
                    Console.WriteLine("OnCompleted.");
                    foreach (var row in csv.Zip(expected01, (l, r) => new { expected = r, actual = l }))
                    {
                        Console.WriteLine(row.actual.ConcatWith("|") + "\\n");
                        CollectionAssert.AreEqual((ICollection)row.expected, (ICollection)row.actual);
                    }
                });
            }
        }
        public async Task ReadXsvToEndAsyncTest()
        {
            using (var reader2 = new XsvReader(new StringReader(testData01)))
            {
                var rows = await reader2.ReadXsvToEndAsync(new[] { "," });

                foreach (var row in rows.Zip(expected01, (l, r) => new { expected = r, actual = l }))
                {
                    Console.WriteLine(row.actual.ConcatWith("|") + "\\n");
                    CollectionAssert.AreEqual((ICollection)row.expected, (ICollection)row.actual);
                }
            }
        }
        public void ReadAsObservable()
        {
            Cancel();

            var reader = new XsvReader(new System.IO.StreamReader(@".\ModelShips.txt"));
            var header = reader.ReadXsvLine(new[] { "," }).ToList();

            disposable = reader.ReadXsvAsObservable(new[] { "," })
                .Where(row => !row.First().StartsWith(";"))
                .Select(row => ModelShip.FromCsvRecord(header, row))
                //.Where(row => row.Price >= 2500)
                //.Where(row => row.ClassOfShip == "戦艦")
                .ObserveOn(System.Threading.SynchronizationContext.Current)
                .Subscribe(
                    row => 
                    {
                        System.Diagnostics.Debug.WriteLine("OnNext");
                        this.Rows.Add(row); 
                    },
                    error => 
                    {
                        System.Diagnostics.Debug.WriteLine("OnError:" + error);
                        reader.Dispose();
                    },
                    () =>
                    {
                        System.Diagnostics.Debug.WriteLine("OnCompleted");
                        reader.Dispose();
                    });
        }