file.inc 7.5 KB

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