sysfile.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2005 by Free Pascal development team
  4. Low level file functions for Nintendo Wii
  5. Copyright (c) 2011 by Francesco Lombardi
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {****************************************************************************
  13. Low level File Routines
  14. All these functions can set InOutRes on errors
  15. ****************************************************************************}
  16. { close a file from the handle value }
  17. procedure do_close(handle: THandle);
  18. begin
  19. if FileIODevice.FileIO.DoClose <> nil then
  20. FileIODevice.FileIO.DoClose(handle);
  21. //_fclose (_PFILE(pointer(handle))^);
  22. end;
  23. procedure do_erase(p: pchar; pchangeable: boolean);
  24. begin
  25. if FileIODevice.FileIO.DoErase <> nil then
  26. FileIODevice.FileIO.DoErase(p);
  27. // _unlink(p);
  28. end;
  29. procedure do_rename(p1, p2: pchar; p1changeable, p2changeable: boolean);
  30. begin
  31. // _rename(p1, p2);
  32. if FileIODevice.FileIO.DoRename <> nil then
  33. FileIODevice.FileIO.DoRename(p1, p2);
  34. end;
  35. function do_write(h: THandle; addr: pointer; len: longint) : longint;
  36. begin
  37. // result := _fwrite(addr, 1, len, _PFILE(pointer(h))^);
  38. if FileIODevice.FileIO.DoWrite <> nil then
  39. result := FileIODevice.FileIO.DoWrite(h, addr, len);
  40. end;
  41. function do_read(h: THandle; addr: pointer; len: longint) : longint;
  42. begin
  43. // result := _fread(addr, 1, len, _PFILE(pointer(h))^);
  44. if FileIODevice.FileIO.DoRead <> nil then
  45. result := FileIODevice.FileIO.DoRead(h, addr, len);
  46. end;
  47. function do_filepos(handle: THandle): longint;
  48. begin
  49. // result := _ftell(_PFILE(pointer(handle))^);
  50. if FileIODevice.FileIO.DoFilePos <> nil then
  51. result := FileIODevice.FileIO.DoFilePos(handle);
  52. end;
  53. procedure do_seek(handle: THandle; pos: longint);
  54. begin
  55. //_fseek(_PFILE(pointer(handle))^, pos, SEEK_SET);
  56. if FileIODevice.FileIO.DoSeek <> nil then
  57. FileIODevice.FileIO.DoSeek(handle, pos);
  58. end;
  59. function do_seekend(handle: THandle): longint;
  60. begin
  61. // result := _fseek(_PFILE(pointer(handle))^, 0, SEEK_END);
  62. if FileIODevice.FileIO.DoSeekend <> nil then
  63. result := FileIODevice.FileIO.DoSeekend(handle);
  64. end;
  65. function do_filesize(handle: THandle): longint;
  66. begin
  67. // result := -1;
  68. if FileIODevice.FileIO.DoFilesize <> nil then
  69. result := FileIODevice.FileIO.DoFilesize(handle);
  70. end;
  71. { truncate at a given position }
  72. procedure do_truncate(handle: THandle; pos: longint);
  73. begin
  74. // _ftruncate(_fileno(_PFILE(pointer(handle))^), pos);
  75. if FileIODevice.FileIO.DoTruncate <> nil then
  76. FileIODevice.FileIO.DoTruncate(handle, pos);
  77. end;
  78. procedure do_open(var f; p: pchar; flags: longint; pchangeable: boolean);
  79. begin
  80. (*
  81. { close first if opened }
  82. if ((flags and $10000) = 0) then
  83. begin
  84. case FileRec(f).mode of
  85. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  86. fmclosed : ;
  87. else
  88. begin
  89. // inoutres:=102; {not assigned}
  90. exit;
  91. end;
  92. end;
  93. end;
  94. { reset file Handle }
  95. FileRec(f).Handle:=UnusedHandle;
  96. { We do the conversion of filemodes here, concentrated on 1 place }
  97. case (flags and 3) of
  98. 0 : begin
  99. oflags := 'rb'#0;
  100. filerec(f).mode := fminput;
  101. end;
  102. 1 : begin
  103. if (flags and $1000)=$1000 then
  104. oflags := 'w+b' else
  105. oflags := 'wb';
  106. filerec(f).mode := fmoutput;
  107. end;
  108. 2 : begin
  109. if (flags and $1000)=$1000 then
  110. oflags := 'w+' else
  111. oflags := 'r+';
  112. filerec(f).mode := fminout;
  113. end;
  114. end;
  115. {if (flags and $1000)=$1000 then
  116. oflags:=oflags or (O_CREAT or O_TRUNC)
  117. else
  118. if (flags and $100)=$100 then
  119. oflags:=oflags or (O_APPEND);}
  120. { empty name is special }
  121. if p[0]=#0 then
  122. begin
  123. case FileRec(f).mode of
  124. fminput: FileRec(f).Handle:=StdInputHandle;
  125. fminout, { this is set by rewrite }
  126. fmoutput: FileRec(f).Handle:=StdOutputHandle;
  127. fmappend:
  128. begin
  129. FileRec(f).Handle:=StdOutputHandle;
  130. FileRec(f).mode:=fmoutput; {fool fmappend}
  131. end;
  132. end;
  133. exit;
  134. end;
  135. { real open call }
  136. FileRec(f).Handle := longint(fopen(p, @oflags[1]));//_open(p,oflags,438);
  137. // errno does not seem to be set on succsess ??
  138. {
  139. if FileRec(f).Handle = 0 then
  140. Errno2Inoutres
  141. else
  142. InOutRes := 0;
  143. }
  144. *)
  145. // FileRec(f).Handle := THandle (_fopen(p, @oflags[1]));
  146. if FileIODevice.FileIO.DoOpen <> nil then
  147. FileIODevice.FileIO.DoOpen(f, p, flags);
  148. end;
  149. function do_isdevice(handle: THandle): boolean;
  150. begin
  151. // result := (_isatty(_fileno(_PFILE(pointer(handle))^)) > 0);
  152. if FileIODevice.FileIO.DoIsdevice <> nil then
  153. result := FileIODevice.FileIO.DoIsdevice(handle);
  154. end;