csvdocument: initial commit to new repository

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1476 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
vvzh
2011-01-31 15:09:56 +00:00
parent 28c6a5a058
commit 5fcdc6d075
21 changed files with 2408 additions and 0 deletions

View File

@ -0,0 +1,921 @@
{
CSV Parser and Document classes.
Version 0.4 2011-01-31
Copyright (C) 2010-2011 Vladimir Zhirov <vvzh.home@gmail.com>
Contributors:
Luiz Americo Pereira Camara
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)
protected
// special chars
FDelimiter: TCSVChar;
FQuoteChar: TCSVChar;
FLineEnding: 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 FDelimiter;
property QuoteChar: TCSVChar read FQuoteChar write FQuoteChar;
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: TSysCharSet = [HTAB, SPACE];
LineEndingChars: TSysCharSet = [CR, LF];
function ChangeLineEndings(const AString, ALineEnding: String): String;
var
I: Integer;
begin
for I := 1 to Length(AString) do
if AString[I] in LineEndingChars then
begin
// first unify line endings to single-char value
Result := StringReplace(AString, CR + LF, LF, [rfReplaceAll]);
Result := StringReplace(Result, CR, LF, [rfReplaceAll]);
// then replace this single-char value with value we need
if ALineEnding <> LF then
Result := StringReplace(Result, LF, ALineEnding, [rfReplaceAll]);
Exit;
end;
Result := AString;
end;
{ TCSVHandler }
constructor TCSVHandler.Create;
begin
inherited Create;
FDelimiter := ',';
FQuoteChar := '"';
FLineEnding := CR + LF;
FIgnoreOuterWhitespace := False;
FQuoteOuterWhitespace := True;
FEqualColCountPerRow := True;
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;
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;
NeedQuotation: Boolean;
begin
NeedQuotation := (AValue <> '') and FQuoteOuterWhitespace
and ((AValue[1] in WhitespaceChars) or (AValue[Length(AValue)] in WhitespaceChars));
if not NeedQuotation then
for I := 1 to Length(AValue) do
begin
if AValue[I] in [CR, LF, FDelimiter, FQuoteChar] then
begin
NeedQuotation := True;
Break;
end;
end;
if NeedQuotation then
begin
// double existing quotes
Result := StringReplace(AValue, FQuoteChar,
FQuoteChar + FQuoteChar, [rfReplaceAll]);
Result := FQuoteChar + Result + FQuoteChar;
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
for I := 0 to FRows.Count - 1 do
TCSVRow(FRows[I]).TrimEmptyCells;
end;
end.

View File

@ -0,0 +1,62 @@
<?xml version="1.0"?>
<CONFIG>
<Package Version="3">
<PathDelim Value="\"/>
<Name Value="csvdocument_package"/>
<Author Value="Vladimir Zhirov"/>
<CompilerOptions>
<Version Value="10"/>
<PathDelim Value="\"/>
<SearchPaths>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<CStyleOperator Value="False"/>
</SyntaxOptions>
</Parsing>
<CodeGeneration>
<SmartLinkUnit Value="True"/>
<Checks>
<IOChecks Value="True"/>
<RangeChecks Value="True"/>
<OverflowChecks Value="True"/>
<StackChecks Value="True"/>
</Checks>
<Optimizations>
<OptimizationLevel Value="2"/>
</Optimizations>
</CodeGeneration>
<Linking>
<Debugging>
<UseLineInfoUnit Value="False"/>
</Debugging>
</Linking>
<Other>
<CustomOptions Value="-al"/>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
<Description Value="CsvDocument library is a unit contaning set of classes for CSV files handling."/>
<License Value="Modified LGPL (same as FPC RTL and Lazarus LCL)"/>
<Version Minor="4"/>
<Files Count="1">
<Item1>
<Filename Value="csvdocument.pas"/>
<UnitName Value="CsvDocument"/>
</Item1>
</Files>
<RequiredPkgs Count="1">
<Item1>
<PackageName Value="FCL"/>
<MinVersion Major="1" Valid="True"/>
</Item1>
</RequiredPkgs>
<UsageOptions>
<UnitPath Value="$(PkgOutDir)"/>
</UsageOptions>
<PublishOptions>
<Version Value="2"/>
</PublishOptions>
</Package>
</CONFIG>

View File

@ -0,0 +1,14 @@
{ This file was automatically created by Lazarus. Do not edit!
This source is only used to compile and install the package.
}
unit csvdocument_package;
interface
uses
CsvDocument;
implementation
end.

View File

@ -0,0 +1,115 @@
<?xml version="1.0"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<PathDelim Value="\"/>
<General>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="CsvDemo"/>
<UseXPManifest Value="True"/>
</General>
<VersionInfo>
<Language Value=""/>
<CharSet Value=""/>
<StringTable ProductVersion=""/>
</VersionInfo>
<BuildModes Count="1">
<Item1 Name="default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<IgnoreBinaries Value="False"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
<ExcludeFileFilter Value="*.(bak|ppu|ppw|o|so);*~;backup"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
<LaunchingApplication PathPlusParams="\usr\X11R6\bin\xterm -T 'Lazarus Run Output' -e $(LazarusDir)\tools\runwait.sh $(TargetCmdLine)"/>
</local>
</RunParams>
<RequiredPackages Count="2">
<Item1>
<PackageName Value="LCLBase"/>
<MinVersion Major="1" Release="1" Valid="True"/>
</Item1>
<Item2>
<PackageName Value="LCL"/>
</Item2>
</RequiredPackages>
<Units Count="3">
<Unit0>
<Filename Value="csvdemo.lpr"/>
<IsPartOfProject Value="True"/>
<UnitName Value="csvdemo"/>
</Unit0>
<Unit1>
<Filename Value="mainfrm.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="fmCSVTest"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="mainfrm"/>
</Unit1>
<Unit2>
<Filename Value="csvdocument.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="CsvDocument"/>
</Unit2>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="10"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="csvdemo"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value=".."/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
<SrcPath Value=".."/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<CStyleOperator Value="False"/>
<UseAnsiStrings Value="False"/>
</SyntaxOptions>
</Parsing>
<CodeGeneration>
<SmartLinkUnit Value="True"/>
<Checks>
<IOChecks Value="True"/>
<RangeChecks Value="True"/>
<OverflowChecks Value="True"/>
<StackChecks Value="True"/>
</Checks>
<VerifyObjMethodCallValidity Value="True"/>
</CodeGeneration>
<Linking>
<Debugging>
<UseExternalDbgSyms Value="True"/>
</Debugging>
<LinkSmart Value="True"/>
</Linking>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

View File

@ -0,0 +1,21 @@
program csvdemo;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, mainfrm, CsvDocument
{ you can add units after this };
{$R csvdemo.res}
begin
Application.Title := 'CsvDemo';
Application.Initialize;
Application.CreateForm(TfmCSVTest, fmCSVTest);
Application.Run;
end.

Binary file not shown.

View File

@ -0,0 +1,237 @@
object fmCSVTest: TfmCSVTest
Left = 401
Height = 450
Top = 175
Width = 401
Caption = 'CSV Demo'
ClientHeight = 450
ClientWidth = 401
OnCreate = FormCreate
OnDestroy = FormDestroy
LCLVersion = '0.9.31'
object sgView: TStringGrid
Left = 0
Height = 119
Top = 139
Width = 401
Align = alClient
Anchors = []
ColCount = 1
Constraints.MinHeight = 10
DefaultColWidth = 100
FixedCols = 0
FixedRows = 0
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goDrawFocusSelected, goRowSizing, goColSizing, goThumbTracking, goSmoothScroll]
RowCount = 1
TabOrder = 0
OnSelectCell = sgViewSelectCell
end
object mmSource: TMemo
Left = 0
Height = 51
Top = 22
Width = 401
Align = alTop
Constraints.MinHeight = 10
OnChange = mmSourceChange
ScrollBars = ssVertical
TabOrder = 1
end
object splTop: TSplitter
Cursor = crVSplit
Left = 0
Height = 5
Top = 73
Width = 401
Align = alTop
ResizeAnchor = akTop
end
object mmResult: TMemo
Left = 0
Height = 64
Top = 386
Width = 401
Align = alBottom
Constraints.MinHeight = 10
ReadOnly = True
ScrollBars = ssVertical
TabOrder = 3
end
object splBottom1: TSplitter
Cursor = crVSplit
Left = 0
Height = 5
Top = 381
Width = 401
Align = alBottom
Anchors = [akLeft, akBottom]
ResizeAnchor = akBottom
end
object mmCellValue: TMemo
Left = 0
Height = 74
Top = 285
Width = 401
Align = alBottom
Constraints.MinHeight = 10
OnChange = mmCellValueChange
ScrollBars = ssBoth
TabOrder = 5
WordWrap = False
end
object splBottom2: TSplitter
Cursor = crVSplit
Left = 0
Height = 5
Top = 280
Width = 401
Align = alBottom
Anchors = [akLeft, akBottom]
ResizeAnchor = akBottom
end
object lblSource: TLabel
Left = 5
Height = 17
Top = 5
Width = 391
Align = alTop
BorderSpacing.Left = 5
BorderSpacing.Top = 5
BorderSpacing.Right = 5
Caption = 'Source CSV string'
ParentColor = False
end
object lblOutput: TLabel
Left = 5
Height = 17
Top = 364
Width = 391
Align = alBottom
BorderSpacing.Left = 5
BorderSpacing.Top = 5
BorderSpacing.Right = 5
Caption = 'Output CSV string'
ParentColor = False
end
object lblCSVDoc: TLabel
Left = 5
Height = 17
Top = 83
Width = 391
Align = alTop
BorderSpacing.Left = 5
BorderSpacing.Top = 5
BorderSpacing.Right = 5
Caption = 'CSV Document'
ParentColor = False
end
object lblCellContent: TLabel
Left = 5
Height = 17
Top = 263
Width = 391
Align = alBottom
Anchors = [akLeft, akBottom]
BorderSpacing.Left = 5
BorderSpacing.Top = 5
BorderSpacing.Right = 5
Caption = 'Selected cell content'
ParentColor = False
end
object pnButtons: TPanel
Left = 0
Height = 39
Top = 100
Width = 401
Align = alTop
AutoSize = True
ClientHeight = 39
ClientWidth = 401
TabOrder = 7
object btnSave: TButton
AnchorSideLeft.Control = cbbDelimiter
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = pnButtons
Left = 116
Height = 27
Top = 6
Width = 85
AutoSize = True
BorderSpacing.Left = 5
BorderSpacing.Top = 5
BorderSpacing.Bottom = 5
Caption = 'Save'
OnClick = btnSaveClick
TabOrder = 0
end
object Delimiter: TLabel
AnchorSideLeft.Control = pnButtons
AnchorSideTop.Control = cbbDelimiter
AnchorSideBottom.Control = cbbDelimiter
AnchorSideBottom.Side = asrBottom
Left = 6
Height = 27
Top = 6
Width = 52
Anchors = [akTop, akLeft, akBottom]
BorderSpacing.Left = 5
Caption = 'Delimiter'
Layout = tlCenter
ParentColor = False
end
object cbbDelimiter: TComboBox
AnchorSideLeft.Control = Delimiter
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = pnButtons
Left = 63
Height = 27
Top = 6
Width = 48
BorderSpacing.Left = 5
BorderSpacing.Top = 5
BorderSpacing.Bottom = 5
ItemHeight = 0
ItemIndex = 0
Items.Strings = (
';'
','
)
OnChange = cbbDelimiterChange
TabOrder = 1
Text = ';'
end
object btnLoad: TButton
AnchorSideLeft.Control = btnSave
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = pnButtons
Left = 206
Height = 27
Top = 6
Width = 85
AutoSize = True
BorderSpacing.Left = 5
BorderSpacing.Top = 5
BorderSpacing.Bottom = 5
Caption = 'Load'
OnClick = btnLoadClick
TabOrder = 2
end
end
object UpdateTimer: TIdleTimer
Interval = 100
OnTimer = UpdateTimerTimer
left = 24
top = 176
end
object SaveDialog: TSaveDialog
DefaultExt = '.csv'
Filter = 'CSV files|*.csv'
left = 96
top = 176
end
object OpenDialog: TOpenDialog
Filter = 'CSV files|*.csv'
left = 168
top = 176
end
end

View File

@ -0,0 +1,139 @@
unit mainfrm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
Grids, StdCtrls, ExtCtrls, CsvDocument;
type
TfmCSVTest = class(TForm)
btnSave: TButton;
cbbDelimiter: TComboBox;
Delimiter: TLabel;
SaveDialog: TSaveDialog;
sgView: TStringGrid;
mmSource: TMemo;
splTop: TSplitter;
mmResult: TMemo;
splBottom1: TSplitter;
mmCellValue: TMemo;
splBottom2: TSplitter;
UpdateTimer: TIdleTimer;
lblSource: TLabel;
lblOutput: TLabel;
lblCSVDoc: TLabel;
lblCellContent: TLabel;
pnButtons: TPanel;
btnLoad: TButton;
OpenDialog: TOpenDialog;
procedure btnSaveClick(Sender: TObject);
procedure cbbDelimiterChange(Sender: TObject);
procedure mmSourceChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure sgViewSelectCell(Sender: TObject; aCol, aRow: Integer;
var CanSelect: Boolean);
procedure mmCellValueChange(Sender: TObject);
procedure UpdateTimerTimer(Sender: TObject);
procedure btnLoadClick(Sender: TObject);
private
FDoc: TCSVDocument;
procedure UpdateView;
public
{ public declarations }
end;
var
fmCSVTest: TfmCSVTest;
implementation
{ TfmCSVTest }
procedure TfmCSVTest.mmSourceChange(Sender: TObject);
begin
FDoc.CSVText := mmSource.Text;
UpdateTimer.Enabled := True;
end;
procedure TfmCSVTest.btnSaveClick(Sender: TObject);
begin
if SaveDialog.Execute then
FDoc.SaveToFile(SaveDialog.FileName);
end;
procedure TfmCSVTest.cbbDelimiterChange(Sender: TObject);
begin
FDoc.Delimiter := cbbDelimiter.Text[1];
FDoc.CSVText := mmSource.Text;
UpdateTimer.Enabled := True;
end;
procedure TfmCSVTest.FormCreate(Sender: TObject);
begin
FDoc := TCSVDocument.Create;
FDoc.Delimiter := ';';
end;
procedure TfmCSVTest.FormDestroy(Sender: TObject);
begin
FreeAndNil(FDoc);
end;
procedure TfmCSVTest.sgViewSelectCell(Sender: TObject; aCol, aRow: Integer;
var CanSelect: Boolean);
begin
mmCellValue.OnChange := nil;
mmCellValue.Text := FDoc.Cells[aCol, aRow];
mmCellValue.OnChange := @mmCellValueChange;
end;
procedure TfmCSVTest.mmCellValueChange(Sender: TObject);
begin
if not Assigned(FDoc) then Exit; // qt workaround
FDoc.Cells[sgView.Col, sgView.Row] := mmCellValue.Text;
UpdateTimer.Enabled := True;
end;
procedure TfmCSVTest.UpdateTimerTimer(Sender: TObject);
begin
UpdateView;
mmResult.Text := FDoc.CSVText;
UpdateTimer.Enabled := False;
end;
procedure TfmCSVTest.btnLoadClick(Sender: TObject);
begin
if OpenDialog.Execute then
FDoc.LoadFromFile(OpenDialog.FileName);
end;
procedure TfmCSVTest.UpdateView;
var
i, j: Integer;
begin
sgView.BeginUpdate;
try
i := FDoc.RowCount;
if sgView.RowCount <> i then
sgView.RowCount := i;
i := FDoc.MaxColCount;
if sgView.ColCount <> i then
sgView.ColCount := i;
for i := 0 to FDoc.RowCount - 1 do
for j := 0 to sgView.ColCount - 1 do
sgView.Cells[j, i] := FDoc.Cells[j, i];
finally
sgView.EndUpdate;
end;
end;
initialization
{$I mainfrm.lrs}
end.

View File

@ -0,0 +1,38 @@
=== TODO ===
* Write more tests for different CSV variations
=== Warning about speed optimizations ===
A try to speed up buffer operations (FCellBuffer, FWhitespaceBuffer)
by memory preallocation using straightforward String Builder implementation
resulted in about 25% slowdown compared with current implementation based
on string concatenation. This happened on Linux and was not tested on other
platforms. These changes were not commited.
Using TStrBuf object (http://freepascal-bits.blogspot.com/2010/02/simple-string-buffer.html)
for the same purpose showed neither noticable performance improvement nor a slowdown with
the following results on 5,4 MB CSV file:
Without StrBuf: 2392, 2363, 2544, 2441, 2422, 2407, 2467 ms
With StrBuf: 2423, 2437, 2404, 2471, 2405 ms
This happened on Linux too and was not tested on other platforms.
These changes were not commited either.
=== Warning about CSV extensions like escaping special chars and line breaks ===
There are more problems in implementing them than it seems at first glance:
* It should be clearly defined what escaping scheme should be used:
- what characters must be escaped,
- what escaped characters have special meaning (like \r and \n),
- how to include these special characters into text
i.e. how to escape escaping (like \\).
* It should be clearly defined whether/how escaping can be mixed with
traditional quotation scheme and what should take precedence.
Consider the following examples:
"quoted \"" field"
"embedded \, delimiter"
embedded \, delimiter
"embedded \\, delimiter"
\w\w\wescaped non-trimmable whitespace\w\w\w
" quoted non-trimmable whitespace "
=== Links ===
http://tools.ietf.org/html/rfc4180#section-2
http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm#FileFormat

View File

@ -0,0 +1,75 @@
{{CsvDocument}}
=== About ===
CsvDocument library is a unit contaning set of classes for [http://en.wikipedia.org/wiki/Comma-separated_values CSV files] handling. The library was created to exchange data with OpenOffice Calc / MS Office Excel using CSV as intermediate format.
Its main characteristics are:
* Random read/write access to CSV fields based on object model (TCSVDocument class). This requires preloading CSV file content into memory but allows to edit any field at any moment.
* Serial access (field-by-field) parsing of CSV files using TCSVParser class. It helps to read CSV file content directly without preloading the whole file into memory.
* Field-by-field building of CSV files using TCSVBuilder class. This helps to write CSV content directly to file and thus avoid using in-memory buffer. Implemented in version 0.4 and above.
* CSV implementation is compatible with such of OpenOffice Calc / MS Office Excel, that means CSV files saved from Calc/Excel can be edited using CsvDocument library and vice versa.
* Both UTF-8 encoding and windows-xxx codepages can be used with CsvDocument library. The library uses ''string'' type for all string operations and will not do any encoding conversion for you. Keep in mind though that Excel does not support CSV files in UTF-8.
* Support for line breaks embedded into CSV fields. It was one of the reasons to reinvent the wheel. OO Calc supports this feature as well, but MS Excel does not.
* StringGrid-like field access with TCsvDocument.Cells[ACol, ARow]. Field access is safe, that means when you try to access non-existing field you do not get "Index out of bounds" exception, but get an empty string result. To distingush between empty and non-existing fields there are methods and properties like HasRow/HasCell and RowCount/ColCount.
* Support for search in single row/column using IndexOfRow(AString, ACol) / IndexOfCol(AString, ARow). Implemented in version 0.3 and above.
* No additional limits on field length, number of rows/fields, etc. other than performance, memory usage, ''string'' type limit of 2 Gb and signed 32 bit integer value limit.
* Compliance with RFC 4180 (http://tools.ietf.org/html/rfc4180#section-2) using default settings, as of version 0.4 and above.
* Compliance with unofficial CSV specification (http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm#FileFormat), as of version 0.3 and above. Requires setting IgnoreOuterWhitespace to True in version 0.4 and above.
=== Author ===
Vladimir Zhirov
=== Contributors ===
Luiz Americo Pereira Camara
=== License ===
[http://svn.freepascal.org/svn/lazarus/trunk/COPYING.modifiedLGPL Modified] [http://svn.freepascal.org/svn/lazarus/trunk/COPYING.LGPL LGPL] (same as FPC RTL and Lazarus LCL).
=== Dependencies ===
The library is a single Pascal source file that only depends on FPC RTL and FCL.
=== Change Log ===
==== Version 0.1 (2010-01-22) ====
* initial release.
==== Version 0.2 (2010-05-31) ====
* fixed bug in line ending conversion.
* fixed compilation with range checking on.
* exposed QuoteCSVString function to simplify CSV generation without TCsvDocument class.
* minor code cleanup.
==== Version 0.3 (2011-01-14) ====
* CsvParser API changed: callback methods were replaced by ResetParser/ParseNextCell methods to allow using the library in pure functional programs. See example of new API usage in TCSVDocument.LoadFromStream.
* CsvDocument API changed: TrimEmptyCells were renamed to RemoveTrailingEmptyCells
* support for trimming leading and trailing whitespace in fields. New TrimWhitespace property added. '''Warning''': TrimWhitespace is enabled by default.
* support for search in single row/column using IndexOfRow(AString, ACol) / IndexOfCol(AString, ARow).
* performance improvements.
* code cleanup.
* added simple test suite.
==== Version 0.4 (2011-01-31) ====
* replaced QuoteCSVString function with TCSVBuilder class. See example of its usage in TCSVDocument.SaveToStream.
* renamed TrimWhitespace to IgnoreOuterWhitespace (to be consistent with newly introduced QuoteOuterWhitespace property).
* support for equal column count throughout the document (required by RFC 4180). See new EqualColCountPerRow property.
* changed default settings to RFC 4180 compliant ones (comma as Delimiter, double quote as QuoteChar, CRLF as line ending, ignoring outer whitespace off, equal column count on).
* implemented conversion of line endings embedded into fields when parsing and building a CSV file. It helps to prevent mixed line endings in output file when using OS-specific line endings and working with the same file on multiple OSes.
* moved CSV format settings to TCSVHandler class (an ancestor of TCSVParser, TCSVBuilder and TCSVDocument).
* updated test suite for RFC 4180 compliance.
* fixed minor annoyances in demo application.
* optimizations, demo improvements and Lazarus package by Luiz Americo Pereira Camara.
=== Status ===
Beta
=== Download ===
The latest release is available at lazarus-ccr:
http://sourceforge.net/projects/lazarus-ccr/files/CsvDocument/CsvDocument%200.4/csvdocument-0.4.zip/download
The download contains the library and demo application.
=== Installation and usage ===
Include unit CsvDocument in the ''uses'' section. Set path for it in project settings if needed.
=== Contact ===
Please send bug reports and patches to the e-mail you find in the unit source code.

View File

@ -0,0 +1,75 @@
{{CsvDocument}}
=== О библиотеке ===
Библиотека CsvDocument - это модуль Free Pascal, содержащий набор классов для работы с файлами в [http://ru.wikipedia.org/wiki/CSV формате CSV]. Библиотека была разработана для обмена табличными данными с OpenOffice Calc / MS Office Excel с использованием CSV в качестве промежуточного формата.
Возможности:
* Чтение/запись произвольного поля с помощью объектной модели CSV-документа (класс TCSVDocument). Данный подход требует предварительной загрузки CSV-файла в память.
* Последовательное чтение файла CSV (поле за полем) с помощью класса TCSVParser. Позволяет читать содержимое файла CSV напрямую, без предварительной загрузки всего файла в память.
* Последовательная запись файла CSV (поле за полем) с помощью класса TCSVBuilder. Позволяет записывать содержимое документа напрямую в файл, без буферизации в памяти. Реализована в версии 0.4 и выше.
* Реализация CSV совместима с реализацией в OpenOffice Calc / MS Office Excel, т.е. CSV-файлы, сохранённые из Calc/Excel, можно читать с помощью CsvDocument, и наоборот.
* Поддерживается как кодировка UTF-8, так и кодовые страницы windows-xxx. Библиотека использует тип ''string'' для всех строковых операций и сама не осуществляет каких-либо преобразований. Следует помнить, что Excel не поддерживает CSV-файлы в кодировке UTF-8.
* Поддержка переводов строки внутри полей CSV. В том числе из-за этого пришлось изобретать велосипед. Следует помнить, что из пары OO Calc / MS Excel переводы строки в полях поддерживает только Calc.
* Доступ к полям по аналогии с компонентом StringGrid: TCsvDocument.Cells[ACol, ARow]. Доступ к полям безопасен, т.е. при чтении несуществующего поля возвращается пустая строка, а не вызывается исключение типа "Index out of bounds". Для того, чтобы отличить пустое поле от несуществующего, можно воспользоваться методами HasRow/HasCell и свойствами RowCount/ColCount.
* Поддержка поиска по строке/столбцу с помощью методов IndexOfRow(AString, ACol) / IndexOfCol(AString, ARow). Реализована в версии 0.3 и выше.
* Нет искусственных ограничений на длину поля, число строк/столбцов, и т.д. Естественные ограничения сохраняются: производительность системы, объём свободной памяти, ограничение в 2 Гб на длину строки типа ''string'', и ограничение на максимальное значение 32-разрядного знакового целого типа.
* Соответствие RFC 4180 (http://tools.ietf.org/html/rfc4180#section-2) при использовании настроек по умолчанию. Реализовано в версии 0.4 и выше.
* Соответствие неофициальной спецификации формата CSV (http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm#FileFormat). Реализовано в версии 0.3 и выше. Начиная с версии 0.4, требует установки свойства IgnoreOuterWhitespace в True.
=== Автор ===
Владимир Жиров
=== Участники, внесшие вклад в развитие библитеки ===
Luiz Américo Pereira Câmara
=== Лицензия ===
[http://svn.freepascal.org/svn/lazarus/trunk/COPYING.modifiedLGPL Modified] [http://svn.freepascal.org/svn/lazarus/trunk/COPYING.LGPL LGPL] (та же, что у FPC RTL и Lazarus LCL).
=== Зависимости ===
Библиотека представляет собой один файл с исходным кодом и зависит только от FPC RTL и FCL.
=== Изменения ===
==== Версия 0.1 (22.01.2010) ====
* первый выпуск
==== Версия 0.2 (31.05.2010) ====
* исправлено некорректное преобразование символов конца строки
* исправлена компиляция со включенными проверками диапазона (Range checks)
* стала доступна функция QuoteCSVString, упрощающая генерацию CSV без использования класса TCsvDocument
* выполнена небольшая чистка кода
==== Версия 0.3 (14.01.2011) ====
* изменён API CsvParser: callback-методы заменены на пару методов ResetParser/ParseNextCell, чтобы библиотеку можно было использовать в функциональных программах (без объявления объектов). Пример использования нового API можно посмотреть в методе TCSVDocument.LoadFromStream.
* изменён API CsvDocument: метод TrimEmptyCells переименован в RemoveTrailingEmptyCells
* поддерживается удаление пробельных символов в начале и конце поля. Добавлено новое свойство TrimWhitespace. '''Внимание''': TrimWhitespace по умолчанию включено.
* поддерживается поиск по строке/столбцу с помощью методов IndexOfRow(AString, ACol) / IndexOfCol(AString, ARow)
* улучшена производительность
* выполнена чистка кода
* добавлен набор простых тестов
==== Версия 0.4 (20.01.2011) ====
* функция QuoteCSVString заменена классом TCSVBuilder. Пример его использования можно посмотреть в методе TCSVDocument.SaveToStream.
* свойство TrimWhitespace переименовано в IgnoreOuterWhitespace (для соответствия с новым свойством QuoteOuterWhitespace).
* поддержка одинакового числа столбцов во всём документе (требуется для соответствия RFC 4180). См. свойство EqualColCountPerRow.
* настройки по умолчанию изменены на соответствующие RFC 4180 (Delimiter - запятая, QuoteChar - двойная кавычка, окончания строк - CRLF, удаление пробельных символов в начале и конце поля выключено, одинаковое число столбцов во всём документе включено).
* реализовано преобразование окончаний строк, включенных в содержимое полей, при разборе и генерации CSV-файла. Это позволяет избежать смешения различных окончаний строк в выходном файле при использовании принятых в ОС окончаний строк и работе с одним и тем же документом под разными ОС.
* параметры формата CSV перемещены в класс TCSVHandler (предка классов TCSVParser, TCSVBuilder и TCSVDocument).
* набор тестов обновлён для соответствия RFC 4180.
* исправлены мелкие недоработки в demo-приложении.
* оптимизация, улучшенное demo-приложение и пакет для Lazarus от Luiz Américo Pereira Câmara.
=== Статус ===
Beta
=== Загрузка ===
Последнюю версию можно загрузить с lazarus-ccr:
http://sourceforge.net/projects/lazarus-ccr/files/CsvDocument/CsvDocument%200.4/csvdocument-0.4.zip/download
Архив содержит саму библиотеку и демонстрационное приложение.
=== Установка и использование ===
Достаточно добавить модуль CsvDocument в секцию ''uses''. Если необходимо, можно установить путь к модулю в свойствах проекта.
=== Обратная связь ===
Просьба отправлять сообщения об ошибках и патчи по адресу, указанному в исходном коде библиотеки.

View File

@ -0,0 +1,481 @@
GNU LIBRARY GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1991 Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
[This is the first released version of the library GPL. It is
numbered 2 because it goes with version 2 of the ordinary GPL.]
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
Licenses are intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.
This license, the Library General Public License, applies to some
specially designated Free Software Foundation software, and to any
other libraries whose authors decide to use it. You can use it for
your libraries, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if
you distribute copies of the library, or if you modify it.
For example, if you distribute copies of the library, whether gratis
or for a fee, you must give the recipients all the rights that we gave
you. You must make sure that they, too, receive or can get the source
code. If you link a program with the library, you must provide
complete object files to the recipients so that they can relink them
with the library, after making changes to the library and recompiling
it. And you must show them these terms so they know their rights.
Our method of protecting your rights has two steps: (1) copyright
the library, and (2) offer you this license which gives you legal
permission to copy, distribute and/or modify the library.
Also, for each distributor's protection, we want to make certain
that everyone understands that there is no warranty for this free
library. If the library is modified by someone else and passed on, we
want its recipients to know that what they have is not the original
version, so that any problems introduced by others will not reflect on
the original authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that companies distributing free
software will individually obtain patent licenses, thus in effect
transforming the program into proprietary software. To prevent this,
we have made it clear that any patent must be licensed for everyone's
free use or not licensed at all.
Most GNU software, including some libraries, is covered by the ordinary
GNU General Public License, which was designed for utility programs. This
license, the GNU Library General Public License, applies to certain
designated libraries. This license is quite different from the ordinary
one; be sure to read it in full, and don't assume that anything in it is
the same as in the ordinary license.
The reason we have a separate public license for some libraries is that
they blur the distinction we usually make between modifying or adding to a
program and simply using it. Linking a program with a library, without
changing the library, is in some sense simply using the library, and is
analogous to running a utility program or application program. However, in
a textual and legal sense, the linked executable is a combined work, a
derivative of the original library, and the ordinary General Public License
treats it as such.
Because of this blurred distinction, using the ordinary General
Public License for libraries did not effectively promote software
sharing, because most developers did not use the libraries. We
concluded that weaker conditions might promote sharing better.
However, unrestricted linking of non-free programs would deprive the
users of those programs of all benefit from the free status of the
libraries themselves. This Library General Public License is intended to
permit developers of non-free programs to use free libraries, while
preserving your freedom as a user of such programs to change the free
libraries that are incorporated in them. (We have not seen how to achieve
this as regards changes in header files, but we have achieved it as regards
changes in the actual functions of the Library.) The hope is that this
will lead to faster development of free libraries.
The precise terms and conditions for copying, distribution and
modification follow. Pay close attention to the difference between a
"work based on the library" and a "work that uses the library". The
former contains code derived from the library, while the latter only
works together with the library.
Note that it is possible for a library to be covered by the ordinary
General Public License rather than by this special one.
GNU LIBRARY GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any software library which
contains a notice placed by the copyright holder or other authorized
party saying it may be distributed under the terms of this Library
General Public License (also called "this License"). Each licensee is
addressed as "you".
A "library" means a collection of software functions and/or data
prepared so as to be conveniently linked with application programs
(which use some of those functions and data) to form executables.
The "Library", below, refers to any such software library or work
which has been distributed under these terms. A "work based on the
Library" means either the Library or any derivative work under
copyright law: that is to say, a work containing the Library or a
portion of it, either verbatim or with modifications and/or translated
straightforwardly into another language. (Hereinafter, translation is
included without limitation in the term "modification".)
"Source code" for a work means the preferred form of the work for
making modifications to it. For a library, complete source code means
all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control compilation
and installation of the library.
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running a program using the Library is not restricted, and output from
such a program is covered only if its contents constitute a work based
on the Library (independent of the use of the Library in a tool for
writing it). Whether that is true depends on what the Library does
and what the program that uses the Library does.
1. You may copy and distribute verbatim copies of the Library's
complete source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty; keep intact
all the notices that refer to this License and to the absence of any
warranty; and distribute a copy of this License along with the
Library.
You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for a
fee.
2. You may modify your copy or copies of the Library or any portion
of it, thus forming a work based on the Library, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) The modified work must itself be a software library.
b) You must cause the files modified to carry prominent notices
stating that you changed the files and the date of any change.
c) You must cause the whole of the work to be licensed at no
charge to all third parties under the terms of this License.
d) If a facility in the modified Library refers to a function or a
table of data to be supplied by an application program that uses
the facility, other than as an argument passed when the facility
is invoked, then you must make a good faith effort to ensure that,
in the event an application does not supply such function or
table, the facility still operates, and performs whatever part of
its purpose remains meaningful.
(For example, a function in a library to compute square roots has
a purpose that is entirely well-defined independent of the
application. Therefore, Subsection 2d requires that any
application-supplied function or table used by this function must
be optional: if the application does not supply it, the square
root function must still compute square roots.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Library,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Library, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote
it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Library.
In addition, mere aggregation of another work not based on the Library
with the Library (or with a work based on the Library) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may opt to apply the terms of the ordinary GNU General Public
License instead of this License to a given copy of the Library. To do
this, you must alter all the notices that refer to this License, so
that they refer to the ordinary GNU General Public License, version 2,
instead of to this License. (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.) Do not make any other change in
these notices.
Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.
This option is useful when you wish to copy part of the code of
the Library into a program that is not a library.
4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable form
under the terms of Sections 1 and 2 above provided that you accompany
it with the complete corresponding machine-readable source code, which
must be distributed under the terms of Sections 1 and 2 above on a
medium customarily used for software interchange.
If distribution of object code is made by offering access to copy
from a designated place, then offering equivalent access to copy the
source code from the same place satisfies the requirement to
distribute the source code, even though third parties are not
compelled to copy the source along with the object code.
5. A program that contains no derivative of any portion of the
Library, but is designed to work with the Library by being compiled or
linked with it, is called a "work that uses the Library". Such a
work, in isolation, is not a derivative work of the Library, and
therefore falls outside the scope of this License.
However, linking a "work that uses the Library" with the Library
creates an executable that is a derivative of the Library (because it
contains portions of the Library), rather than a "work that uses the
library". The executable is therefore covered by this License.
Section 6 states terms for distribution of such executables.
When a "work that uses the Library" uses material from a header file
that is part of the Library, the object code for the work may be a
derivative work of the Library even though the source code is not.
Whether this is true is especially significant if the work can be
linked without the Library, or if the work is itself a library. The
threshold for this to be true is not precisely defined by law.
If such an object file uses only numerical parameters, data
structure layouts and accessors, and small macros and small inline
functions (ten lines or less in length), then the use of the object
file is unrestricted, regardless of whether it is legally a derivative
work. (Executables containing this object code plus portions of the
Library will still fall under Section 6.)
Otherwise, if the work is a derivative of the Library, you may
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.
6. As an exception to the Sections above, you may also compile or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
under terms of your choice, provided that the terms permit
modification of the work for the customer's own use and reverse
engineering for debugging such modifications.
You must give prominent notice with each copy of the work that the
Library is used in it and that the Library and its use are covered by
this License. You must supply a copy of this License. If the work
during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference
directing the user to the copy of this License. Also, you must do one
of these things:
a) Accompany the work with the complete corresponding
machine-readable source code for the Library including whatever
changes were used in the work (which must be distributed under
Sections 1 and 2 above); and, if the work is an executable linked
with the Library, with the complete machine-readable "work that
uses the Library", as object code and/or source code, so that the
user can modify the Library and then relink to produce a modified
executable containing the modified Library. (It is understood
that the user who changes the contents of definitions files in the
Library will not necessarily be able to recompile the application
to use the modified definitions.)
b) Accompany the work with a written offer, valid for at
least three years, to give the same user the materials
specified in Subsection 6a, above, for a charge no more
than the cost of performing this distribution.
c) If distribution of the work is made by offering access to copy
from a designated place, offer equivalent access to copy the above
specified materials from the same place.
d) Verify that the user has already received a copy of these
materials or that you have already sent this user a copy.
For an executable, the required form of the "work that uses the
Library" must include any data and utility programs needed for
reproducing the executable from it. However, as a special exception,
the source code distributed need not include anything that is normally
distributed (in either source or binary form) with the major
components (compiler, kernel, and so on) of the operating system on
which the executable runs, unless that component itself accompanies
the executable.
It may happen that this requirement contradicts the license
restrictions of other proprietary libraries that do not normally
accompany the operating system. Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.
7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
library, provided that the separate distribution of the work based on
the Library and of the other library facilities is otherwise
permitted, and provided that you do these two things:
a) Accompany the combined library with a copy of the same work
based on the Library, uncombined with any other library
facilities. This must be distributed under the terms of the
Sections above.
b) Give prominent notice with the combined library of the fact
that part of it is a work based on the Library, and explaining
where to find the accompanying uncombined form of the same work.
8. You may not copy, modify, sublicense, link with, or distribute
the Library except as expressly provided under this License. Any
attempt otherwise to copy, modify, sublicense, link with, or
distribute the Library is void, and will automatically terminate your
rights under this License. However, parties who have received copies,
or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
9. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Library or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Library (or any work based on the
Library), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Library or works based on it.
10. Each time you redistribute the Library (or any work based on the
Library), the recipient automatically receives a license from the
original licensor to copy, distribute, link with or modify the Library
subject to these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
11. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Library at all. For example, if a patent
license would not permit royalty-free redistribution of the Library by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Library.
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply,
and the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
12. If the distribution and/or use of the Library is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Library under this License may add
an explicit geographical distribution limitation excluding those countries,
so that distribution is permitted only in or among countries not thus
excluded. In such case, this License incorporates the limitation as if
written in the body of this License.
13. The Free Software Foundation may publish revised and/or new
versions of the Library General Public License from time to time.
Such new versions will be similar in spirit to the present version,
but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Library
specifies a version number of this License which applies to it and
"any later version", you have the option of following the terms and
conditions either of that version or of any later version published by
the Free Software Foundation. If the Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.
14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
write to the author to ask for permission. For software which is
copyrighted by the Free Software Foundation, write to the Free
Software Foundation; we sometimes make exceptions for this. Our
decision will be guided by the two goals of preserving the free status
of all derivatives of our free software and of promoting the sharing
and reuse of software generally.
NO WARRANTY
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Libraries
If you develop a new library, and you want it to be of the greatest
possible use to the public, we recommend making it free software that
everyone can redistribute and change. You can do so by permitting
redistribution under these terms (or, alternatively, under the terms of the
ordinary General Public License).
To apply these terms, attach the following notices to the library. It is
safest to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the library's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
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.
This library 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Also add information on how to contact you by electronic and paper mail.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the library, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the
library `Frob' (a library for tweaking knobs) written by James Random Hacker.
<signature of Ty Coon>, 1 April 1990
Ty Coon, President of Vice
That's all there is to it!

View File

@ -0,0 +1,12 @@
*.pas
*.lpi
*.lpr
*.lfm
*.txt
*.csv
*.lpk
csvdocument/demo/csvdemo.res
csvdocument/testsuite/tests/msexcel/
csvdocument/testsuite/tests/oocalc/
csvdocument/testsuite/tests/rfc4180/
csvdocument/testsuite/tests/unofficial/

View File

@ -0,0 +1,3 @@
#!/bin/bash
cd ..
zip -r ./csvdocument/csvdocument-0.4.zip csvdocument -i@csvdocument/release.lst

View File

@ -0,0 +1,90 @@
<?xml version="1.0"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<ResourceType Value="res"/>
</General>
<i18n>
<EnableI18N LFM="False"/>
</i18n>
<VersionInfo>
<StringTable ProductVersion=""/>
</VersionInfo>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
<ExcludeFileFilter Value="*.(bak|ppu|o|so);*~;backup"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
<LaunchingApplication PathPlusParams="/usr/bin/xterm -T 'Lazarus Run Output' -e $(LazarusDir)/tools/runwait.sh $(TargetCmdLine)"/>
</local>
</RunParams>
<Units Count="1">
<Unit0>
<Filename Value="testcsvdoc.lpr"/>
<IsPartOfProject Value="True"/>
<UnitName Value="testcsvdoc"/>
</Unit0>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="9"/>
<Target>
<Filename Value="testcsvdoc"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value=".."/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<CStyleOperator Value="False"/>
</SyntaxOptions>
</Parsing>
<CodeGeneration>
<Checks>
<IOChecks Value="True"/>
<RangeChecks Value="True"/>
<OverflowChecks Value="True"/>
<StackChecks Value="True"/>
</Checks>
</CodeGeneration>
<Linking>
<Debugging>
<UseExternalDbgSyms Value="True"/>
</Debugging>
</Linking>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

View File

@ -0,0 +1,114 @@
program testcsvdoc;
{$mode objfpc}{$H+}
uses
SysUtils, Classes, CsvDocument, DateUtils;
function ReadStringFromFile(AFileName: string): string;
var
FileStream: TFileStream;
Size: Integer;
begin
Result := '';
if not FileExists(AFileName) then
Exit;
FileStream := TFileStream.Create(AFileName, fmOpenRead);
Size := FileStream.Size;
if Size > 0 then
begin
SetLength(Result, Size);
FileStream.ReadBuffer(Result[1], Size);
end;
FreeAndNil(FileStream);
end;
procedure FindTestFiles(AFileList: TStringList; const ASpec: string);
var
SearchRec: TSearchRec;
TestFilesPath: String;
begin
AFileList.Clear;
TestFilesPath := IncludeTrailingPathDelimiter(GetCurrentDir)
+ 'tests' + DirectorySeparator + ASpec + DirectorySeparator;
if FindFirst(TestFilesPath + '*.csv', faAnyFile, SearchRec) = 0 then
repeat
AFileList.Add(TestFilesPath + SearchRec.Name);
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
procedure TestCsvFile(const AFilename: String; ADocument: TCSVDocument);
var
InBuffer, OutBuffer: String;
SampleBuffer: String;
CsvDoc: TCSVDocument;
Start: TDateTime;
MSec: Int64;
begin
InBuffer := ReadStringFromFile(AFilename);
SampleBuffer := ReadStringFromFile(ChangeFileExt(AFilename,
'.sample' + ExtractFileExt(AFilename)));
if SampleBuffer = '' then
SampleBuffer := InBuffer;
ADocument.CSVText := '';
Start := Now;
ADocument.CSVText := InBuffer;
MSec := MilliSecondsBetween(Start, Now);
OutBuffer := ADocument.CSVText;
Write(ExtractFileName(AFilename));
if OutBuffer = InBuffer then
begin
Write(': ok');
WriteLn(' (parsed in ', MSec, ' ms)');
end else
begin
WriteLn(': FAILED');
WriteLn('--- Expected: ---');
WriteLn(SampleBuffer);
WriteLn('--- Got: --------');
WriteLn(OutBuffer);
WriteLn('-----------------');
end;
end;
procedure PerformTests(ADocument: TCSVDocument; const ASpec: String);
var
I: Integer;
TestFiles: TStringList;
begin
WriteLn('== Format: ', ASpec, ' ==');
TestFiles := TStringList.Create;
FindTestFiles(TestFiles, ASpec);
for I := 0 to TestFiles.Count - 1 do
TestCsvFile(TestFiles[I], ADocument);
FreeAndNil(TestFiles);
WriteLn();
end;
var
CsvDoc: TCSVDocument;
begin
WriteLn('Testing CSVDocument');
WriteLn('-------------------');
CsvDoc := TCSVDocument.Create;
// no setup needed, rfc4180 supported out-of-the-box
PerformTests(CsvDoc, 'rfc4180');
// setup for unofficial Creativyst spec
PerformTests(CsvDoc, 'unofficial');
// setup for MS Excel files
PerformTests(CsvDoc, 'msexcel');
// setup for OOo Calc files
PerformTests(CsvDoc, 'oocalc');
FreeAndNil(CsvDoc);
WriteLn('------------------');
WriteLn('All tests complete');
end.

View File

@ -0,0 +1,2 @@
a,b,c
1,2,3
1 a b c
2 1 2 3

View File

@ -0,0 +1,2 @@
,a,b,c,,,
,1,,2,,3,
1 a b c
2 1 2 3

View File

@ -0,0 +1,3 @@
a,"b
c",d
1,2,3
1 a b c d
2 1 2 3

View File

@ -0,0 +1,2 @@
" a "," b "," c "," "," "
"1 "," "," 2 "," "," 3 "
1 a b c
2 1 2 3

View File

@ -0,0 +1,2 @@
a,"b""c",d
1,2,3
1 a b"c d
2 1 2 3