file.inc 7.6 KB

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