-
Notifications
You must be signed in to change notification settings - Fork 2
/
csvdocument.pas
1005 lines (887 loc) · 24 KB
/
csvdocument.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
CSV Parser and Document classes.
Version 0.4 2011-05-10
Copyright (C) 2010-2011 Vladimir Zhirov <[email protected]>
Contributors:
Luiz Americo Pereira Camara
Mattias Gaertner
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit CsvDocument;
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
interface
uses
Classes, SysUtils, Contnrs, StrUtils;
type
{$IFNDEF FPC}
TFPObjectList = TObjectList;
{$ENDIF}
TCSVChar = Char;
TCSVHandler = class(TObject)
private
procedure SetDelimiter(const AValue: TCSVChar);
procedure SetQuoteChar(const AValue: TCSVChar);
procedure UpdateCachedChars;
protected
// special chars
FDelimiter: TCSVChar;
FQuoteChar: TCSVChar;
FLineEnding: String;
// cached values to speed up special chars operations
FSpecialChars: TSysCharSet;
FDoubleQuote: String;
// parser settings
FIgnoreOuterWhitespace: Boolean;
// builder settings
FQuoteOuterWhitespace: Boolean;
// document settings
FEqualColCountPerRow: Boolean;
public
constructor Create;
procedure AssignCSVProperties(ASource: TCSVHandler);
property Delimiter: TCSVChar read FDelimiter write SetDelimiter;
property QuoteChar: TCSVChar read FQuoteChar write SetQuoteChar;
property LineEnding: String read FLineEnding write FLineEnding;
property IgnoreOuterWhitespace: Boolean read FIgnoreOuterWhitespace write FIgnoreOuterWhitespace;
property QuoteOuterWhitespace: Boolean read FQuoteOuterWhitespace write FQuoteOuterWhitespace;
property EqualColCountPerRow: Boolean read FEqualColCountPerRow write FEqualColCountPerRow;
end;
TCSVParser = class(TCSVHandler)
private
// fields
FSourceStream: TStream;
FStrStreamWrapper: TStringStream;
// parser state
EndOfFile: Boolean;
EndOfLine: Boolean;
FCurrentChar: TCSVChar;
FCurrentRow: Integer;
FCurrentCol: Integer;
// output buffers
FCellBuffer: String;
FWhitespaceBuffer: String;
procedure ClearOutput;
// basic parsing
procedure SkipEndOfLine;
procedure SkipDelimiter;
procedure SkipWhitespace;
procedure NextChar;
// complex parsing
procedure ParseCell;
procedure ParseQuotedValue;
procedure ParseValue;
public
constructor Create;
destructor Destroy; override;
procedure SetSource(AStream: TStream); overload;
procedure SetSource(const AString: String); overload;
procedure ResetParser;
function ParseNextCell: Boolean;
property CurrentRow: Integer read FCurrentRow;
property CurrentCol: Integer read FCurrentCol;
property CurrentCellText: String read FCellBuffer;
end;
TCSVBuilder = class(TCSVHandler)
private
FOutputStream: TStream;
FDefaultOutput: TMemoryStream;
FNeedLeadingDelimiter: Boolean;
function GetDefaultOutputAsString: String;
protected
procedure AppendStringToStream(const AString: String; AStream: TStream);
function QuoteCSVString(const AValue: String): String;
public
constructor Create;
destructor Destroy; override;
procedure SetOutput(AStream: TStream);
procedure ResetBuilder;
procedure AppendCell(const AValue: String);
procedure AppendRow;
property DefaultOutput: TMemoryStream read FDefaultOutput;
property DefaultOutputAsString: String read GetDefaultOutputAsString;
end;
TCSVDocument = class(TCSVHandler)
private
FRows: TFPObjectList;
FParser: TCSVParser;
FBuilder: TCSVBuilder;
// helpers
procedure ForceRowIndex(ARowIndex: Integer);
function CreateNewRow(const AFirstCell: String = ''): TObject;
// property getters/setters
function GetCell(ACol, ARow: Integer): String;
procedure SetCell(ACol, ARow: Integer; const AValue: String);
function GetCSVText: String;
procedure SetCSVText(const AValue: String);
function GetRowCount: Integer;
function GetColCount(ARow: Integer): Integer;
function GetMaxColCount: Integer;
public
constructor Create;
destructor Destroy; override;
// input/output
procedure LoadFromFile(const AFilename: String);
procedure LoadFromStream(AStream: TStream);
procedure SaveToFile(const AFilename: String);
procedure SaveToStream(AStream: TStream);
// row and cell operations
procedure AddRow(const AFirstCell: String = '');
procedure AddCell(ARow: Integer; const AValue: String = '');
procedure InsertRow(ARow: Integer; const AFirstCell: String = '');
procedure InsertCell(ACol, ARow: Integer; const AValue: String = '');
procedure RemoveRow(ARow: Integer);
procedure RemoveCell(ACol, ARow: Integer);
function HasRow(ARow: Integer): Boolean;
function HasCell(ACol, ARow: Integer): Boolean;
// search
function IndexOfCol(const AString: String; ARow: Integer): Integer;
function IndexOfRow(const AString: String; ACol: Integer): Integer;
// utils
procedure Clear;
procedure CloneRow(ARow, AInsertPos: Integer);
procedure ExchangeRows(ARow1, ARow2: Integer);
procedure UnifyEmbeddedLineEndings;
procedure RemoveTrailingEmptyCells;
// properties
property Cells[ACol, ARow: Integer]: String read GetCell write SetCell; default;
property RowCount: Integer read GetRowCount;
property ColCount[ARow: Integer]: Integer read GetColCount;
property MaxColCount: Integer read GetMaxColCount;
property CSVText: String read GetCSVText write SetCSVText;
end;
implementation
const
CsvCharSize = SizeOf(TCSVChar);
CR = #13;
LF = #10;
HTAB = #9;
SPACE = #32;
WhitespaceChars = [HTAB, SPACE];
LineEndingChars = [CR, LF];
// The following implementation of ChangeLineEndings function originates from
// Lazarus CodeTools library by Mattias Gaertner. It was explicitly allowed
// by Mattias to relicense it under modified LGPL and include into CsvDocument.
function ChangeLineEndings(const AString, ALineEnding: String): String;
var
I: Integer;
Src: PChar;
Dest: PChar;
DestLength: Integer;
EndingLength: Integer;
EndPos: PChar;
begin
if AString = '' then
Exit(AString);
EndingLength := Length(ALineEnding);
DestLength := Length(AString);
Src := PChar(AString);
EndPos := Src + DestLength;
while Src < EndPos do
begin
if (Src^ = CR) then
begin
Inc(Src);
if (Src^ = LF) then
begin
Inc(Src);
Inc(DestLength, EndingLength - 2);
end else
Inc(DestLength, EndingLength - 1);
end else
begin
if (Src^ = LF) then
Inc(DestLength, EndingLength - 1);
Inc(Src);
end;
end;
SetLength(Result, DestLength);
Src := PChar(AString);
Dest := PChar(Result);
EndPos := Dest + DestLength;
while (Dest < EndPos) do
begin
if Src^ in LineEndingChars then
begin
for I := 1 to EndingLength do
begin
Dest^ := ALineEnding[I];
Inc(Dest);
end;
if (Src^ = CR) and (Src[1] = LF) then
Inc(Src, 2)
else
Inc(Src);
end else
begin
Dest^ := Src^;
Inc(Src);
Inc(Dest);
end;
end;
end;
{ TCSVHandler }
procedure TCSVHandler.SetDelimiter(const AValue: TCSVChar);
begin
if FDelimiter <> AValue then
begin
FDelimiter := AValue;
UpdateCachedChars;
end;
end;
procedure TCSVHandler.SetQuoteChar(const AValue: TCSVChar);
begin
if FQuoteChar <> AValue then
begin
FQuoteChar := AValue;
UpdateCachedChars;
end;
end;
procedure TCSVHandler.UpdateCachedChars;
begin
FDoubleQuote := FQuoteChar + FQuoteChar;
FSpecialChars := [CR, LF, FDelimiter, FQuoteChar];
end;
constructor TCSVHandler.Create;
begin
inherited Create;
FDelimiter := ',';
FQuoteChar := '"';
FLineEnding := CR + LF;
FIgnoreOuterWhitespace := False;
FQuoteOuterWhitespace := True;
FEqualColCountPerRow := True;
UpdateCachedChars;
end;
procedure TCSVHandler.AssignCSVProperties(ASource: TCSVHandler);
begin
FDelimiter := ASource.FDelimiter;
FQuoteChar := ASource.FQuoteChar;
FLineEnding := ASource.FLineEnding;
FIgnoreOuterWhitespace := ASource.FIgnoreOuterWhitespace;
FQuoteOuterWhitespace := ASource.FQuoteOuterWhitespace;
FEqualColCountPerRow := ASource.FEqualColCountPerRow;
UpdateCachedChars;
end;
{ TCSVParser }
procedure TCSVParser.ClearOutput;
begin
FCellBuffer := '';
FWhitespaceBuffer := '';
FCurrentRow := 0;
FCurrentCol := -1;
end;
procedure TCSVParser.SkipEndOfLine;
begin
// treat LF+CR as two linebreaks, not one
if (FCurrentChar = CR) then
NextChar;
if (FCurrentChar = LF) then
NextChar;
end;
procedure TCSVParser.SkipDelimiter;
begin
if FCurrentChar = FDelimiter then
NextChar;
end;
procedure TCSVParser.SkipWhitespace;
begin
while FCurrentChar = SPACE do
NextChar;
end;
procedure TCSVParser.NextChar;
begin
if FSourceStream.Read(FCurrentChar, CsvCharSize) < CsvCharSize then
begin
FCurrentChar := #0;
EndOfFile := True;
end;
EndOfLine := FCurrentChar in LineEndingChars;
end;
procedure TCSVParser.ParseCell;
begin
FCellBuffer := '';
if FIgnoreOuterWhitespace then
SkipWhitespace;
if FCurrentChar = FQuoteChar then
ParseQuotedValue
else
ParseValue;
end;
procedure TCSVParser.ParseQuotedValue;
var
QuotationEnd: Boolean;
begin
NextChar; // skip opening quotation char
repeat
// read value up to next quotation char
while not ((FCurrentChar = FQuoteChar) or EndOfFile) do
begin
if EndOfLine then
begin
AppendStr(FCellBuffer, FLineEnding);
SkipEndOfLine;
end else
begin
AppendStr(FCellBuffer, FCurrentChar);
NextChar;
end;
end;
// skip quotation char (closing or escaping)
if not EndOfFile then
NextChar;
// check if it was escaping
if FCurrentChar = FQuoteChar then
begin
AppendStr(FCellBuffer, FCurrentChar);
QuotationEnd := False;
NextChar;
end else
QuotationEnd := True;
until QuotationEnd;
// read the rest of the value until separator or new line
ParseValue;
end;
procedure TCSVParser.ParseValue;
begin
while not ((FCurrentChar = FDelimiter) or EndOfFile or EndOfLine) do
begin
AppendStr(FWhitespaceBuffer, FCurrentChar);
NextChar;
end;
// merge whitespace buffer
if FIgnoreOuterWhitespace then
RemoveTrailingChars(FWhitespaceBuffer, WhitespaceChars);
AppendStr(FCellBuffer, FWhitespaceBuffer);
FWhitespaceBuffer := '';
end;
constructor TCSVParser.Create;
begin
inherited Create;
ClearOutput;
FStrStreamWrapper := nil;
EndOfFile := True;
end;
destructor TCSVParser.Destroy;
begin
FreeAndNil(FStrStreamWrapper);
inherited Destroy;
end;
procedure TCSVParser.SetSource(AStream: TStream);
begin
FSourceStream := AStream;
ResetParser;
end;
procedure TCSVParser.SetSource(const AString: String); overload;
begin
FreeAndNil(FStrStreamWrapper);
FStrStreamWrapper := TStringStream.Create(AString);
SetSource(FStrStreamWrapper);
end;
procedure TCSVParser.ResetParser;
begin
ClearOutput;
FSourceStream.Seek(0, soFromBeginning);
EndOfFile := False;
NextChar;
end;
function TCSVParser.ParseNextCell: Boolean;
begin
if EndOfFile then
Exit(False);
if EndOfLine then
begin
SkipEndOfLine;
if EndOfFile then
Exit(False);
FCurrentCol := 0;
Inc(FCurrentRow);
end else
Inc(FCurrentCol);
// Skipping a delimiter should be immediately followed by parsing a cell
// without checking for line break first, otherwise we miss last empty cell.
// But 0th cell does not start with delimiter unlike other cells, so
// the following check is required not to miss the first empty cell:
if FCurrentCol > 0 then
SkipDelimiter;
ParseCell;
Result := True;
end;
{ TCSVBuilder }
function TCSVBuilder.GetDefaultOutputAsString: String;
var
StreamSize: Integer;
begin
Result := '';
StreamSize := FDefaultOutput.Size;
if StreamSize > 0 then
begin
SetLength(Result, StreamSize);
FDefaultOutput.ReadBuffer(Result[1], StreamSize);
end;
end;
procedure TCSVBuilder.AppendStringToStream(const AString: String; AStream: TStream);
var
StrLen: Integer;
begin
StrLen := Length(AString);
if StrLen > 0 then
AStream.WriteBuffer(AString[1], StrLen);
end;
function TCSVBuilder.QuoteCSVString(const AValue: String): String;
var
I: Integer;
ValueLen: Integer;
NeedQuotation: Boolean;
begin
ValueLen := Length(AValue);
NeedQuotation := (AValue <> '') and FQuoteOuterWhitespace
and ((AValue[1] in WhitespaceChars) or (AValue[ValueLen] in WhitespaceChars));
if not NeedQuotation then
for I := 1 to ValueLen do
begin
if AValue[I] in FSpecialChars then
begin
NeedQuotation := True;
Break;
end;
end;
if NeedQuotation then
begin
// double existing quotes
Result := FDoubleQuote;
Insert(StringReplace(AValue, FQuoteChar, FDoubleQuote, [rfReplaceAll]),
Result, 2);
end else
Result := AValue;
end;
constructor TCSVBuilder.Create;
begin
inherited Create;
FDefaultOutput := TMemoryStream.Create;
FOutputStream := FDefaultOutput;
end;
destructor TCSVBuilder.Destroy;
begin
FreeAndNil(FDefaultOutput);
inherited Destroy;
end;
procedure TCSVBuilder.SetOutput(AStream: TStream);
begin
if Assigned(AStream) then
FOutputStream := AStream
else
FOutputStream := FDefaultOutput;
ResetBuilder;
end;
procedure TCSVBuilder.ResetBuilder;
begin
if FOutputStream = FDefaultOutput then
FDefaultOutput.Clear;
// Do not clear external FOutputStream because it may be pipe stream
// or something else that does not support size and position.
// To clear external output is up to the user of TCSVBuilder.
FNeedLeadingDelimiter := False;
end;
procedure TCSVBuilder.AppendCell(const AValue: String);
var
CellValue: String;
begin
if FNeedLeadingDelimiter then
FOutputStream.WriteBuffer(FDelimiter, CsvCharSize);
CellValue := ChangeLineEndings(AValue, FLineEnding);
CellValue := QuoteCSVString(CellValue);
AppendStringToStream(CellValue, FOutputStream);
FNeedLeadingDelimiter := True;
end;
procedure TCSVBuilder.AppendRow;
begin
AppendStringToStream(FLineEnding, FOutputStream);
FNeedLeadingDelimiter := False;
end;
//------------------------------------------------------------------------------
type
TCSVCell = class
public
Value: String;
end;
TCSVRow = class
private
FCells: TFPObjectList;
procedure ForceCellIndex(ACellIndex: Integer);
function CreateNewCell(const AValue: String): TCSVCell;
function GetCellValue(ACol: Integer): String;
procedure SetCellValue(ACol: Integer; const AValue: String);
function GetColCount: Integer;
public
constructor Create;
destructor Destroy; override;
// cell operations
procedure AddCell(const AValue: String = '');
procedure InsertCell(ACol: Integer; const AValue: String);
procedure RemoveCell(ACol: Integer);
function HasCell(ACol: Integer): Boolean;
// utilities
function Clone: TCSVRow;
procedure TrimEmptyCells;
procedure SetValuesLineEnding(const ALineEnding: String);
// properties
property CellValue[ACol: Integer]: String read GetCellValue write SetCellValue;
property ColCount: Integer read GetColCount;
end;
{ TCSVRow }
procedure TCSVRow.ForceCellIndex(ACellIndex: Integer);
begin
while FCells.Count <= ACellIndex do
AddCell();
end;
function TCSVRow.CreateNewCell(const AValue: String): TCSVCell;
begin
Result := TCSVCell.Create;
Result.Value := AValue;
end;
function TCSVRow.GetCellValue(ACol: Integer): String;
begin
if HasCell(ACol) then
Result := TCSVCell(FCells[ACol]).Value
else
Result := '';
end;
procedure TCSVRow.SetCellValue(ACol: Integer; const AValue: String);
begin
ForceCellIndex(ACol);
TCSVCell(FCells[ACol]).Value := AValue;
end;
function TCSVRow.GetColCount: Integer;
begin
Result := FCells.Count;
end;
constructor TCSVRow.Create;
begin
inherited Create;
FCells := TFPObjectList.Create;
end;
destructor TCSVRow.Destroy;
begin
FreeAndNil(FCells);
inherited Destroy;
end;
procedure TCSVRow.AddCell(const AValue: String = '');
begin
FCells.Add(CreateNewCell(AValue));
end;
procedure TCSVRow.InsertCell(ACol: Integer; const AValue: String);
begin
FCells.Insert(ACol, CreateNewCell(AValue));
end;
procedure TCSVRow.RemoveCell(ACol: Integer);
begin
if HasCell(ACol) then
FCells.Delete(ACol);
end;
function TCSVRow.HasCell(ACol: Integer): Boolean;
begin
Result := (ACol >= 0) and (ACol < FCells.Count);
end;
function TCSVRow.Clone: TCSVRow;
var
I: Integer;
begin
Result := TCSVRow.Create;
for I := 0 to ColCount - 1 do
Result.AddCell(CellValue[I]);
end;
procedure TCSVRow.TrimEmptyCells;
var
I: Integer;
MaxCol: Integer;
begin
MaxCol := FCells.Count - 1;
for I := MaxCol downto 0 do
if (TCSVCell(FCells[I]).Value = '') and (FCells.Count > 1) then
FCells.Delete(I);
end;
procedure TCSVRow.SetValuesLineEnding(const ALineEnding: String);
var
I: Integer;
begin
for I := 0 to FCells.Count - 1 do
CellValue[I] := ChangeLineEndings(CellValue[I], ALineEnding);
end;
{ TCSVDocument }
procedure TCSVDocument.ForceRowIndex(ARowIndex: Integer);
begin
while FRows.Count <= ARowIndex do
AddRow();
end;
function TCSVDocument.CreateNewRow(const AFirstCell: String): TObject;
var
NewRow: TCSVRow;
begin
NewRow := TCSVRow.Create;
if AFirstCell <> '' then
NewRow.AddCell(AFirstCell);
Result := NewRow;
end;
function TCSVDocument.GetCell(ACol, ARow: Integer): String;
begin
if HasRow(ARow) then
Result := TCSVRow(FRows[ARow]).CellValue[ACol]
else
Result := '';
end;
procedure TCSVDocument.SetCell(ACol, ARow: Integer; const AValue: String);
begin
ForceRowIndex(ARow);
TCSVRow(FRows[ARow]).CellValue[ACol] := AValue;
end;
function TCSVDocument.GetCSVText: String;
var
StringStream: TStringStream;
begin
StringStream := TStringStream.Create('');
try
SaveToStream(StringStream);
Result := StringStream.DataString;
finally
FreeAndNil(StringStream);
end;
end;
procedure TCSVDocument.SetCSVText(const AValue: String);
var
StringStream: TStringStream;
begin
StringStream := TStringStream.Create(AValue);
try
LoadFromStream(StringStream);
finally
FreeAndNil(StringStream);
end;
end;
function TCSVDocument.GetRowCount: Integer;
begin
Result := FRows.Count;
end;
function TCSVDocument.GetColCount(ARow: Integer): Integer;
begin
if HasRow(ARow) then
Result := TCSVRow(FRows[ARow]).ColCount
else
Result := 0;
end;
function TCSVDocument.GetMaxColCount: Integer;
var
I, CC: Integer;
begin
Result := 0;
for I := 0 to RowCount - 1 do
begin
CC := ColCount[I];
if CC > Result then
Result := CC;
end;
end;
constructor TCSVDocument.Create;
begin
inherited Create;
FRows := TFPObjectList.Create;
FParser := nil;
FBuilder := nil;
end;
destructor TCSVDocument.Destroy;
begin
FreeAndNil(FBuilder);
FreeAndNil(FParser);
FreeAndNil(FRows);
inherited Destroy;
end;
procedure TCSVDocument.LoadFromFile(const AFilename: String);
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyNone);
try
LoadFromStream(FileStream);
finally
FileStream.Free;
end;
end;
procedure TCSVDocument.LoadFromStream(AStream: TStream);
var
I, J, MaxCol: Integer;
begin
Clear;
if not Assigned(FParser) then
FParser := TCSVParser.Create;
FParser.AssignCSVProperties(Self);
with FParser do
begin
SetSource(AStream);
while ParseNextCell do
Cells[CurrentCol, CurrentRow] := CurrentCellText;
end;
if FEqualColCountPerRow then
begin
MaxCol := MaxColCount - 1;
for I := 0 to RowCount - 1 do
for J := ColCount[I] to MaxCol do
Cells[J, I] := '';
end;
end;
procedure TCSVDocument.SaveToFile(const AFilename: String);
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create(AFilename, fmCreate);
try
SaveToStream(FileStream);
finally
FileStream.Free;
end;
end;
procedure TCSVDocument.SaveToStream(AStream: TStream);
var
I, J, MaxCol: Integer;
begin
if not Assigned(FBuilder) then
FBuilder := TCSVBuilder.Create;
FBuilder.AssignCSVProperties(Self);
with FBuilder do
begin
if FEqualColCountPerRow then
MaxCol := MaxColCount - 1;
SetOutput(AStream);
for I := 0 to RowCount - 1 do
begin
if not FEqualColCountPerRow then
MaxCol := ColCount[I] - 1;
for J := 0 to MaxCol do
AppendCell(Cells[J, I]);
AppendRow;
end;
end;
end;
procedure TCSVDocument.AddRow(const AFirstCell: String = '');
begin
FRows.Add(CreateNewRow(AFirstCell));
end;
procedure TCSVDocument.AddCell(ARow: Integer; const AValue: String = '');
begin
ForceRowIndex(ARow);
TCSVRow(FRows[ARow]).AddCell(AValue);
end;
procedure TCSVDocument.InsertRow(ARow: Integer; const AFirstCell: String = '');
begin
if HasRow(ARow) then
FRows.Insert(ARow, CreateNewRow(AFirstCell))
else
AddRow(AFirstCell);
end;
procedure TCSVDocument.InsertCell(ACol, ARow: Integer; const AValue: String);
begin
ForceRowIndex(ARow);
TCSVRow(FRows[ARow]).InsertCell(ACol, AValue);
end;
procedure TCSVDocument.RemoveRow(ARow: Integer);
begin
if HasRow(ARow) then
FRows.Delete(ARow);
end;
procedure TCSVDocument.RemoveCell(ACol, ARow: Integer);
begin
if HasRow(ARow) then
TCSVRow(FRows[ARow]).RemoveCell(ACol);
end;
function TCSVDocument.HasRow(ARow: Integer): Boolean;
begin
Result := (ARow >= 0) and (ARow < FRows.Count);
end;
function TCSVDocument.HasCell(ACol, ARow: Integer): Boolean;
begin
if HasRow(ARow) then
Result := TCSVRow(FRows[ARow]).HasCell(ACol)
else
Result := False;
end;
function TCSVDocument.IndexOfCol(const AString: String; ARow: Integer): Integer;
var
CC: Integer;
begin
CC := ColCount[ARow];
Result := 0;
while (Result < CC) and (Cells[Result, ARow] <> AString) do
Inc(Result);
if Result = CC then
Result := -1;
end;
function TCSVDocument.IndexOfRow(const AString: String; ACol: Integer): Integer;
var
RC: Integer;
begin
RC := RowCount;
Result := 0;
while (Result < RC) and (Cells[ACol, Result] <> AString) do
Inc(Result);
if Result = RC then
Result := -1;
end;
procedure TCSVDocument.Clear;
begin
FRows.Clear;
end;
procedure TCSVDocument.CloneRow(ARow, AInsertPos: Integer);
var
NewRow: TObject;
begin
if not HasRow(ARow) then
Exit;
NewRow := TCSVRow(FRows[ARow]).Clone;
if not HasRow(AInsertPos) then
begin
ForceRowIndex(AInsertPos - 1);
FRows.Add(NewRow);
end else
FRows.Insert(AInsertPos, NewRow);
end;
procedure TCSVDocument.ExchangeRows(ARow1, ARow2: Integer);
begin
if not (HasRow(ARow1) and HasRow(ARow2)) then
Exit;
FRows.Exchange(ARow1, ARow2);
end;
procedure TCSVDocument.UnifyEmbeddedLineEndings;
var
I: Integer;
begin
for I := 0 to FRows.Count - 1 do
TCSVRow(FRows[I]).SetValuesLineEnding(FLineEnding);
end;
procedure TCSVDocument.RemoveTrailingEmptyCells;
var
I: Integer;
begin