From d530b4fce736fca09581927f37e86c9eb27503c6 Mon Sep 17 00:00:00 2001 From: Legolas Date: Thu, 23 Jun 2011 21:15:19 +0000 Subject: [PATCH] * Some work on sysutils.pp about file I/O git-svn-id: trunk@17813 - --- rtl/nds/libch.inc | 84 +++++++++++++++++++++++++++++++++++++++++++-- rtl/nds/sysutils.pp | 60 ++++++++++++++++++++++---------- 2 files changed, 123 insertions(+), 21 deletions(-) diff --git a/rtl/nds/libch.inc b/rtl/nds/libch.inc index f519d2adc9..54327d8f94 100644 --- a/rtl/nds/libch.inc +++ b/rtl/nds/libch.inc @@ -2,7 +2,7 @@ This file is part of the Free Component Library (FCL) Copyright (c) 1999-2002 by the Free Pascal development team - BIOS functions unit for Nintendo DS + libc unit for Nintendo DS Copyright (c) 2006 by Francesco Lombardi See the file COPYING.FPC, included in this distribution, @@ -84,6 +84,17 @@ const SEEK_SET = 0; SEEK_CUR = 1; SEEK_END = 2; + R_OK = 1; + W_OK = 2; + X_OK = 4; + F_OK = 8; + L_SET = SEEK_SET; + L_INCR = SEEK_CUR; + L_XTND = SEEK_END; + EFF_ONLY_OK = 8; + STDIN_FILENO = 0; + STDOUT_FILENO = 1; + STDERR_FILENO = 2; (* ------------------------------------------------------------------------------ @@ -170,7 +181,6 @@ procedure rewinddir(dirp: PDIR); cdecl; external; procedure seekdir(dirp: PDIR; loc: longint); cdecl; external; function telldir(dirp: PDIR): longint; cdecl; external; - function diropen(const path: pchar): PDIR_ITER; cdecl; external; function dirreset(dirState: PDIR_ITER): longint; cdecl; external; function dirnext(dirState: PDIR_ITER; filename: pchar; filestat: Pstat): longint; cdecl; external; @@ -197,3 +207,73 @@ function _stat(__file:Pchar; var __buf:Tstat):longint; cdecl; external name 'sta function ftruncate(fildes: longint; len: longint): longint; cdecl; external; function unlink(path: Pchar): longint; cdecl; external; function rename(para1: Pchar; para2: Pchar): longint; cdecl; external; + +function FpOpen(path: Pchar; oflag: longint): longint; cdecl; external name 'open'; +function FpOpen(path: Pchar; oflag, mode: longint): longint; cdecl; external name 'open'; +function FpRead(fildes:longint; buf:pointer; nbytes:dword):longint;cdecl;external name 'read'; +function FpRead(fildes:longint; var buf; nbytes:dword):longint;cdecl;external name 'read'; +function FpWrite(fildes:longint; buf:pointer; nbytes:dword):longint;cdecl;external name 'write'; +function FpWrite(fildes:longint; var buf; nbytes:dword):longint;cdecl;external name 'write'; +function fplseek(fildes:longint; offset:longint; whence:longint):longint;cdecl;external name 'lseek'; +function FpClose(fildes:longint):longint;cdecl;external name 'close'; +function FpChsize(fildes:longint; size:dword):longint;cdecl;external name 'chsize'; +function FpUnlink(path:Pchar):longint;cdecl;external name 'unlink'; +function FpRename(para1: Pchar; para2: Pchar): longint; cdecl; external name 'rename'; +function Fpstat(path:Pchar; buf:Pstat):longint;cdecl;external name 'stat'; +function Fpstat(path:Pchar; var buf:Tstat):longint;cdecl;external name 'stat'; +function FpAccess(path:Pchar; mode:longint):longint;cdecl;external name 'access'; + + + +const + F_GETFL = 1; // get file status flags + F_SETFL = 2; // set file status flags + F_DUPFD = 3; // duplicate file descriptor + F_GETFD = 4; // get file descriptor flags + F_SETFD = 5; // set file descriptor flags + F_SETLK = 6; // set record locking info + F_SETLK64 = 16; // set record locking info (64-bit) + F_GETLK = 7; // get record locking info + F_GETLK64 = 17; // get record locking info (64-bit) + F_SETLKW = 8; // get record locking info; wait if blocked + F_SETLKW64 = 18; // get record locking info (64-bit) + F_CLOEXEC = 9; // close on execute + +// values for 'l_type' field of 'struct flock'... + F_RDLCK = 1; // shared or read lock + F_WRLCK = 2; // exclusive or write lock + F_UNLCK = 3; // unlock + +// values for 'oflag' in open()... + O_RDONLY =$00000000; // open for read only + O_WRONLY =$00000001; // open for write only + O_RDWR =$00000002; // open for read and write + O_ACCMODE =$00000003; // access flags mask + O_reserved1 =$00000004; // reserved + O_reserved2 =$00000008; // reserved + O_APPEND =$00000010; // writes done at end of file + O_CREAT =$00000020; // create new file + O_TRUNC =$00000040; // truncate existing file + O_EXCL =$00000080; // exclusive open + O_NOCTTY =$00000100; // no controlling terminal--unsupported + O_BINARY =$00000200; // binary file--all files + O_NDELAY =$00000400; // nonblocking flag + O_reserved3 =$00000800; // reserved + O_SYNC =$00001000; // synchronized I/O file integrity + O_DSYNC =$00002000; // synchronized I/O data integrity + O_RSYNC =$00004000; // synchronized read I/O + O_NONBLOCK = O_NDELAY; // alias + FD_CLOEXEC =$00008000; // parent closes after call to process() + O_UPDATE =$00010000; // keep legacy files updated + O_FIFO =$00100000; // opening one end of a FIFO [non-standard] + +// value for third argument when 'cmd' is F_SETFL in fcntl()... + FNDELAY = O_NDELAY; // fcntl() non-blocking I/O + +// 'shflag' values for sopen()... + SH_DENYRW = $00000010; // deny read/write mode + SH_DENYWR = $00000020; // deny write mode + SH_DENYRD = $00000030; // deny read mode + SH_DENYNO = $00000040; // deny none mode + + \ No newline at end of file diff --git a/rtl/nds/sysutils.pp b/rtl/nds/sysutils.pp index 9a37d6f998..58964aed75 100644 --- a/rtl/nds/sysutils.pp +++ b/rtl/nds/sysutils.pp @@ -32,12 +32,11 @@ interface { Include platform independent interface part } {$i sysutilh.inc} - implementation -uses - dos, sysconst; - +uses + sysconst; + { Include platform independent implementation part } {$i sysutils.inc} @@ -46,8 +45,17 @@ uses File Functions ****************************************************************************} function FileOpen(const FileName: string; Mode: Integer): LongInt; +var + NDSFlags: longint; begin - result := -1; + NDSFlags := 0; + + case (Mode and (fmOpenRead or fmOpenWrite or fmOpenReadWrite)) of + fmOpenRead : NDSFlags := NDSFlags or O_RdOnly; + fmOpenWrite : NDSFlags := NDSFlags or O_WrOnly; + fmOpenReadWrite : NDSFlags := NDSFlags or O_RdWr; + end; + FileOpen := fpopen(pchar(FileName), NDSFlags); end; @@ -65,65 +73,69 @@ end; function FileCreate(const FileName: string) : LongInt; begin - result := -1; + FileCreate:=fpopen(pointer(FileName), O_RdWr or O_Creat or O_Trunc); end; function FileCreate(const FileName: string; Rights: integer): LongInt; begin - result := -1; + FileCreate:=fpOpen(pointer(FileName),O_RdWr or O_Creat or O_Trunc,Rights); end; function FileCreate(const FileName: string; ShareMode : Integer; Rights: integer): LongInt; begin - result := -1; + result := FileCreate(FileName, Rights); end; function FileRead(Handle: LongInt; Out Buffer; Count: LongInt): LongInt; begin - result := -1; + FileRead := fpRead(Handle, Buffer, Count); end; function FileWrite(Handle: LongInt; const Buffer; Count: LongInt): LongInt; begin - result := -1; + FileWrite := fpWrite(Handle, @Buffer, Count); end; function FileSeek(Handle, FOffset, Origin: LongInt) : LongInt; begin - result := -1; + result := longint(FileSeek(Handle, int64(FOffset), Origin)); end; function FileSeek(Handle: LongInt; FOffset: Int64; Origin: Longint): Int64; begin - result := -1; + FileSeek := fplSeek(Handle, FOffset, Origin); end; procedure FileClose(Handle: LongInt); begin + fpclose(Handle); end; function FileTruncate(Handle: THandle; Size: Int64): Boolean; begin - result := false; + if Size > high (longint) then + FileTruncate := false + else + FileTruncate:=(fpchsize(Handle,Size) = 0); end; function DeleteFile(const FileName: string) : Boolean; begin - result := false; + Result := fpUnLink(pointer(FileName))>= 0; end; function RenameFile(const OldName, NewName: string): Boolean; begin - result := false; + RenameFile := FpRename(pointer(OldNAme), pointer(NewName)) >= 0; end; @@ -131,14 +143,19 @@ end; Function FileAge (Const FileName : String): Longint; +var + info: Stat; begin - result := -1; + if (fpstat(pointer(FileName), Info) < 0) or S_ISDIR(info.st_mode) then + exit(-1) + else + Result := (info.st_mtime); end; Function FileExists (Const FileName : String) : Boolean; -Begin - result := false; +begin + FileExists := fpAccess(pointer(filename), F_OK) = 0; end; @@ -156,11 +173,16 @@ end; Procedure FindClose (Var F : TSearchrec); begin + end; Function FileGetAttr (Const FileName : String) : Longint; +Var Info : TStat; begin - result := -1; + If Fpstat(pchar(FileName), Info) <> 0 then + Result := -1 + Else + Result := (Info.st_mode shr 16) and $ffff; end;