瀏覽代碼

+ rawbytestring/unicodestring overloads for FileCreate and FileOpen.
The WinCE implementation converts the rawbytestring arguments to
unicodestring and calls unicode OS APIs, while the others convert
unicodestring arguments to DefaultFileSystemCodePage and call
single byte OS APIs
+ test for the above

git-svn-id: branches/cpstrrtl@22467 -

Jonas Maebe 13 年之前
父節點
當前提交
a3c936fe5f

+ 2 - 0
.gitattributes

@@ -8294,6 +8294,7 @@ rtl/objpas/sysconst.pp svneol=native#text/plain
 rtl/objpas/sysutils/dati.inc svneol=native#text/plain
 rtl/objpas/sysutils/dati.inc svneol=native#text/plain
 rtl/objpas/sysutils/datih.inc svneol=native#text/plain
 rtl/objpas/sysutils/datih.inc svneol=native#text/plain
 rtl/objpas/sysutils/diskh.inc svneol=native#text/plain
 rtl/objpas/sysutils/diskh.inc svneol=native#text/plain
+rtl/objpas/sysutils/filutil.inc svneol=native#text/plain
 rtl/objpas/sysutils/filutilh.inc svneol=native#text/plain
 rtl/objpas/sysutils/filutilh.inc svneol=native#text/plain
 rtl/objpas/sysutils/fina.inc svneol=native#text/plain
 rtl/objpas/sysutils/fina.inc svneol=native#text/plain
 rtl/objpas/sysutils/finah.inc svneol=native#text/plain
 rtl/objpas/sysutils/finah.inc svneol=native#text/plain
@@ -11636,6 +11637,7 @@ tests/test/units/sysutils/tlocale.pp svneol=native#text/plain
 tests/test/units/sysutils/trwsync.pp svneol=native#text/plain
 tests/test/units/sysutils/trwsync.pp svneol=native#text/plain
 tests/test/units/sysutils/tsscanf.pp svneol=native#text/plain
 tests/test/units/sysutils/tsscanf.pp svneol=native#text/plain
 tests/test/units/sysutils/tstrtobool.pp svneol=native#text/plain
 tests/test/units/sysutils/tstrtobool.pp svneol=native#text/plain
+tests/test/units/sysutils/tunifile.pp svneol=native#text/plain
 tests/test/units/variants/tcustomvariant.pp svneol=native#text/plain
 tests/test/units/variants/tcustomvariant.pp svneol=native#text/plain
 tests/test/units/variants/tvararrayofintf.pp svneol=native#text/plain
 tests/test/units/variants/tvararrayofintf.pp svneol=native#text/plain
 tests/test/uobjc24.pp svneol=native#text/plain
 tests/test/uobjc24.pp svneol=native#text/plain

+ 14 - 5
rtl/amiga/sysutils.pp

@@ -41,6 +41,9 @@ uses dos,sysconst;
 {$DEFINE FPC_FEXPAND_DRIVESEP_IS_ROOT}
 {$DEFINE FPC_FEXPAND_DRIVESEP_IS_ROOT}
 {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
 {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -104,13 +107,16 @@ end;
 
 
 (****** non portable routines ******)
 (****** non portable routines ******)
 
 
-function FileOpen(const FileName: string; Mode: Integer): LongInt;
+function FileOpen(const FileName: rawbytestring; Mode: Integer): LongInt;
 var
 var
+  SystemFileName: RawByteString;
   dosResult: LongInt;
   dosResult: LongInt;
   tmpStr   : array[0..255] of char;
   tmpStr   : array[0..255] of char;
 begin
 begin
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
   {$WARNING FIX ME! To do: FileOpen Access Modes}
   {$WARNING FIX ME! To do: FileOpen Access Modes}
-  tmpStr:=PathConv(FileName)+#0;
+  {$WARNING FIX ME! PathConv takes a shortstring, which means 255 char truncation and conversion to defaultsystemcodepage, ignoring the defaultfilesystemcodepage setting}
+  tmpStr:=PathConv(SystemFileName)+#0;
   dosResult:=Open(@tmpStr,MODE_OLDFILE);
   dosResult:=Open(@tmpStr,MODE_OLDFILE);
   if dosResult=0 then
   if dosResult=0 then
     dosResult:=-1
     dosResult:=-1
@@ -134,11 +140,14 @@ begin
 end;
 end;
 
 
 
 
-function FileCreate(const FileName: string) : LongInt;
+function FileCreate(const FileName: RawByteString) : LongInt;
 var
 var
+  SystemFileName: RawByteString;
   dosResult: LongInt;
   dosResult: LongInt;
   tmpStr   : array[0..255] of char;
   tmpStr   : array[0..255] of char;
 begin
 begin
+ SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
+ {$WARNING FIX ME! PathConv takes a shortstring, which means 255 char truncation and conversion to defaultsystemcodepage, ignoring the defaultfilesystemcodepage setting}
  tmpStr:=PathConv(FileName)+#0;
  tmpStr:=PathConv(FileName)+#0;
  dosResult:=Open(@tmpStr,MODE_NEWFILE);
  dosResult:=Open(@tmpStr,MODE_NEWFILE);
  if dosResult=0 then
  if dosResult=0 then
@@ -150,13 +159,13 @@ begin
 end;
 end;
 
 
 
 
-function FileCreate(const FileName: string; Rights: integer): LongInt;
+function FileCreate(const FileName: RawByteString; Rights: integer): LongInt;
 begin
 begin
   {$WARNING FIX ME! To do: FileCreate Access Modes}
   {$WARNING FIX ME! To do: FileCreate Access Modes}
   FileCreate:=FileCreate(FileName);
   FileCreate:=FileCreate(FileName);
 end;
 end;
 
 
-function FileCreate(const FileName: string; ShareMode: integer; Rights : Integer): LongInt;
+function FileCreate(const FileName: RawByteString; ShareMode: integer; Rights : Integer): LongInt;
 begin
 begin
   {$WARNING FIX ME! To do: FileCreate Access Modes}
   {$WARNING FIX ME! To do: FileCreate Access Modes}
   FileCreate:=FileCreate(FileName);
   FileCreate:=FileCreate(FileName);

+ 21 - 4
rtl/emx/sysutils.pp

@@ -38,6 +38,9 @@ implementation
 {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
 {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
 {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
 {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -452,7 +455,7 @@ const
                              specification for DosFindFirst call.}
                              specification for DosFindFirst call.}
 
 
 {$ASMMODE INTEL}
 {$ASMMODE INTEL}
-function FileOpen (const FileName: string; Mode: integer): longint; assembler;
+function FileOpen (const FileName: pointer; Mode: integer): longint; assembler;
 asm
 asm
  push ebx
  push ebx
 {$IFDEF REGCALL}
 {$IFDEF REGCALL}
@@ -477,21 +480,28 @@ asm
  pop ebx
  pop ebx
 end {['eax', 'ebx', 'ecx', 'edx']};
 end {['eax', 'ebx', 'ecx', 'edx']};
 
 
+function FileOpen (const FileName: rawbytestring; Mode: integer): longint;
+var
+  SystemFileName: RawByteString;
+begin
+  SystemFileName := ToSingleByteFileSystemEncodedFileName(FileName);
+  FileOpen := FileOpen(pointer(SystemFileName),Mode);
+end;
 
 
-function FileCreate (const FileName: string): longint;
+function FileCreate (const FileName: RawByteString): longint;
 begin
 begin
   FileCreate := FileCreate (FileName, ofReadWrite or faCreate or doDenyRW, 777);
   FileCreate := FileCreate (FileName, ofReadWrite or faCreate or doDenyRW, 777);
                                                        (* Sharing to DenyAll *)
                                                        (* Sharing to DenyAll *)
 end;
 end;
 
 
 
 
-function FileCreate (const FileName: string; Rights: integer): longint;
+function FileCreate (const FileName: RawByteString; Rights: integer): longint;
 begin
 begin
   FileCreate := FileCreate (FileName, ofReadWrite or faCreate or doDenyRW,
   FileCreate := FileCreate (FileName, ofReadWrite or faCreate or doDenyRW,
                                               Rights); (* Sharing to DenyAll *)
                                               Rights); (* Sharing to DenyAll *)
 end;
 end;
 
 
-function FileCreate (const FileName: string; ShareMode: integer; Rights: integer): longint; assembler;
+function FileCreate (const FileName: Pointer; ShareMode: integer; Rights: integer): longint; assembler;
 asm
 asm
  push ebx
  push ebx
 {$IFDEF REGCALL}
 {$IFDEF REGCALL}
@@ -515,6 +525,13 @@ asm
  pop ebx
  pop ebx
 end {['eax', 'ebx', 'ecx', 'edx']};
 end {['eax', 'ebx', 'ecx', 'edx']};
 
 
+function FileCreate (const FileName: RawByteString; ShareMode: integer; Rights: integer): longint;
+var
+  SystemFileName: RawByteString;
+begin
+  SystemFileName := ToSingleByteFileSystemEncodedFileName(FileName);
+  FileOpen := FileCreate(pointer(SystemFileName),ShareMode,Rights);
+end;
 
 
 function FileRead (Handle: longint; Out Buffer; Count: longint): longint;
 function FileRead (Handle: longint; Out Buffer; Count: longint): longint;
                                                                      assembler;
                                                                      assembler;

+ 7 - 4
rtl/gba/sysutils.pp

@@ -41,6 +41,9 @@ implementation
 uses
 uses
   dos, sysconst;
   dos, sysconst;
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -48,7 +51,7 @@ uses
 {****************************************************************************
 {****************************************************************************
                               File Functions
                               File Functions
 ****************************************************************************}
 ****************************************************************************}
-function FileOpen(const FileName: string; Mode: Integer): LongInt;
+function FileOpen(const FileName: rawbytestring; Mode: Integer): LongInt;
 begin
 begin
   result := -1;
   result := -1;
 end;
 end;
@@ -66,19 +69,19 @@ begin
 end;
 end;
 
 
 
 
-function FileCreate(const FileName: string) : LongInt;
+function FileCreate(const FileName: RawByteString) : LongInt;
 begin
 begin
   result := -1;
   result := -1;
 end;
 end;
 
 
 
 
-function FileCreate(const FileName: string; Rights: integer): LongInt;
+function FileCreate(const FileName: RawByteString; Rights: integer): LongInt;
 begin
 begin
   result := -1;
   result := -1;
 end;
 end;
 
 
 
 
-function FileCreate(const FileName: string; ShareMode: integer; rights : integer): LongInt;
+function FileCreate(const FileName: RawByteString; ShareMode: integer; rights : integer): LongInt;
 begin
 begin
   result := -1;
   result := -1;
 end;
 end;

+ 28 - 8
rtl/go32v2/sysutils.pp

@@ -40,6 +40,9 @@ implementation
 {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
 {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
 {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
 {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -67,7 +70,7 @@ Type
 procedure StringToTB(const S: string);
 procedure StringToTB(const S: string);
 var
 var
   P: pchar;
   P: pchar;
-  Len: integer;
+  Len: longint;
 begin
 begin
   Len := Length(S) + 1;
   Len := Length(S) + 1;
   P := StrPCopy(StrAlloc(Len), S);
   P := StrPCopy(StrAlloc(Len), S);
@@ -75,10 +78,23 @@ begin
   StrDispose(P);
   StrDispose(P);
 end ;
 end ;
 
 
+procedure StringToTB(const S: rawbytestring);
+var
+  P: pchar;
+  Len: longint;
+begin
+  Len := Length(S) + 1;
+  if Len > tb_size then
+    Len := tb_size;
+  P := StrPCopy(StrAlloc(Len), S);
+  SysCopyToDos(longint(P), Len);
+  StrDispose(P);
+end ;
+
 
 
 {  Native OpenFile function.
 {  Native OpenFile function.
    if return value <> 0 call failed.  }
    if return value <> 0 call failed.  }
-function OpenFile(const FileName: string; var Handle: longint; Mode, Action: word): longint;
+function OpenFile(const FileName: rawbytestring; var Handle: longint; Mode, Action: word): longint;
 var
 var
    Regs: registers;
    Regs: registers;
 begin
 begin
@@ -110,33 +126,37 @@ begin
 end;
 end;
 
 
 
 
-Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
+Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : Longint;
 var
 var
+  SystemFileName: RawByteString;
   e: integer;
   e: integer;
-Begin
-  e := OpenFile(FileName, result, Mode, faOpen);
+begin
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
+  e := OpenFile(SystemFileName, result, Mode, faOpen);
   if e <> 0 then
   if e <> 0 then
     result := -1;
     result := -1;
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String) : Longint;
+Function FileCreate (Const FileName : RawByteString) : Longint;
 var
 var
+  SystemFileName: RawByteString;
   e: integer;
   e: integer;
 begin
 begin
+  SystemFileName := ToSingleByteFileSystemEncodedFileName(FileName);
   e := OpenFile(FileName, result, ofReadWrite, faCreate or faOpenReplace);
   e := OpenFile(FileName, result, ofReadWrite, faCreate or faOpenReplace);
   if e <> 0 then
   if e <> 0 then
     result := -1;
     result := -1;
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String; ShareMode:longint; Rights : longint) : Longint;
+Function FileCreate (Const FileName : RawByteString; ShareMode:longint; Rights : longint) : Longint;
 begin
 begin
   FileCreate:=FileCreate(FileName);
   FileCreate:=FileCreate(FileName);
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String; Rights:longint) : Longint;
+Function FileCreate (Const FileName : RawByteString; Rights:longint) : Longint;
 begin
 begin
   FileCreate:=FileCreate(FileName);
   FileCreate:=FileCreate(FileName);
 end;
 end;

+ 10 - 6
rtl/macos/sysutils.pp

@@ -58,6 +58,9 @@ uses
 {$DEFINE FPC_FEXPAND_NO_DOTS_UPDIR}
 {$DEFINE FPC_FEXPAND_NO_DOTS_UPDIR}
 {$DEFINE FPC_FEXPAND_NO_CURDIR}
 {$DEFINE FPC_FEXPAND_NO_CURDIR}
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -66,12 +69,13 @@ uses
                               File Functions
                               File Functions
 ****************************************************************************}
 ****************************************************************************}
 
 
-Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
+Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : Longint;
 
 
 Var LinuxFlags : longint;
 Var LinuxFlags : longint;
-
-BEGIN
+    SystemFileName: RawByteString;
+begin
   (* TODO fix
   (* TODO fix
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
   LinuxFlags:=0;
   LinuxFlags:=0;
   Case (Mode and 3) of
   Case (Mode and 3) of
     0 : LinuxFlags:=LinuxFlags or Open_RdOnly;
     0 : LinuxFlags:=LinuxFlags or Open_RdOnly;
@@ -84,7 +88,7 @@ BEGIN
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String) : Longint;
+Function FileCreate (Const FileName : RawByteString) : Longint;
 
 
 begin
 begin
   (* TODO fix
   (* TODO fix
@@ -93,7 +97,7 @@ begin
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String;Rights : Longint) : Longint;
+Function FileCreate (Const FileName : RawByteString;Rights : Longint) : Longint;
 
 
 Var LinuxFlags : longint;
 Var LinuxFlags : longint;
 
 
@@ -109,7 +113,7 @@ BEGIN
   *)
   *)
 end;
 end;
 
 
-Function FileCreate (Const FileName : String;ShareMode : Longint; Rights : Longint) : Longint;
+Function FileCreate (Const FileName : RawByteString;ShareMode : Longint; Rights : Longint) : Longint;
 
 
 Var LinuxFlags : longint;
 Var LinuxFlags : longint;
 
 

+ 15 - 6
rtl/morphos/sysutils.pp

@@ -42,6 +42,9 @@ uses dos,sysconst;
 {$DEFINE FPC_FEXPAND_DRIVESEP_IS_ROOT}
 {$DEFINE FPC_FEXPAND_DRIVESEP_IS_ROOT}
 {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
 {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -105,13 +108,16 @@ end;
 
 
 (****** non portable routines ******)
 (****** non portable routines ******)
 
 
-function FileOpen(const FileName: string; Mode: Integer): LongInt;
+function FileOpen(const FileName: rawbytestring; Mode: Integer): LongInt;
 var
 var
+  SystemFileName: RawByteString;
   dosResult: LongInt;
   dosResult: LongInt;
   tmpStr   : array[0..255] of char;
   tmpStr   : array[0..255] of char;
 begin
 begin
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
   {$WARNING FIX ME! To do: FileOpen Access Modes}
   {$WARNING FIX ME! To do: FileOpen Access Modes}
-  tmpStr:=PathConv(FileName)+#0;
+  {$WARNING FIX ME! PathConv takes a shortstring, which means 255 char truncation and conversion to defaultsystemcodepage, ignoring the defaultfilesystemcodepage setting}
+  tmpStr:=PathConv(SystemFileName)+#0;
   dosResult:=Open(@tmpStr,MODE_OLDFILE);
   dosResult:=Open(@tmpStr,MODE_OLDFILE);
   if dosResult=0 then
   if dosResult=0 then
     dosResult:=-1
     dosResult:=-1
@@ -135,12 +141,15 @@ begin
 end;
 end;
 
 
 
 
-function FileCreate(const FileName: string) : LongInt;
+function FileCreate(const FileName: RawByteString) : LongInt;
 var
 var
+  SystemFileName: RawByteString;
   dosResult: LongInt;
   dosResult: LongInt;
   tmpStr   : array[0..255] of char;
   tmpStr   : array[0..255] of char;
 begin
 begin
- tmpStr:=PathConv(FileName)+#0;
+ SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
+ {$WARNING FIX ME! PathConv takes a shortstring, which means 255 char truncation and conversion to defaultsystemcodepage, ignoring the defaultfilesystemcodepage setting}
+ tmpStr:=PathConv(SystemFileName)+#0;
  dosResult:=Open(@tmpStr,MODE_NEWFILE);
  dosResult:=Open(@tmpStr,MODE_NEWFILE);
  if dosResult=0 then
  if dosResult=0 then
    dosResult:=-1
    dosResult:=-1
@@ -151,13 +160,13 @@ begin
 end;
 end;
 
 
 
 
-function FileCreate(const FileName: string; Rights: integer): LongInt;
+function FileCreate(const FileName: RawByteString; Rights: integer): LongInt;
 begin
 begin
   {$WARNING FIX ME! To do: FileCreate Access Modes}
   {$WARNING FIX ME! To do: FileCreate Access Modes}
   FileCreate:=FileCreate(FileName);
   FileCreate:=FileCreate(FileName);
 end;
 end;
 
 
-function FileCreate(const FileName: string; ShareMode: integer; Rights : integer): LongInt;
+function FileCreate(const FileName: RawByteString; ShareMode: integer; Rights : integer): LongInt;
 begin
 begin
   {$WARNING FIX ME! To do: FileCreate Access Modes}
   {$WARNING FIX ME! To do: FileCreate Access Modes}
   FileCreate:=FileCreate(FileName);
   FileCreate:=FileCreate(FileName);

+ 2 - 2
rtl/nativent/ndkutils.pas

@@ -24,7 +24,7 @@ uses
 
 
 // Helpers for converting Pascal string types to NT's UNICODE_STRING
 // Helpers for converting Pascal string types to NT's UNICODE_STRING
 procedure ShortStrToNTStr(aStr: ShortString; var aNTStr: UNICODE_STRING);
 procedure ShortStrToNTStr(aStr: ShortString; var aNTStr: UNICODE_STRING);
-procedure AnsiStrToNTStr(const aStr: String; var aNTStr: UNICODE_STRING);
+procedure AnsiStrToNTStr(const aStr: RawByteString; var aNTStr: UNICODE_STRING);
 procedure UnicodeStrToNtStr(const aStr: UnicodeString;
 procedure UnicodeStrToNtStr(const aStr: UnicodeString;
     var aNTStr: UNICODE_STRING);
     var aNTStr: UNICODE_STRING);
 procedure PCharToNTStr(aStr: PChar; aLen: Cardinal; var aNTStr: UNICODE_STRING);
 procedure PCharToNTStr(aStr: PChar; aLen: Cardinal; var aNTStr: UNICODE_STRING);
@@ -53,7 +53,7 @@ begin
   aNTStr.MaximumLength := aNTStr.Length;
   aNTStr.MaximumLength := aNTStr.Length;
 end;
 end;
 
 
-procedure AnsiStrToNTStr(const aStr: String; var aNTStr: UNICODE_STRING);
+procedure AnsiStrToNTStr(const aStr: RawByteString; var aNTStr: UNICODE_STRING);
 var
 var
   buf: PWideChar;
   buf: PWideChar;
   i: Integer;
   i: Integer;

+ 9 - 6
rtl/nativent/sysutils.pp

@@ -49,6 +49,9 @@ implementation
 
 
 {$DEFINE FPC_NOGENERICANSIROUTINES}
 {$DEFINE FPC_NOGENERICANSIROUTINES}
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -56,7 +59,7 @@ implementation
                               File Functions
                               File Functions
 ****************************************************************************}
 ****************************************************************************}
 
 
-function FileOpen(const FileName : string; Mode : Integer) : THandle;
+function FileOpen(const FileName : rawbytestring; Mode : Integer) : THandle;
 const
 const
   AccessMode: array[0..2] of ACCESS_MASK  = (
   AccessMode: array[0..2] of ACCESS_MASK  = (
     GENERIC_READ,
     GENERIC_READ,
@@ -73,7 +76,7 @@ var
   objattr: OBJECT_ATTRIBUTES;
   objattr: OBJECT_ATTRIBUTES;
   iostatus: IO_STATUS_BLOCK;
   iostatus: IO_STATUS_BLOCK;
 begin
 begin
-  AnsiStrToNtStr(FileName, ntstr);
+  AnsiStrToNtStr(ToSingleByteFileSystemEncodedFileName(FileName), ntstr);
   InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
   InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
   NtCreateFile(@Result, AccessMode[Mode and 3] or NT_SYNCHRONIZE, @objattr,
   NtCreateFile(@Result, AccessMode[Mode and 3] or NT_SYNCHRONIZE, @objattr,
     @iostatus, Nil, FILE_ATTRIBUTE_NORMAL, ShareMode[(Mode and $F0) shr 4],
     @iostatus, Nil, FILE_ATTRIBUTE_NORMAL, ShareMode[(Mode and $F0) shr 4],
@@ -82,19 +85,19 @@ begin
 end;
 end;
 
 
 
 
-function FileCreate(const FileName : String) : THandle;
+function FileCreate(const FileName : RawByteString) : THandle;
 begin
 begin
   FileCreate := FileCreate(FileName, fmShareDenyNone, 0);
   FileCreate := FileCreate(FileName, fmShareDenyNone, 0);
 end;
 end;
 
 
 
 
-function FileCreate(const FileName : String; Rights: longint) : THandle;
+function FileCreate(const FileName : RawByteString; Rights: longint) : THandle;
 begin
 begin
   FileCreate := FileCreate(FileName, fmShareDenyNone, Rights);
   FileCreate := FileCreate(FileName, fmShareDenyNone, Rights);
 end;
 end;
 
 
 
 
-function FileCreate(const FileName : String; ShareMode : longint; Rights: longint) : THandle;
+function FileCreate(const FileName : RawByteString; ShareMode : longint; Rights: longint) : THandle;
 const
 const
   ShareModeFlags: array[0..4] of ULONG = (
   ShareModeFlags: array[0..4] of ULONG = (
                0,
                0,
@@ -108,7 +111,7 @@ var
   iostatus: IO_STATUS_BLOCK;
   iostatus: IO_STATUS_BLOCK;
   res: NTSTATUS;
   res: NTSTATUS;
 begin
 begin
-  AnsiStrToNTStr(FileName, ntstr);
+  AnsiStrToNTStr(ToSingleByteFileSystemEncodedFileName(FileName), ntstr);
   InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
   InitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
   NtCreateFile(@Result, GENERIC_READ or GENERIC_WRITE or NT_SYNCHRONIZE,
   NtCreateFile(@Result, GENERIC_READ or GENERIC_WRITE or NT_SYNCHRONIZE,
     @objattr, @iostatus, Nil, FILE_ATTRIBUTE_NORMAL,
     @objattr, @iostatus, Nil, FILE_ATTRIBUTE_NORMAL,

+ 18 - 7
rtl/nds/sysutils.pp

@@ -37,6 +37,9 @@ implementation
 uses
 uses
   sysconst;
   sysconst;
     
     
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -44,10 +47,12 @@ uses
 {****************************************************************************
 {****************************************************************************
                               File Functions
                               File Functions
 ****************************************************************************}
 ****************************************************************************}
-function FileOpen(const FileName: string; Mode: Integer): LongInt;
+function FileOpen(const FileName: rawbytestring; Mode: Integer): LongInt;
 var
 var
   NDSFlags: longint;
   NDSFlags: longint;
+  SystemFileName: RawByteString;
 begin
 begin
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
   NDSFlags := 0;
   NDSFlags := 0;
 
 
   case (Mode and (fmOpenRead or fmOpenWrite or fmOpenReadWrite)) of
   case (Mode and (fmOpenRead or fmOpenWrite or fmOpenReadWrite)) of
@@ -55,7 +60,7 @@ begin
     fmOpenWrite : NDSFlags := NDSFlags or O_WrOnly;
     fmOpenWrite : NDSFlags := NDSFlags or O_WrOnly;
     fmOpenReadWrite : NDSFlags := NDSFlags or O_RdWr;
     fmOpenReadWrite : NDSFlags := NDSFlags or O_RdWr;
   end;
   end;
-  FileOpen := _open(pchar(FileName), NDSFlags);
+  FileOpen := _open(pchar(SystemFileName), NDSFlags);
 end;
 end;
 
 
 
 
@@ -71,19 +76,25 @@ begin
 end;
 end;
 
 
 
 
-function FileCreate(const FileName: string) : LongInt;
+function FileCreate(const FileName: RawByteString) : LongInt;
+var
+  SystemFileName: RawByteString;
 begin
 begin
-  FileCreate:=_open(pointer(FileName), O_RdWr or O_Creat or O_Trunc);
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
+  FileCreate:=_open(pointer(SystemFileName), O_RdWr or O_Creat or O_Trunc);
 end;
 end;
 
 
 
 
-function FileCreate(const FileName: string; Rights: integer): LongInt;
+function FileCreate(const FileName: RawByteString; Rights: integer): LongInt;
+var
+  SystemFileName: RawByteString;
 begin
 begin
-  FileCreate:=_Open(pointer(FileName),O_RdWr or O_Creat or O_Trunc,Rights);
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
+  FileCreate:=_Open(pointer(SystemFileName),O_RdWr or O_Creat or O_Trunc,Rights);
 end;
 end;
 
 
 
 
-function FileCreate(const FileName: string; ShareMode : Integer; Rights: integer): LongInt;
+function FileCreate(const FileName: RawByteString; ShareMode : Integer; Rights: integer): LongInt;
 begin
 begin
   result := FileCreate(FileName, Rights);
   result := FileCreate(FileName, Rights);
 end;
 end;

+ 14 - 8
rtl/netware/sysutils.pp

@@ -77,6 +77,9 @@ implementation
 {$define FPC_FEXPAND_VOLUMES}
 {$define FPC_FEXPAND_VOLUMES}
 {$define FPC_FEXPAND_NO_DEFAULT_PATHS}
 {$define FPC_FEXPAND_NO_DEFAULT_PATHS}
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -85,35 +88,38 @@ implementation
                               File Functions
                               File Functions
 ****************************************************************************}
 ****************************************************************************}
 
 
-Function FileOpen (Const FileName : string; Mode : Integer) : THandle;
+Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : THandle;
 VAR NWOpenFlags : longint;
 VAR NWOpenFlags : longint;
-BEGIN
+    SystemFileName: RawByteString;
+begin
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
   NWOpenFlags:=0;
   NWOpenFlags:=0;
   Case (Mode and 3) of
   Case (Mode and 3) of
     0 : NWOpenFlags:=NWOpenFlags or O_RDONLY;
     0 : NWOpenFlags:=NWOpenFlags or O_RDONLY;
     1 : NWOpenFlags:=NWOpenFlags or O_WRONLY;
     1 : NWOpenFlags:=NWOpenFlags or O_WRONLY;
     2 : NWOpenFlags:=NWOpenFlags or O_RDWR;
     2 : NWOpenFlags:=NWOpenFlags or O_RDWR;
   end;
   end;
-  FileOpen := _open (pchar(FileName),NWOpenFlags,0);
+  FileOpen := _open (pchar(SystemFileName),NWOpenFlags,0);
 
 
   //!! We need to set locking based on Mode !!
   //!! We need to set locking based on Mode !!
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String) : THandle;
-
+Function FileCreate (Const FileName : RawByteString) : THandle;
+VAR SystemFileName: RawByteString;
 begin
 begin
-  FileCreate:=_open(Pchar(FileName),O_RdWr or O_Creat or O_Trunc,0);
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
+  FileCreate:=_open(Pchar(SystemFileName),O_RdWr or O_Creat or O_Trunc,0);
 end;
 end;
 
 
-Function FileCreate (Const FileName : String; Rights:longint) : THandle;
+Function FileCreate (Const FileName : RawByteString; Rights:longint) : THandle;
 
 
 begin
 begin
   FileCreate:=FileCreate (FileName);
   FileCreate:=FileCreate (FileName);
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String; ShareMode: Longint; Rights:longint) : THandle;
+Function FileCreate (Const FileName : RawByteString; ShareMode: Longint; Rights:longint) : THandle;
 
 
 begin
 begin
   FileCreate:=FileCreate (FileName);
   FileCreate:=FileCreate (FileName);

+ 14 - 7
rtl/netwlibc/sysutils.pp

@@ -79,6 +79,9 @@ implementation
 {$DEFINE FPC_FEXPAND_VOLUMES}
 {$DEFINE FPC_FEXPAND_VOLUMES}
 {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
 {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -87,35 +90,39 @@ implementation
                               File Functions
                               File Functions
 ****************************************************************************}
 ****************************************************************************}
 
 
-Function FileOpen (Const FileName : string; Mode : Integer) : THandle;
+Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : THandle;
 VAR NWOpenFlags : longint;
 VAR NWOpenFlags : longint;
-BEGIN
+    SystemFileName: RawByteString;
+begin
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
   NWOpenFlags:=0;
   NWOpenFlags:=0;
   Case (Mode and 3) of
   Case (Mode and 3) of
     0 : NWOpenFlags:=NWOpenFlags or O_RDONLY;
     0 : NWOpenFlags:=NWOpenFlags or O_RDONLY;
     1 : NWOpenFlags:=NWOpenFlags or O_WRONLY;
     1 : NWOpenFlags:=NWOpenFlags or O_WRONLY;
     2 : NWOpenFlags:=NWOpenFlags or O_RDWR;
     2 : NWOpenFlags:=NWOpenFlags or O_RDWR;
   end;
   end;
-  FileOpen := Fpopen (pchar(FileName),NWOpenFlags);
+  FileOpen := Fpopen (pchar(SystemFileName),NWOpenFlags);
 
 
   //!! We need to set locking based on Mode !!
   //!! We need to set locking based on Mode !!
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String) : THandle;
+Function FileCreate (Const FileName : RawByteString) : THandle;
+var SystemFileName: RawByteString;
 begin
 begin
-  FileCreate:=Fpopen(Pchar(FileName),O_RdWr or O_Creat or O_Trunc or O_Binary);
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
+  FileCreate:=Fpopen(Pchar(SystemFileName),O_RdWr or O_Creat or O_Trunc or O_Binary);
   if FileCreate >= 0 then
   if FileCreate >= 0 then
     FileSetAttr (Filename, 0);  // dont know why but open always sets ReadOnly flag
     FileSetAttr (Filename, 0);  // dont know why but open always sets ReadOnly flag
 end;
 end;
 
 
-Function FileCreate (Const FileName : String; rights:longint) : THandle;
+Function FileCreate (Const FileName : RawByteString; rights:longint) : THandle;
 begin
 begin
   FileCreate:=FileCreate (FileName);
   FileCreate:=FileCreate (FileName);
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String; ShareMode:longint; rights : longint) : THandle;
+Function FileCreate (Const FileName : RawByteString; ShareMode:longint; rights : longint) : THandle;
 begin
 begin
   FileCreate:=FileCreate (FileName);
   FileCreate:=FileCreate (FileName);
 end;
 end;

+ 80 - 0
rtl/objpas/sysutils/filutil.inc

@@ -0,0 +1,80 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 2012 by the Free Pascal development team
+
+    File utility calls
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+Function ToSingleByteFileSystemEncodedFileName(const Str: UnicodeString): RawByteString;
+Begin
+  widestringmanager.Unicode2AnsiMoveProc(punicodechar(Str),Result,
+    DefaultFileSystemCodePage,Length(Str));
+End;
+
+
+Function ToSingleByteFileSystemEncodedFileName(const Str: RawByteString): RawByteString;
+Begin
+  Result:=Str;
+  SetCodePage(Result,DefaultFileSystemCodePage,True);
+End;
+
+
+{$ifndef SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
+Function FileOpen (Const FileName : unicodestring; Mode : Integer) : THandle;
+begin
+  Result:=FileOpen(ToSingleByteFileSystemEncodedFileName(FileName),Mode);
+end;
+
+
+Function FileCreate (Const FileName : UnicodeString) : THandle;
+begin
+  Result:=FileCreate(ToSingleByteFileSystemEncodedFileName(FileName));
+end;
+
+
+Function FileCreate (Const FileName : UnicodeString; Rights : Integer) : THandle;
+begin
+  Result:=FileCreate(ToSingleByteFileSystemEncodedFileName(FileName),Rights);
+end;
+
+
+Function FileCreate (Const FileName : UnicodeString; ShareMode : Integer; Rights : Integer) : THandle;
+begin
+  Result:=FileCreate(ToSingleByteFileSystemEncodedFileName(FileName),ShareMode,Rights);
+end;
+{$endif}
+
+
+{$ifndef SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : THandle;
+begin
+  Result:=FileOpen(UnicodeString(FileName),Mode);
+end;
+
+
+Function FileCreate (Const FileName : RawByteString) : THandle;
+begin
+  Result:=FileCreate(UnicodeString(FileName));
+end;
+
+
+Function FileCreate (Const FileName : RawByteString; Rights : Integer) : THandle;
+begin
+  Result:=FileCreate(UnicodeString(FileName),Rights);
+end;
+
+
+Function FileCreate (Const FileName : RawByteString; ShareMode : Integer; Rights : Integer) : THandle;
+begin
+  Result:=FileCreate(UnicodeString(FileName),ShareMode,Rights);
+end;
+{$endif}
+

+ 8 - 4
rtl/objpas/sysutils/filutilh.inc

@@ -78,10 +78,14 @@ Type
   TFileSearchOption = (sfoImplicitCurrentDir,sfoStripQuotes);
   TFileSearchOption = (sfoImplicitCurrentDir,sfoStripQuotes);
   TFileSearchOptions = set of TFileSearchOption;
   TFileSearchOptions = set of TFileSearchOption;
 
 
-Function FileOpen (Const FileName : string; Mode : Integer) : THandle;
-Function FileCreate (Const FileName : String) : THandle;
-Function FileCreate (Const FileName : String; Rights : Integer) : THandle;
-Function FileCreate (Const FileName : String; ShareMode : Integer; Rights : Integer) : THandle;
+Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : THandle;
+Function FileOpen (Const FileName : unicodestring; Mode : Integer) : THandle;
+Function FileCreate (Const FileName : RawByteString) : THandle;
+Function FileCreate (Const FileName : RawByteString; Rights : Integer) : THandle;
+Function FileCreate (Const FileName : RawByteString; ShareMode : Integer; Rights : Integer) : THandle;
+Function FileCreate (Const FileName : UnicodeString) : THandle;
+Function FileCreate (Const FileName : UnicodeString; Rights : Integer) : THandle;
+Function FileCreate (Const FileName : UnicodeString; ShareMode : Integer; Rights : Integer) : THandle;
 Function FileRead (Handle : THandle; out Buffer; Count : longint) : Longint;
 Function FileRead (Handle : THandle; out Buffer; Count : longint) : Longint;
 Function FileWrite (Handle : THandle; const Buffer; Count : Longint) : Longint;
 Function FileWrite (Handle : THandle; const Buffer; Count : Longint) : Longint;
 Function FileSeek (Handle : THandle; FOffset, Origin: Longint) : Longint;
 Function FileSeek (Handle : THandle; FOffset, Origin: Longint) : Longint;

+ 3 - 0
rtl/objpas/sysutils/sysutils.inc

@@ -15,6 +15,9 @@
   { Read filename handling functions implementation }
   { Read filename handling functions implementation }
   {$i fina.inc}
   {$i fina.inc}
 
 
+  { Read file utility functions implementation }
+  {$i filutil.inc}
+
   { variant error codes }
   { variant error codes }
   {$i varerror.inc}
   {$i varerror.inc}
 
 

+ 13 - 6
rtl/os2/sysutils.pp

@@ -41,6 +41,9 @@ type
 {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
 {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
 {$DEFINE FPC_FEXPAND_GETENV_PCHAR}
 {$DEFINE FPC_FEXPAND_GETENV_PCHAR}
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -61,15 +64,17 @@ const
  FindResvdMask = $00003737; {Allowed bits in attribute
  FindResvdMask = $00003737; {Allowed bits in attribute
                              specification for DosFindFirst call.}
                              specification for DosFindFirst call.}
 
 
-function FileOpen (const FileName: string; Mode: integer): THandle;
+function FileOpen (const FileName: rawbytestring; Mode: integer): THandle;
 Var
 Var
+  SystemFileName: RawByteString;
   Handle: THandle;
   Handle: THandle;
   Rc, Action: cardinal;
   Rc, Action: cardinal;
 begin
 begin
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
 (* DenyNone if sharing not specified. *)
 (* DenyNone if sharing not specified. *)
   if (Mode and 112 = 0) or (Mode and 112 > 64) then
   if (Mode and 112 = 0) or (Mode and 112 > 64) then
    Mode := Mode or 64;
    Mode := Mode or 64;
-  Rc:=Sys_DosOpenL(PChar (FileName), Handle, Action, 0, 0, 1, Mode, nil);
+  Rc:=Sys_DosOpenL(PChar (SystemFileName), Handle, Action, 0, 0, 1, Mode, nil);
   If Rc=0 then
   If Rc=0 then
     FileOpen:=Handle
     FileOpen:=Handle
   else
   else
@@ -77,28 +82,30 @@ begin
     //should return feInvalidHandle(=-1) if fail, other negative returned value are no more errors
     //should return feInvalidHandle(=-1) if fail, other negative returned value are no more errors
 end;
 end;
 
 
-function FileCreate (const FileName: string): THandle;
+function FileCreate (const FileName: RawByteString): THandle;
 begin
 begin
   FileCreate := FileCreate (FileName, doDenyRW, 777); (* Sharing to DenyAll *)
   FileCreate := FileCreate (FileName, doDenyRW, 777); (* Sharing to DenyAll *)
 end;
 end;
 
 
-function FileCreate (const FileName: string; Rights: integer): THandle;
+function FileCreate (const FileName: RawByteString; Rights: integer): THandle;
 begin
 begin
   FileCreate := FileCreate (FileName, doDenyRW, Rights);
   FileCreate := FileCreate (FileName, doDenyRW, Rights);
                                       (* Sharing to DenyAll *)
                                       (* Sharing to DenyAll *)
 end;
 end;
 
 
-function FileCreate (const FileName: string; ShareMode: integer;
+function FileCreate (const FileName: RawByteString; ShareMode: integer;
                                                      Rights: integer): THandle;
                                                      Rights: integer): THandle;
 var
 var
+  SystemFileName: RawByteString;
   Handle: THandle;
   Handle: THandle;
   RC, Action: cardinal;
   RC, Action: cardinal;
 begin
 begin
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
   ShareMode := ShareMode and 112;
   ShareMode := ShareMode and 112;
   (* Sharing to DenyAll as default in case of values not allowed by OS/2. *)
   (* Sharing to DenyAll as default in case of values not allowed by OS/2. *)
   if (ShareMode = 0) or (ShareMode > 64) then
   if (ShareMode = 0) or (ShareMode > 64) then
    ShareMode := doDenyRW;
    ShareMode := doDenyRW;
-  RC := Sys_DosOpenL (PChar (FileName), Handle, Action, 0, 0, $12,
+  RC := Sys_DosOpenL (PChar (SystemFileName), Handle, Action, 0, 0, $12,
                                     faCreate or ofReadWrite or ShareMode, nil);
                                     faCreate or ofReadWrite or ShareMode, nil);
   if RC = 0 then
   if RC = 0 then
    FileCreate := Handle
    FileCreate := Handle

+ 18 - 7
rtl/unix/sysutils.pp

@@ -264,6 +264,9 @@ procedure UnhookSignal(RtlSigNum: Integer; OnlyIfHooked: Boolean = True);
 {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
 {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
 {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
 {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -424,9 +427,10 @@ begin
 end;
 end;
 
 
 
 
-Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
+Function FileOpen (Const FileName : RawbyteString; Mode : Integer) : Longint;
 
 
 Var
 Var
+  SystemFileName: RawByteString;
   LinuxFlags : longint;
   LinuxFlags : longint;
 begin
 begin
   LinuxFlags:=0;
   LinuxFlags:=0;
@@ -436,32 +440,39 @@ begin
     fmOpenReadWrite : LinuxFlags:=LinuxFlags or O_RdWr;
     fmOpenReadWrite : LinuxFlags:=LinuxFlags or O_RdWr;
   end;
   end;
 
 
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
   repeat
   repeat
-    FileOpen:=fpOpen (pointer(FileName),LinuxFlags);
+    FileOpen:=fpOpen (pointer(SystemFileName),LinuxFlags);
   until (FileOpen<>-1) or (fpgeterrno<>ESysEINTR);
   until (FileOpen<>-1) or (fpgeterrno<>ESysEINTR);
 
 
   FileOpen:=DoFileLocking(FileOpen, Mode);
   FileOpen:=DoFileLocking(FileOpen, Mode);
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String) : Longint;
+Function FileCreate (Const FileName : RawByteString) : Longint;
 
 
+Var
+  SystemFileName: RawByteString;
 begin
 begin
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
   repeat
   repeat
-    FileCreate:=fpOpen(pointer(FileName),O_RdWr or O_Creat or O_Trunc);
+    FileCreate:=fpOpen(pointer(SystemFileName),O_RdWr or O_Creat or O_Trunc);
   until (FileCreate<>-1) or (fpgeterrno<>ESysEINTR);
   until (FileCreate<>-1) or (fpgeterrno<>ESysEINTR);
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String;Rights : Longint) : Longint;
+Function FileCreate (Const FileName : RawByteString;Rights : Longint) : Longint;
 
 
+Var
+  SystemFileName: RawByteString;
 begin
 begin
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
   repeat
   repeat
-    FileCreate:=fpOpen(pointer(FileName),O_RdWr or O_Creat or O_Trunc,Rights);
+    FileCreate:=fpOpen(pointer(SystemFileName),O_RdWr or O_Creat or O_Trunc,Rights);
   until (FileCreate<>-1) or (fpgeterrno<>ESysEINTR);
   until (FileCreate<>-1) or (fpgeterrno<>ESysEINTR);
 end;
 end;
 
 
-Function FileCreate (Const FileName : String; ShareMode : Longint; Rights:LongInt ) : Longint;
+Function FileCreate (Const FileName : RawByteString; ShareMode : Longint; Rights:LongInt ) : Longint;
 
 
 begin
 begin
   Result:=FileCreate( FileName, Rights );
   Result:=FileCreate( FileName, Rights );

+ 14 - 7
rtl/watcom/sysutils.pp

@@ -40,6 +40,9 @@ implementation
 {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
 {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
 {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
 {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -115,32 +118,36 @@ begin
 end;
 end;
 
 
 
 
-Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
-var
+Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : Longint;
+Var
+  SystemFileName: RawByteString;
   e: integer;
   e: integer;
 Begin
 Begin
-  e := OpenFile(FileName, result, Mode, faOpen);
+  SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
+  e := OpenFile(SystemFileName, result, Mode, faOpen);
   if e <> 0 then
   if e <> 0 then
     result := -1;
     result := -1;
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String) : Longint;
+Function FileCreate (Const FileName : RawByteString) : Longint;
 var
 var
+  SystemFileName: RawByteString;
   e: integer;
   e: integer;
 begin
 begin
-  e := OpenFile(FileName, result, ofReadWrite, faCreate or faOpenReplace);
+  SystemFileName := ToSingleByteFileSystemEncodedFileName(FileName);
+  e := OpenFile(SystemFileName, result, ofReadWrite, faCreate or faOpenReplace);
   if e <> 0 then
   if e <> 0 then
     result := -1;
     result := -1;
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String; Rights:longint) : Longint;
+Function FileCreate (Const FileName : RawByteString; Rights:longint) : Longint;
 begin
 begin
   FileCreate:=FileCreate(FileName);
   FileCreate:=FileCreate(FileName);
 end;
 end;
 
 
-Function FileCreate (Const FileName : String; ShareMode:longint; Rights: Longint) : Longint;
+Function FileCreate (Const FileName : RawByteString; ShareMode:longint; Rights: Longint) : Longint;
 begin
 begin
   FileCreate:=FileCreate(FileName);
   FileCreate:=FileCreate(FileName);
 end;
 end;

+ 4 - 4
rtl/wii/sysutils.pp

@@ -45,7 +45,7 @@ uses
 {****************************************************************************
 {****************************************************************************
                               File Functions
                               File Functions
 ****************************************************************************}
 ****************************************************************************}
-function FileOpen(const FileName: string; Mode: Integer): LongInt;
+function FileOpen(const FileName: rawbytestring; Mode: Integer): LongInt;
 begin
 begin
   result := -1;
   result := -1;
 end;
 end;
@@ -63,18 +63,18 @@ begin
 end;
 end;
 
 
 
 
-function FileCreate(const FileName: string) : LongInt;
+function FileCreate(const FileName: RawByteString) : LongInt;
 begin
 begin
   result := -1;
   result := -1;
 end;
 end;
 
 
 
 
-function FileCreate(const FileName: string; Rights: integer): LongInt;
+function FileCreate(const FileName: RawByteString; Rights: integer): LongInt;
 begin
 begin
   result := -1;
   result := -1;
 end;
 end;
 
 
-function FileCreate(const FileName: string; ShareMode: integer; Rights: integer): LongInt;
+function FileCreate(const FileName: RawByteString; ShareMode: integer; Rights: integer): LongInt;
 begin
 begin
   result := -1;
   result := -1;
 end;
 end;

+ 14 - 6
rtl/win/sysutils.pp

@@ -151,6 +151,8 @@ function GetFileVersion(const AFileName:string):Cardinal;
 {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
 {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
 {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
 {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
 
 
+{ used OS file system APIs use ansistring }
+{$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
 
 
 function ConvertEraYearString(Count ,Year,Month,Day : integer) : string; forward;
 function ConvertEraYearString(Count ,Year,Month,Day : integer) : string; forward;
 function ConvertEraString(Count ,Year,Month,Day : integer) : string; forward;
 function ConvertEraString(Count ,Year,Month,Day : integer) : string; forward;
@@ -225,27 +227,33 @@ const
                FILE_SHARE_WRITE,
                FILE_SHARE_WRITE,
                FILE_SHARE_READ or FILE_SHARE_WRITE);
                FILE_SHARE_READ or FILE_SHARE_WRITE);
 
 
-Function FileOpen (Const FileName : string; Mode : Integer) : THandle;
+Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : THandle;
+Var
+  SystemFileName: RawByteString;
 begin
 begin
-  result := CreateFile(PChar(FileName), dword(AccessMode[Mode and 3]),
+  SystemFileName := ToSingleByteFileSystemEncodedFileName(FileName);
+  result := CreateFile(PChar(SystemFileName), dword(AccessMode[Mode and 3]),
                        dword(ShareModes[(Mode and $F0) shr 4]), nil, OPEN_EXISTING,
                        dword(ShareModes[(Mode and $F0) shr 4]), nil, OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL, 0);
                        FILE_ATTRIBUTE_NORMAL, 0);
   //if fail api return feInvalidHandle (INVALIDE_HANDLE=feInvalidHandle=-1)
   //if fail api return feInvalidHandle (INVALIDE_HANDLE=feInvalidHandle=-1)
 end;
 end;
 
 
-Function FileCreate (Const FileName : String) : THandle;
+Function FileCreate (Const FileName : RawByteString) : THandle;
 begin
 begin
   FileCreate:=FileCreate(FileName, fmShareExclusive, 0);
   FileCreate:=FileCreate(FileName, fmShareExclusive, 0);
 end;
 end;
 
 
-Function FileCreate (Const FileName : String; Rights:longint) : THandle;
+Function FileCreate (Const FileName : RawByteString; Rights:longint) : THandle;
 begin
 begin
   FileCreate:=FileCreate(FileName, fmShareExclusive, Rights);
   FileCreate:=FileCreate(FileName, fmShareExclusive, Rights);
 end;
 end;
 
 
-Function FileCreate (Const FileName : String; ShareMode : Integer; Rights : Integer) : THandle;
+Function FileCreate (Const FileName : RawByteString; ShareMode : Integer; Rights : Integer) : THandle;
+Var
+  SystemFileName: RawByteString;
 begin
 begin
-  Result := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE,
+  SystemFileName := ToSingleByteFileSystemEncodedFileName(FileName);
+  Result := CreateFile(PChar(SystemFileName), GENERIC_READ or GENERIC_WRITE,
                        dword(ShareModes[(ShareMode and $F0) shr 4]), nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
                        dword(ShareModes[(ShareMode and $F0) shr 4]), nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
 end;
 end;
 
 

+ 9 - 14
rtl/wince/sysutils.pp

@@ -62,6 +62,9 @@ implementation
 {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
 {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
 {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
 {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
 
 
+{ used OS file system APIs use unicodestring }
+{$DEFINE SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
+
 { Include platform independent implementation part }
 { Include platform independent implementation part }
 {$i sysutils.inc}
 {$i sysutils.inc}
 
 
@@ -130,7 +133,7 @@ end;
                               File Functions
                               File Functions
 ****************************************************************************}
 ****************************************************************************}
 
 
-Function FileOpen (Const FileName : string; Mode : Integer) : THandle;
+Function FileOpen (Const FileName : unicodestring; Mode : Integer) : THandle;
 const
 const
   AccessMode: array[0..2] of Cardinal  = (
   AccessMode: array[0..2] of Cardinal  = (
     GENERIC_READ,
     GENERIC_READ,
@@ -142,36 +145,28 @@ const
                FILE_SHARE_READ,
                FILE_SHARE_READ,
                FILE_SHARE_WRITE,
                FILE_SHARE_WRITE,
                FILE_SHARE_READ or FILE_SHARE_WRITE);
                FILE_SHARE_READ or FILE_SHARE_WRITE);
-var
-  fn: PWideChar;
 begin
 begin
-  fn:=StringToPWideChar(FileName);
-  result := CreateFile(fn, dword(AccessMode[Mode and 3]),
+  result := CreateFile(PWideChar(FileName), dword(AccessMode[Mode and 3]),
                        dword(ShareMode[(Mode and $F0) shr 4]), nil, OPEN_EXISTING,
                        dword(ShareMode[(Mode and $F0) shr 4]), nil, OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL, 0);
                        FILE_ATTRIBUTE_NORMAL, 0);
   //if fail api return feInvalidHandle (INVALIDE_HANDLE=feInvalidHandle=-1)
   //if fail api return feInvalidHandle (INVALIDE_HANDLE=feInvalidHandle=-1)
-  FreeMem(fn);
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String) : THandle;
-var
-  fn: PWideChar;
+Function FileCreate (Const FileName : UnicodeString) : THandle;
 begin
 begin
-  fn:=StringToPWideChar(FileName);
-  Result := CreateFile(fn, GENERIC_READ or GENERIC_WRITE,
+  Result := CreateFile(PWideChar(FileName), GENERIC_READ or GENERIC_WRITE,
                        0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
                        0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
-  FreeMem(fn);
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String; Rights:longint) : THandle;
+Function FileCreate (Const FileName : UnicodeString; Rights:longint) : THandle;
 begin
 begin
   FileCreate:=FileCreate(FileName);
   FileCreate:=FileCreate(FileName);
 end;
 end;
 
 
 
 
-Function FileCreate (Const FileName : String; ShareMode:longint; Rights:longint) : THandle;
+Function FileCreate (Const FileName : UnicodeString; ShareMode:longint; Rights:longint) : THandle;
 begin
 begin
   FileCreate:=FileCreate(FileName);
   FileCreate:=FileCreate(FileName);
 end;
 end;

+ 100 - 0
tests/test/units/sysutils/tunifile.pp

@@ -0,0 +1,100 @@
+{$codepage utf8}
+{$mode objfpc}{$h+}
+
+uses
+{$ifdef unix}
+  cwstring,
+{$endif}
+  sysutils;
+
+type
+  tcpstr866 = type ansistring(866);
+
+procedure error(const s: string);
+begin
+  writeln('Error: ',s);
+  halt(1);
+end;
+
+
+procedure warn(const s: string);
+begin
+  writeln('Warning: cannot test '+s+' scenario fully because not all characters are supported by DefaultFileSystemCodePage');
+end;
+
+
+procedure testsinglebyte;
+var
+  u: utf8string;
+  c: tcpstr866;
+  f: THandle;
+  r: rawbytestring;
+begin
+  { can't set code page of an empty string }
+  r:=' ';
+  setcodepage(r,DefaultFileSystemCodePage,false);
+  u:='‹≈©◊';
+  r:=u;
+  if r=u then
+    begin
+      f:=FileCreate(u,fmShareDenyNone,(6 shl 6) or (4 shl 3) or 4);
+      if f=-1 then
+        Error('Creating utf8string');
+      FileClose(f);
+    end
+  else
+    warn('utf8string');
+  c:='Русская';
+  setcodepage(rawbytestring(c),866);
+  r:=c;
+  setcodepage(r,DefaultFileSystemCodePage);
+  if r=c then
+    begin
+      f:=FileCreate(c,fmShareDenyNone,(6 shl 6) or (4 shl 3) or 4);
+      if f=-1 then
+        Error('Creating tcpstr866');
+      FileClose(f);
+    end
+  else
+    warn('tcpstr866');
+end;
+
+
+procedure testtwobyte;
+var
+  u: unicodestring;
+  f: THandle;
+  r: rawbytestring;
+begin
+  { can't set code page of an empty string }
+  r:=' ';
+  setcodepage(r,DefaultFileSystemCodePage,false);
+  u:='‹≈©◊';
+  r:=u;
+  if r=u then
+    begin
+      f:=FileCreate(u,fmShareDenyNone,(6 shl 6) or (4 shl 3) or 4);
+      if f=-1 then
+        Error('Creating unicodestring 1');
+      FileClose(f);
+    end
+  else
+    warn('random unicodestring');
+  u:='Русская';
+  r:=u;
+  if r=u then
+    begin
+      f:=FileCreate(u,fmShareDenyNone,(6 shl 6) or (4 shl 3) or 4);
+      if f=-1 then
+        Error('Creating unicodestring 2');
+      FileClose(f);
+    end
+  else
+    warn('cp866 unicodestring');
+end;
+
+
+begin
+  testsinglebyte;
+  testtwobyte;
+end.