Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CsvParser"/> class.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="configuration">The configuration.</param>
        public CsvParser(TextReader reader, CsvConfiguration configuration)
        {
            this.reader   = reader;
            Configuration = configuration;
            Context       = new CsvContext(this);

            allowComments      = configuration.AllowComments;
            badDataFound       = configuration.BadDataFound;
            bufferSize         = configuration.BufferSize;
            cacheFields        = configuration.CacheFields;
            comment            = configuration.Comment;
            countBytes         = configuration.CountBytes;
            delimiter          = configuration.Delimiter;
            delimiterFirstChar = configuration.Delimiter[0];
            encoding           = configuration.Encoding;
            escape             = configuration.Escape;
            ignoreBlankLines   = configuration.IgnoreBlankLines;
            isNewLineSet       = configuration.IsNewLineSet;
            leaveOpen          = configuration.LeaveOpen;
            lineBreakInQuotedFieldIsBadData = configuration.LineBreakInQuotedFieldIsBadData;
            newLine                = configuration.NewLine;
            newLineFirstChar       = configuration.NewLine[0];
            mode                   = configuration.Mode;
            processFieldBufferSize = 1024;
            quote                  = configuration.Quote;
            whiteSpaceChars        = configuration.WhiteSpaceChars;
            trimOptions            = configuration.TrimOptions;

            buffer = ArrayPool <char> .Shared.Rent(bufferSize);

            processFieldBuffer = ArrayPool <char> .Shared.Rent(processFieldBufferSize);

            fields = new Field[128];
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CsvParser"/> class.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="configuration">The configuration.</param>
        public CsvParser(TextReader reader, IParserConfiguration configuration)
        {
            configuration.Validate();

            this.reader        = reader;
            this.configuration = configuration;
            Context            = new CsvContext(this);

            allowComments      = configuration.AllowComments;
            badDataFound       = configuration.BadDataFound;
            bufferSize         = configuration.BufferSize;
            cacheFields        = configuration.CacheFields;
            comment            = configuration.Comment;
            countBytes         = configuration.CountBytes;
            delimiter          = configuration.Delimiter;
            delimiterFirstChar = configuration.Delimiter[0];
            delimiterValues    = configuration.DetectDelimiterValues;
            detectDelimiter    = configuration.DetectDelimiter;
            encoding           = configuration.Encoding;
            escape             = configuration.Escape;
            ignoreBlankLines   = configuration.IgnoreBlankLines;
            isNewLineSet       = configuration.IsNewLineSet;
            leaveOpen          = configuration.LeaveOpen;
            lineBreakInQuotedFieldIsBadData = configuration.LineBreakInQuotedFieldIsBadData;
            newLine                = configuration.NewLine;
            newLineFirstChar       = configuration.NewLine[0];
            mode                   = configuration.Mode;
            processFieldBufferSize = configuration.ProcessFieldBufferSize;
            quote                  = configuration.Quote;
            whiteSpaceChars        = configuration.WhiteSpaceChars;
            trimOptions            = configuration.TrimOptions;

            buffer             = new char[bufferSize];
            processFieldBuffer = new char[processFieldBufferSize];
            fields             = new Field[128];
            processedFields    = new string[128];
        }
        public void Constructor_AllArguments_SetsProperites()
        {
            BadDataFound       badDataFound        = (context) => { };
            IComparer <string> dynamicPropertySort = Comparer <string> .Create((x, y) => x.CompareTo(y));

            GetConstructor           getConstructor           = (type) => type.GetConstructor(null);
            GetDynamicPropertyName   getDynamicPropertyName   = (context, field) => string.Empty;
            HeaderValidated          headerValidated          = (invalidHeaders, context) => { };
            MissingFieldFound        missingFieldFound        = (headerNames, index, context) => { };
            PrepareHeaderForMatch    prepareHeaderForMatch    = (header, fieldIndex) => header;
            ReadingExceptionOccurred readingExceptionOccurred = (ex) => true;
            ReferenceHeaderPrefix    referenceHeaderPrefix    = (type, memberName) => string.Empty;
            ShouldQuote      shouldQuote      = (_, _, _) => true;
            ShouldSkipRecord shouldSkipRecord = (record) => true;
            ShouldUseConstructorParameters shouldUseConstructorParameters = (parameterType) => true;

            var config = new CsvConfiguration(CultureInfo.CurrentCulture,
                                              allowComments: true,
                                              badDataFound: badDataFound,
                                              bufferSize: 1,
                                              cacheFields: true,
                                              comment: '^',
                                              countBytes: true,
                                              delimiter: ":",
                                              detectColumnCountChanges: true,
                                              dynamicPropertySort: dynamicPropertySort,
                                              encoding: Encoding.ASCII,
                                              escape: '\\',
                                              getConstructor: getConstructor,
                                              getDynamicPropertyName: getDynamicPropertyName,
                                              hasHeaderRecord: false,
                                              headerValidated: headerValidated,
                                              ignoreBlankLines: false,
                                              ignoreReferences: true,
                                              includePrivateMembers: true,
                                              injectionCharacters: new char[] { '*' },
                                              injectionEscapeCharacter: '`',
                                              leaveOpen: true,
                                              lineBreakInQuotedFieldIsBadData: true,
                                              memberTypes: MemberTypes.Fields,
                                              missingFieldFound: missingFieldFound,
                                              mode: CsvMode.Escape,
                                              newLine: "\n",
                                              prepareHeaderForMatch: prepareHeaderForMatch,
                                              quote: '\'',
                                              readingExceptionOccurred: readingExceptionOccurred,
                                              referenceHeaderPrefix: referenceHeaderPrefix,
                                              sanitizeForInjection: true,
                                              shouldQuote: shouldQuote,
                                              shouldSkipRecord: shouldSkipRecord,
                                              shouldUseConstructorParameters: shouldUseConstructorParameters,
                                              trimOptions: TrimOptions.InsideQuotes,
                                              useNewObjectForNullReferenceMembers: false,
                                              whiteSpaceChars: new char[] { '~' }
                                              );

            Assert.IsTrue(config.AllowComments);
            Assert.AreEqual(badDataFound, config.BadDataFound);
            Assert.AreEqual(1, config.BufferSize);
            Assert.IsTrue(config.CacheFields);
            Assert.AreEqual('^', config.Comment);
            Assert.IsTrue(config.CountBytes);
            Assert.AreEqual(CultureInfo.CurrentCulture, config.CultureInfo);
            Assert.AreEqual(":", config.Delimiter);
            Assert.IsTrue(config.DetectColumnCountChanges);
            Assert.AreEqual(dynamicPropertySort, config.DynamicPropertySort);
            Assert.AreEqual(Encoding.ASCII, config.Encoding);
            Assert.AreEqual('\\', config.Escape);
            Assert.AreEqual(getConstructor, config.GetConstructor);
            Assert.AreEqual(getDynamicPropertyName, config.GetDynamicPropertyName);
            Assert.IsFalse(config.HasHeaderRecord);
            Assert.AreEqual(headerValidated, config.HeaderValidated);
            Assert.IsFalse(config.IgnoreBlankLines);
            Assert.IsTrue(config.IgnoreReferences);
            Assert.IsTrue(config.IncludePrivateMembers);
            Assert.AreEqual('*', config.InjectionCharacters[0]);
            Assert.AreEqual('`', config.InjectionEscapeCharacter);
            Assert.IsTrue(config.IsNewLineSet);
            Assert.IsTrue(config.LeaveOpen);
            Assert.IsTrue(config.LineBreakInQuotedFieldIsBadData);
            Assert.AreEqual(MemberTypes.Fields, config.MemberTypes);
            Assert.AreEqual(missingFieldFound, config.MissingFieldFound);
            Assert.AreEqual(CsvMode.Escape, config.Mode);
            Assert.AreEqual("\n", config.NewLine);
            Assert.AreEqual(prepareHeaderForMatch, config.PrepareHeaderForMatch);
            Assert.AreEqual('\'', config.Quote);
            Assert.AreEqual(readingExceptionOccurred, config.ReadingExceptionOccurred);
            Assert.AreEqual(referenceHeaderPrefix, config.ReferenceHeaderPrefix);
            Assert.IsTrue(config.SanitizeForInjection);
            Assert.AreEqual(shouldQuote, config.ShouldQuote);
            Assert.AreEqual(shouldSkipRecord, config.ShouldSkipRecord);
            Assert.AreEqual(shouldUseConstructorParameters, config.ShouldUseConstructorParameters);
            Assert.AreEqual(TrimOptions.InsideQuotes, config.TrimOptions);
            Assert.IsFalse(config.UseNewObjectForNullReferenceMembers);
            Assert.AreEqual('~', config.WhiteSpaceChars[0]);
        }