file.inc 7.1 KB

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