filectrl.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. {
  2. $Id$
  3. System independent low-level file interface
  4. Copyright (c) 1997 Balazs Scheidler ([email protected])
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free
  15. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. Todo:
  18. OS/2 versions of TruncateFile, FlushFile
  19. ****************************************************************************}
  20. unit FileCtrl;
  21. interface
  22. {$I platform.inc}
  23. uses
  24. Common, FileSys;
  25. { The following platforms are supported
  26. OS/2 either 1.x, or 2.x
  27. Linux
  28. DOS 16 bit, DPMI, Windows 3.1
  29. Not supported:
  30. Win32 (yet)
  31. }
  32. const
  33. { standard file handles under DOS, under linux only stdin, stdout and strerr
  34. is defined }
  35. stdin = 0;
  36. stdout = 1;
  37. stderr = 2;
  38. stdaux = 3;
  39. stdprn = 4;
  40. { file access constants }
  41. filRead = 0;
  42. filWrite = 1;
  43. filReadWrite = 2;
  44. { seek constants }
  45. skBeg = 0;
  46. skCur = 1;
  47. skEnd = 2;
  48. FilePerms: Word = $1A4; { rw-r--r-- }
  49. type
  50. {$IFDEF BIT_16}
  51. TFileHandle = Word;
  52. {$ELSE}
  53. TFileHandle = Longint;
  54. {$ENDIF}
  55. { System independent calls }
  56. { All of these functions do what their name imply, set ErrorCode (in Common)
  57. to the returned error. }
  58. { Under linux, I'll use FilePerms as permissions, instead of expecting an
  59. additional parameter }
  60. {$IFDEF PPC_Feature_Overriding}
  61. function OpenFile(FName: PChar; Flags: Longint): TFileHandle;
  62. function CreateFile(FName: PChar): TFileHandle;
  63. procedure DeleteFile(FName: PChar); { should be moved to FileSys }
  64. {$ENDIF}
  65. function OpenFileStr(FName: PChar; Flags: Longint): TFileHandle;
  66. function CreateFileStr(FName: PChar): TFileHandle;
  67. procedure DeleteFileStr(FName: PChar); { should be moved to FileSys }
  68. function OpenFile(FName: TFileName; Flags: Longint): TFileHandle;
  69. function CreateFile(FName: TFileName): TFileHandle;
  70. procedure DeleteFile(FName: TFileName);
  71. procedure CloseFile(Handle: TFileHandle);
  72. function SeekFile(Handle: TFileHandle; Pos: TFileInt; SeekType: Word): TFileInt;
  73. function ReadFile(Handle: TFileHandle; var Buff; Count: CPUWord): CPUWord;
  74. function WriteFile(Handle: TFileHandle; var Buff; Count: CPUWord): CPUWord;
  75. procedure FlushFile(Handle: TFileHandle);
  76. procedure TruncateFile(Handle: TFileHandle);
  77. function EndOfFile(Handle: TFileHandle): Boolean;
  78. function FilePos(Handle: TFileHandle): TFileInt;
  79. function FileSize(Handle: TFileHandle): TFileInt;
  80. procedure CopyFile(F1, F2: TFileHandle; Length: TFileInt);
  81. implementation
  82. { Include system dependent part }
  83. {$i filectrl.inc}
  84. function OpenFile(FName: TFileName; Flags: Longint): TFileHandle;
  85. begin
  86. FName := FName + #0;
  87. OpenFile := OpenFileStr(@FName[1], Flags);
  88. end;
  89. function CreateFile(FName: TFileName): TFileHandle;
  90. begin
  91. FName := FName+#0;
  92. CreateFile := CreateFileStr(@FName[1]);
  93. end;
  94. procedure DeleteFile(FName: TFileName);
  95. begin
  96. FName := FName + #0;
  97. DeleteFileStr(@FName[1]);
  98. end;
  99. {$IFDEF PPC_Feature_Overriding}
  100. function OpenFile(FName: PChar; Flags: Longint): TFileHandle;
  101. begin
  102. OpenFile := OpenFileStr(FName, Flags);
  103. end;
  104. function CreateFile(FName: PChar): TFileHandle;
  105. begin
  106. CreateFile := CreateFileStr(FName);
  107. end;
  108. procedure DeleteFile(FName: PChar);
  109. begin
  110. DeleteFileStr(FName);
  111. end;
  112. {$ENDIF}
  113. procedure CopyFile(F1, F2: TFileHandle; Length: TFileInt);
  114. var
  115. Buf: array [0..1023] of Byte;
  116. Len: Word;
  117. begin
  118. while (ErrorCode = 0) and (Length <> 0) do begin
  119. if Length < 1024 then Len := Length else Len := 1024;
  120. Len := ReadFile(F1, Buf, Len);
  121. WriteFile(F2, Buf, Len);
  122. Dec(Length, Len);
  123. end;
  124. end;
  125. end.
  126. {
  127. $Log$
  128. Revision 1.1 2000-01-06 01:20:31 peter
  129. * moved out of packages/ back to topdir
  130. Revision 1.1 1999/12/23 19:36:47 peter
  131. * place unitfiles in target dirs
  132. Revision 1.1 1999/11/24 23:36:37 peter
  133. * moved to packages dir
  134. Revision 1.3 1999/04/13 09:29:44 daniel
  135. * Reverted a terrible mistake
  136. Revision 1.1 1998/12/04 12:48:24 peter
  137. * moved some dirs
  138. Revision 1.6 1998/10/26 11:22:49 peter
  139. * updates
  140. Date Version Who Comments
  141. 07/06/97 0.1 bazsi Initial implementation
  142. many of the platforms implemented, but not
  143. tested at all
  144. 07/07/97 0.1.1 bazsi Some changes suggested by Marco Schmidt
  145. (TFileInt)
  146. Tested under Linux (FPC) and DOS (BP).
  147. 07/12/97 0.1.2 bazsi Converted to the new error-handling scheme,
  148. began adding error codes, but this will be
  149. changed (!)
  150. 07/18/97 0.2 bazsi Error codes moved to common
  151. 07/18/97 0.2.1 bazsi Corrected some syntactical errors (haven't
  152. checked before uploading...)
  153. 07/19/97 0.2.2 bazsi Overriden versions using Pascal style strings
  154. 07/19/97 0.3 bazsi EndOfFile, TruncateFile added, FlushFile
  155. implemented on Linux, DOS
  156. 07/28/97 0.3.1 bazsi Corrected some DOS 16 bit bugs (setting ErrorCode)
  157. 08/07/97 0.3.2 bazsi renamed to .PAS
  158. PChar versions are named xxxxStr, overriden
  159. versions are provided if PPC_Feature_Overriding is
  160. defined (the Str versions are provided in both cases)
  161. 08/24/97 0.3.3 bazsi FileSys added to uses clause
  162. 04/15/98 0.3.4 Michael Updated Linux implementation.
  163. 05/05/98 0.3.5 mkoeppe Fixed ReadFile, WriteFile return value in Linux.
  164. }