delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Generic File Importer Base ClassFormat this article printer-friendly!Bookmark function is only available for registered users!
For importing flat files of various formats
Product:
Delphi all versions
Category:
Files Operation
Skill Level:
Scoring:
Last Update:
08/23/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet flat-file file-import import ascii virutal override
Times Scored:
1
Visits:
2412
Uploader: Stewart Moss
Company: New Heights Software Developme
Reference: http://www.new-heights.co.za/Delphi/Components
 
Question/Problem/Abstract:
Here is a useful base class to create derived classes to import data from any flat file format you can think of...

ENJOY
Answer:




{-----------------------------------------------------------------------------
Unit Name:     classParentDataManipulator
Author:        StewartM (Stewart Moss)

Documentation Date: 23, 08, 2002  (14:39,)

Version 1.0
-----------------------------------------------------------------------------

Compiler Directives:

Purpose:

Dependancies:

Description:

Parent Class for data manipulation
Creates the basic skelton for adding data manipulation sub-classes

Each of the inherited classes must override the ProcessData method and provide
  their own properties specific to the class (ie Invoice Number etc...)

Very useful class.

inheritance Diagram

  + -- TParentDataProcessor    // base class
       +
       |
       + --- TDerivedImporter  // sub class

Notes:

History:

Copyright 2002 by Stewart Moss.
All rights reserved.
-----------------------------------------------------------------------------}


unit classParentDataManipulator;

interface

uses
Sysutils, Classes;

type
  
TParentDataProcessor = class(TObject)
  
private
    
StringIn: string;
    
LineCounter: Integer;

  
public
    
FieldNames,
      
FieldValues,
      
MultiFieldNames,
      
MultiFieldValues: TStringList;

    
FormName,
      
FileName: string;
    
Delimiters: string;
      
// A list of delimiters (ie ',/[];:') used in inherited ProcessData()

    
constructor create;
    
destructor Destroy;  Override;
    
procedure ProcessFile;

    
function DataAtPos(S: string; StartP, EndP: Integer): string;
      
// Returns the data from "StartP" to "EndP" in String "S"

    
function ExpandTabs(s: string): string;
      
// ExpandTabs to 8 Spaces

    
procedure ProcessData(StrIn: string; LineNumber: Integer); virtual;
      
// Virtual method for override in sub-classes

    
procedure FieldAdd(FieldName, Data: string; GenException: Boolean);
      
// Adds FieldName and FieldValue to Strings and can generate exception if
      // string is empty when required

    
procedure MultiFieldAdd(FieldName, Data: string; GenException: Boolean);
      
// Adds FieldName and FieldValue to Multi Field Strings and can generate exception if
      // string is empty when required

  
end;

  
TProcessException = Exception;

implementation

var
F: text;
//  Exception: TProcessException;

{ TDataProcessor }

constructor TParentDataProcessor.create;
begin
  inherited
create;
  
FieldNames := TStringList.Create;
  
FieldValues := TStringList.Create;
  
MultiFieldNames := TStringList.Create;
  
MultiFieldValues := TStringList.Create;
  
FieldNames.Clear;
  
FieldValues.Clear;
  
MultiFieldNames.Clear;
  
MultiFieldValues.Clear;
end;

procedure TParentDataProcessor.ProcessFile;
begin
  if
Filename = '' then
    raise
Exception.Create('No Filename specified');

  
try
    
AssignFile(F, Filename);
    
Reset(f);
  
except
    try
      
CloseFile(F);
    
except
    end
;
    
raise Exception.Create('Could not open file ' + FileName);
  
end;

  
LineCounter := 0;

  
while not eof(f) do
  begin
    
Inc(LineCounter);

    
try
      
Readln(f, StringIn);
    
except
      try
        
CloseFile(f);
      
except // swallow CloseFile errors
      
end;

      
raise Exception.Create('Could not read from file. Line number ' + IntToStr(LineCounter));
    
end;

    
StringIn := ExpandTabs(StringIn);
    
// Exapnd Tabs to 8 Spaces

    
ProcessData(StringIn, LineCounter);
    
// Execute virutal method in sub-classes passing current line and LineNumber
  
end;

  
try
    
closefile(f);
  
except
    raise
Exception.Create('Could not close file ' + FileName);
  
end;
end;

procedure TParentDataProcessor.ProcessData(StrIn: string; LineNumber: Integer);
// Virtual method for override in sub-classes
begin
//
end;

destructor TParentDataProcessor.Destroy;
begin
  
FieldNames.Free;
  
FieldValues.Free;
  
MultiFieldNames.Free;
  
MultiFieldValues.Free;
end;

function TParentDataProcessor.DataAtPos(S: string; StartP,
  
EndP: Integer): string;
begin
  
// Returns the data from "StartP" to "EndP" in String "S"
  
Result := trim(Copy(S, StartP, EndP - StartP));
end;

function TParentDataProcessor.ExpandTabs(s: string): string;
begin
  
// ExpandTabs to 8 Spaces
  
Result := StringReplace(S, #09, '        ', [rfReplaceAll]);
end;

procedure TParentDataProcessor.FieldAdd(FieldName, Data: string;
  
GenException: Boolean);
begin
  
// Adds FieldName and FieldValue to Strings and can generate exception if
  // string is empty
  
if (GenException) and (Data = '') then
    raise
Exception.create('-- No ' + FieldName + ' Specified --');
  
Fieldnames.add(FieldName);
  
FieldValues.add(Data);
end;

procedure TParentDataProcessor.MultiFieldAdd(FieldName, Data: string;
  
GenException: Boolean);

var loop: integer;
  
flag: Boolean;
begin
  
// Adds FieldName and FieldValue to Multi Field Strings and can generate exception if
  // string is empty

  
if (GenException) and (Data = '') then
    raise
Exception.create('-- No Multiple Field - ' + FieldName + ' Specified --');

  
flag := false;
  
loop:=0;
  
while (loop < MultiFieldNames.count ) and not flag do
  begin
    if
MultiFieldNames.Strings[loop] = FieldName then
      
flag := true;
    
inc(Loop);
  
end;

  
dec(loop);

  
if Flag then
    
MultiFieldValues.Strings[loop] := MultiFieldValues.Strings[loop] + ';' + Data
  else
  begin
    
MultiFieldNames.add(FieldName);
    
MultiFieldValues.add(Data);
  
end;
end;

end.







Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
L. Rosenstein
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)