The CSV file is read line by line to limit the amount of memory used by this unit. Default separator is char ',' (comma) Copyright TridenT 2003 - Under GNU GPL licence
{-------------------------------------------------------------------------------
Handle Comma-Separated Values files
The CSV file is read line by line to limit the amount of memory used by this
unit. Default separator is char ',' (comma)
Copyright TridenT 2003 - Under GNU GPL licence
@Author TridenT
@Version 2003/08/11 TridenT v1.0 Initial revision
@Todo Use TStringList to only load a part of the CSV file, and handle
the loading-window
-------------------------------------------------------------------------------}
unit u_CSV_File;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
// CSV Support file
TCSV_File = class
private
CSVFile: TextFile;
FileName: string;
FValueList: TStringList;
FSeparator: char;
procedure SetSeparator(const ch: char);
published
property ValueList: TStringList read FValueList;
property Separator: char read FSeparator write SetSeparator;
public
function Open(const CSVFileName: string): integer;
procedure Close;
function isEOF: boolean;
function ReadNextLine: integer;
procedure Find(const st: string);
end;
implementation
{-------------------------------------------------------------------------------
Open CSV file (read only) and initialize the class.
You must open a file before calling other method (ReadNextLine or Find)
At the end of the process, call Close method.
@param CSVFileName Name of the CVS file to open
@return integer 0 if succesfull, -1 otherwise
-------------------------------------------------------------------------------}
function TCSV_File.Open(const CSVFileName: string): integer;
begin
// Test if file exist
if FileExists(CSVFileName) then
begin
FileName := CSVFileName;
AssignFile(CSVFile, CSVFileName);
FileMode := 0; // Read Only
Reset(CSVFile);
// Initialize Value list
FValueList := TStringList.Create;
Separator := ',';
Result := 0;
end
else
Result := -1;
end;
// Close a CVS file
procedure TCSV_File.Close;
begin
// Close file
System.CloseFile(CSVFile);
FreeAndNil(FValueList);
end;
// Set CVS file separator.
procedure TCSV_File.SetSeparator(const ch: char);
begin
if ch <> '' then FSeparator := ch;
end;
// Test end of file
function TCSV_File.isEOF: boolean;
begin
Result := System.EOF(CSVFile);
end;
{-------------------------------------------------------------------------------
Read next line and extract values
@return integer Number of Values reads. Values are accessible
with <code> FValueList </code>
-------------------------------------------------------------------------------}
function TCSV_File.ReadNextLine: integer;
var
ResultLine, Line: string;
Pos_S: integer;
begin
FValueList.Clear;
Result := 0;
// Read value line
ReadLn(CSVfile, Line);
// Parsing line
while Line <> '' do
begin
Pos_S := Pos(Separator, Line);
if Pos_S = 1 then
// delete separator
Delete(Line, Pos_S, 1)
else
begin
// Test if separator found
if Pos_S <> 0 then
begin
// Isolate value
ResultLine := Copy(Line, 1,Pos_S - 1);
// kill substring
Delete(Line, 1,Pos_S);
end
else
// if no more separator
begin
ResultLine := Line;
Line := '';
end;
FValueList.Add(ResultLine);
Inc(Result);
end;
end;
end;
// Find a string in file
procedure TCSV_File.Find(const st: string);
var
isFound: boolean;
begin
isFound := False;
while not isFound do
if ReadNExtLine <> 0 then isFound := (St = FValueList[0]);
end;
end.
| Class | Description |
| TCSV_File | CSV Support file |