From bf3f2431f6708840bbdb4a1a43b325ae1055c3c9 Mon Sep 17 00:00:00 2001 From: bart <9132501-flyingsheep@users.noreply.gitlab.com> Date: Wed, 15 May 2013 16:52:42 +0000 Subject: [PATCH] LazUtils: fix LazFileUtils.FileIsText when filename has unicode characters outside currrent codepage on Windows. Needs refactoring of LazUtf8Classes (use LazFileUtils instead of FileUtil). Next step in moving UTF89 file routines to LazFileUil. git-svn-id: trunk@41209 - --- components/lazutils/lazfileutils.pas | 10 ++++-- components/lazutils/lazutf8classes.pas | 5 ++- components/lazutils/unixlazfileutils.inc | 15 ++++++++ components/lazutils/winlazfileutils.inc | 46 ++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 4 deletions(-) diff --git a/components/lazutils/lazfileutils.pas b/components/lazutils/lazfileutils.pas index 4eea5dfba6..4da46af27f 100644 --- a/components/lazutils/lazfileutils.pas +++ b/components/lazutils/lazfileutils.pas @@ -5,7 +5,7 @@ unit LazFileUtils; interface uses - Classes, SysUtils, LazUTF8, LUResStrings; + Classes, SysUtils, LazUTF8, LazUtf8Classes, LUResStrings; {$IFDEF Windows} {$define CaseInsensitiveFilenames} @@ -84,6 +84,10 @@ function CreateDirUTF8(const NewDir: String): Boolean; function RemoveDirUTF8(const Dir: String): Boolean; function ForceDirectoriesUTF8(const Dir: string): Boolean; +function FileOpenUTF8(Const FileName : string; Mode : Integer) : THandle; +function FileCreateUTF8(Const FileName : string) : THandle; overload; +function FileCreateUTF8(Const FileName : string; Rights: Cardinal) : THandle; overload; + // UNC paths function IsUNCPath(const {%H-}Path: String): Boolean; function ExtractUNCVolume(const {%H-}Path: String): String; @@ -487,7 +491,7 @@ end; function FileIsText(const AFilename: string; out FileReadable: boolean): boolean; var - fs: TFileStream; + fs: TFileStreamUtf8; Buf: string; Len: integer; NewLine: boolean; @@ -497,7 +501,7 @@ begin Result:=false; FileReadable:=true; try - fs := TFileStream.Create(UTF8ToSys(AFilename), fmOpenRead or fmShareDenyNone); + fs := TFileStreamUtf8.Create(AFilename, fmOpenRead or fmShareDenyNone); try // read the first 1024 bytes Len:=1024; diff --git a/components/lazutils/lazutf8classes.pas b/components/lazutils/lazutf8classes.pas index 5996816278..3f8e46975b 100644 --- a/components/lazutils/lazutf8classes.pas +++ b/components/lazutils/lazutf8classes.pas @@ -5,7 +5,7 @@ unit lazutf8classes; interface uses - Classes, SysUtils, fileutil, lazutf8; + Classes, SysUtils, lazutf8; type TFileStreamUTF8 = class(THandleStream) @@ -33,6 +33,9 @@ function CompareStringListItemsUTF8LowerCase(List: TStringList; Index1, Index2: implementation +uses + LazFileUtils; //avoid circular reference with LazFileUtils + procedure LoadStringsFromFileUTF8(List: TStrings; const FileName: string); var uList: TStringListUTF8; diff --git a/components/lazutils/unixlazfileutils.inc b/components/lazutils/unixlazfileutils.inc index daa646b1e2..6a814c2483 100644 --- a/components/lazutils/unixlazfileutils.inc +++ b/components/lazutils/unixlazfileutils.inc @@ -5,6 +5,21 @@ begin Result:=FilenameIsUnixAbsolute(TheFilename); end; +function FileOpenUTF8(Const FileName : string; Mode : Integer) : THandle; +begin + Result := SysUtils.FileOpen(FileName, Mode); +end; + +function FileCreateUTF8(Const FileName : string) : THandle; +begin + Result := SysUtils.FileCreate(FileName); +end; + +function FileCreateUTF8(Const FileName : string; Rights: Cardinal) : THandle; +begin + Result := SysUtils.FileCreate(FileName, Rights); +end; + function ExpandFileNameUtf8(const FileName: string; {const} BaseDir: String = ''): String; var IsAbs: Boolean; diff --git a/components/lazutils/winlazfileutils.inc b/components/lazutils/winlazfileutils.inc index ed42cbd69a..c9e479b60c 100644 --- a/components/lazutils/winlazfileutils.inc +++ b/components/lazutils/winlazfileutils.inc @@ -26,6 +26,52 @@ begin Result:=FilenameIsWinAbsolute(TheFilename); end; +function FileOpenUTF8(Const FileName : string; Mode : Integer) : THandle; +const + AccessMode: array[0..2] of Cardinal = ( + GENERIC_READ, + GENERIC_WRITE, + GENERIC_READ or GENERIC_WRITE); + ShareMode: array[0..4] of Integer = ( + 0, + 0, + FILE_SHARE_READ, + FILE_SHARE_WRITE, + FILE_SHARE_READ or FILE_SHARE_WRITE); +begin +{$ifndef WinCE} + if (Win32Platform = VER_PLATFORM_WIN32_WINDOWS) then + Result := FileOpen(UTF8ToSys(FileName), Mode) + else +{$endif} + Result := CreateFileW(PWideChar(UTF8Decode(FileName)), dword(AccessMode[Mode and 3]), + dword(ShareMode[(Mode and $F0) shr 4]), nil, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, 0); + //if fail api return feInvalidHandle (INVALIDE_HANDLE=feInvalidHandle=-1) +end; + +function FileCreateUTF8(Const FileName : string) : THandle; +begin +{$ifndef WinCE} + if (Win32Platform = VER_PLATFORM_WIN32_WINDOWS) then + Result := FileCreate(Utf8ToSys(FileName)) + else +{$endif} + Result := CreateFileW(PWideChar(UTF8Decode(FileName)), GENERIC_READ or GENERIC_WRITE, + 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); +end; + +function FileCreateUTF8(Const FileName : string; Rights: Cardinal) : THandle; +begin +{$ifndef WinCE} + if (Win32Platform = VER_PLATFORM_WIN32_WINDOWS) then + Result := FileCreate(Utf8ToSys(FileName), Rights) + else +{$endif} + Result := CreateFileW(PWideChar(UTF8Decode(FileName)), GENERIC_READ or GENERIC_WRITE, + 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); +end; + function ExpandFileNameUtf8(const FileName: string; {const} BaseDir: String = ''): String; var