1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
// Copyright 2009-2022 Josh Close
// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0.
// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0.
// https://github.com/JoshClose/CsvHelper
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using CsvHelper.Configuration.Attributes;
using CsvHelper.Delegates;
using CsvHelper.TypeConversion;
namespace CsvHelper.Configuration
{
/// <summary>
/// Configuration used for reading and writing CSV data.
/// </summary>
public record CsvConfiguration : IReaderConfiguration, IWriterConfiguration
{
private string newLine = "\r\n";
/// <inheritdoc/>
public virtual bool AllowComments { get; set; }
/// <inheritdoc/>
public virtual BadDataFound BadDataFound { get; set; } = ConfigurationFunctions.BadDataFound;
/// <inheritdoc/>
public virtual int BufferSize { get; set; } = 0x1000;
/// <inheritdoc/>
public virtual bool CacheFields { get; set; }
/// <inheritdoc/>
public virtual char Comment { get; set; } = '#';
/// <inheritdoc/>
public virtual bool CountBytes { get; set; }
/// <inheritdoc/>
public virtual CultureInfo CultureInfo { get; protected set; }
/// <inheritdoc/>
public virtual string Delimiter { get; set; }
/// <inheritdoc/>
public virtual bool DetectDelimiter { get; set; }
/// <inheritdoc/>
public virtual GetDelimiter GetDelimiter { get; set; } = ConfigurationFunctions.GetDelimiter;
/// <inheritdoc/>
public virtual string[] DetectDelimiterValues { get; set; } = new[] { ",", ";", "|", "\t" };
/// <inheritdoc/>
public virtual bool DetectColumnCountChanges { get; set; }
/// <inheritdoc/>
public virtual IComparer<string>? DynamicPropertySort { get; set; }
/// <inheritdoc/>
public virtual Encoding Encoding { get; set; } = Encoding.UTF8;
/// <inheritdoc/>
public virtual char Escape { get; set; } = '"';
/// <inheritdoc/>
public virtual bool ExceptionMessagesContainRawData { get; set; } = true;
/// <inheritdoc/>
public virtual GetConstructor GetConstructor { get; set; } = ConfigurationFunctions.GetConstructor;
/// <inheritdoc/>
public virtual GetDynamicPropertyName GetDynamicPropertyName { get; set; } = ConfigurationFunctions.GetDynamicPropertyName;
/// <inheritdoc/>
public virtual bool HasHeaderRecord { get; set; } = true;
/// <inheritdoc/>
public virtual HeaderValidated HeaderValidated { get; set; } = ConfigurationFunctions.HeaderValidated;
/// <inheritdoc/>
public virtual bool IgnoreBlankLines { get; set; } = true;
/// <inheritdoc/>
public virtual bool IgnoreReferences { get; set; }
/// <inheritdoc/>
public virtual bool IncludePrivateMembers { get; set; }
/// <inheritdoc/>
public virtual char[] InjectionCharacters { get; set; } = new[] { '=', '@', '+', '-', '\t', '\r' };
/// <inheritdoc/>
public virtual char InjectionEscapeCharacter { get; set; } = '\'';
/// <inheritdoc />
public virtual InjectionOptions InjectionOptions { get; set; }
/// <inheritdoc/>
public bool IsNewLineSet { get; private set; }
/// <inheritdoc/>
public virtual bool LineBreakInQuotedFieldIsBadData { get; set; }
/// <inheritdoc/>
public double MaxFieldSize { get; set; }
/// <inheritdoc/>
public virtual MemberTypes MemberTypes { get; set; } = MemberTypes.Properties;
/// <inheritdoc/>
public virtual MissingFieldFound MissingFieldFound { get; set; } = ConfigurationFunctions.MissingFieldFound;
/// <inheritdoc/>
public virtual CsvMode Mode { get; set; }
/// <inheritdoc/>
public virtual string NewLine
{
get => newLine;
set
{
IsNewLineSet = true;
newLine = value;
}
}
/// <inheritdoc/>
public virtual PrepareHeaderForMatch PrepareHeaderForMatch { get; set; } = ConfigurationFunctions.PrepareHeaderForMatch;
/// <inheritdoc/>
public virtual int ProcessFieldBufferSize { get; set; } = 1024;
/// <inheritdoc/>
public virtual char Quote { get; set; } = '"';
/// <inheritdoc/>
public virtual ReadingExceptionOccurred ReadingExceptionOccurred { get; set; } = ConfigurationFunctions.ReadingExceptionOccurred;
/// <inheritdoc/>
public virtual ReferenceHeaderPrefix? ReferenceHeaderPrefix { get; set; }
/// <inheritdoc/>
public ShouldQuote ShouldQuote { get; set; } = ConfigurationFunctions.ShouldQuote;
/// <inheritdoc/>
public virtual ShouldSkipRecord? ShouldSkipRecord { get; set; }
/// <inheritdoc/>
public virtual ShouldUseConstructorParameters ShouldUseConstructorParameters { get; set; } = ConfigurationFunctions.ShouldUseConstructorParameters;
/// <inheritdoc/>
public virtual TrimOptions TrimOptions { get; set; }
/// <inheritdoc/>
public virtual bool UseNewObjectForNullReferenceMembers { get; set; } = true;
/// <inheritdoc/>
public virtual char[] WhiteSpaceChars { get; set; } = new char[] { ' ' };
/// <summary>
/// Initializes a new instance of the <see cref="CsvConfiguration"/> class
/// using the given <see cref="System.Globalization.CultureInfo"/>. Since <see cref="Delimiter"/>
/// uses <see cref="CultureInfo"/> for it's default, the given <see cref="System.Globalization.CultureInfo"/>
/// will be used instead.
/// </summary>
/// <param name="cultureInfo">The culture information.</param>
/// <param name="attributesType">The type that contains the configuration attributes.
/// This will call <see cref="ApplyAttributes(Type)"/> automatically.</param>
public CsvConfiguration(CultureInfo cultureInfo, Type? attributesType = null)
{
CultureInfo = cultureInfo;
Delimiter = cultureInfo.TextInfo.ListSeparator;
if (attributesType != null)
{
ApplyAttributes(attributesType);
}
}
/// <summary>
/// Validates the configuration.
/// </summary>
public void Validate()
{
var escape = Escape.ToString();
var quote = Quote.ToString();
var lineEndings = new[] { "\r", "\n", "\r\n" };
var whiteSpaceChars = WhiteSpaceChars.Select(c => c.ToString()).ToArray();
// Escape
if (escape == Delimiter) throw new ConfigurationException($"The escape character '{Escape}' and delimiter '{Delimiter}' cannot be the same.");
if (escape == NewLine && IsNewLineSet) throw new ConfigurationException($"The escape character '{Escape}' and new line '{NewLine}' cannot be the same.");
if (lineEndings.Contains(Escape.ToString()) && !IsNewLineSet) throw new ConfigurationException($"The escape character '{Escape}' cannot be a line ending. ('\\r', '\\n', '\\r\\n')");
if (whiteSpaceChars.Contains(escape)) throw new ConfigurationException($"The escape character '{Escape}' cannot be a WhiteSpaceChar.");
// Quote
if (quote == Delimiter) throw new ConfigurationException($"The quote character '{Quote}' and the delimiter '{Delimiter}' cannot be the same.");
if (quote == NewLine && IsNewLineSet) throw new ConfigurationException($"The quote character '{Quote}' and new line '{NewLine}' cannot be the same.");
if (lineEndings.Contains(quote)) throw new ConfigurationException($"The quote character '{Quote}' cannot be a line ending. ('\\r', '\\n', '\\r\\n')");
if (whiteSpaceChars.Contains(quote)) throw new ConfigurationException($"The quote character '{Quote}' cannot be a WhiteSpaceChar.");
// Delimiter
if (Delimiter == NewLine && IsNewLineSet) throw new ConfigurationException($"The delimiter '{Delimiter}' and new line '{NewLine}' cannot be the same.");
if (lineEndings.Contains(Delimiter)) throw new ConfigurationException($"The delimiter '{Delimiter}' cannot be a line ending. ('\\r', '\\n', '\\r\\n')");
if (whiteSpaceChars.Contains(Delimiter)) throw new ConfigurationException($"The delimiter '{Delimiter}' cannot be a WhiteSpaceChar.");
// Detect Delimiter
if (DetectDelimiter && DetectDelimiterValues.Length == 0) throw new ConfigurationException($"At least one value is required for {nameof(DetectDelimiterValues)} when {nameof(DetectDelimiter)} is enabled.");
}
/// <summary>
/// Applies class level attribute to configuration.
/// </summary>
/// <typeparam name="T">Type with attributes.</typeparam>
public CsvConfiguration ApplyAttributes<T>()
{
return ApplyAttributes(typeof(T));
}
/// <summary>
/// Applies class level attribute to configuration.
/// </summary>
/// <param name="type">Type with attributes.</param>
public CsvConfiguration ApplyAttributes(Type type)
{
var attributes = type.GetCustomAttributes().OfType<IClassMapper>();
foreach (var attribute in attributes)
{
attribute.ApplyTo(this);
}
return this;
}
}
}
|