file.inc 8.1 KB

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