file.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Run time library.
  4. Copyright (c) 1993,97 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WithOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************
  12. subroutines For UnTyped File handling
  13. ****************************************************************************}
  14. type
  15. UnTypedFile=File;
  16. Procedure Assign(var f:File;const Name:string);
  17. {
  18. Assign Name to file f so it can be used with the file routines
  19. }
  20. Begin
  21. FillChar(f,SizeOf(FileRec),0);
  22. FileRec(f).Handle:=UnusedHandle;
  23. FileRec(f).mode:=fmClosed;
  24. Move(Name[1],FileRec(f).Name,Length(Name));
  25. End;
  26. Procedure Rewrite(var f:File;l:Word);[IOCheck];
  27. {
  28. Create file f with recordsize of l
  29. }
  30. Begin
  31. If l=0 Then
  32. InOutRes:=2
  33. else
  34. Begin
  35. Do_Open(f,PChar(@FileRec(f).Name),$101);
  36. FileRec(f).RecSize:=l;
  37. End;
  38. End;
  39. Procedure Reset(var f:File;l:Word);[IOCheck];
  40. {
  41. Open file f with recordsize of l and filemode
  42. }
  43. Begin
  44. If l=0 Then
  45. InOutRes:=2
  46. else
  47. Begin
  48. Do_Open(f,PChar(@FileRec(f).Name),Filemode);
  49. FileRec(f).RecSize:=l;
  50. End;
  51. End;
  52. Procedure Rewrite(Var f:File);[IOCheck];
  53. {
  54. Create file with (default) 128 byte records
  55. }
  56. Begin
  57. Rewrite(f,128);
  58. End;
  59. Procedure Reset(Var f:File);[IOCheck];
  60. {
  61. Open file with (default) 128 byte records
  62. }
  63. Begin
  64. Reset(f,128);
  65. End;
  66. Procedure BlockWrite(Var f:File;Var Buf;Count:Longint;var Result:Longint);[IOCheck];
  67. {
  68. Write Count records from Buf to file f, return written records in result
  69. }
  70. Begin
  71. Result:=Do_Write(FileRec(f).Handle,Longint(@Buf),Count*FileRec(f).RecSize) div FileRec(f).RecSize;
  72. End;
  73. Procedure BlockWrite(Var f:File;Var Buf;Count:Word;var Result:Word);[IOCheck];
  74. {
  75. Write Count records from Buf to file f, return written records in Result
  76. }
  77. var
  78. l : longint;
  79. Begin
  80. BlockWrite(f,Buf,Count,l);
  81. Result:=l;
  82. End;
  83. Procedure BlockWrite(Var f:File;Var Buf;Count:Word;var Result:Integer);[IOCheck];
  84. {
  85. Write Count records from Buf to file f, return written records in Result
  86. }
  87. var
  88. l : longint;
  89. Begin
  90. BlockWrite(f,Buf,Count,l);
  91. Result:=l;
  92. End;
  93. Procedure BlockWrite(Var f:File;Var Buf;Count:Longint);[IOCheck];
  94. {
  95. Write Count records from Buf to file f, if none a Read and Count>0 then
  96. InOutRes is set
  97. }
  98. var
  99. Result : Longint;
  100. Begin
  101. BlockWrite(f,Buf,Count,Result);
  102. If (Result=0) and (Count>0) Then
  103. InOutRes:=101;
  104. End;
  105. Procedure BlockRead(var f:File;var Buf;Count:Longint;var Result:Longint);[IOCheck];
  106. {
  107. Read Count records from file f ro Buf, return nuùber of read records in
  108. Result
  109. }
  110. Begin
  111. Result:=Do_Read(FileRec(f).Handle,Longint(@Buf),count*FileRec(f).RecSize) div FileRec(f).RecSize;
  112. End;
  113. Procedure BlockRead(var f:File;var Buf;count:Word;var Result:Word);[IOCheck];
  114. {
  115. Read Count records from file f to Buf, return number of read records in
  116. Result
  117. }
  118. var
  119. l : longint;
  120. Begin
  121. BlockRead(f,Buf,Count,l);
  122. Result:=l;
  123. End;
  124. Procedure BlockRead(var f:File;var Buf;count:Word;var Result:Integer);[IOCheck];
  125. {
  126. Read Count records from file f to Buf, return number of read records in
  127. Result
  128. }
  129. var
  130. l : longint;
  131. Begin
  132. BlockRead(f,Buf,Count,l);
  133. Result:=l;
  134. End;
  135. Procedure BlockRead(Var f:File;Var Buf;Count:Longint);[IOCheck];
  136. {
  137. Read Count records from file f to Buf, if none are read and Count>0 then
  138. InOutRes is set
  139. }
  140. var
  141. Result : Longint;
  142. Begin
  143. BlockRead(f,Buf,Count,Result);
  144. If (Result=0) and (Count>0) Then
  145. InOutRes:=100;
  146. End;
  147. Function FilePos(var f:File):Longint;[IOCheck];
  148. {
  149. Return current Position In file f in records
  150. }
  151. Begin
  152. FilePos:=Do_FilePos(FileRec(f).Handle) div FileRec(f).RecSize;
  153. End;
  154. Function FileSize(var f:File):Longint;[IOCheck];
  155. {
  156. Return the size of file f in records
  157. }
  158. Begin
  159. FileSize:=Do_FileSize(FileRec(f).Handle) div FileRec(f).RecSize;
  160. End;
  161. Function Eof(var f:File):Boolean;[IOCheck];
  162. {
  163. Return True if we're at the end of the file f, else False is returned
  164. }
  165. Begin
  166. {Can't use do_ routines because we need record support}
  167. Eof:=(FileSize(f)<=FilePos(f));
  168. End;
  169. Procedure Seek(var f:File;Pos:Longint);[IOCheck];
  170. {
  171. Goto record Pos in file f
  172. }
  173. Begin
  174. Do_Seek(FileRec(f).Handle,Pos*FileRec(f).RecSize);
  175. End;
  176. Procedure Truncate(Var f:File);[IOCheck];
  177. {
  178. Truncate/Cut file f at the current record Position
  179. }
  180. Begin
  181. Do_Truncate(FileRec(f).Handle,FilePos(f)*FileRec(f).RecSize);
  182. End;
  183. Procedure Close(var f:File);[IOCheck];
  184. {
  185. Close file f
  186. }
  187. Begin
  188. If (FileRec(f).mode<>fmClosed) Then
  189. Begin
  190. FileRec(f).mode:=fmClosed;
  191. Do_Close(FileRec(f).Handle);
  192. End;
  193. End;
  194. Procedure Erase(var f : File);[IOCheck];
  195. Begin
  196. If FileRec(f).mode=fmClosed Then
  197. Do_Erase(PChar(@FileRec(f).Name));
  198. End;
  199. Procedure Rename(var f : File;const s : string);[IOCheck];
  200. var
  201. p : array[0..255] Of Char;
  202. Begin
  203. If FileRec(f).mode=fmClosed Then
  204. Begin
  205. Move(s[1],p,Length(s));
  206. p[Length(s)]:=#0;
  207. Do_Rename(PChar(@FileRec(f).Name),PChar(@p));
  208. Move(p,FileRec(f).Name,Length(s)+1);
  209. End;
  210. End;
  211. {
  212. $Log$
  213. Revision 1.1 1998-03-25 11:18:43 root
  214. Initial revision
  215. Revision 1.4 1998/03/05 02:42:29 peter
  216. + blockread/blockwrite with integer result
  217. Revision 1.3 1998/01/26 11:59:44 michael
  218. + Added log at the end
  219. Working file: rtl/inc/file.inc
  220. description:
  221. ----------------------------
  222. revision 1.2
  223. date: 1998/01/25 21:53:28; author: peter; state: Exp; lines: +2 -2
  224. + Universal Handles support for StdIn/StdOut/StdErr
  225. * Updated layout of sysamiga.pas
  226. ----------------------------
  227. revision 1.1
  228. date: 1998/01/11 02:43:11; author: michael; state: Exp;
  229. + Initial implementation of these files (by Peter Vreman).
  230. file operations are now in separate files per type of file.
  231. =============================================================================
  232. }