file.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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;Const 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. Result:=Do_Write(FileRec(f).Handle,Longint(@Buf),Count*FileRec(f).RecSize)
  118. div FileRec(f).RecSize;
  119. fmInPut: inOutRes := 105;
  120. else InOutRes:=103;
  121. end;
  122. End;
  123. Procedure BlockWrite(Var f:File;Const Buf;Count:Word;var Result:Word);[IOCheck];
  124. {
  125. Write Count records from Buf to file f, return written records in Result
  126. }
  127. var
  128. l : longint;
  129. Begin
  130. BlockWrite(f,Buf,Count,l);
  131. Result:=word(l);
  132. End;
  133. Procedure BlockWrite(Var f:File;Const Buf;Count:Cardinal;var Result:Cardinal);[IOCheck];
  134. {
  135. Write Count records from Buf to file f, return written records in Result
  136. }
  137. var
  138. l : longint;
  139. Begin
  140. BlockWrite(f,Buf,Count,l);
  141. Result:=l;
  142. End;
  143. Procedure BlockWrite(Var f:File;Const Buf;Count:Word;var Result:Integer);[IOCheck];
  144. {
  145. Write Count records from Buf to file f, return written records in Result
  146. }
  147. var
  148. l : longint;
  149. Begin
  150. BlockWrite(f,Buf,Count,l);
  151. Result:=integer(l);
  152. End;
  153. Procedure BlockWrite(Var f:File;Const Buf;Count:Longint);[IOCheck];
  154. {
  155. Write Count records from Buf to file f, if none a Read and Count>0 then
  156. InOutRes is set
  157. }
  158. var
  159. Result : Longint;
  160. Begin
  161. BlockWrite(f,Buf,Count,Result);
  162. If (Result<Count) and (Count>0) Then
  163. InOutRes:=101;
  164. End;
  165. Procedure BlockRead(var f:File;var Buf;Count:Longint;var Result:Longint);[IOCheck];
  166. {
  167. Read Count records from file f ro Buf, return number of read records in
  168. Result
  169. }
  170. Begin
  171. Result:=0;
  172. If InOutRes <> 0 then
  173. exit;
  174. case FileRec(f).Mode of
  175. fmInOut,fmInput :
  176. Result:=Do_Read(FileRec(f).Handle,Longint(@Buf),count*FileRec(f).RecSize)
  177. div FileRec(f).RecSize;
  178. fmOutput: inOutRes := 104;
  179. else InOutRes:=103;
  180. end;
  181. End;
  182. Procedure BlockRead(var f:File;var Buf;count:Word;var Result:Word);[IOCheck];
  183. {
  184. Read Count records from file f to Buf, return number of read records in
  185. Result
  186. }
  187. var
  188. l : longint;
  189. Begin
  190. BlockRead(f,Buf,Count,l);
  191. Result:=word(l);
  192. End;
  193. Procedure BlockRead(var f:File;var Buf;count:Cardinal;var Result:Cardinal);[IOCheck];
  194. {
  195. Read Count records from file f to Buf, return number of read records in
  196. Result
  197. }
  198. var
  199. l : longint;
  200. Begin
  201. BlockRead(f,Buf,Count,l);
  202. Result:=l;
  203. End;
  204. Procedure BlockRead(var f:File;var Buf;count:Word;var Result:Integer);[IOCheck];
  205. {
  206. Read Count records from file f to Buf, return number of read records in
  207. Result
  208. }
  209. var
  210. l : longint;
  211. Begin
  212. BlockRead(f,Buf,Count,l);
  213. Result:=integer(l);
  214. End;
  215. Procedure BlockRead(Var f:File;Var Buf;Count:Longint);[IOCheck];
  216. {
  217. Read Count records from file f to Buf, if none are read and Count>0 then
  218. InOutRes is set
  219. }
  220. var
  221. Result : Longint;
  222. Begin
  223. BlockRead(f,Buf,Count,Result);
  224. If (Result<Count) and (Count>0) Then
  225. InOutRes:=100;
  226. End;
  227. Function FilePos(var f:File):Longint;[IOCheck];
  228. {
  229. Return current Position In file f in records
  230. }
  231. Begin
  232. FilePos:=0;
  233. If InOutRes <> 0 then
  234. exit;
  235. case FileRec(f).Mode of
  236. fmInOut,fmInput,fmOutput :
  237. FilePos:=Do_FilePos(FileRec(f).Handle) div FileRec(f).RecSize;
  238. else
  239. InOutRes:=103;
  240. end;
  241. End;
  242. Function FileSize(var f:File):Longint;[IOCheck];
  243. {
  244. Return the size of file f in records
  245. }
  246. Begin
  247. FileSize:=0;
  248. If InOutRes <> 0 then
  249. exit;
  250. case FileRec(f).Mode of
  251. fmInOut,fmInput,fmOutput :
  252. begin
  253. if (FileRec(f).RecSize>0) then
  254. FileSize:=Do_FileSize(FileRec(f).Handle) div FileRec(f).RecSize;
  255. end;
  256. else InOutRes:=103;
  257. end;
  258. End;
  259. Function Eof(var f:File):Boolean;[IOCheck];
  260. {
  261. Return True if we're at the end of the file f, else False is returned
  262. }
  263. Begin
  264. Eof:=false;
  265. If InOutRes <> 0 then
  266. exit;
  267. case FileRec(f).Mode of
  268. {Can't use do_ routines because we need record support}
  269. fmInOut,fmInput,fmOutput : Eof:=(FileSize(f)<=FilePos(f));
  270. else InOutRes:=103;
  271. end;
  272. End;
  273. Procedure Seek(var f:File;Pos:Longint);[IOCheck];
  274. {
  275. Goto record Pos in file f
  276. }
  277. Begin
  278. If InOutRes <> 0 then
  279. exit;
  280. case FileRec(f).Mode of
  281. fmInOut,fmInput,fmOutput :
  282. Do_Seek(FileRec(f).Handle,Pos*FileRec(f).RecSize);
  283. else InOutRes:=103;
  284. end;
  285. End;
  286. Procedure Truncate(Var f:File);[IOCheck];
  287. {
  288. Truncate/Cut file f at the current record Position
  289. }
  290. Begin
  291. If InOutRes <> 0 then
  292. exit;
  293. case FileRec(f).Mode of
  294. fmInOut,fmOutput :
  295. Do_Truncate(FileRec(f).Handle,FilePos(f)*FileRec(f).RecSize);
  296. else InOutRes:=103;
  297. end;
  298. End;
  299. Procedure Close(var f:File);[IOCheck];
  300. {
  301. Close file f
  302. }
  303. Begin
  304. If InOutRes <> 0 then
  305. exit;
  306. case FileRec(f).Mode of
  307. fmInOut,fmInput,fmOutput :
  308. begin
  309. Do_Close(FileRec(f).Handle);
  310. FileRec(f).mode:=fmClosed;
  311. end
  312. else InOutRes:=103;
  313. end;
  314. End;
  315. Procedure Erase(var f : File);[IOCheck];
  316. Begin
  317. If InOutRes <> 0 then
  318. exit;
  319. If FileRec(f).mode=fmClosed Then
  320. Do_Erase(PChar(@FileRec(f).Name));
  321. End;
  322. Procedure Rename(var f : File;p:pchar);[IOCheck];
  323. Begin
  324. If InOutRes <> 0 then
  325. exit;
  326. If FileRec(f).mode=fmClosed Then
  327. Begin
  328. Do_Rename(PChar(@FileRec(f).Name),p);
  329. { check error code of do_rename }
  330. If InOutRes = 0 then
  331. Move(p^,FileRec(f).Name,StrLen(p)+1);
  332. End;
  333. End;
  334. Procedure Rename(var f : File;const s : string);[IOCheck];
  335. var
  336. p : array[0..255] Of Char;
  337. Begin
  338. If InOutRes <> 0 then
  339. exit;
  340. Move(s[1],p,Length(s));
  341. p[Length(s)]:=#0;
  342. Rename(f,Pchar(@p));
  343. End;
  344. Procedure Rename(var f : File;c : char);[IOCheck];
  345. var
  346. p : array[0..1] Of Char;
  347. Begin
  348. If InOutRes <> 0 then
  349. exit;
  350. p[0]:=c;
  351. p[1]:=#0;
  352. Rename(f,Pchar(@p));
  353. End;
  354. {
  355. $Log$
  356. Revision 1.6 2002-12-07 14:36:14 carl
  357. - avoid warnings (add typecast)
  358. Revision 1.5 2002/09/07 15:07:45 peter
  359. * old logs removed and tabs fixed
  360. }