Einzelnen Beitrag anzeigen

Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.580 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: Objekte aus Textdatei erstellen

  Alt 16. Jun 2013, 21:53
Hier mal ein Beispiel:
Delphi-Quellcode:
unit UnitXYZ;

interface

uses
  Classes, SysUtils;

type
  TStringReaderWriter = class helper for TStream
    function ReadString: string;
    procedure WriteString(const AValue: string);
  end;

  TMyGameObject = class
  private
    type
      TExample = class
      private
        var
          FExampleString: string;
          FExampleInteger: LongInt;
        procedure SetExampleInteger(const Value: LongInt);
        procedure SetExampleString(const Value: string);
      public
        procedure SaveToStream(const ATarget: TStream);
        procedure LoadFromStream(const ASource: TStream);
        property ExampleString: string read FExampleString write SetExampleString;
        property ExampleInteger: LongInt read FExampleInteger write SetExampleInteger;
      end;
    var
      FInterestingString: string;
      FExampleObject: TExample;
    procedure SetInterestingString(const Value: string);
  public
    constructor Create;
    destructor Destroy; override;
    procedure SaveToStream(const ATarget: TStream);
    procedure LoadFromStream(const ASource: TStream);
    procedure SaveToFile(const AFilename: String);
    procedure LoadFromFile(const AFilename: String);
    property InterestingString: string read FInterestingString write SetInterestingString;
    property ExampleObject: TExample read FExampleObject;
  end;

implementation

{ TMyGameObject.TExample }

procedure TMyGameObject.TExample.LoadFromStream(const ASource: TStream);
begin
  FExampleString := ASource.ReadString;
  ASource.ReadBuffer(FExampleInteger, SizeOf(FExampleInteger));
end;

procedure TMyGameObject.TExample.SaveToStream(const ATarget: TStream);
begin
  ATarget.WriteString(FExampleString);
  ATarget.WriteBuffer(FExampleInteger, SizeOf(FExampleInteger));
end;

procedure TMyGameObject.TExample.SetExampleInteger(const Value: LongInt);
begin
  FExampleInteger := Value;
end;

procedure TMyGameObject.TExample.SetExampleString(const Value: string);
begin
  FExampleString := Value;
end;

{ TStringReaderWriter }

function TStringReaderWriter.ReadString: string;
var
  ResultString: AnsiString;
  StringSize: Integer;
begin
  Result := '';
  ReadBuffer(StringSize, SizeOf(StringSize));
  SetLength(ResultString, StringSize);
  ReadBuffer(Pointer(ResultString)^, StringSize);
  {$ifdef UNICODE}
  Result := Utf8ToString(ResultString);
  {$else}
  Result := Utf8Decode(ResultString);
  {$endif}
end;

procedure TStringReaderWriter.WriteString(const AValue: string);
var
  StringSize: Integer;
  StringToSave: AnsiString;
begin
  StringToSave := Utf8Encode(AValue);
  StringSize := Length(StringToSave);
  WriteBuffer(StringSize, SizeOf(StringSize));
  WriteBuffer(Pointer(StringToSave)^, StringSize);
end;

{ TMyGameObject }

constructor TMyGameObject.Create;
begin
  FExampleObject := TExample.Create;
end;

destructor TMyGameObject.Destroy;
begin
  FExampleObject.Free;
  inherited;
end;

procedure TMyGameObject.LoadFromFile(const AFilename: String);
var
  FileContents: TFileStream;
begin
  FileContents := TFileStream.Create(AFilename, fmOpenRead);
  try
    LoadFromStream(FileContents);
  finally
    FileContents.Free;
  end;
end;

procedure TMyGameObject.LoadFromStream(const ASource: TStream);
begin
  FInterestingString := ASource.ReadString;
  FExampleObject.LoadFromStream(ASource);
end;

procedure TMyGameObject.SaveToFile(const AFilename: String);
var
  FileContents: TFileStream;
begin
  FileContents := TFileStream.Create(AFilename, fmCreate);
  try
    SaveToStream(FileContents);
  finally
    FileContents.Free;
  end;
end;

procedure TMyGameObject.SaveToStream(const ATarget: TStream);
begin
  ATarget.WriteString(FInterestingString);
  FExampleObject.SaveToStream(ATarget);
end;

procedure TMyGameObject.SetInterestingString(const Value: string);
begin
  FInterestingString := Value;
end;

end.
Und benutzen kannst du das dann so:
Delphi-Quellcode:
var
  MyGameData: TMyGameObject;
begin
  if dlgOpen.Execute then
  begin
    MyGameData := TMyGameObject.Create;
    try
      MyGameData.LoadFromFile(dlgOpen.Filename);
      ShowMessage(MyGameData.InterestingString);
    finally
      MyGameData.Free;
    end;
  end;
Auf diese Weise ist nach außen immer alles gekapselt und du kannst die Objekte nach außen immer einfach benutzen ohne dich um die Interna zu kümmern.
Sebastian Jänicke
Alle eigenen Projekte sind eingestellt, ebenso meine Homepage, Downloadlinks usw. im Forum bleiben aktiv!
  Mit Zitat antworten Zitat