file.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WithOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************
  11. subroutines For UnTyped File handling
  12. ****************************************************************************}
  13. type
  14. UnTypedFile=File;
  15. Procedure Assign({$ifdef PARAOUTFILE}out{$else}var{$endif} f:File;const Name:RtlString);
  16. {
  17. Assign Name to file f so it can be used with the file routines
  18. }
  19. Begin
  20. FillChar(f,SizeOf(FileRec),0);
  21. FileRec(f).Handle:=UnusedHandle;
  22. FileRec(f).mode:=fmClosed;
  23. StrLCopy(FileRec(f).Name, PRtlChar(Name), SizeOf(FileRec(f).Name) div SizeOf(RtlChar));
  24. End;
  25. Procedure Rewrite(var f:File;l:Longint);[IOCheck];
  26. {
  27. Create file f with recordsize of l
  28. }
  29. Begin
  30. If InOutRes <> 0 then
  31. exit;
  32. Case FileRec(f).mode Of
  33. fmInOut,fmInput,fmOutput : Close(f);
  34. fmClosed : ;
  35. else
  36. Begin
  37. InOutRes:=102;
  38. exit;
  39. End;
  40. End;
  41. If l=0 Then
  42. InOutRes:=2
  43. else
  44. Begin
  45. { Reopen with filemode 2, to be Tp compatible (PFV) }
  46. Do_Open(f,PRtlChar(@FileRec(f).Name),$1002);
  47. FileRec(f).RecSize:=l;
  48. End;
  49. End;
  50. Procedure Reset(var f:File;l:Longint);[IOCheck];
  51. {
  52. Open file f with recordsize of l and filemode
  53. }
  54. Begin
  55. If InOutRes <> 0 then
  56. Exit;
  57. Case FileRec(f).mode Of
  58. fmInOut,fmInput,fmOutput : Close(f);
  59. fmClosed : ;
  60. else
  61. Begin
  62. InOutRes:=102;
  63. exit;
  64. End;
  65. End;
  66. If l=0 Then
  67. InOutRes:=2
  68. else
  69. Begin
  70. Do_Open(f,PRtlChar(@FileRec(f).Name),Filemode);
  71. FileRec(f).RecSize:=l;
  72. End;
  73. End;
  74. Procedure Rewrite(Var f:File);[IOCheck];
  75. {
  76. Create file with (default) 128 byte records
  77. }
  78. Begin
  79. If InOutRes <> 0 then
  80. exit;
  81. Rewrite(f,128);
  82. End;
  83. Procedure Reset(Var f:File);[IOCheck];
  84. {
  85. Open file with (default) 128 byte records
  86. }
  87. Begin
  88. If InOutRes <> 0 then
  89. exit;
  90. Reset(f,128);
  91. End;
  92. Procedure BlockWrite(Var f:File;Const Buf;Count:Int64;var Result:Int64);[IOCheck];
  93. {
  94. Write Count records from Buf to file f, return written records in result
  95. }
  96. Begin
  97. Result:=0;
  98. If InOutRes <> 0 then
  99. exit;
  100. case FileRec(f).Mode of
  101. fmInOut,fmOutput :
  102. Result:=Do_Write(FileRec(f).Handle,@Buf,Count*FileRec(f).RecSize)
  103. div FileRec(f).RecSize;
  104. fmInPut: inOutRes := 105;
  105. else InOutRes:=103;
  106. end;
  107. End;
  108. Procedure BlockWrite(Var f:File;Const Buf;Count:Longint;var Result:Longint);[IOCheck];
  109. {
  110. Write Count records from Buf to file f, return written records in result
  111. }
  112. var
  113. l : Int64;
  114. Begin
  115. BlockWrite(f,Buf,Count,l);
  116. Result:=longint(l);
  117. End;
  118. Procedure BlockWrite(Var f:File;Const Buf;Count:Word;var Result:Word);[IOCheck];
  119. {
  120. Write Count records from Buf to file f, return written records in Result
  121. }
  122. var
  123. l : Int64;
  124. Begin
  125. BlockWrite(f,Buf,Count,l);
  126. Result:=word(l);
  127. End;
  128. Procedure BlockWrite(Var f:File;Const Buf;Count:Cardinal;var Result:Cardinal);[IOCheck];
  129. {
  130. Write Count records from Buf to file f, return written records in Result
  131. }
  132. var
  133. l : Int64;
  134. Begin
  135. BlockWrite(f,Buf,Count,l);
  136. Result:=l;
  137. End;
  138. Procedure BlockWrite(Var f:File;Const Buf;Count:Word;var Result:Integer);[IOCheck];
  139. {
  140. Write Count records from Buf to file f, return written records in Result
  141. }
  142. var
  143. l : Int64;
  144. Begin
  145. BlockWrite(f,Buf,Count,l);
  146. Result:=integer(l);
  147. End;
  148. Procedure BlockWrite(Var f:File;Const Buf;Count:Longint);[IOCheck];
  149. {
  150. Write Count records from Buf to file f, if none a Read and Count>0 then
  151. InOutRes is set
  152. }
  153. var
  154. Result : Int64;
  155. Begin
  156. BlockWrite(f,Buf,Count,Result);
  157. If (InOutRes=0) and (Result<Count) and (Count>0) Then
  158. InOutRes:=101;
  159. End;
  160. Procedure BlockRead(var f:File;var Buf;Count:Int64;var Result:Int64);[IOCheck];
  161. {
  162. Read Count records from file f ro Buf, return number of read records in
  163. Result
  164. }
  165. Begin
  166. Result:=0;
  167. If InOutRes <> 0 then
  168. exit;
  169. case FileRec(f).Mode of
  170. fmInOut,fmInput :
  171. Result:=Do_Read(FileRec(f).Handle,@Buf,count*FileRec(f).RecSize)
  172. div FileRec(f).RecSize;
  173. fmOutput: inOutRes := 104;
  174. else InOutRes:=103;
  175. end;
  176. End;
  177. Procedure BlockRead(var f:File;var Buf;Count:Longint;var Result:Longint);[IOCheck];
  178. {
  179. Read Count records from file f ro Buf, return number of read records in
  180. Result
  181. }
  182. var
  183. l : int64;
  184. Begin
  185. BlockRead(f,Buf,Count,l);
  186. Result:=longint(l);
  187. End;
  188. Procedure BlockRead(var f:File;var Buf;count:Word;var Result:Word);[IOCheck];
  189. {
  190. Read Count records from file f to Buf, return number of read records in
  191. Result
  192. }
  193. var
  194. l : int64;
  195. Begin
  196. BlockRead(f,Buf,Count,l);
  197. Result:=word(l);
  198. End;
  199. Procedure BlockRead(var f:File;var Buf;count:Cardinal;var Result:Cardinal);[IOCheck];
  200. {
  201. Read Count records from file f to Buf, return number of read records in
  202. Result
  203. }
  204. var
  205. l : int64;
  206. Begin
  207. BlockRead(f,Buf,Count,l);
  208. Result:=l;
  209. End;
  210. Procedure BlockRead(var f:File;var Buf;count:Word;var Result:Integer);[IOCheck];
  211. {
  212. Read Count records from file f to Buf, return number of read records in
  213. Result
  214. }
  215. var
  216. l : int64;
  217. Begin
  218. BlockRead(f,Buf,Count,l);
  219. Result:=integer(l);
  220. End;
  221. Procedure BlockRead(Var f:File;Var Buf;Count:Int64);[IOCheck];
  222. {
  223. Read Count records from file f to Buf, if none are read and Count>0 then
  224. InOutRes is set
  225. }
  226. var
  227. Result : int64;
  228. Begin
  229. BlockRead(f,Buf,Count,Result);
  230. If (InOutRes=0) and (Result<Count) and (Count>0) Then
  231. InOutRes:=100;
  232. End;
  233. Function FilePos(var f:File):Int64;[IOCheck];
  234. {
  235. Return current Position In file f in records
  236. }
  237. Begin
  238. FilePos:=0;
  239. If InOutRes <> 0 then
  240. exit;
  241. case FileRec(f).Mode of
  242. fmInOut,fmInput,fmOutput :
  243. FilePos:=Do_FilePos(FileRec(f).Handle) div FileRec(f).RecSize;
  244. else
  245. InOutRes:=103;
  246. end;
  247. End;
  248. Function FileSize(var f:File):Int64;[IOCheck];
  249. {
  250. Return the size of file f in records
  251. }
  252. Begin
  253. FileSize:=0;
  254. If InOutRes <> 0 then
  255. exit;
  256. case FileRec(f).Mode of
  257. fmInOut,fmInput,fmOutput :
  258. begin
  259. if (FileRec(f).RecSize>0) then
  260. FileSize:=Do_FileSize(FileRec(f).Handle) div FileRec(f).RecSize;
  261. end;
  262. else InOutRes:=103;
  263. end;
  264. End;
  265. Function Eof(var f:File):Boolean;[IOCheck];
  266. {
  267. Return True if we're at the end of the file f, else False is returned
  268. }
  269. Begin
  270. Eof:=false;
  271. If InOutRes <> 0 then
  272. exit;
  273. case FileRec(f).Mode of
  274. {Can't use do_ routines because we need record support}
  275. fmInOut,fmInput,fmOutput : Eof:=(FileSize(f)<=FilePos(f));
  276. else InOutRes:=103;
  277. end;
  278. End;
  279. Procedure Seek(var f:File;Pos:Int64);[IOCheck];
  280. {
  281. Goto record Pos in file f
  282. }
  283. Begin
  284. If InOutRes <> 0 then
  285. exit;
  286. case FileRec(f).Mode of
  287. fmInOut,fmInput,fmOutput :
  288. Do_Seek(FileRec(f).Handle,Pos*FileRec(f).RecSize);
  289. else InOutRes:=103;
  290. end;
  291. End;
  292. Procedure Truncate(Var f:File);[IOCheck];
  293. {
  294. Truncate/Cut file f at the current record Position
  295. }
  296. Begin
  297. If InOutRes <> 0 then
  298. exit;
  299. case FileRec(f).Mode of
  300. fmInOut,fmOutput :
  301. Do_Truncate(FileRec(f).Handle,FilePos(f)*FileRec(f).RecSize);
  302. else InOutRes:=103;
  303. end;
  304. End;
  305. Procedure Close(var f:File);[IOCheck];
  306. {
  307. Close file f
  308. }
  309. Begin
  310. If InOutRes <> 0 then
  311. exit;
  312. case FileRec(f).Mode of
  313. fmInOut,fmInput,fmOutput :
  314. begin
  315. Do_Close(FileRec(f).Handle);
  316. FileRec(f).mode:=fmClosed;
  317. end
  318. else InOutRes:=103;
  319. end;
  320. End;
  321. Procedure Erase(var f : File);[IOCheck];
  322. Begin
  323. If InOutRes <> 0 then
  324. exit;
  325. If FileRec(f).mode=fmClosed Then
  326. Do_Erase(PRtlChar(@FileRec(f).Name));
  327. End;
  328. Procedure Rename(var f : File;p:PRtlChar);[IOCheck];
  329. Begin
  330. If InOutRes <> 0 then
  331. exit;
  332. If FileRec(f).mode=fmClosed Then
  333. Begin
  334. Do_Rename(PRtlChar(@FileRec(f).Name),p);
  335. { check error code of do_rename }
  336. If InOutRes = 0 then
  337. StrLCopy(FileRec(f).Name, p, SizeOf(FileRec(f).Name) div SizeOf(RtlChar));
  338. End;
  339. End;
  340. Procedure Rename(var f : File;const s : RtlString);[IOCheck];
  341. Begin
  342. Rename(f,PRtlChar(s));
  343. End;