This repository was archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 616
Expand file tree
/
Copy pathCsvConfig.cs
More file actions
164 lines (144 loc) · 5.4 KB
/
CsvConfig.cs
File metadata and controls
164 lines (144 loc) · 5.4 KB
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
using System;
using System.Collections.Generic;
using System.Globalization;
using ServiceStack.Text.Common;
namespace ServiceStack.Text
{
public static class CsvConfig
{
static CsvConfig()
{
Reset();
}
private static CultureInfo sRealNumberCultureInfo;
public static CultureInfo RealNumberCultureInfo
{
get => sRealNumberCultureInfo ?? CultureInfo.InvariantCulture;
set => sRealNumberCultureInfo = value;
}
[ThreadStatic]
private static string tsItemSeperatorString;
private static string sItemSeperatorString;
public static string ItemSeperatorString
{
get => tsItemSeperatorString ?? sItemSeperatorString ?? JsWriter.ItemSeperatorString;
set
{
tsItemSeperatorString = value;
if (sItemSeperatorString == null) sItemSeperatorString = value;
ResetEscapeStrings();
}
}
[ThreadStatic]
private static string tsItemDelimiterString;
private static string sItemDelimiterString;
public static string ItemDelimiterString
{
get => tsItemDelimiterString ?? sItemDelimiterString ?? JsWriter.QuoteString;
set
{
tsItemDelimiterString = value;
if (sItemDelimiterString == null) sItemDelimiterString = value;
EscapedItemDelimiterString = value + value;
ResetEscapeStrings();
}
}
private const string DefaultEscapedItemDelimiterString = JsWriter.QuoteString + JsWriter.QuoteString;
[ThreadStatic]
private static string tsEscapedItemDelimiterString;
private static string sEscapedItemDelimiterString;
internal static string EscapedItemDelimiterString
{
get => tsEscapedItemDelimiterString ?? sEscapedItemDelimiterString ?? DefaultEscapedItemDelimiterString;
set
{
tsEscapedItemDelimiterString = value;
if (sEscapedItemDelimiterString == null) sEscapedItemDelimiterString = value;
}
}
private static readonly string[] defaultEscapeStrings = GetEscapeStrings();
[ThreadStatic]
private static string[] tsEscapeStrings;
private static string[] sEscapeStrings;
public static string[] EscapeStrings
{
get => tsEscapeStrings ?? sEscapeStrings ?? defaultEscapeStrings;
private set
{
tsEscapeStrings = value;
if (sEscapeStrings == null) sEscapeStrings = value;
}
}
private static string[] GetEscapeStrings()
{
return new[] { ItemDelimiterString, ItemSeperatorString, RowSeparatorString, "\r", "\n" };
}
private static void ResetEscapeStrings()
{
EscapeStrings = GetEscapeStrings();
}
[ThreadStatic]
private static string tsRowSeparatorString;
private static string sRowSeparatorString;
public static string RowSeparatorString
{
get => tsRowSeparatorString ?? sRowSeparatorString ?? "\r\n";
set
{
tsRowSeparatorString = value;
if (sRowSeparatorString == null) sRowSeparatorString = value;
ResetEscapeStrings();
}
}
public static void Reset()
{
tsItemSeperatorString = sItemSeperatorString = null;
tsItemDelimiterString = sItemDelimiterString = null;
tsEscapedItemDelimiterString = sEscapedItemDelimiterString = null;
tsRowSeparatorString = sRowSeparatorString = null;
tsEscapeStrings = sEscapeStrings = null;
}
}
public static class CsvConfig<T>
{
public static bool OmitHeaders { get; set; }
private static Dictionary<string, string> customHeadersMap;
public static Dictionary<string, string> CustomHeadersMap
{
get => customHeadersMap;
set
{
customHeadersMap = value;
if (value == null) return;
CsvWriter<T>.ConfigureCustomHeaders(customHeadersMap);
CsvReader<T>.ConfigureCustomHeaders(customHeadersMap);
}
}
public static object CustomHeaders
{
set
{
if (value == null) return;
if (value.GetType().IsValueType)
throw new ArgumentException("CustomHeaders is a ValueType");
var propertyInfos = value.GetType().GetProperties();
if (propertyInfos.Length == 0) return;
customHeadersMap = new Dictionary<string, string>();
foreach (var pi in propertyInfos)
{
var getMethod = pi.GetGetMethod(nonPublic:true);
if (getMethod == null) continue;
var oValue = getMethod.Invoke(value, TypeConstants.EmptyObjectArray);
if (oValue == null) continue;
customHeadersMap[pi.Name] = oValue.ToString();
}
CsvWriter<T>.ConfigureCustomHeaders(customHeadersMap);
}
}
public static void Reset()
{
OmitHeaders = false;
CsvWriter<T>.Reset();
}
}
}