sysfile.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Main OS dependant body of the system unit, loosely modelled
  4. after POSIX. *BSD version (Linux version is near identical)
  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. Const
  12. UnusedHandle = Nil;
  13. DefaultTextLineBreakStyle = tlbsLF;
  14. CtrlZMarksEOF: boolean = false;
  15. Procedure Do_Close(Handle:thandle);
  16. Begin
  17. Try
  18. (JLObject(Handle) as JICloseable).close();
  19. except
  20. InoutRes:=1
  21. end;
  22. End;
  23. Procedure Do_Erase(p: PFileTextRecChar; pchangeable: boolean);
  24. var
  25. F : JIFile;
  26. S : String;
  27. Begin
  28. InoutRes:=0;
  29. S:=P;
  30. F:=JIFile.Create(S);
  31. if Not F.Exists then
  32. InoutRes:=2
  33. else
  34. if F.isDirectory then
  35. InoutRes:=2
  36. else
  37. try
  38. F.Delete;
  39. except
  40. InoutRes:=5;
  41. end;
  42. End;
  43. { truncate at a given position }
  44. procedure do_truncate (handle:thandle;fpos:int64);
  45. Var
  46. F : JIFile;
  47. C : JNCFileChannel;
  48. begin
  49. InoutRes:=0;
  50. try
  51. // Will raise an error for stdout, since it is JIPrintStream, not JIFileOutputStream
  52. C:=(JLObject(handle) as JIFileOutputStream).getChannel();
  53. C.truncate(fpos);
  54. except
  55. InoutRes:=2;
  56. end;
  57. end;
  58. Procedure Do_Rename(p1 : PFileTextRecChar ; p2:UnicodeString; p1changeable, p2changeable: boolean);
  59. Var
  60. S1,S2 : string;
  61. F1,F2 : JIFile;
  62. Begin
  63. S1:=P1;
  64. S2:=P2;
  65. F1:=JIFile.Create(S1);
  66. F2:=JIFile.Create(S2);
  67. if not F1.Exists then
  68. InoutRes:=2
  69. else
  70. try
  71. F1.RenameTo(F2);
  72. InoutRes:=1;
  73. except
  74. InoutRes:=5;
  75. end;
  76. End;
  77. Function Do_Write(Handle:thandle;Addr:Pointer;Len:Longint):longint;
  78. var
  79. F : JIOutputStream;
  80. Begin
  81. InoutRes:=0;
  82. F:=JIOutputStream(Handle);
  83. try
  84. F.write(Arr1jbyte(Addr),0,Len);
  85. Do_Write:=Len;
  86. except
  87. InoutRes:=101;
  88. end;
  89. End;
  90. Function Do_Read(Handle:thandle;Addr:Pointer;Len:Longint):Longint;
  91. var
  92. F : JIInputStream;
  93. Begin
  94. InoutRes:=0;
  95. F:=JIInputStream(Handle);
  96. try
  97. Do_Read:=F.read(Arr1jbyte(Addr),0,Len);
  98. except
  99. InoutRes:=101;
  100. end;
  101. End;
  102. function Do_FilePos(Handle: thandle):Int64;
  103. Var
  104. C : JNCFileChannel;
  105. Begin
  106. Do_FilePos:=0;
  107. if JLObject(Handle) is JIFileInputStream then
  108. C:=JIFileInputStream(Handle).getChannel()
  109. else if JLObject(Handle) is JIFileOutputStream then
  110. C:=JIFileOutputStream(Handle).getChannel()
  111. else
  112. C:=Nil;
  113. try
  114. if (C<>Nil) then
  115. begin
  116. Do_FilePos:=C.position();
  117. InoutRes:=0;
  118. end
  119. else
  120. InoutRes:=5;
  121. except
  122. InoutRes:=5;
  123. end;
  124. End;
  125. Procedure Do_Seek(Handle:thandle;Pos:Int64);
  126. Var
  127. C : JNCFileChannel;
  128. Begin
  129. if JLObject(Handle) is JIFileInputStream then
  130. C:=JIFileInputStream(Handle).getChannel()
  131. else
  132. C:=JIFileOutputStream(Handle).getChannel();
  133. try
  134. C.position(Pos);
  135. InoutRes:=0;
  136. except
  137. InoutRes:=5;
  138. end;
  139. End;
  140. Function GetChannel(Handle:thandle):JNCFileChannel;
  141. begin
  142. if JLObject(Handle) is JIFileInputStream then
  143. GetChannel:=JIFileInputStream(Handle).getChannel()
  144. else if JLObject(Handle) is JIFileOutputStream then
  145. GetChannel:=JIFileOutputStream(Handle).getChannel()
  146. else if JLObject(Handle) is JIRandomAccessFile then
  147. GetChannel:=JIRandomAccessFile(Handle).getChannel()
  148. else
  149. begin
  150. GetChannel:=Nil;
  151. InoutRes:=5;
  152. end;
  153. end;
  154. Function Do_SeekEnd(Handle:thandle):Int64;
  155. Var
  156. C : JNCFileChannel;
  157. Begin
  158. C:=GetChannel(Handle); // Sets inoutres if Nil
  159. try
  160. if Assigned(C) then
  161. begin
  162. C.position(C.Size);
  163. InoutRes:=0;
  164. end
  165. except
  166. InoutRes:=5;
  167. end;
  168. end;
  169. Function Do_FileSize(Handle:thandle):Int64;
  170. Var
  171. C : JNCFileChannel;
  172. Begin
  173. C:=GetChannel(Handle); // Sets inoutres if Nil
  174. try
  175. if Assigned(C) then
  176. begin
  177. Result:=C.Size;
  178. InoutRes:=0;
  179. end
  180. except
  181. InoutRes:=5;
  182. end;
  183. End;
  184. Function Do_IsDevice(Handle : THandle) : boolean;
  185. begin
  186. Result:=JLObject(Handle) is JIPrintStream ;
  187. end;
  188. Procedure Do_Open(var F : TextRec; p: PFileTextRecChar; flags: longint; pchangeable: boolean);
  189. {
  190. FileRec and textrec have both Handle and mode as the first items so
  191. they could use the same routine for opening/creating.
  192. when (flags and $100) the file will be append
  193. when (flags and $1000) the file will be truncate/rewritten
  194. when (flags and $10000) there is no check for close (needed for textfiles)
  195. }
  196. var
  197. S : String;
  198. Begin
  199. S:=p;
  200. { close first if opened }
  201. if ((flags and $10000)=0) then
  202. begin
  203. case f.mode of
  204. fminput,fmoutput,fminout : Do_Close(TextRec(f).Handle);
  205. fmclosed : ;
  206. else
  207. begin
  208. inoutres:=102; {not assigned}
  209. exit;
  210. end;
  211. end;
  212. end;
  213. { reset file Handle }
  214. f.Handle:=Nil;
  215. { We do the conversion of filemodes here, concentrated on 1 place }
  216. case (flags and 3) of
  217. 0 : begin
  218. if S='' then
  219. f.Handle:=JLsystem.fin
  220. else
  221. F.Handle:=JIFileInputStream.create(S);
  222. f.mode:=fminput;
  223. end;
  224. 1 : begin
  225. if S='' then
  226. f.Handle:=JLsystem.fout
  227. else
  228. F.Handle:=JIFileOutputStream.Create(S,(flags and $100)=$100);
  229. f.mode:=fmoutput;
  230. end;
  231. 2 : begin
  232. // We may need to check existence first ?
  233. F.Handle:=JIRandomAccessFile.Create(S,'rw');
  234. if (flags and $100)=$100 then
  235. With GetChannel(F.Handle) do
  236. Position(Size);
  237. f.mode:=fminout;
  238. end;
  239. end;
  240. if (S='') then
  241. exit;
  242. If f.Handle=Nil Then
  243. begin
  244. InoutRes:=5;
  245. f.mode:=fmclosed;
  246. end
  247. else
  248. InOutRes:=0;
  249. End;