Einzelnen Beitrag anzeigen

Fritzew

Registriert seit: 18. Nov 2015
Ort: Kehl
678 Beiträge
 
Delphi 11 Alexandria
 
#23

AW: Ini-Writes global abfangen?

  Alt 11. Apr 2017, 11:08
Warum nicht einen ganz anderen Ansatz?

nur mal so schnell getippt als Anregung:

Das ganze über Interfaces realisieren. Stichwort testbar etc....

3 Units:

Interface
Delphi-Quellcode:
unit my.ini.interfaces;

interface

type
// Hier alles rein was im Prog tatsäclich benötigt wird
   iIniReadWriter = interface
      ['{AE9CC6E5-F0B8-4D39-8F6C-799423C60A37}']
      function ReadString(const Section, Ident, Default: string): string;
      procedure WriteString(const Section, Ident, Value: String);
   end;

implementation

end.
Implementation
Delphi-Quellcode:
unit my.ini.implementations;

interface

uses
   System.SysUtils,
   System.IniFiles,
   my.ini.interfaces;


type
   tiniReadWriterFile = class(TInterfacedObject, iIniReadWriter)
   private
      fchanged : boolean;
      fautosave: boolean;
      fini : TMemIniFile;

   private // iIniReadWriter implementations
     function ReadString(const Section, Ident, Default: string): string;
      procedure WriteString(const Section, Ident, Value: String);

   public
      constructor Create(const FileName: string; const autosave: boolean = true); overload;
      constructor Create(const FileName: string; const Encoding: TEncoding; const autosave: boolean = true); overload;
      destructor Destroy; override;
   end;

implementation



constructor tiniReadWriterFile.Create(const FileName: string; const Encoding: TEncoding; const autosave: boolean = true);
begin
   inherited Create;
    fini := TMemIniFile.Create(FileName, Encoding);
   fautosave := autosave;
end;

constructor tiniReadWriterFile.Create(const FileName: string; const autosave: boolean = true);
begin
   inherited Create;
   fini := TMemIniFile.Create(FileName);
   fautosave := autosave;
end;

destructor tiniReadWriterFile.Destroy;
begin
   if fautosave and fchanged then
      fini.UpdateFile;

   fini.free;
   inherited;
end;

function tiniReadWriterFile.ReadString(const Section, Ident, Default: string): string;
begin
 result := fini.ReadString(Section, Ident, Default);
end;

procedure tiniReadWriterFile.WriteString(const Section, Ident, Value: String);
begin
  fini.WriteString(Section, Ident, Value);
  fchanged := true;
end;

end.
Factory
Delphi-Quellcode:
unit my.ini.factory;

interface

uses
  my.ini.interfaces;
type
   tIniReadWriterTypes = (tirwFileWrite, tirReadonly, tirwLogging) ;

  function CreateIniReadWriterFactory(aType : tIniReadWriterTypes; const Filename : String): iIniReadWriter;

implementation

uses
  my.ini.implementations;

 function CreateIniReadWriterFactory(aType : tIniReadWriterTypes; const Filename : String): iIniReadWriter;
 begin
   case aType of
      tirwFileWrite: result := tiniReadWriterFile.Create(Filename, true);
      tirReadonly :Result := tiniReadWriterFile.Create(Filename, false);
   // tirwLogging: result := tiniReadWriterLogger.Create(Filename);
   end;
 end;

end.


Delphi-Quellcode:
unit my.useini;

interface

uses
  my.ini.interfaces;

  type

    tuseini = class
     private
     public
       procedure SaveSettings(Writer : iIniReadWriter);
    end;



implementation

{ tuseini }

procedure tuseini.SaveSettings(Writer: iIniReadWriter);
begin
 assert(Writer <> nil, 'Writer must be set');

 Writer.WriteString('Test', 'Dummy', 'Default');

end;

end.

TestProjekt
Delphi-Quellcode:
program Project13;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  my.ini.implementations in 'my.ini.implementations.pas',
  my.ini.interfaces in 'my.ini.interfaces.pas',
  my.ini.factory in 'my.ini.factory.pas',
  my.useini in 'my.useini.pas';



 const aFile : String = 'c:\test\Test.ini';


procedure Test;
Var UseIni : tuseini;
begin
    Useini := tuseini.create;
     Writeln('Test Readonly') ;
    Useini.SaveSettings(CreateIniReadWriterFactory(tirReadonly, aFile));
    Writeln('Readonly Done');
    Useini.SaveSettings(CreateIniReadWriterFactory(tirwFileWrite, aFile));
    Writeln('Write Ini Done') ;
    Useini.free;
end;


begin
  try
    Test;

   Writeln('Sucess');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  readln;
end.
Fritz Westermann
  Mit Zitat antworten Zitat