file.inc 8.6 KB

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