-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCSVUtils.pas
162 lines (143 loc) · 4.3 KB
/
CSVUtils.pas
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
unit CSVUtils;
interface
uses
System.Rtti, Spring.Collections;
type
ICSVWriter=Interface(IInvokable)
['{F78811E7-21F2-4C20-935D-1157C8B817D7}']
function AddRow: ICSVWriter;
function Delimiter(const pDelimiter: Char): ICSVWriter;
function Field(const pFieldName: string): ICSVWriter;
function Fields(const pFieldNames: array of string): ICSVWriter;
function QuoteStr(const pEnable: Boolean): ICSVWriter;
procedure SaveToFile(const pFileName: string);
function Value(const pFieldName: string; const pValue: TValue): ICSVWriter;
End;
TCSVWriter=class(TInterfacedObject,ICSVWriter)
private
FCurrentLine: IDictionary<string,TValue>;
FIntDelimiter: Char;
FIntFields: IList<string>;
FIntQuoteStr: Boolean;
FLines: IList<IDictionary<string,TValue>>;
property CurrentLine: IDictionary<string,TValue> read FCurrentLine write
FCurrentLine;
property IntDelimiter: Char read FIntDelimiter write FIntDelimiter;
property IntFields: IList<string> read FIntFields write FIntFields;
property IntQuoteStr: Boolean read FIntQuoteStr write FIntQuoteStr;
property Lines: IList<IDictionary<string,TValue>> read FLines write FLines;
public
constructor Create;
function AddRow: ICSVWriter;
function Delimiter(const pDelimiter: Char): ICSVWriter;
function Field(const pFieldName: string): ICSVWriter;
function Fields(const pFieldNames: array of string): ICSVWriter;
function QuoteStr(const pEnable: Boolean): ICSVWriter;
procedure SaveToFile(const pFileName: string);
function Value(const pFieldName: string; const pValue: TValue): ICSVWriter;
end;
implementation
uses
System.Classes, Spring, System.SysUtils;
constructor TCSVWriter.Create;
begin
inherited;
FIntDelimiter:=';';
FIntQuoteStr:=False;
FIntFields:=TCollections.CreateList<string>;
FLines:=TCollections.CreateList<IDictionary<string,TValue>>;
end;
function TCSVWriter.AddRow: ICSVWriter;
begin
Result:=self;
CurrentLine:=TCollections.CreateDictionary<string,TValue>;
Lines.Add(CurrentLine);
end;
function TCSVWriter.Delimiter(const pDelimiter: Char): ICSVWriter;
begin
Result:=self;
FIntDelimiter:=pDelimiter;
end;
function TCSVWriter.Field(const pFieldName: string): ICSVWriter;
begin
Result:=self;
FIntFields.Add(pFieldName);
end;
function TCSVWriter.Fields(const pFieldNames: array of string): ICSVWriter;
begin
Result:=self;
FIntFields.AddRange(pFieldNames);
end;
function TCSVWriter.QuoteStr(const pEnable: Boolean): ICSVWriter;
begin
Result:=self;
FIntQuoteStr:=pEnable;
end;
procedure TCSVWriter.SaveToFile(const pFileName: string);
var
header: TStringList;
sl: TStringList;
begin
sl:=TStringList.Create;
try
//hlavicka
header:=TStringList.Create;
try
header.Delimiter:=IntDelimiter;
header.StrictDelimiter:=True;
IntFields.ForEach(
procedure(const pFieldName:string)
begin
header.Add(pFieldName);
end
);
sl.Add(header.DelimitedText);
finally
header.Free;
end;
//polozky
Lines.ForEach(
procedure(const line:IDictionary<string,TValue>)
var
row:TStringList;
lValue:TValue;
begin
row:=TStringList.Create;
try
row.Delimiter:=IntDelimiter;
row.StrictDelimiter:=True;
IntFields.ForEach(
procedure(const pFieldName:string)
begin
if line.TryGetValue(pFieldName,lValue) then
if lValue.IsString then
begin
if IntQuoteStr then
row.Add(lValue.ToString.QuotedString('"'))
else
row.Add(lValue.ToString);
end
else
row.Add(lValue.ToString)
else
row.Add('');
end
);
sl.Add(row.DelimitedText);
finally
row.Free;
end;
end
);
sl.SaveToFile(pFileName);
finally
sl.Free;
end;
end;
function TCSVWriter.Value(const pFieldName: string; const pValue: TValue):
ICSVWriter;
begin
Result:=self;
CurrentLine.AddOrSetValue(pFieldName,pValue);
end;
end.