Thema: Delphi CSV Parser (wieder mal)

Einzelnen Beitrag anzeigen

Benutzerbild von Der.Kaktus
Der.Kaktus

Registriert seit: 22. Jan 2008
Ort: Erfurt
958 Beiträge
 
Delphi 7 Enterprise
 
#6

Re: CSV Parser (wieder mal)

  Alt 17. Jun 2009, 15:37
Hallo,
ich nutze seit Delphi4 dieses Tool..mir reicht es und ist einfach.
[c] iss nicht von mir aber Freeware!!!!

Delphi-Quellcode:
unit MyParser;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TMyParser = class(TComponent)
   private
      FieldSep: char;
      FLine: string;
      NumFields: byte;
      Index, Ct: array[0..255] of integer;
      function Getstr(n: byte): string;
         constructor Create( AParent : TComponent ); override;
    public
      property Sepchar: char read FieldSep write FieldSep;
      property count: byte read NumFields;
      property fields[n: byte]: string read Getstr; default;
      procedure Parse(const Line: string);
    end;

procedure Register;

implementation

constructor TMyParser.Create( AParent : TComponent );
begin
  inherited Create(aParent);
  FieldSep:=' ';
  FLine:='';
  NumFields:= 0;
end;

function TMyParser.Getstr(n: byte): string;
  begin
  result := '';
  if n > NumFields then
    exit;
  if n = 0 then
    result := FLine
  else if n > 0 then
   begin
    dec(n); {0-based arrays!}
    if Index[n] > 0 then
      result := copy(FLine, index[n], ct[n]);
   end;
  end;

procedure TMyParser.Parse(const line: string);
var
 i,p : byte;
 s: string;
 s2 : string;
begin
 Fline := Line;
 s:= line;
 NumFields := 0;
 i := pos(FieldSep, s);
 while i > 0 do
   begin
    s2 := copy (s,1,i -1);
    if NumFields = 0 then
     index[NumFields] := 1
    else
     index[NumFields] := pos(s2,fline);
    ct[NumFields] := length(s2);
    delete(s,1,i);
    i := pos(FieldSep, s);
    inc(NumFields);
   end;
end;




procedure Register;
begin
  RegisterComponents('Parser', [TMyParser]);
end;

end.
Anwendung..auch ganz einfach

Delphi-Quellcode:
  MyParser1.sepchar := ';';
  MyParser1.parse(s);
  for i := 1 to MyParser1.count do ListBox1.items.add(MyParser1.fields[i]);
hoffe, hilft Dir
Gruss Kaki

Repeat Until true=false;
  Mit Zitat antworten Zitat