/// <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]; }
/// <summary> /// Initializes a new instance of the <see cref="CsvWriter"/> class. /// </summary> /// <param name="writer">The writer.</param> /// <param name="configuration">The configuration.</param> public CsvWriter(TextWriter writer, CsvConfiguration configuration) { configuration.Validate(); this.writer = writer; Configuration = configuration; context = new CsvContext(this); typeConverterCache = context.TypeConverterCache; recordManager = new Lazy <RecordManager>(() => ObjectResolver.Current.Resolve <RecordManager>(this)); comment = configuration.Comment; bufferSize = configuration.BufferSize; delimiter = configuration.Delimiter; cultureInfo = configuration.CultureInfo; dynamicPropertySort = configuration.DynamicPropertySort; escapeDelimiterString = new string(configuration.Delimiter.SelectMany(c => new[] { configuration.Escape, c }).ToArray()); escapeNewlineString = new string(configuration.NewLine.SelectMany(c => new[] { configuration.Escape, c }).ToArray()); escapeQuoteString = new string(new[] { configuration.Escape, configuration.Quote }); hasHeaderRecord = configuration.HasHeaderRecord; includePrivateMembers = configuration.IncludePrivateMembers; injectionCharacters = configuration.InjectionCharacters; injectionEscapeCharacter = configuration.InjectionEscapeCharacter; leaveOpen = configuration.LeaveOpen; mode = configuration.Mode; newLine = configuration.NewLine; quote = configuration.Quote; quoteString = configuration.Quote.ToString(); sanitizeForInjection = configuration.SanitizeForInjection; shouldQuote = configuration.ShouldQuote; trimOptions = configuration.TrimOptions; buffer = new char[bufferSize]; }
/// <summary> /// Initializes a new instance of the <see cref="CsvWriter"/> class. /// </summary> /// <param name="writer">The writer.</param> /// <param name="configuration">The configuration.</param> public CsvWriter(TextWriter writer, CsvConfiguration configuration) { this.writer = writer; Configuration = configuration; context = new CsvContext(this); typeConverterCache = context.TypeConverterCache; recordManager = new Lazy <RecordManager>(() => ObjectResolver.Current.Resolve <RecordManager>(this)); comment = configuration.Comment; bufferSize = configuration.BufferSize; delimiter = configuration.Delimiter; cultureInfo = configuration.CultureInfo; doubleQuoteString = configuration.DoubleQuoteString; dynamicPropertySort = configuration.DynamicPropertySort; hasHeaderRecord = configuration.HasHeaderRecord; includePrivateMembers = configuration.IncludePrivateMembers; injectionCharacters = configuration.InjectionCharacters; injectionEscapeCharacter = configuration.InjectionEscapeCharacter; leaveOpen = configuration.LeaveOpen; newLine = configuration.NewLine; quote = configuration.Quote; quoteString = configuration.QuoteString; sanitizeForInjection = configuration.SanitizeForInjection; shouldQuote = configuration.ShouldQuote; trimOptions = configuration.TrimOptions; buffer = new char[bufferSize]; }
/// <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]; }
/// <summary> /// Initializes a new instance of the <see cref="BadDataException"/> class. /// </summary> /// <param name="context">The reading context.</param> public BadDataException(CsvContext context) : base(context) { }
/// <summary> /// Initializes a new instance of the <see cref="MissingFieldException"/> class /// with a specified error message and a reference to the inner exception that /// is the cause of this exception. /// </summary> /// <param name="context">The reading context.</param> /// <param name="message">The error message that explains the reason for the exception.</param> /// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param> public MissingFieldException(CsvContext context, string message, Exception innerException) : base(context, message, innerException) { }
/// <summary> /// Initializes a new instance of the <see cref="MissingFieldException"/> class /// with a specified error message. /// </summary> /// <param name="context">The reading context.</param> /// <param name="message">The message that describes the error.</param> public MissingFieldException(CsvContext context, string message) : base(context, message) { }
private static string AddDetails(string message, CsvContext context) { var indent = new string(' ', 3); var details = new StringBuilder(); if (context.Reader != null) { details.AppendLine($"{nameof(IReader)} state:"); details.AppendLine($"{indent}{nameof(IReader.ColumnCount)}: {context.Reader.ColumnCount}"); details.AppendLine($"{indent}{nameof(IReader.CurrentIndex)}: {context.Reader.CurrentIndex}"); try { var record = new StringBuilder(); if (context.Reader.HeaderRecord != null) { record.Append("[\""); record.Append(string.Join("\",\"", context.Reader.HeaderRecord)); record.Append("\"]"); } details.AppendLine($"{indent}{nameof(IReader.HeaderRecord)}:{Environment.NewLine}{record}"); } catch { } } if (context.Parser != null) { details.AppendLine($"{nameof(IParser)} state:"); details.AppendLine($"{indent}{nameof(IParser.ByteCount)}: {context.Parser.ByteCount}"); details.AppendLine($"{indent}{nameof(IParser.CharCount)}: {context.Parser.CharCount}"); details.AppendLine($"{indent}{nameof(IParser.Row)}: {context.Parser.Row}"); details.AppendLine($"{indent}{nameof(IParser.RawRow)}: {context.Parser.RawRow}"); details.AppendLine($"{indent}{nameof(IParser.Count)}: {context.Parser.Count}"); try { var rawRecord = context.Configuration.ExceptionMessagesContainRawData ? context.Parser.RawRecord : $"Hidden because {nameof(IParserConfiguration.ExceptionMessagesContainRawData)} is false."; details.AppendLine($"{indent}{nameof(IParser.RawRecord)}:{Environment.NewLine}{rawRecord}"); } catch { } } if (context.Writer != null) { details.AppendLine($"{nameof(IWriter)} state:"); details.AppendLine($"{indent}{nameof(IWriter.Row)}: {context.Writer.Row}"); details.AppendLine($"{indent}{nameof(IWriter.Index)}: {context.Writer.Index}"); var record = new StringBuilder(); if (context.Writer.HeaderRecord != null) { record.Append("["); if (context.Writer.HeaderRecord.Length > 0) { record.Append("\""); record.Append(string.Join("\",\"", context.Writer.HeaderRecord)); record.Append("\""); } record.Append("]"); } details.AppendLine($"{indent}{nameof(IWriter.HeaderRecord)}:{Environment.NewLine}{context.Writer.Row}"); } return($"{message}{Environment.NewLine}{details}"); }
/// <summary> /// Creates a new instance of BadDataFoundArgs. /// </summary> /// <param name="field">The full field unedited.</param> /// <param name="rawRecord">The full row unedited.</param> /// <param name="context">The context.</param> public BadDataFoundArgs(string field, string rawRecord, CsvContext context) { Field = field; RawRecord = rawRecord; Context = context; }
/// <summary> /// Initializes a new instance of the <see cref="WriterException"/> class /// with a specified error message and a reference to the inner exception that /// is the cause of this exception. /// </summary> /// <param name="context">The writing context.</param> /// <param name="message">The error message that explains the reason for the exception.</param> /// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param> public WriterException(CsvContext context, string message, Exception innerException) : base(context, message, innerException) { }
/// <summary> /// Initializes a new instance of the <see cref="WriterException"/> class. /// </summary> /// <param name="context">The writing context.</param> public WriterException(CsvContext context) : base(context) { }
/// <summary> /// Creates a new instance of GetDynamicPropertyNameArgs. /// </summary> /// <param name="fieldIndex">The field index.</param> /// <param name="context">The context.</param> public GetDynamicPropertyNameArgs(int fieldIndex, CsvContext context) { FieldIndex = fieldIndex; Context = context; }
/// <summary> /// Initializes a new instance of the <see cref="ValidationException"/> class /// with a specified error message and a reference to the inner exception that /// is the cause of this exception. /// </summary> /// <param name="context">The reading context.</param> /// <param name="invalidHeaders">The invalid headers.</param> /// <param name="message">The error message that explains the reason for the exception.</param> /// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param> public HeaderValidationException(CsvContext context, InvalidHeader[] invalidHeaders, string message, Exception innerException) : base(context, message, innerException) { InvalidHeaders = invalidHeaders; }
/// <summary> /// Initializes a new instance of the <see cref="ValidationException"/> class. /// </summary> /// <param name="context">The reading context.</param> /// <param name="invalidHeaders">The invalid headers.</param> public HeaderValidationException(CsvContext context, InvalidHeader[] invalidHeaders) : base(context) { InvalidHeaders = invalidHeaders; }
/// <summary> /// Initializes a new instance of the <see cref="ValidationException"/> class /// with a specified error message and a reference to the inner exception that /// is the cause of this exception. /// </summary> /// <param name="context">The reading context.</param> /// <param name="field">The field that failed validation.</param> /// <param name="message">The error message that explains the reason for the exception.</param> /// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param> public FieldValidationException(CsvContext context, string field, string message, Exception innerException) : base(context, message, innerException) { Field = field; }
/// <summary> /// Initializes a new instance of the <see cref="ValidationException"/> class. /// </summary> /// <param name="context">The reading context.</param> /// <param name="field">The field that failed validation.</param> public FieldValidationException(CsvContext context, string field) : base(context) { Field = field; }
/// <summary> /// Initializes a new instance of the <see cref="BadDataException"/> class /// with a specified error message. /// </summary> /// <param name="context">The reading context.</param> /// <param name="message">The message that describes the error.</param> public BadDataException(CsvContext context, string message) : base(context, message) { }
/// <summary> /// Initializes a new instance of the <see cref="ReaderException"/> class. /// </summary> /// <param name="context">The reading context.</param> public ReaderException(CsvContext context) : base(context) { }
/// <summary> /// Initializes a new instance of the <see cref="ValidationException"/> class. /// </summary> /// <param name="context">The reading context.</param> public ValidationException(CsvContext context) : base(context) { }
/// <summary> /// Initializes a new instance of the <see cref="WriterException"/> class /// with a specified error message. /// </summary> /// <param name="context">The writing context.</param> /// <param name="message">The message that describes the error.</param> public WriterException(CsvContext context, string message) : base(context, message) { }
/// <summary> /// Initializes a new instance of the <see cref="ValidationException"/> class /// with a specified error message. /// </summary> /// <param name="context">The reading context.</param> /// <param name="message">The message that describes the error.</param> public ValidationException(CsvContext context, string message) : base(context, message) { }
/// <summary> /// Creates a new instance of HeaderValidatedArgs. /// </summary> /// <param name="invalidHeaders">The invalid headers.</param> /// <param name="context">The context.</param> public HeaderValidatedArgs(InvalidHeader[] invalidHeaders, CsvContext context) { InvalidHeaders = invalidHeaders; Context = context; }
/// <summary> /// Initializes a new instance of the <see cref="CsvHelperException"/> class. /// </summary> public CsvHelperException(CsvContext context) { this.context = context; }
/// <summary> /// Creates a new instance of MissingFieldFoundArgs. /// </summary> /// <param name="headerNames">The header names.</param> /// <param name="index">The index.</param> /// <param name="context">The context.</param> public MissingFieldFoundArgs(string[] headerNames, int index, CsvContext context) { HeaderNames = headerNames; Index = index; Context = context; }
/// <summary> /// Initializes a new instance of the <see cref="CsvHelperException"/> class /// with a specified error message and a reference to the inner exception that /// is the cause of this exception. /// </summary> /// <param name="context">The context.</param> /// <param name="message">The error message that explains the reason for the exception.</param> /// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param> public CsvHelperException(CsvContext context, string message, Exception innerException) : base(AddDetails(message, context), innerException) { this.context = context; }
/// <summary> /// Initializes a new instance of the <see cref="MissingFieldException"/> class. /// </summary> /// <param name="context">The reading context.</param> public MissingFieldException(CsvContext context) : base(context) { }
/// <summary> /// Initializes a new instance of the <see cref="ParserException"/> class. /// </summary> /// <param name="context">The reading context.</param> public ParserException(CsvContext context) : base(context) { }