public static void ReadAll(DelimitedRecordReaderBenchmarkArguments args)
		{
			using (var reader = new DelimitedRecordReader(new StreamReader(args.Path, args.Encoding, true, args.BufferSize), args.BufferSize))
			{
				reader.AdvancedEscapingEnabled = args.AdvancedEscapingEnabled;
				reader.DoubleQuoteEscapingEnabled = args.DoubleQuoteEscapingEnabled;
				reader.SkipEmptyLines = args.SkipEmptyLines;
				reader.TrimWhiteSpaces = args.TrimWhiteSpaces;

				if (args.FieldIndex > -1)
				{
					reader.DynamicColumnCount = false;

					for (int i = 0; i < args.FieldIndex + 1; i++)
						reader.Columns.Add(new DelimitedRecordColumn(reader.GetDefaultColumnName(i)));
				}

				string s;

				while (reader.Read() != ReadResult.EndOfFile)
				{
					for (int i = 0; i < reader.Columns.Count - 1; i++)
						s = reader[i];
				}
			}
		}
        public static void ReadAll(DelimitedRecordReaderBenchmarkArguments args)
        {
            using (var reader = new DelimitedRecordReader(new StreamReader(args.Path, args.Encoding, true, args.BufferSize), args.BufferSize))
            {
                reader.AdvancedEscapingEnabled    = args.AdvancedEscapingEnabled;
                reader.DoubleQuoteEscapingEnabled = args.DoubleQuoteEscapingEnabled;
                reader.SkipEmptyLines             = args.SkipEmptyLines;
                reader.TrimWhiteSpaces            = args.TrimWhiteSpaces;

                if (args.FieldIndex > -1)
                {
                    reader.DynamicColumnCount = false;

                    for (int i = 0; i < args.FieldIndex + 1; i++)
                    {
                        reader.Columns.Add(new DelimitedRecordColumn(reader.GetDefaultColumnName(i)));
                    }
                }

                string s;

                while (reader.Read() != ReadResult.EndOfFile)
                {
                    for (int i = 0; i < reader.Columns.Count - 1; i++)
                    {
                        s = reader[i];
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void MissingDelimiterAfterQuotedFieldTest2()
        {
            const string Data = "\"111\",\"222\",\"333\"\n\"111\",\"222\"\"333\"";

            using (var csv = new DelimitedRecordReader(new StringReader(Data)))
            {
                csv.DynamicColumnCount = false;
                for (int i = 0; i < 3; i++)
                {
                    csv.Columns.Add(new DelimitedRecordColumn(csv.GetDefaultColumnName(i)));
                }

                csv.DoubleQuoteEscapingEnabled = false;

                var ex = Assert.Throws <MalformedRecordException>(
                    () =>
                {
                    while (csv.Read() == ReadResult.Success)
                    {
                    }
                });

                Assert.AreEqual(1, ex.CurrentRecordIndex);
                Assert.AreEqual(1, ex.CurrentColumnIndex);
            }
        }
Ejemplo n.º 4
0
        public void MissingFieldQuotedTest4()
        {
            const string Data = "a,b,c,d\n1,1,1,1\n2,\"2\",\n\"3\",3,3,3";

            using (var csv = new DelimitedRecordReader(new StringReader(Data), 11))
            {
                csv.DynamicColumnCount = false;
                for (int i = 0; i < 4; i++)
                {
                    csv.Columns.Add(new DelimitedRecordColumn(csv.GetDefaultColumnName(i)));
                }

                var ex = Assert.Throws <MissingRecordFieldException>(
                    () =>
                {
                    while (csv.Read() == ReadResult.Success)
                    {
                    }
                });

                Assert.AreEqual(2, ex.CurrentRecordIndex);
                Assert.AreEqual(3, ex.CurrentColumnIndex);
            }
        }
Ejemplo n.º 5
0
        private void CheckMissingFieldUnquoted(long recordCount, int columnCount, long badRecordIndex, int badColumnIndex, int bufferSize, MissingRecordFieldAction action)
        {
            // construct the csv data with template "00,01,02\n10,11,12\n...." and calculate expected error position

            long capacity = recordCount * (columnCount * 2 + columnCount - 1) + recordCount;

            Assert.IsTrue(capacity <= int.MaxValue);

            var sb = new StringBuilder((int)capacity);

            for (long i = 0; i < recordCount; i++)
            {
                int readColumnCount;

                if (i == badRecordIndex)
                {
                    readColumnCount = badColumnIndex;
                }
                else
                {
                    readColumnCount = columnCount;
                }

                for (int j = 0; j < readColumnCount; j++)
                {
                    sb.Append(i);
                    sb.Append(j);
                    sb.Append(DelimitedRecordReader.DefaultDelimiterCharacter);
                }

                sb.Length--;
                sb.Append('\n');
            }

            // test csv

            const string AssertMessage = "RecordIndex={0}; ColumnIndex={1}; Position={2}; Action={3}";

            using (var csv = new DelimitedRecordReader(new StringReader(sb.ToString()), bufferSize))
            {
                csv.DynamicColumnCount = false;
                for (int i = 0; i < columnCount; i++)
                {
                    csv.Columns.Add(new DelimitedRecordColumn(csv.GetDefaultColumnName(i)));
                }

                csv.MissingFieldAction = action;
                Assert.AreEqual(columnCount, csv.Columns.Count);

                try
                {
                    while (csv.Read() == ReadResult.Success)
                    {
                        Assert.AreEqual(columnCount, csv.Columns.Count);

                        for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
                        {
                            string s = csv[columnIndex];

                            if (csv.CurrentRecordIndex != badRecordIndex || columnIndex < badColumnIndex)
                            {
                                Assert.AreEqual(csv.CurrentRecordIndex.ToString() + columnIndex.ToString(), s);                                //, AssertMessage, csv.CurrentRecordIndex, columnIndex, -1, action);
                            }
                            else
                            {
                                switch (action)
                                {
                                case MissingRecordFieldAction.ReturnEmptyValue:
                                    Assert.AreEqual(string.Empty, s);                                            //, AssertMessage, csv.CurrentRecordIndex, columnIndex, -1, action);
                                    break;

                                case MissingRecordFieldAction.ReturnNullValue:
                                    Assert.IsNull(s);                                            //, AssertMessage, csv.CurrentRecordIndex, columnIndex, -1, action);
                                    break;

                                case MissingRecordFieldAction.HandleAsParseError:
                                    throw new AssertionException(string.Format("Failed to throw HandleAsParseError. - " + AssertMessage, csv.CurrentRecordIndex, columnIndex, -1, action));

                                default:
                                    throw new AssertionException(string.Format("'{0}' is not handled by this test.", action));
                                }
                            }
                        }
                    }
                }
                catch (MissingRecordFieldException ex)
                {
                    Assert.AreEqual(badRecordIndex, ex.CurrentRecordIndex, AssertMessage, ex.CurrentRecordIndex, ex.CurrentColumnIndex, ex.BufferPosition, action);
                    Assert.IsTrue(ex.CurrentColumnIndex >= badColumnIndex, AssertMessage, ex.CurrentRecordIndex, ex.CurrentColumnIndex, ex.BufferPosition, action);
                }
            }
        }