Browse Source

* Added a first file I/O handling implementation

git-svn-id: trunk@17780 -
Legolas 14 years ago
parent
commit
e3aa30f67c
6 changed files with 50 additions and 147 deletions
  1. 1 1
      rtl/nds/Makefile
  2. 1 1
      rtl/nds/libch.inc
  3. 0 5
      rtl/nds/nds.inc
  4. 0 77
      rtl/nds/ndsh.inc
  5. 4 8
      rtl/nds/sysdir.inc
  6. 44 55
      rtl/nds/sysfile.inc

+ 1 - 1
rtl/nds/Makefile

@@ -1,5 +1,5 @@
 #
-# Don't edit, this file is generated by FPCMake Version 2.0.0 [2010/09/26]
+# Don't edit, this file is generated by FPCMake Version 2.0.0 [2011/04/23]
 #
 default: all
 MAKEFILETARGETS=i386-linux i386-go32v2 i386-win32 i386-os2 i386-freebsd i386-beos i386-haiku i386-netbsd i386-solaris i386-qnx i386-netware i386-openbsd i386-wdosx i386-darwin i386-emx i386-watcom i386-netwlibc i386-wince i386-embedded i386-symbian m68k-linux m68k-freebsd m68k-netbsd m68k-amiga m68k-atari m68k-openbsd m68k-palmos m68k-embedded powerpc-linux powerpc-netbsd powerpc-amiga powerpc-macos powerpc-darwin powerpc-morphos powerpc-embedded sparc-linux sparc-netbsd sparc-solaris sparc-embedded x86_64-linux x86_64-freebsd x86_64-solaris x86_64-darwin x86_64-win64 x86_64-embedded arm-linux arm-palmos arm-darwin arm-wince arm-gba arm-nds arm-embedded arm-symbian powerpc64-linux powerpc64-darwin powerpc64-embedded avr-embedded armeb-linux armeb-embedded mipsel-linux

+ 1 - 1
rtl/nds/libch.inc

@@ -97,7 +97,7 @@ type
   end;
   PDIR_ITER = ^DIR_ITER;
 
-  stat = packed record
+  stat = record
     st_dev: longint;
     st_ino: longword;
     st_mode : longword;

+ 0 - 5
rtl/nds/nds.inc

@@ -30,8 +30,3 @@ function IsARM9(): boolean;
 begin
   IsARM9 := integer(@irq_vector) = $0B003FFC;
 end;
-
-procedure AssignDevice(const FIOD: TFileIODevice);
-begin
-  FileIODevice := FIOD;
-end;

+ 0 - 77
rtl/nds/ndsh.inc

@@ -14,81 +14,4 @@
 
  *****************************************************************************}
 
-type
-  TDoOpen     = procedure (var f; p: pchar; flags: longint);
-  TDoClose    = procedure (handle: THandle);
-  TDoWrite    = function  (h: THandle; addr: pointer; len: longint): longint;
-  TDoRead     = function  (h: THandle; addr: pointer; len: longint): longint;
-  TDoSeek     = procedure (handle: THandle; pos: longint);
-  TDoSeekend  = function  (handle: THandle): longint;
-  TDoErase    = procedure (p: pchar);
-  TDoRename   = procedure (p1, p2: pchar);
-  TDoFilepos  = function  (handle: THandle): longint;
-  TDoFilesize = function  (handle: THandle): longint;
-  TDoTruncate = procedure (handle: THandle; pos: longint);
-  TDoIsdevice = function  (handle: THandle): boolean;
-
-  TFileIO = packed record
-    DoOpen    : TDoOpen; 
-    DoClose   : TDoClose;
-    DoWrite   : TDoWrite;
-    DoRead    : TDoRead;
-    DoSeek    : TDoSeek;
-    DoSeekend : TDoSeekend;
-    DoErase   : TDoErase;
-    DoRename  : TDoRename;
-    DoFilepos : TDoFilepos;
-    DoFilesize: TDoFilesize;
-    DoTruncate: TDoTruncate;
-    DoIsdevice: TDoIsdevice;
-  end;
-  PFileIO = ^TFileIO;
-
-  TDoMkdir  = procedure (const s: string);
-  TDoRmdir  = procedure (const s: string);
-  TDoChdir  = procedure (const s: string);
-  TDoGetdir = procedure (DriveNr: byte; var Dir: ShortString);
-
-  TDirIO = packed record
-    DoMkdir : TDoMkdir;
-    DoRmdir : TDoRmdir;
-    DoChdir : TDoChdir;
-    DoGetdir: TDoGetdir;
-  end;
-  PDirIO = ^TDirIO;
-
-  TFileIODevice = packed record
-    FileIO: TFileIO;
-    DirIO: TDirIO;
-  end;
-  PFileIODevice = ^TFileIODevice;
-
-
 function IsARM9(): boolean;
-
-
-procedure AssignDevice(const FIOD: TFileIODevice);
-
-var
-  FileIODevice: TFileIODevice = (
-    FileIO: ( 
-              DoOpen: nil;
-              DoClose: nil;
-              DoWrite: nil;
-              DoRead: nil;
-              DoSeek: nil;
-              DoSeekend: nil;
-              DoErase: nil;
-              DoRename: nil;
-              DoFilepos: nil;
-              DoFilesize: nil;
-              DoTruncate: nil;
-              DoIsdevice: nil;
-            );
-    DirIO:  ( 
-              DoMkdir:  nil;
-              DoRmdir:  nil;
-              DoChdir:  nil;
-              DoGetdir: nil;
-            );
-  );

+ 4 - 8
rtl/nds/sysdir.inc

@@ -22,28 +22,24 @@
 procedure mkdir(s: pchar; len: sizeuint);[IOCheck, public, alias : 'FPC_SYS_MKDIR'];
 begin
   if not assigned(s) or (len=0) or (InOutRes<>0) then exit;
-  if FileIODevice.DirIO.DoMkdir <> nil then
-    FileIODevice.DirIO.DoMkdir(strpas(s));
+
 end;
 
 procedure rmdir(s: pchar; len: sizeuint);[IOCheck, public, alias : 'FPC_SYS_RMDIR'];
 begin
   if not assigned(s) or (len=0) then exit;
-  if FileIODevice.DirIO.DoRmdir <> nil then
-    FileIODevice.DirIO.DoRmdir(strpas(s));
+
 end;
 
 procedure chdir(s: pchar; len: sizeuint);[IOCheck, public, alias : 'FPC_SYS_CHDIR'];
 begin
   if not assigned(s) or (len=0) then exit;
-  if FileIODevice.DirIO.DoChdir <> nil then
-    FileIODevice.DirIO.DoChdir(strpas(s));
+
 end;
 
 procedure GetDir(DriveNr: byte; var Dir: ShortString);
 begin
-  if FileIODevice.DirIO.DoGetdir <> nil then
-    FileIODevice.DirIO.DoGetdir(DriveNr, Dir);
+
 end;
 
 

+ 44 - 55
rtl/nds/sysfile.inc

@@ -25,78 +25,66 @@
 { close a file from the handle value }
 procedure do_close(handle: THandle);
 begin
-  if FileIODevice.FileIO.DoClose <> nil then
-    FileIODevice.FileIO.DoClose(handle);
-  //_fclose (_PFILE(pointer(handle))^);
+  fclose(P_FILE(Handle));
 end;
 
 procedure do_erase(p: pchar);
 begin
-  if FileIODevice.FileIO.DoErase <> nil then
-    FileIODevice.FileIO.DoErase(p);
-//  _unlink(p);
+  unlink(p);
 end;
 
 procedure do_rename(p1, p2: pchar);
 begin
-//  _rename(p1, p2);
-  if FileIODevice.FileIO.DoRename <> nil then
-    FileIODevice.FileIO.DoRename(p1, p2);
+  rename(p1, p2);
 end;
 
 function do_write(h: THandle; addr: pointer; len: longint) : longint;
 begin
-//  result := _fwrite(addr, 1, len, _PFILE(pointer(h))^);
-  if FileIODevice.FileIO.DoWrite <> nil then
-    result :=  FileIODevice.FileIO.DoWrite(h, addr, len);
+  result := fwrite(addr, 1, len, P_FILE(h));
 end;
 
 function do_read(h: THandle; addr: pointer; len: longint) : longint;
 begin
-//  result := _fread(addr, 1, len, _PFILE(pointer(h))^);
-  if FileIODevice.FileIO.DoRead <> nil then
-    result := FileIODevice.FileIO.DoRead(h, addr, len);
+  result := fread(addr, 1, len, P_FILE(h));
 end;
 
 function do_filepos(handle: THandle): longint;
 begin
-//  result := _ftell(_PFILE(pointer(handle))^);
-  if FileIODevice.FileIO.DoFilePos <> nil then
-    result := FileIODevice.FileIO.DoFilePos(handle);
+  result := ftell(P_FILE(handle));
 end;
 
 procedure do_seek(handle: THandle; pos: longint);
 begin
-  //_fseek(_PFILE(pointer(handle))^, pos, SEEK_SET);
-  if FileIODevice.FileIO.DoSeek <> nil then
-    FileIODevice.FileIO.DoSeek(handle, pos);
+  fseek(P_FILE(handle), pos, SEEK_SET);
 end;
 
 function do_seekend(handle: THandle): longint;
 begin
-//  result := _fseek(_PFILE(pointer(handle))^, 0, SEEK_END);
-  if FileIODevice.FileIO.DoSeekend <> nil then
-    result := FileIODevice.FileIO.DoSeekend(handle);
+  result := fseek(P_FILE(handle), 0, SEEK_END);
 end;
 
 function do_filesize(handle: THandle): longint;
+var
+  res     : LONGINT;
+  statbuf : TStat;
 begin
-//  result := -1;
-  if FileIODevice.FileIO.DoFilesize <> nil then
-    result := FileIODevice.FileIO.DoFilesize(handle);
+  res := fstat(fileno(P_FILE(handle)), statbuf);
+  if res = 0 then
+    result := statbuf.st_size
+  else
+    result := -1;
 end;
 
 { truncate at a given position }
 procedure do_truncate(handle: THandle; pos: longint);
 begin
-//  _ftruncate(_fileno(_PFILE(pointer(handle))^), pos);
-  if FileIODevice.FileIO.DoTruncate <> nil then
-    FileIODevice.FileIO.DoTruncate(handle, pos);
+  ftruncate(fileno(P_FILE(handle)), pos);
 end;
 
 procedure do_open(var f; p: pchar; flags: longint);
+var
+  oflags : string[10];
 begin
-(*
 { close first if opened }
   if ((flags and $10000) = 0) then
    begin
@@ -105,7 +93,7 @@ begin
       fmclosed : ;
      else
       begin
-//        inoutres:=102; {not assigned}
+        inoutres:=102; {not assigned}
         exit;
       end;
      end;
@@ -137,43 +125,44 @@ begin
   else
    if (flags and $100)=$100 then
     oflags:=oflags or (O_APPEND);}
-
 { empty name is special }
   if p[0]=#0 then
-  begin
-    case FileRec(f).mode of
-      fminput: FileRec(f).Handle:=StdInputHandle;
-      fminout, { this is set by rewrite }
-      fmoutput: FileRec(f).Handle:=StdOutputHandle;
-      fmappend:
-      begin
-        FileRec(f).Handle:=StdOutputHandle;
-        FileRec(f).mode:=fmoutput; {fool fmappend}
-      end;
-    end;
-    exit;
-  end;
-
+   begin
+     case FileRec(f).mode of
+       fminput :
+         FileRec(f).Handle:=StdInputHandle;
+       fminout, { this is set by rewrite }
+       fmoutput :
+         FileRec(f).Handle:=StdOutputHandle;
+       fmappend :
+         begin
+           FileRec(f).Handle:=StdOutputHandle;
+           FileRec(f).mode:=fmoutput; {fool fmappend}
+         end;
+     end;
+     exit;
+   end;
 { real open call }
-  FileRec(f).Handle := longint(fopen(p, @oflags[1]));//_open(p,oflags,438);
+  FileRec(f).Handle := THandle(fopen(p, @oflags[1]));//_open(p,oflags,438);
+  //WriteLn ('_open (',p,') returned ',ErrNo, 'Handle: ',FileRec(f).Handle);
   // errno does not seem to be set on succsess ??
+  {IF FileRec(f).Handle < 0 THEN
+    if (ErrNo=Sys_EROFS) and ((OFlags and O_RDWR)<>0) then
+    begin  // i.e. for cd-rom
+      Oflags:=Oflags and not(O_RDWR);
+      FileRec(f).Handle := _open(p,oflags,438);
+    end;}
 {
   if FileRec(f).Handle = 0 then
     Errno2Inoutres
   else
     InOutRes := 0;
 }
-*)
-//  FileRec(f).Handle := THandle (_fopen(p, @oflags[1]));
-  if FileIODevice.FileIO.DoOpen <> nil then
-    FileIODevice.FileIO.DoOpen(f, p, flags);
 end;
 
 function do_isdevice(handle: THandle): boolean;
 begin
-//  result :=  (_isatty(_fileno(_PFILE(pointer(handle))^)) > 0);
-  if FileIODevice.FileIO.DoIsdevice <> nil then
-    result := FileIODevice.FileIO.DoIsdevice(handle);
+  result :=  (isatty(fileno(P_FILE(handle))) > 0);
 end;