Thema: Delphi CSV Parser (wieder mal)

Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.183 Beiträge
 
Delphi 12 Athens
 
#3

Re: CSV Parser (wieder mal)

  Alt 17. Jun 2009, 14:43
Also wenn man sich nur mal estwas aus der System.pas mopst und etwas umbaut, dann dürfte es so auch gehen.

Nun sollte man sich nur noch schnell die Funktionen etwas passender um und fertig

Delphi-Quellcode:
function GetParamStr(P: PChar; var Param: string): PChar;
var
  i, Len: Integer;
  Start, S: PChar;
begin
  // U-OK
  while True do
  begin
    while (P[0] <> #0) and (P[0] <> ';') do
      Inc(P);
    if (P[0] = '"') and (P[1] = '"') then Inc(P, 2) else Break;
  end;
  Len := 0;
  Start := P;
  while (P[0] <> #0) and (P[0] <> ';') do
  begin
    if P[0] = '"then
    begin
      Inc(P);
      while (P[0] <> #0) and (P[0] <> '"') do
      begin
        Inc(Len);
        Inc(P);
      end;
      if P[0] <> #0 then
        Inc(P);
    end
    else
    begin
      Inc(Len);
      Inc(P);
    end;
  end;

  SetLength(Param, Len);

  P := Start;
  S := Pointer(Param);
  i := 0;
  while (P[0] <> #0) and (P[0] <> ';') do
  begin
    if P[0] = '"then
    begin
      Inc(P);
      while (P[0] <> #0) and (P[0] <> '"') do
      begin
        S[i] := P^;
        Inc(P);
        Inc(i);
      end;
      if P[0] <> #0 then Inc(P);
    end
    else
    begin
      S[i] := P^;
      Inc(P);
      Inc(i);
    end;
  end;

  Result := P;
end;

function ParamCount(const S: String): Integer;
var
  P: PChar;
  S2: string;
begin
  // U-OK
  Result := 0;
  P := PChar(S);
  while True do
  begin
    P := GetParamStr(P, S2);
    if S2 = 'then Break;
    Inc(Result);
  end;
end;

function ParamStr(const S: String; Index: Integer): string;
var
  P: PChar;
begin
    P := PChar(S);
    while True do
    begin
      Dec(Index);
      P := GetParamStr(P, Result);
      if (Index = 0) or (Result = '') then Break;
    end;
  end;
end;
nur die Zeilen muß man dann noch selber einlesen
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat