Explorar o código

* patch by Mattias Gaertner to allow to override how the compiler reads source/ppu files, resolves #18740

git-svn-id: trunk@17255 -
florian %!s(int64=14) %!d(string=hai) anos
pai
achega
0c62133d38
Modificáronse 7 ficheiros con 100 adicións e 82 borrados
  1. 3 3
      compiler/comprsrc.pas
  2. 28 8
      compiler/cstreams.pas
  3. 22 23
      compiler/finput.pas
  4. 1 1
      compiler/link.pas
  5. 2 2
      compiler/owar.pas
  6. 4 4
      compiler/owbase.pas
  7. 40 41
      compiler/ppu.pas

+ 3 - 3
compiler/comprsrc.pas

@@ -377,18 +377,18 @@ end;
 
 
 function CopyResFile(inf,outf : TCmdStr) : boolean;
 function CopyResFile(inf,outf : TCmdStr) : boolean;
 var
 var
-  src,dst : TCFileStream;
+  src,dst : TCCustomFileStream;
 begin
 begin
   { Copy .res file to units output dir. }
   { Copy .res file to units output dir. }
   Result:=false;
   Result:=false;
-  src:=TCFileStream.Create(inf,fmOpenRead or fmShareDenyNone);
+  src:=CFileStreamClass.Create(inf,fmOpenRead or fmShareDenyNone);
   if CStreamError<>0 then
   if CStreamError<>0 then
     begin
     begin
       Message1(exec_e_cant_open_resource_file, src.FileName);
       Message1(exec_e_cant_open_resource_file, src.FileName);
       Include(current_settings.globalswitches, cs_link_nolink);
       Include(current_settings.globalswitches, cs_link_nolink);
       exit;
       exit;
     end;
     end;
-  dst:=TCFileStream.Create(current_module.outputpath^+outf,fmCreate);
+  dst:=CFileStreamClass.Create(current_module.outputpath^+outf,fmCreate);
   if CStreamError<>0 then
   if CStreamError<>0 then
     begin
     begin
       Message1(exec_e_cant_write_resource_file, dst.FileName);
       Message1(exec_e_cant_write_resource_file, dst.FileName);

+ 28 - 8
compiler/cstreams.pas

@@ -100,23 +100,38 @@ type
     property Size: Longint read GetSize write SetSize;
     property Size: Longint read GetSize write SetSize;
   end;
   end;
 
 
+{ TCCustomFileStream class }
+
+  TCCustomFileStream = class(TCStream)
+  protected
+    FFileName : String;
+  public
+    constructor Create(const AFileName: string;{shortstring!} Mode: Word); virtual; abstract;
+    function EOF: boolean; virtual; abstract;
+    property FileName : String Read FFilename;
+  end;
+
 { TFileStream class }
 { TFileStream class }
 
 
-  TCFileStream = class(TCStream)
+  TCFileStream = class(TCCustomFileStream)
   Private
   Private
-    FFileName : String;
     FHandle: File;
     FHandle: File;
   protected
   protected
     procedure SetSize(NewSize: Longint); override;
     procedure SetSize(NewSize: Longint); override;
   public
   public
-    constructor Create(const AFileName: string; Mode: Word);
+    constructor Create(const AFileName: string; Mode: Word); override;
     destructor Destroy; override;
     destructor Destroy; override;
     function Read(var Buffer; Count: Longint): Longint; override;
     function Read(var Buffer; Count: Longint): Longint; override;
     function Write(const Buffer; Count: Longint): Longint; override;
     function Write(const Buffer; Count: Longint): Longint; override;
     function Seek(Offset: Longint; Origin: Word): Longint; override;
     function Seek(Offset: Longint; Origin: Word): Longint; override;
-    property FileName : String Read FFilename;
+    function EOF: boolean; override;
   end;
   end;
 
 
+  TCFileStreamClass = class of TCCustomFileStream;
+var
+  CFileStreamClass: TCFileStreamClass = TCFileStream;
+
+type
 { TCustomMemoryStream abstract class }
 { TCustomMemoryStream abstract class }
 
 
   TCCustomMemoryStream = class(TCStream)
   TCCustomMemoryStream = class(TCStream)
@@ -441,6 +456,11 @@ begin
   Result:=l;
   Result:=l;
 end;
 end;
 
 
+function TCFileStream.EOF: boolean;
+begin
+  EOF:=system.eof(FHandle);
+end;
+
 
 
 {****************************************************************************}
 {****************************************************************************}
 {*                             TCustomMemoryStream                          *}
 {*                             TCustomMemoryStream                          *}
@@ -489,11 +509,11 @@ end;
 
 
 procedure TCCustomMemoryStream.SaveToFile(const FileName: string);
 procedure TCCustomMemoryStream.SaveToFile(const FileName: string);
 
 
-Var S : TCFileStream;
+Var S : TCCustomFileStream;
 
 
 begin
 begin
   Try
   Try
-    S:=TCFileStream.Create (FileName,fmCreate);
+    S:=CFileStreamClass.Create (FileName,fmCreate);
     SaveToStream(S);
     SaveToStream(S);
   finally
   finally
     S.free;
     S.free;
@@ -574,11 +594,11 @@ end;
 
 
 procedure TCMemoryStream.LoadFromFile(const FileName: string);
 procedure TCMemoryStream.LoadFromFile(const FileName: string);
 
 
-Var S : TCFileStream;
+Var S : TCCustomFileStream;
 
 
 begin
 begin
   Try
   Try
-    S:=TCFileStream.Create (FileName,fmOpenRead);
+    S:=CFileStreamClass.Create (FileName,fmOpenRead);
     LoadFromStream(S);
     LoadFromStream(S);
   finally
   finally
     S.free;
     S.free;

+ 22 - 23
compiler/finput.pas

@@ -26,7 +26,7 @@ unit finput;
 interface
 interface
 
 
     uses
     uses
-      cutils,cclasses;
+      cutils,cclasses,cstreams;
 
 
     const
     const
        InputFileBufSize=32*1024+1;
        InputFileBufSize=32*1024+1;
@@ -91,7 +91,7 @@ interface
          function fileclose: boolean; override;
          function fileclose: boolean; override;
          procedure filegettime; override;
          procedure filegettime; override;
        private
        private
-         f            : file;       { current file handle }
+         f            : TCCustomFileStream;       { current file handle }
        end;
        end;
 
 
        tinputfilemanager = class
        tinputfilemanager = class
@@ -457,47 +457,46 @@ uses
             exit;
             exit;
           end;
           end;
         { Open file }
         { Open file }
-        ofm:=filemode;
-        filemode:=0;
-        Assign(f,filename);
-        {$I-}
-         reset(f,1);
-        {$I+}
-        filemode:=ofm;
-        fileopen:=(ioresult=0);
+        fileopen:=false;
+        try
+          f:=CFileStreamClass.Create(filename,fmOpenRead);
+          fileopen:=true;
+        except
+        end;
       end;
       end;
 
 
 
 
     function tdosinputfile.fileseek(pos: longint): boolean;
     function tdosinputfile.fileseek(pos: longint): boolean;
       begin
       begin
-        {$I-}
-         seek(f,Pos);
-        {$I+}
-        fileseek:=(ioresult=0);
+        fileseek:=false;
+        try
+          f.position:=Pos;
+          fileseek:=true;
+        except
+        end;
       end;
       end;
 
 
 
 
     function tdosinputfile.fileread(var databuf; maxsize: longint): longint;
     function tdosinputfile.fileread(var databuf; maxsize: longint): longint;
-      var
-        w : longint;
       begin
       begin
-        blockread(f,databuf,maxsize,w);
-        fileread:=w;
+        fileread:=f.Read(databuf,maxsize);
       end;
       end;
 
 
 
 
     function tdosinputfile.fileeof: boolean;
     function tdosinputfile.fileeof: boolean;
       begin
       begin
-        fileeof:=eof(f);
+        fileeof:=f.eof();
       end;
       end;
 
 
 
 
     function tdosinputfile.fileclose: boolean;
     function tdosinputfile.fileclose: boolean;
       begin
       begin
-        {$I-}
-         system.close(f);
-        {$I+}
-        fileclose:=(ioresult=0);
+        fileclose:=false;
+        try
+          f.Free;
+          fileclose:=true;
+        except
+        end;
       end;
       end;
 
 
 
 

+ 1 - 1
compiler/link.pas

@@ -150,7 +150,7 @@ Implementation
       begin
       begin
         result:=0;
         result:=0;
         bufsize:=64*1024;
         bufsize:=64*1024;
-	      fs:=TCFileStream.Create(fn,fmOpenRead or fmShareDenyNone);
+	      fs:=CFileStreamClass.Create(fn,fmOpenRead or fmShareDenyNone);
 	      if CStreamError<>0 then
 	      if CStreamError<>0 then
 	        begin
 	        begin
 	          fs.Free;
 	          fs.Free;

+ 2 - 2
compiler/owar.pas

@@ -262,11 +262,11 @@ implementation
 
 
     procedure tarobjectwriter.writear;
     procedure tarobjectwriter.writear;
       var
       var
-        arf      : TCFileStream;
+        arf      : TCCustomFileStream;
         fixup,l,
         fixup,l,
         relocs,i : longint;
         relocs,i : longint;
       begin
       begin
-        arf:=TCFileStream.Create(arfn,fmCreate);
+        arf:=CFileStreamClass.Create(arfn,fmCreate);
         if CStreamError<>0 then
         if CStreamError<>0 then
           begin
           begin
              Message1(exec_e_cant_create_archivefile,arfn);
              Message1(exec_e_cant_create_archivefile,arfn);

+ 4 - 4
compiler/owbase.pas

@@ -31,7 +31,7 @@ uses
 type
 type
   tobjectwriter=class
   tobjectwriter=class
   private
   private
-    f      : TCFileStream;
+    f      : TCCustomFileStream;
     opened : boolean;
     opened : boolean;
     buf    : pchar;
     buf    : pchar;
     bufidx : longword;
     bufidx : longword;
@@ -54,7 +54,7 @@ type
 
 
   tobjectreader=class
   tobjectreader=class
   private
   private
-    f      : TCFileStream;
+    f      : TCCustomFileStream;
     opened : boolean;
     opened : boolean;
     buf    : pchar;
     buf    : pchar;
     ffilename : string;
     ffilename : string;
@@ -108,7 +108,7 @@ end;
 function tobjectwriter.createfile(const fn:string):boolean;
 function tobjectwriter.createfile(const fn:string):boolean;
 begin
 begin
   createfile:=false;
   createfile:=false;
-  f:=TCFileStream.Create(fn,fmCreate);
+  f:=CFileStreamClass.Create(fn,fmCreate);
   if CStreamError<>0 then
   if CStreamError<>0 then
     begin
     begin
        Message1(exec_e_cant_create_objectfile,fn);
        Message1(exec_e_cant_create_objectfile,fn);
@@ -233,7 +233,7 @@ end;
 function tobjectreader.openfile(const fn:string):boolean;
 function tobjectreader.openfile(const fn:string):boolean;
 begin
 begin
   openfile:=false;
   openfile:=false;
-  f:=TCFileStream.Create(fn,fmOpenRead);
+  f:=CFileStreamClass.Create(fn,fmOpenRead);
   if CStreamError<>0 then
   if CStreamError<>0 then
     begin
     begin
        Comment(V_Error,'Can''t open object file: '+fn);
        Comment(V_Error,'Can''t open object file: '+fn);

+ 40 - 41
compiler/ppu.pas

@@ -26,7 +26,7 @@ unit ppu;
 interface
 interface
 
 
   uses
   uses
-    globtype,constexp;
+    globtype,constexp,cstreams;
 
 
 { Also write the ppu if only crc if done, this can be used with ppudump to
 { Also write the ppu if only crc if done, this can be used with ppudump to
   see the differences between the intf and implementation }
   see the differences between the intf and implementation }
@@ -188,7 +188,7 @@ type
 
 
   tppufile=class
   tppufile=class
   private
   private
-    f        : file;
+    f        : TCCustomFileStream;
     mode     : byte; {0 - Closed, 1 - Reading, 2 - Writing}
     mode     : byte; {0 - Closed, 1 - Reading, 2 - Writing}
     fname    : string;
     fname    : string;
     fsize    : integer;
     fsize    : integer;
@@ -282,8 +282,8 @@ type
     procedure putstring(const s:string);
     procedure putstring(const s:string);
     procedure putnormalset(const b);
     procedure putnormalset(const b);
     procedure putsmallset(const b);
     procedure putsmallset(const b);
-    procedure tempclose;
-    function  tempopen:boolean;
+    procedure tempclose;        // MG: not used, obsolete?
+    function  tempopen:boolean; // MG: not used, obsolete?
   end;
   end;
 
 
 implementation
 implementation
@@ -356,10 +356,7 @@ begin
   if Mode<>0 then
   if Mode<>0 then
    begin
    begin
      Flush;
      Flush;
-     {$I-}
-      system.close(f);
-     {$I+}
-     if ioresult<>0 then;
+     f.Free;
      Mode:=0;
      Mode:=0;
      closed:=true;
      closed:=true;
    end;
    end;
@@ -415,21 +412,17 @@ var
   i      : integer;
   i      : integer;
 begin
 begin
   openfile:=false;
   openfile:=false;
-  assign(f,fname);
-  ofmode:=filemode;
-  filemode:=$0;
-  {$I-}
-   reset(f,1);
-  {$I+}
-  filemode:=ofmode;
-  if ioresult<>0 then
-   exit;
+  try
+    f:=CFileStreamClass.Create(fname,fmOpenRead)
+  except
+    exit;
+  end;
   closed:=false;
   closed:=false;
 {read ppuheader}
 {read ppuheader}
-  fsize:=filesize(f);
+  fsize:=f.Size;
   if fsize<sizeof(tppuheader) then
   if fsize<sizeof(tppuheader) then
    exit;
    exit;
-  blockread(f,header,sizeof(tppuheader),i);
+  i:=f.Read(header,sizeof(tppuheader));
   { The header is always stored in little endian order }
   { The header is always stored in little endian order }
   { therefore swap if on a big endian machine          }
   { therefore swap if on a big endian machine          }
 {$IFDEF ENDIAN_BIG}
 {$IFDEF ENDIAN_BIG}
@@ -478,7 +471,7 @@ end;
 procedure tppufile.reloadbuf;
 procedure tppufile.reloadbuf;
 begin
 begin
   inc(bufstart,bufsize);
   inc(bufstart,bufsize);
-  blockread(f,buf^,ppubufsize,bufsize);
+  bufsize:=f.Read(buf^,ppubufsize);
   bufidx:=0;
   bufidx:=0;
 end;
 end;
 
 
@@ -827,6 +820,8 @@ end;
 *****************************************************************************}
 *****************************************************************************}
 
 
 function tppufile.createfile:boolean;
 function tppufile.createfile:boolean;
+var
+  ok: boolean;
 begin
 begin
   createfile:=false;
   createfile:=false;
 {$ifdef INTFPPU}
 {$ifdef INTFPPU}
@@ -838,24 +833,26 @@ begin
 {$endif}
 {$endif}
   if not crc_only then
   if not crc_only then
     begin
     begin
-      assign(f,fname);
       {$ifdef MACOS}
       {$ifdef MACOS}
       {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
       {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
       SetDefaultMacOSCreator('FPas');
       SetDefaultMacOSCreator('FPas');
       SetDefaultMacOSFiletype('FPPU');
       SetDefaultMacOSFiletype('FPPU');
       {$endif}
       {$endif}
-      {$I-}
-      rewrite(f,1);
-      {$I+}
+      ok:=false;
+      try
+        f:=CFileStreamClass.Create(fname,fmCreate);
+        ok:=true;
+      except
+      end;
       {$ifdef MACOS}
       {$ifdef MACOS}
       SetDefaultMacOSCreator('MPS ');
       SetDefaultMacOSCreator('MPS ');
       SetDefaultMacOSFiletype('TEXT');
       SetDefaultMacOSFiletype('TEXT');
       {$endif}
       {$endif}
-      if ioresult<>0 then
+      if not ok then
        exit;
        exit;
       Mode:=2;
       Mode:=2;
     {write header for sure}
     {write header for sure}
-      blockwrite(f,header,sizeof(tppuheader));
+      f.Write(header,sizeof(tppuheader));
     end;
     end;
   bufsize:=ppubufsize;
   bufsize:=ppubufsize;
   bufstart:=sizeof(tppuheader);
   bufstart:=sizeof(tppuheader);
@@ -904,10 +901,10 @@ begin
     header.symlistsize:=swapendian(header.symlistsize);
     header.symlistsize:=swapendian(header.symlistsize);
 {$endif not FPC_BIG_ENDIAN}
 {$endif not FPC_BIG_ENDIAN}
 { write header and restore filepos after it }
 { write header and restore filepos after it }
-  opos:=filepos(f);
-  seek(f,0);
-  blockwrite(f,header,sizeof(tppuheader));
-  seek(f,opos);
+  opos:=f.Position;
+  f.Position:=0;
+  f.Write(header,sizeof(tppuheader));
+  f.Position:=opos;
 end;
 end;
 
 
 
 
@@ -915,7 +912,7 @@ procedure tppufile.writebuf;
 begin
 begin
   if not crc_only and
   if not crc_only and
      (bufidx <> 0) then
      (bufidx <> 0) then
-    blockwrite(f,buf^,bufidx);
+    f.Write(buf^,bufidx);
   inc(bufstart,bufidx);
   inc(bufstart,bufidx);
   bufidx:=0;
   bufidx:=0;
 end;
 end;
@@ -985,10 +982,10 @@ begin
       {flush to be sure}
       {flush to be sure}
         WriteBuf;
         WriteBuf;
       {write entry}
       {write entry}
-        opos:=filepos(f);
-        seek(f,entrystart);
-        blockwrite(f,entry,sizeof(tppuentry));
-        seek(f,opos);
+        opos:=f.Position;
+        f.Position:=entrystart;
+        f.write(entry,sizeof(tppuentry));
+        f.Position:=opos;
       end;
       end;
      entrybufstart:=bufstart;
      entrybufstart:=bufstart;
    end
    end
@@ -1152,11 +1149,8 @@ procedure tppufile.tempclose;
   begin
   begin
     if not closed then
     if not closed then
      begin
      begin
-       closepos:=filepos(f);
-       {$I-}
-        system.close(f);
-       {$I+}
-       if ioresult<>0 then;
+       closepos:=f.Position;
+       f.Free;
        closed:=true;
        closed:=true;
        tempclosed:=true;
        tempclosed:=true;
      end;
      end;
@@ -1170,6 +1164,10 @@ function tppufile.tempopen:boolean;
     tempopen:=false;
     tempopen:=false;
     if not closed or not tempclosed then
     if not closed or not tempclosed then
      exit;
      exit;
+    // MG: not sure, if this is correct
+
+    f.Position:=0;
+    (*
     ofm:=filemode;
     ofm:=filemode;
     filemode:=0;
     filemode:=0;
     {$I-}
     {$I-}
@@ -1178,11 +1176,12 @@ function tppufile.tempopen:boolean;
     filemode:=ofm;
     filemode:=ofm;
     if ioresult<>0 then
     if ioresult<>0 then
      exit;
      exit;
+    *)
     closed:=false;
     closed:=false;
     tempclosed:=false;
     tempclosed:=false;
 
 
   { restore state }
   { restore state }
-    seek(f,closepos);
+    f.Position:=closepos;
     tempopen:=true;
     tempopen:=true;
   end;
   end;