file.inc 8.4 KB

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