[fpc-pascal] Porting code from Windows D2007, missing Windows functions

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Thu Jul 14 12:50:44 CEST 2016


On 2016-07-14 11:31, Bo Berglund wrote:
> 
> function TSSCommHandler.ZipFiles(TargetFile: string; FileList:
> TStringList): boolean;
> var
>   ZW: TZipWrite;
>   i: integer;
>   FileName: string;
> begin
>   Result := false;
>   ZW := TZipWrite.Create(TargetFile);

I would replace that with a Factory Method call. For example:

  var
    lCompress: TCompressAbs;
    ...
  begin
    lCompress := gCompressFactory.CreateInstance(cZipWrite);
    lCompress.OutputFile := 'something.zip';
    for i := 0 to FileList.Count-1 do
      lCompress.AddFile(FileList[i]);


So later when you want to switch to say TurboPower or ZLib, you simply
need to change one line.

    lCompress := gCompressFactory.CreateInstance(cZLib);
or
    lCompress := gCompressFactory.CreateInstance(cTPAbbrevia);


What I also do in my applications is to reduce that to a single
constant, so only one location (or compiler define) needs to change. I
do this always for database connections with tiOPF too.

eg:

  -----------[ constants.pas ]-----------
  const
    {$IFDEF ZIPWRITE}
      cCompress = cZipWrite;
    {$ENDIF}

    {$IFDEF ZLIB}
      cCompress = cZLib;
    {$ENDIF}

    {$IFDEF ABBREVIA}
      cCompress = cTPAbbrevia;
    {$ENDIF}


then everywhere in my application I use the new constant

    lCompress := gCompressFactory.CreateInstance(cCompress);

So now a simple single project wide compiler define change and
recompile, and my program will use a different compression component
suite throughout.


> Do you have an example of how to do the above with FPC ZLib?

Yes, and even a nice chapter on explaining it all.


http://tiopf.sourceforge.net/Doc/Concepts/6_TheAdaptorForDatabaseIndependence.shtml

See the following units in the tiOPF’s “tiopf2” branch:

Git repository:
   https://sourceforge.net/p/tiopf/code/ci/tiopf2/tree/

Files:
   Core/tiCompress.pas
   Options/tiCompressNone.pas
   Options/tiCompressZLib.pas


Regards,
  Graeme

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp



More information about the fpc-pascal mailing list