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 -
This commit is contained in:
bart 2013-05-15 16:52:42 +00:00
parent 553e6bbbc3
commit bf3f2431f6
4 changed files with 72 additions and 4 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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