sysfile.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2020 by Free Pascal development team
  4. Low level file functions for the Sinclair QL
  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. Low level File Routines
  13. All these functions can set InOutRes on errors
  14. ****************************************************************************}
  15. { close a file from the handle value }
  16. procedure do_close(handle : longint);
  17. begin
  18. Error2InOutRes(io_close(handle));
  19. end;
  20. procedure do_erase(p : pchar; pchangeable: boolean);
  21. begin
  22. end;
  23. procedure do_rename(p1,p2 : pchar; p1changeable, p2changeable: boolean);
  24. begin
  25. end;
  26. function do_write(h: longint; addr: pointer; len: longint) : longint;
  27. var
  28. res: longint;
  29. begin
  30. do_write:=0;
  31. res:=io_sstrg(h, -1, addr, len);
  32. if res < 0 then
  33. Error2InOutRes(res)
  34. else
  35. do_write:=res;
  36. end;
  37. function do_read(h: longint; addr: pointer; len: longint) : longint;
  38. var
  39. res: longint;
  40. begin
  41. do_read := 0;
  42. res := io_fline(h, -1, addr, len);
  43. if res < 0 then
  44. Error2InOutRes(res)
  45. else
  46. do_read := res;
  47. end;
  48. function do_filepos(handle: longint): longint;
  49. var
  50. res: longint;
  51. begin
  52. do_filepos := 0;
  53. res := fs_posre(handle, 0);
  54. if res < 0 then
  55. Error2InOutRes(res)
  56. else
  57. do_filepos := res;
  58. end;
  59. procedure do_seek(handle, pos: longint);
  60. var
  61. res: longint;
  62. begin
  63. res := fs_posab(handle, pos);
  64. if res < 0 then
  65. Error2InOutRes(res);
  66. end;
  67. function do_seekend(handle: longint): longint;
  68. begin
  69. do_seek(handle, -1);
  70. do_seekend := do_filepos(handle);
  71. end;
  72. function do_filesize(handle: longint): longint;
  73. var
  74. res: longint;
  75. header: array [0..$39] of byte;
  76. begin
  77. do_filesize := 0;
  78. res := fs_headr(handle, @header, $40);
  79. if res < 0 then
  80. Error2InOutRes(res)
  81. else
  82. do_filesize := plongint(@header[0])^;
  83. end;
  84. { truncate at a given position }
  85. procedure do_truncate(handle, pos: longint);
  86. begin
  87. do_seek(handle, pos);
  88. fs_truncate(handle);
  89. end;
  90. procedure do_open(var f;p:pchar;flags:longint; pchangeable: boolean);
  91. {
  92. filerec and textrec have both handle and mode as the first items so
  93. they could use the same routine for opening/creating.
  94. when (flags and $100) the file will be append
  95. when (flags and $1000) the file will be truncate/rewritten
  96. when (flags and $10000) there is no check for close (needed for textfiles)
  97. }
  98. var
  99. res: longint;
  100. openMode: longint;
  101. begin
  102. openMode:=Q_OPEN;
  103. { close first if opened }
  104. if ((flags and $10000)=0) then
  105. begin
  106. case filerec(f).mode of
  107. fmInput, fmOutput, fmInout:
  108. do_close(filerec(f).handle);
  109. fmClosed: ;
  110. else
  111. begin
  112. InOutRes:=102; {not assigned}
  113. exit;
  114. end;
  115. end;
  116. end;
  117. { reset file handle }
  118. filerec(f).handle:=UnusedHandle;
  119. { convert filemode to filerec modes }
  120. case (flags and 3) of
  121. 0 : filerec(f).mode:=fmInput;
  122. 1 : filerec(f).mode:=fmOutput;
  123. 2 : filerec(f).mode:=fmInout;
  124. end;
  125. { empty name is special }
  126. if p[0]=#0 then begin
  127. case filerec(f).mode of
  128. fminput :
  129. filerec(f).handle:=StdInputHandle;
  130. fmappend,
  131. fmoutput : begin
  132. filerec(f).handle:=StdOutputHandle;
  133. filerec(f).mode:=fmOutput; {fool fmappend}
  134. end;
  135. end;
  136. exit;
  137. end;
  138. { rewrite (create a new file) }
  139. { FIX ME: this will just create a new file, actual overwriting
  140. seems to be a more complex endeavor... }
  141. if (flags and $1000)<>0 then openMode:=Q_OPEN_NEW;
  142. res:=io_open(p,openMode);
  143. if res < 0 then
  144. begin
  145. Error2InOutRes(res);
  146. filerec(f).mode:=fmClosed;
  147. exit;
  148. end
  149. else
  150. filerec(f).handle:=res;
  151. { append mode }
  152. if ((Flags and $100)<>0) and
  153. (FileRec(F).Handle<>UnusedHandle) then begin
  154. do_seekend(filerec(f).handle);
  155. filerec(f).mode:=fmOutput; {fool fmappend}
  156. end;
  157. end;
  158. function do_isdevice(handle: thandle): boolean;
  159. begin
  160. do_isdevice:=false;
  161. end;