Einzelnen Beitrag anzeigen

Benutzerbild von SirThornberry
SirThornberry
(Moderator)

Registriert seit: 23. Sep 2003
Ort: Bockwen
12.235 Beiträge
 
Delphi 2006 Professional
 
#16

Re: baum bestehend aus arrays abspeichern

  Alt 13. Mär 2005, 13:24
ok, hier schonmal die speichernprocedure
Delphi-Quellcode:
function LoadKnotFromFile(AFilename: String; AKnot: TKnot): Integer;
  procedure LLoadKnotFromStream(AStream: TStream; APKnot: PKnot; AStrCount: Integer);
  var LChilds, LCount, LInt: Integer;
  begin
    for LCount := 1 to AStrCount do
    begin
      //Länge des Strings lesen
      AStream.Read(LInt, SizeOf(LInt));
      setlength(APKnot.A[LCount], LInt);
      //String aus Stream lesen
      AStream.Read(APKnot.A[LCount][1], LInt);
    end;

    AStream.Read(LChilds, SizeOf(LChilds));
    for LCount := 1 to 11 do
    begin
      if LCount <= LChilds then
      begin
        //speicher für unterknoten anfordern
        new(APKnot.S[LCount]);
        LLoadKnotFromStream(AStream, APKnot.S[LCount], AStrCount);
      end else
        APKnot.S[LCount] := nil;
    end;
  end;
var LStream: TMemoryStream;
    LStrCount: Integer;
begin
  LStream := TMemoryStream.Create;
  LStream.LoadFromFile(AFilename);
  LStream.Read(LStrCount, SizeOf(LStrCount));
  LLoadKnotFromStream(LStream, @AKnot, LStrCount);
  LStream.Free;
  result := LStrCount;
end;

procedure SaveToFile(AFilename: String; AKnot: TKnot; AStrCount: Integer);
  procedure LSaveKnotToStream(AStream: TStream; APKnot: PKnot);
  var LChilds, LCount, LInt: Integer;
  begin
    for LCount := 1 to AStrCount do
    begin
      //Länge des Strings ermitteln
      LInt := Length(APKnot.A[LCount]);
      //Länge des Strings speichern
      AStream.Write(LInt, SizeOf(LInt));
      //String speichern
      AStream.Write(APKnot.A[LCount][1], LInt);
    end;

    //ermitteln wieviel Chidls
    LChilds := 0;
    while (LChilds < 11) and (APKnot.S[LChilds + 1] <> nil) do
      inc(LChilds);
    //Anzahl der Childs in Stream speichern
    AStream.Write(LChilds, SizeOf(LChilds));
    //Childs durchgehen
    for LCount := 1 to LChilds do
      LSaveKnotToStream(AStream, APKnot.S[LChilds]);
  end;
var LStream: TMemoryStream;
begin
  LStream := TMemoryStream.Create;
  LStream.Write(AStrCount, SizeOf(AStrCount));
  LSaveKnotToStream(LStream, @AKnot);
  LStream.SaveToFile(AFilename);
  LStream.Free;
end;
Jens
Mit Source ist es wie mit Kunst - Hauptsache der Künstler versteht's
  Mit Zitat antworten Zitat