file.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Run time library.
  4. Copyright (c) 1999-2000 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. Case FileRec(f).mode Of
  48. fmInOut,fmInput,fmOutput : Close(f);
  49. fmClosed : ;
  50. else
  51. Begin
  52. InOutRes:=102;
  53. exit;
  54. End;
  55. End;
  56. If l=0 Then
  57. InOutRes:=2
  58. else
  59. Begin
  60. { Reopen with filemode 2, to be Tp compatible (PFV) }
  61. Do_Open(f,PChar(@FileRec(f).Name),$1002);
  62. FileRec(f).RecSize:=l;
  63. End;
  64. End;
  65. Procedure Reset(var f:File;l:Longint);[IOCheck];
  66. {
  67. Open file f with recordsize of l and filemode
  68. }
  69. Begin
  70. If InOutRes <> 0 then
  71. Exit;
  72. Case FileRec(f).mode Of
  73. fmInOut,fmInput,fmOutput : Close(f);
  74. fmClosed : ;
  75. else
  76. Begin
  77. InOutRes:=102;
  78. exit;
  79. End;
  80. End;
  81. If l=0 Then
  82. InOutRes:=2
  83. else
  84. Begin
  85. Do_Open(f,PChar(@FileRec(f).Name),Filemode);
  86. FileRec(f).RecSize:=l;
  87. End;
  88. End;
  89. Procedure Rewrite(Var f:File);[IOCheck];
  90. {
  91. Create file with (default) 128 byte records
  92. }
  93. Begin
  94. If InOutRes <> 0 then
  95. exit;
  96. Rewrite(f,128);
  97. End;
  98. Procedure Reset(Var f:File);[IOCheck];
  99. {
  100. Open file with (default) 128 byte records
  101. }
  102. Begin
  103. If InOutRes <> 0 then
  104. exit;
  105. Reset(f,128);
  106. End;
  107. Procedure BlockWrite(Var f:File;Var Buf;Count:Longint;var Result:Longint);[IOCheck];
  108. {
  109. Write Count records from Buf to file f, return written records in result
  110. }
  111. Begin
  112. Result:=0;
  113. If InOutRes <> 0 then
  114. exit;
  115. case FileRec(f).Mode of
  116. fmInOut,fmOutput : ;
  117. else
  118. begin
  119. InOutRes:=103;
  120. exit;
  121. end;
  122. end;
  123. Result:=Do_Write(FileRec(f).Handle,Longint(@Buf),Count*FileRec(f).RecSize) div FileRec(f).RecSize;
  124. End;
  125. Procedure BlockWrite(Var f:File;Var Buf;Count:Word;var Result:Word);[IOCheck];
  126. {
  127. Write Count records from Buf to file f, return written records in Result
  128. }
  129. var
  130. l : longint;
  131. Begin
  132. BlockWrite(f,Buf,Count,l);
  133. Result:=l;
  134. End;
  135. Procedure BlockWrite(Var f:File;Var Buf;Count:Word;var Result:Integer);[IOCheck];
  136. {
  137. Write Count records from Buf to file f, return written records in Result
  138. }
  139. var
  140. l : longint;
  141. Begin
  142. BlockWrite(f,Buf,Count,l);
  143. Result:=l;
  144. End;
  145. Procedure BlockWrite(Var f:File;Var Buf;Count:Longint);[IOCheck];
  146. {
  147. Write Count records from Buf to file f, if none a Read and Count>0 then
  148. InOutRes is set
  149. }
  150. var
  151. Result : Longint;
  152. Begin
  153. BlockWrite(f,Buf,Count,Result);
  154. If (Result<Count) and (Count>0) Then
  155. InOutRes:=101;
  156. End;
  157. Procedure BlockRead(var f:File;var Buf;Count:Longint;var Result:Longint);[IOCheck];
  158. {
  159. Read Count records from file f ro Buf, return number of read records in
  160. Result
  161. }
  162. Begin
  163. Result:=0;
  164. If InOutRes <> 0 then
  165. exit;
  166. case FileRec(f).Mode of
  167. fmInOut,fmInput : ;
  168. else
  169. begin
  170. InOutRes:=103;
  171. exit;
  172. end;
  173. end;
  174. Result:=Do_Read(FileRec(f).Handle,Longint(@Buf),count*FileRec(f).RecSize) div FileRec(f).RecSize;
  175. End;
  176. Procedure BlockRead(var f:File;var Buf;count:Word;var Result:Word);[IOCheck];
  177. {
  178. Read Count records from file f to Buf, return number of read records in
  179. Result
  180. }
  181. var
  182. l : longint;
  183. Begin
  184. BlockRead(f,Buf,Count,l);
  185. Result:=l;
  186. End;
  187. Procedure BlockRead(var f:File;var Buf;count:Word;var Result:Integer);[IOCheck];
  188. {
  189. Read Count records from file f to Buf, return number of read records in
  190. Result
  191. }
  192. var
  193. l : longint;
  194. Begin
  195. BlockRead(f,Buf,Count,l);
  196. Result:=l;
  197. End;
  198. Procedure BlockRead(Var f:File;Var Buf;Count:Longint);[IOCheck];
  199. {
  200. Read Count records from file f to Buf, if none are read and Count>0 then
  201. InOutRes is set
  202. }
  203. var
  204. Result : Longint;
  205. Begin
  206. BlockRead(f,Buf,Count,Result);
  207. If (Result<Count) and (Count>0) Then
  208. InOutRes:=100;
  209. End;
  210. Function FilePos(var f:File):Longint;[IOCheck];
  211. {
  212. Return current Position In file f in records
  213. }
  214. Begin
  215. FilePos:=0;
  216. If InOutRes <> 0 then
  217. exit;
  218. case FileRec(f).Mode of
  219. fmInOut,fmInput,fmOutput : ;
  220. else
  221. begin
  222. InOutRes:=103;
  223. exit;
  224. end;
  225. end;
  226. FilePos:=Do_FilePos(FileRec(f).Handle) div FileRec(f).RecSize;
  227. End;
  228. Function FileSize(var f:File):Longint;[IOCheck];
  229. {
  230. Return the size of file f in records
  231. }
  232. Begin
  233. FileSize:=0;
  234. If InOutRes <> 0 then
  235. exit;
  236. case FileRec(f).Mode of
  237. fmInOut,fmInput,fmOutput : ;
  238. else
  239. begin
  240. InOutRes:=103;
  241. exit;
  242. end;
  243. end;
  244. if (FileRec(f).RecSize>0) then
  245. FileSize:=Do_FileSize(FileRec(f).Handle) div FileRec(f).RecSize;
  246. End;
  247. Function Eof(var f:File):Boolean;[IOCheck];
  248. {
  249. Return True if we're at the end of the file f, else False is returned
  250. }
  251. Begin
  252. Eof:=false;
  253. If InOutRes <> 0 then
  254. exit;
  255. case FileRec(f).Mode of
  256. fmInOut,fmInput,fmOutput : ;
  257. else
  258. begin
  259. InOutRes:=103;
  260. exit;
  261. end;
  262. end;
  263. {Can't use do_ routines because we need record support}
  264. Eof:=(FileSize(f)<=FilePos(f));
  265. End;
  266. Procedure Seek(var f:File;Pos:Longint);[IOCheck];
  267. {
  268. Goto record Pos in file f
  269. }
  270. Begin
  271. If InOutRes <> 0 then
  272. exit;
  273. case FileRec(f).Mode of
  274. fmInOut,fmInput,fmOutput : ;
  275. else
  276. begin
  277. InOutRes:=103;
  278. exit;
  279. end;
  280. end;
  281. Do_Seek(FileRec(f).Handle,Pos*FileRec(f).RecSize);
  282. End;
  283. Procedure Truncate(Var f:File);[IOCheck];
  284. {
  285. Truncate/Cut file f at the current record Position
  286. }
  287. Begin
  288. If InOutRes <> 0 then
  289. exit;
  290. case FileRec(f).Mode of
  291. fmInOut,fmOutput : ;
  292. else
  293. begin
  294. InOutRes:=103;
  295. exit;
  296. end;
  297. end;
  298. Do_Truncate(FileRec(f).Handle,FilePos(f)*FileRec(f).RecSize);
  299. End;
  300. Procedure Close(var f:File);[IOCheck];
  301. {
  302. Close file f
  303. }
  304. Begin
  305. If InOutRes <> 0 then
  306. exit;
  307. case FileRec(f).Mode of
  308. fmInOut,fmInput,fmOutput : ;
  309. else
  310. begin
  311. InOutRes:=103;
  312. exit;
  313. end;
  314. end;
  315. FileRec(f).mode:=fmClosed;
  316. Do_Close(FileRec(f).Handle);
  317. End;
  318. Procedure Erase(var f : File);[IOCheck];
  319. Begin
  320. If InOutRes <> 0 then
  321. exit;
  322. If FileRec(f).mode=fmClosed Then
  323. Do_Erase(PChar(@FileRec(f).Name));
  324. End;
  325. Procedure Rename(var f : File;p:pchar);[IOCheck];
  326. Begin
  327. If InOutRes <> 0 then
  328. exit;
  329. If FileRec(f).mode=fmClosed Then
  330. Begin
  331. Do_Rename(PChar(@FileRec(f).Name),p);
  332. Move(p^,FileRec(f).Name,StrLen(p)+1);
  333. End;
  334. End;
  335. Procedure Rename(var f : File;const s : string);[IOCheck];
  336. var
  337. p : array[0..255] Of Char;
  338. Begin
  339. If InOutRes <> 0 then
  340. exit;
  341. Move(s[1],p,Length(s));
  342. p[Length(s)]:=#0;
  343. Rename(f,Pchar(@p));
  344. End;
  345. Procedure Rename(var f : File;c : char);[IOCheck];
  346. var
  347. p : array[0..1] Of Char;
  348. Begin
  349. If InOutRes <> 0 then
  350. exit;
  351. p[0]:=c;
  352. p[1]:=#0;
  353. Rename(f,Pchar(@p));
  354. End;
  355. {
  356. $Log$
  357. Revision 1.19 2000-02-09 16:59:29 peter
  358. * truncated log
  359. Revision 1.18 2000/01/17 20:02:30 peter
  360. * open with mode 2 in rewrite
  361. Revision 1.17 2000/01/16 22:25:38 peter
  362. * check handle for file closing
  363. Revision 1.16 2000/01/07 16:41:33 daniel
  364. * copyright 2000
  365. Revision 1.15 2000/01/07 16:32:24 daniel
  366. * copyright 2000 added
  367. Revision 1.14 1999/10/28 09:52:50 peter
  368. * use filemode for rewrite instead of mode 1
  369. Revision 1.13 1999/09/10 15:40:33 peter
  370. * fixed do_open flags to be > $100, becuase filemode can be upto 255
  371. Revision 1.12 1999/09/08 16:12:24 peter
  372. * fixed inoutres for diskfull
  373. Revision 1.11 1999/09/07 15:54:18 hajny
  374. * fixed problem with Close under OS/2
  375. }