Re: Jcl String Function

"Paul Di Biasio" <[email protected]> Sun, 15 May 2005 10:31:17 -0000
Newsgroups gmane.comp.lang.delphi.jedi
Message-ID <[email protected]>
I wrote the code years ago to handle all my ascii importing needs.  It
can accept either delimited files (CSV) or fixed column fields.  It
parses the data into a string list that can be accessed the way you want.

MyParsedList:=TParsedList.create;
MyParsedList.add('123,345,this is text,etc...
NinthField:=MyParsedList.strings[9];
MyParsedList.free;

Paul D.
Annandale, VA

------------------------------------------------------------------

TParsedList = class(TStringList)
   private
     FConvertCase: TConvertCase;
     columnwidth: TList;
     procedure SetColumnWidth(w: word);
   protected
   public
     delimiter: char;
     constructor Create; virtual;
     constructor Destroy; virtual;
     function AsDateTime(i: word):TDateTime;
     function AsString(i: word): string;
     function AsLeftPartString(i: word; c: integer): string;
     function AsRealNumber(i: word): real;
     function AsInteger(i: word): integer;
     function Add(const S: string): LongInt; virtual;
     property ConvertCase: TConvertCase read FConvertCase write
FConvertCase;
     property width: word write SetColumnWidth;
   end;

constructor TParsedList.Create;
begin
   inherited Create;
   Delimiter:=',';
   sorted:=false;
   duplicates:=dupAccept;
   columnwidth:=TList.create;
end;

constructor TParsedList.Destroy;
begin
   columnwidth.free;
   inherited Destroy;
end;

function TParsedList.AsDateTime(i: word):TDateTime;
begin
   result:=StrToDate(strings[i]);
end;

function TParsedList.AsInteger(i: word): integer;
begin
   result:=StrToInt(trim(strings[i]));
end;

function TParsedList.AsString(i: word):string;
begin
   case FConvertCase of
     csUpper:  result:=UpperCase(trim(strings[i]));
     csLower:  result:=LowerCase(trim(strings[i]));
     csProper: result:=ProperCase(trim(strings[i]));
     csNone:   result:=trim(strings[i]);
   end;
end;

function TParsedList.AsLeftPartString(i: word; c: integer): string;
var
   s: string;
begin
   s:=AsString(i);
   result:=copy(s,1,length(s)-c);
   s:=result;
end;

function TParsedList.AsRealNumber(i: word):real;
begin
   result:=AsReal(strings[i]);
end;

procedure TParsedList.SetColumnWidth(w: word);
begin
   columnwidth.add(pointer(w));
end;

function TParsedList.Add(const S: string): LongInt;
var
  ParsedStr,TempStr: string;
  i, w, startpos : integer;

begin
  TempStr:=S;

  if columnwidth.count>0 then begin
     startpos:=1;
     inherited Clear;
     for i:=0 to columnwidth.count-1 do
     begin
        w:=integer(columnwidth.items[i]);
        result:=inherited Add(copy(TempStr,startpos,w));
        startpos:=startpos+w;
     end;
     exit;
  end;

  if delimiter=#0 then
     begin
        TempStr:=Trim(TempStr);
        case ConvertCase of
          csUpper: TempStr:=UpperCase(TempStr);
          csLower:TempStr:=LowerCase(TempStr);
          csProper:TempStr:=ProperCase(TempStr);
        end;
        if TempStr>'' then
          result:=inherited Add(TempStr)
     end
  else
     begin
        i:=0;
        repeat
           ParsedStr:=Trim(ParseNextStr(i,TempStr,Delimiter));

           case ConvertCase of
             csUpper: ParsedStr:=UpperCase(ParsedStr);
             csLower: ParsedStr:=LowerCase(ParsedStr);
             csProper:ParsedStr:=ProperCase(ParsedStr);
           end;

           if TempStr>'' then
              result:=inherited Add(ParsedStr);
        until i=-1;
     end;
end;





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/Delphi-JEDI/

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/