file.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. Do_Open(f,PChar(@FileRec(f).Name),$1000+filemode);
  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;Var 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. else
  117. begin
  118. InOutRes:=103;
  119. exit;
  120. end;
  121. end;
  122. Result:=Do_Write(FileRec(f).Handle,Longint(@Buf),Count*FileRec(f).RecSize) div FileRec(f).RecSize;
  123. End;
  124. Procedure BlockWrite(Var f:File;Var Buf;Count:Word;var Result:Word);[IOCheck];
  125. {
  126. Write Count records from Buf to file f, return written records in Result
  127. }
  128. var
  129. l : longint;
  130. Begin
  131. BlockWrite(f,Buf,Count,l);
  132. Result:=l;
  133. End;
  134. Procedure BlockWrite(Var f:File;Var Buf;Count:Word;var Result:Integer);[IOCheck];
  135. {
  136. Write Count records from Buf to file f, return written records in Result
  137. }
  138. var
  139. l : longint;
  140. Begin
  141. BlockWrite(f,Buf,Count,l);
  142. Result:=l;
  143. End;
  144. Procedure BlockWrite(Var f:File;Var Buf;Count:Longint);[IOCheck];
  145. {
  146. Write Count records from Buf to file f, if none a Read and Count>0 then
  147. InOutRes is set
  148. }
  149. var
  150. Result : Longint;
  151. Begin
  152. BlockWrite(f,Buf,Count,Result);
  153. If (Result<Count) and (Count>0) Then
  154. InOutRes:=101;
  155. End;
  156. Procedure BlockRead(var f:File;var Buf;Count:Longint;var Result:Longint);[IOCheck];
  157. {
  158. Read Count records from file f ro Buf, return number of read records in
  159. Result
  160. }
  161. Begin
  162. Result:=0;
  163. If InOutRes <> 0 then
  164. exit;
  165. case FileRec(f).Mode of
  166. fmInOut,fmInput : ;
  167. else
  168. begin
  169. InOutRes:=103;
  170. exit;
  171. end;
  172. end;
  173. Result:=Do_Read(FileRec(f).Handle,Longint(@Buf),count*FileRec(f).RecSize) div FileRec(f).RecSize;
  174. End;
  175. Procedure BlockRead(var f:File;var Buf;count:Word;var Result:Word);[IOCheck];
  176. {
  177. Read Count records from file f to Buf, return number of read records in
  178. Result
  179. }
  180. var
  181. l : longint;
  182. Begin
  183. BlockRead(f,Buf,Count,l);
  184. Result:=l;
  185. End;
  186. Procedure BlockRead(var f:File;var Buf;count:Word;var Result:Integer);[IOCheck];
  187. {
  188. Read Count records from file f to Buf, return number of read records in
  189. Result
  190. }
  191. var
  192. l : longint;
  193. Begin
  194. BlockRead(f,Buf,Count,l);
  195. Result:=l;
  196. End;
  197. Procedure BlockRead(Var f:File;Var Buf;Count:Longint);[IOCheck];
  198. {
  199. Read Count records from file f to Buf, if none are read and Count>0 then
  200. InOutRes is set
  201. }
  202. var
  203. Result : Longint;
  204. Begin
  205. BlockRead(f,Buf,Count,Result);
  206. If (Result<Count) and (Count>0) Then
  207. InOutRes:=100;
  208. End;
  209. Function FilePos(var f:File):Longint;[IOCheck];
  210. {
  211. Return current Position In file f in records
  212. }
  213. Begin
  214. FilePos:=0;
  215. If InOutRes <> 0 then
  216. exit;
  217. case FileRec(f).Mode of
  218. fmInOut,fmInput,fmOutput : ;
  219. else
  220. begin
  221. InOutRes:=103;
  222. exit;
  223. end;
  224. end;
  225. FilePos:=Do_FilePos(FileRec(f).Handle) div FileRec(f).RecSize;
  226. End;
  227. Function FileSize(var f:File):Longint;[IOCheck];
  228. {
  229. Return the size of file f in records
  230. }
  231. Begin
  232. FileSize:=0;
  233. If InOutRes <> 0 then
  234. exit;
  235. case FileRec(f).Mode of
  236. fmInOut,fmInput,fmOutput : ;
  237. else
  238. begin
  239. InOutRes:=103;
  240. exit;
  241. end;
  242. end;
  243. if (FileRec(f).RecSize>0) then
  244. FileSize:=Do_FileSize(FileRec(f).Handle) div FileRec(f).RecSize;
  245. End;
  246. Function Eof(var f:File):Boolean;[IOCheck];
  247. {
  248. Return True if we're at the end of the file f, else False is returned
  249. }
  250. Begin
  251. Eof:=false;
  252. If InOutRes <> 0 then
  253. exit;
  254. case FileRec(f).Mode of
  255. fmInOut,fmInput,fmOutput : ;
  256. else
  257. begin
  258. InOutRes:=103;
  259. exit;
  260. end;
  261. end;
  262. {Can't use do_ routines because we need record support}
  263. Eof:=(FileSize(f)<=FilePos(f));
  264. End;
  265. Procedure Seek(var f:File;Pos:Longint);[IOCheck];
  266. {
  267. Goto record Pos in file f
  268. }
  269. Begin
  270. If InOutRes <> 0 then
  271. exit;
  272. case FileRec(f).Mode of
  273. fmInOut,fmInput,fmOutput : ;
  274. else
  275. begin
  276. InOutRes:=103;
  277. exit;
  278. end;
  279. end;
  280. Do_Seek(FileRec(f).Handle,Pos*FileRec(f).RecSize);
  281. End;
  282. Procedure Truncate(Var f:File);[IOCheck];
  283. {
  284. Truncate/Cut file f at the current record Position
  285. }
  286. Begin
  287. If InOutRes <> 0 then
  288. exit;
  289. case FileRec(f).Mode of
  290. fmInOut,fmOutput : ;
  291. else
  292. begin
  293. InOutRes:=103;
  294. exit;
  295. end;
  296. end;
  297. Do_Truncate(FileRec(f).Handle,FilePos(f)*FileRec(f).RecSize);
  298. End;
  299. Procedure Close(var f:File);[IOCheck];
  300. {
  301. Close file f
  302. }
  303. Begin
  304. If InOutRes <> 0 then
  305. exit;
  306. case FileRec(f).Mode of
  307. fmInOut,fmInput,fmOutput : ;
  308. else
  309. begin
  310. InOutRes:=103;
  311. exit;
  312. end;
  313. end;
  314. FileRec(f).mode:=fmClosed;
  315. {$IFDEF OS2}
  316. if (FileRec (F).Handle > 4) or
  317. (os_MODE = osOS2) and (FileRec (F).Handle > 2) then
  318. { Only three standard handles under real OS/2 }
  319. {$ELSE}
  320. if FileRec(f).Handle>4 then
  321. {$ENDIF}
  322. Do_Close(FileRec(f).Handle);
  323. End;
  324. Procedure Erase(var f : File);[IOCheck];
  325. Begin
  326. If InOutRes <> 0 then
  327. exit;
  328. If FileRec(f).mode=fmClosed Then
  329. Do_Erase(PChar(@FileRec(f).Name));
  330. End;
  331. Procedure Rename(var f : File;p:pchar);[IOCheck];
  332. Begin
  333. If InOutRes <> 0 then
  334. exit;
  335. If FileRec(f).mode=fmClosed Then
  336. Begin
  337. Do_Rename(PChar(@FileRec(f).Name),p);
  338. Move(p^,FileRec(f).Name,StrLen(p)+1);
  339. End;
  340. End;
  341. Procedure Rename(var f : File;const s : string);[IOCheck];
  342. var
  343. p : array[0..255] Of Char;
  344. Begin
  345. If InOutRes <> 0 then
  346. exit;
  347. Move(s[1],p,Length(s));
  348. p[Length(s)]:=#0;
  349. Rename(f,Pchar(@p));
  350. End;
  351. Procedure Rename(var f : File;c : char);[IOCheck];
  352. var
  353. p : array[0..1] Of Char;
  354. Begin
  355. If InOutRes <> 0 then
  356. exit;
  357. p[0]:=c;
  358. p[1]:=#0;
  359. Rename(f,Pchar(@p));
  360. End;
  361. {
  362. $Log$
  363. Revision 1.16 2000-01-07 16:41:33 daniel
  364. * copyright 2000
  365. Revision 1.15 2000/01/07 16:32:24 daniel
  366. * copyright 2000 added
  367. Revision 1.14 1999/10/28 09:52:50 peter
  368. * use filemode for rewrite instead of mode 1
  369. Revision 1.13 1999/09/10 15:40:33 peter
  370. * fixed do_open flags to be > $100, becuase filemode can be upto 255
  371. Revision 1.12 1999/09/08 16:12:24 peter
  372. * fixed inoutres for diskfull
  373. Revision 1.11 1999/09/07 15:54:18 hajny
  374. * fixed problem with Close under OS/2
  375. Revision 1.10 1998/11/29 23:10:12 peter
  376. * also check fmInput,fmOutput
  377. Revision 1.9 1998/11/29 22:28:11 peter
  378. + io-error 103 added
  379. Revision 1.8 1998/09/17 16:34:16 peter
  380. * new eof,eoln,seekeoln,seekeof
  381. * speed upgrade for read_string
  382. * inoutres 104/105 updates for read_* and write_*
  383. Revision 1.7 1998/09/04 18:16:12 peter
  384. * uniform filerec/textrec (with recsize:longint and name:0..255)
  385. Revision 1.6 1998/07/19 19:55:32 michael
  386. + fixed rename. Changed p to p^
  387. Revision 1.5 1998/07/02 12:15:39 carl
  388. + Each IOCheck routine now checks for InOures before executing, like TP
  389. Revision 1.4 1998/06/23 16:57:16 peter
  390. * fixed the filesize() problems under linux and filerec.size=0 error
  391. Revision 1.3 1998/05/21 19:30:56 peter
  392. * objects compiles for linux
  393. + assign(pchar), assign(char), rename(pchar), rename(char)
  394. * fixed read_text_as_array
  395. + read_text_as_pchar which was not yet in the rtl
  396. Revision 1.2 1998/05/12 10:42:44 peter
  397. * moved getopts to inc/, all supported OS's need argc,argv exported
  398. + strpas, strlen are now exported in the systemunit
  399. * removed logs
  400. * removed $ifdef ver_above
  401. }