file.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 assign(var f:File;p:pchar);
  27. {
  28. Assign Name to file f so it can be used with the file routines
  29. }
  30. begin
  31. Assign(f,StrPas(p));
  32. end;
  33. Procedure assign(var f:File;c:char);
  34. {
  35. Assign Name to file f so it can be used with the file routines
  36. }
  37. begin
  38. Assign(f,string(c));
  39. end;
  40. Procedure Rewrite(var f:File;l:Word);[IOCheck];
  41. {
  42. Create file f with recordsize of l
  43. }
  44. Begin
  45. If l=0 Then
  46. InOutRes:=2
  47. else
  48. Begin
  49. Do_Open(f,PChar(@FileRec(f).Name),$101);
  50. FileRec(f).RecSize:=l;
  51. End;
  52. End;
  53. Procedure Reset(var f:File;l:Word);[IOCheck];
  54. {
  55. Open file f with recordsize of l and filemode
  56. }
  57. Begin
  58. If l=0 Then
  59. InOutRes:=2
  60. else
  61. Begin
  62. Do_Open(f,PChar(@FileRec(f).Name),Filemode);
  63. FileRec(f).RecSize:=l;
  64. End;
  65. End;
  66. Procedure Rewrite(Var f:File);[IOCheck];
  67. {
  68. Create file with (default) 128 byte records
  69. }
  70. Begin
  71. Rewrite(f,128);
  72. End;
  73. Procedure Reset(Var f:File);[IOCheck];
  74. {
  75. Open file with (default) 128 byte records
  76. }
  77. Begin
  78. Reset(f,128);
  79. End;
  80. Procedure BlockWrite(Var f:File;Var Buf;Count:Longint;var Result:Longint);[IOCheck];
  81. {
  82. Write Count records from Buf to file f, return written records in result
  83. }
  84. Begin
  85. Result:=Do_Write(FileRec(f).Handle,Longint(@Buf),Count*FileRec(f).RecSize) div FileRec(f).RecSize;
  86. End;
  87. Procedure BlockWrite(Var f:File;Var Buf;Count:Word;var Result:Word);[IOCheck];
  88. {
  89. Write Count records from Buf to file f, return written records in Result
  90. }
  91. var
  92. l : longint;
  93. Begin
  94. BlockWrite(f,Buf,Count,l);
  95. Result:=l;
  96. End;
  97. Procedure BlockWrite(Var f:File;Var Buf;Count:Word;var Result:Integer);[IOCheck];
  98. {
  99. Write Count records from Buf to file f, return written records in Result
  100. }
  101. var
  102. l : longint;
  103. Begin
  104. BlockWrite(f,Buf,Count,l);
  105. Result:=l;
  106. End;
  107. Procedure BlockWrite(Var f:File;Var Buf;Count:Longint);[IOCheck];
  108. {
  109. Write Count records from Buf to file f, if none a Read and Count>0 then
  110. InOutRes is set
  111. }
  112. var
  113. Result : Longint;
  114. Begin
  115. BlockWrite(f,Buf,Count,Result);
  116. If (Result=0) and (Count>0) Then
  117. InOutRes:=101;
  118. End;
  119. Procedure BlockRead(var f:File;var Buf;Count:Longint;var Result:Longint);[IOCheck];
  120. {
  121. Read Count records from file f ro Buf, return nuùber of read records in
  122. Result
  123. }
  124. Begin
  125. Result:=Do_Read(FileRec(f).Handle,Longint(@Buf),count*FileRec(f).RecSize) div FileRec(f).RecSize;
  126. End;
  127. Procedure BlockRead(var f:File;var Buf;count:Word;var Result:Word);[IOCheck];
  128. {
  129. Read Count records from file f to Buf, return number of read records in
  130. Result
  131. }
  132. var
  133. l : longint;
  134. Begin
  135. BlockRead(f,Buf,Count,l);
  136. Result:=l;
  137. End;
  138. Procedure BlockRead(var f:File;var Buf;count:Word;var Result:Integer);[IOCheck];
  139. {
  140. Read Count records from file f to Buf, return number of read records in
  141. Result
  142. }
  143. var
  144. l : longint;
  145. Begin
  146. BlockRead(f,Buf,Count,l);
  147. Result:=l;
  148. End;
  149. Procedure BlockRead(Var f:File;Var Buf;Count:Longint);[IOCheck];
  150. {
  151. Read Count records from file f to Buf, if none are read and Count>0 then
  152. InOutRes is set
  153. }
  154. var
  155. Result : Longint;
  156. Begin
  157. BlockRead(f,Buf,Count,Result);
  158. If (Result=0) and (Count>0) Then
  159. InOutRes:=100;
  160. End;
  161. Function FilePos(var f:File):Longint;[IOCheck];
  162. {
  163. Return current Position In file f in records
  164. }
  165. Begin
  166. FilePos:=Do_FilePos(FileRec(f).Handle) div FileRec(f).RecSize;
  167. End;
  168. Function FileSize(var f:File):Longint;[IOCheck];
  169. {
  170. Return the size of file f in records
  171. }
  172. Begin
  173. FileSize:=Do_FileSize(FileRec(f).Handle) div FileRec(f).RecSize;
  174. End;
  175. Function Eof(var f:File):Boolean;[IOCheck];
  176. {
  177. Return True if we're at the end of the file f, else False is returned
  178. }
  179. Begin
  180. {Can't use do_ routines because we need record support}
  181. Eof:=(FileSize(f)<=FilePos(f));
  182. End;
  183. Procedure Seek(var f:File;Pos:Longint);[IOCheck];
  184. {
  185. Goto record Pos in file f
  186. }
  187. Begin
  188. Do_Seek(FileRec(f).Handle,Pos*FileRec(f).RecSize);
  189. End;
  190. Procedure Truncate(Var f:File);[IOCheck];
  191. {
  192. Truncate/Cut file f at the current record Position
  193. }
  194. Begin
  195. Do_Truncate(FileRec(f).Handle,FilePos(f)*FileRec(f).RecSize);
  196. End;
  197. Procedure Close(var f:File);[IOCheck];
  198. {
  199. Close file f
  200. }
  201. Begin
  202. If (FileRec(f).mode<>fmClosed) Then
  203. Begin
  204. FileRec(f).mode:=fmClosed;
  205. Do_Close(FileRec(f).Handle);
  206. End;
  207. End;
  208. Procedure Erase(var f : File);[IOCheck];
  209. Begin
  210. If FileRec(f).mode=fmClosed Then
  211. Do_Erase(PChar(@FileRec(f).Name));
  212. End;
  213. Procedure Rename(var f : File;p:pchar);[IOCheck];
  214. Begin
  215. If FileRec(f).mode=fmClosed Then
  216. Begin
  217. Do_Rename(PChar(@FileRec(f).Name),p);
  218. Move(p,FileRec(f).Name,StrLen(p)+1);
  219. End;
  220. End;
  221. Procedure Rename(var f : File;const s : string);[IOCheck];
  222. var
  223. p : array[0..255] Of Char;
  224. Begin
  225. Move(s[1],p,Length(s));
  226. p[Length(s)]:=#0;
  227. Rename(f,Pchar(@p));
  228. End;
  229. Procedure Rename(var f : File;c : char);[IOCheck];
  230. var
  231. p : array[0..1] Of Char;
  232. Begin
  233. p[0]:=c;
  234. p[1]:=#0;
  235. Rename(f,Pchar(@p));
  236. End;
  237. {
  238. $Log$
  239. Revision 1.3 1998-05-21 19:30:56 peter
  240. * objects compiles for linux
  241. + assign(pchar), assign(char), rename(pchar), rename(char)
  242. * fixed read_text_as_array
  243. + read_text_as_pchar which was not yet in the rtl
  244. Revision 1.2 1998/05/12 10:42:44 peter
  245. * moved getopts to inc/, all supported OS's need argc,argv exported
  246. + strpas, strlen are now exported in the systemunit
  247. * removed logs
  248. * removed $ifdef ver_above
  249. }