mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-04-06 06:57:54 +02:00
Patch from 19742: Adds more file utf8 routines to FileUtil and adds a new unit for utf8 classes
git-svn-id: trunk@32656 -
This commit is contained in:
parent
358e3ee690
commit
2710107266
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -5638,6 +5638,7 @@ lcl/lazconfigstorage.pas svneol=native#text/pascal
|
||||
lcl/lazhelphtml.pas svneol=native#text/pascal
|
||||
lcl/lazhelpintf.pas svneol=native#text/pascal
|
||||
lcl/lazlinkedlist.pas svneol=native#text/pascal
|
||||
lcl/lazutf8classes.pas svneol=native#text/pascal
|
||||
lcl/lcl_calc_images.lrs svneol=native#text/pascal
|
||||
lcl/lcl_dbgrid_images.lrs svneol=native#text/pascal
|
||||
lcl/lcl_dbnav_images.lrs svneol=native#text/pascal
|
||||
|
@ -25,7 +25,7 @@ uses
|
||||
WSExtCtrls, WSExtDlgs, WSFactory, WSForms, WSGrids, WSImgList, WSLCLClasses,
|
||||
WSMenus, WSPairSplitter, WSProc, WSReferences, WSSpin, WSStdCtrls,
|
||||
WSToolwin, ActnList, Arrow, AsyncProcess, AvgLvlTree, ButtonPanel, Buttons,
|
||||
Calendar, RegisterLCL, ValEdit, LazarusPackageIntf;
|
||||
Calendar, RegisterLCL, ValEdit, lazutf8classes, LazarusPackageIntf;
|
||||
|
||||
implementation
|
||||
|
||||
|
@ -213,6 +213,9 @@ function SetCurrentDirUTF8(const NewDir: String): Boolean;
|
||||
function CreateDirUTF8(const NewDir: String): Boolean;
|
||||
function RemoveDirUTF8(const Dir: String): Boolean;
|
||||
function ForceDirectoriesUTF8(const Dir: string): Boolean;
|
||||
function FileOpenUTF8(Const FileName : utf8string; Mode : Integer) : THandle;
|
||||
function FileCreateUTF8(Const FileName : utf8String) : THandle; overload;
|
||||
function FileCreateUTF8(Const FileName : utf8String; Rights: Cardinal) : THandle; overload;
|
||||
|
||||
// environment
|
||||
function ParamStrUTF8(Param: Integer): string;
|
||||
|
@ -377,6 +377,21 @@ begin
|
||||
Result:=SysUtils.DirectoryExists(UTF8ToSys(Directory));
|
||||
end;
|
||||
|
||||
function FileOpenUTF8(Const FileName : utf8string; Mode : Integer) : THandle;
|
||||
begin
|
||||
Result := SysUtils.FileOpen(FileName, Mode);
|
||||
end;
|
||||
|
||||
function FileCreateUTF8(Const FileName : utf8string) : THandle;
|
||||
begin
|
||||
Result := SysUtils.FileCreate(FileName);
|
||||
end;
|
||||
|
||||
function FileCreateUTF8(Const FileName : utf8String; Rights: Cardinal) : THandle;
|
||||
begin
|
||||
Result := SysUtils.FileCreate(FileName, Rights);
|
||||
end;
|
||||
|
||||
procedure InitFileUtils;
|
||||
begin
|
||||
end;
|
||||
|
@ -588,6 +588,37 @@ begin
|
||||
Result:=False;
|
||||
end;
|
||||
|
||||
function FileOpenUTF8(Const FileName : utf8string; 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
|
||||
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 : utf8string) : THandle;
|
||||
begin
|
||||
Result := CreateFileW(PWideChar(UTF8Decode(FileName)), GENERIC_READ or GENERIC_WRITE,
|
||||
0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
|
||||
end;
|
||||
|
||||
function FileCreateUTF8(Const FileName : utf8String; Rights: Cardinal) : THandle;
|
||||
begin
|
||||
Result := CreateFileW(PWideChar(UTF8Decode(FileName)), GENERIC_READ or GENERIC_WRITE,
|
||||
0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
|
||||
end;
|
||||
|
||||
procedure InitFileUtils;
|
||||
begin
|
||||
{$ifndef WinCE}
|
||||
|
108
lcl/lazutf8classes.pas
Normal file
108
lcl/lazutf8classes.pas
Normal file
@ -0,0 +1,108 @@
|
||||
unit lazutf8classes;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, fileutil;
|
||||
|
||||
type
|
||||
TFileStreamUTF8 = class(THandleStream)
|
||||
private
|
||||
FFileName: utf8string;
|
||||
public
|
||||
constructor Create(const AFileName: utf8string; Mode: Word);
|
||||
constructor Create(const AFileName: utf8string; Mode: Word; Rights: Cardinal);
|
||||
destructor Destroy; override;
|
||||
property FileName: utf8string Read FFilename;
|
||||
end;
|
||||
|
||||
TStringListUTF8 = class(TStringList)
|
||||
protected
|
||||
function DoCompareText(const s1,s2 : string) : PtrInt; override;
|
||||
procedure LoadFromFile(const FileName: string); override;
|
||||
procedure SaveToFile(const FileName: string); override;
|
||||
public
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
constructor TFileStreamUTF8.Create(const AFileName: utf8string; Mode: Word);
|
||||
var
|
||||
lHandle: THandle;
|
||||
begin
|
||||
FFileName:= AFileName;
|
||||
if Mode = fmcreate then
|
||||
lHandle:= FileCreateUTF8(AFileName)
|
||||
else
|
||||
lHandle:= FileOpenUTF8(AFileName, Mode);
|
||||
|
||||
If (THandle(lHandle)=feInvalidHandle) then
|
||||
begin
|
||||
if Mode = fmCreate then
|
||||
raise EFCreateError.createfmt({SFCreateError}'Unable to create file "%s"', [AFileName])
|
||||
else
|
||||
raise EFOpenError.Createfmt({SFOpenError}'Unable to open file "%s"', [AFilename]);
|
||||
end
|
||||
else
|
||||
inherited Create(lHandle);
|
||||
end;
|
||||
|
||||
constructor TFileStreamUTF8.Create(const AFileName: utf8string; Mode: Word; Rights: Cardinal);
|
||||
var
|
||||
lHandle: THandle;
|
||||
begin
|
||||
FFileName:=AFileName;
|
||||
if Mode=fmcreate then
|
||||
lHandle:=FileCreateUTF8(AFileName,Rights)
|
||||
else
|
||||
lHandle:=FileOpenUTF8(AFileName,Mode);
|
||||
|
||||
if (THandle(lHandle)=feInvalidHandle) then
|
||||
begin
|
||||
if Mode=fmcreate then
|
||||
raise EFCreateError.createfmt({SFCreateError}'Unable to create file "%s"',[AFileName])
|
||||
else
|
||||
raise EFOpenError.Createfmt({SFOpenError}'Unable to open file "%s"',[AFilename]);
|
||||
end
|
||||
else
|
||||
inherited Create(lHandle);
|
||||
end;
|
||||
|
||||
destructor TFileStreamUTF8.Destroy;
|
||||
begin
|
||||
FileClose(Handle);
|
||||
end;
|
||||
|
||||
function TStringListUTF8.DoCompareText(const s1, s2: string): PtrInt;
|
||||
begin
|
||||
Result := CompareText(s1, s2);
|
||||
end;
|
||||
|
||||
procedure TStringListUTF8.LoadFromFile(const FileName: string);
|
||||
var
|
||||
TheStream: TFileStreamUTF8;
|
||||
begin
|
||||
TheStream:= TFileStreamUTF8.Create(FileName, fmOpenRead or fmShareDenyWrite);
|
||||
try
|
||||
LoadFromStream(TheStream);
|
||||
finally
|
||||
TheStream.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TStringListUTF8.SaveToFile(const FileName: string);
|
||||
var
|
||||
TheStream: TFileStreamUTF8;
|
||||
begin
|
||||
TheStream:=TFileStreamUTF8.Create(FileName,fmCreate);
|
||||
try
|
||||
SaveToStream(TheStream);
|
||||
finally
|
||||
TheStream.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
@ -10,8 +10,14 @@
|
||||
<OtherUnitFiles Value="forms;widgetset"/>
|
||||
<UnitOutputDirectory Value="units/$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
<Conditionals Value="if SrcOS<>'win' then
|
||||
<Conditionals Value="if SrcOS<>'win' then
|
||||
UnitPath := 'nonwin32';"/>
|
||||
<Linking>
|
||||
<Debugging>
|
||||
<GenerateDebugInfo Value="True"/>
|
||||
<DebugInfoType Value="dsAuto"/>
|
||||
</Debugging>
|
||||
</Linking>
|
||||
<Other>
|
||||
<Verbosity>
|
||||
<ShowNotes Value="False"/>
|
||||
@ -31,7 +37,7 @@
|
||||
<License Value="modified LGPL-2
|
||||
"/>
|
||||
<Version Major="1" Release="1"/>
|
||||
<Files Count="294">
|
||||
<Files Count="295">
|
||||
<Item1>
|
||||
<Filename Value="barchart.pp"/>
|
||||
<UnitName Value="BarChart"/>
|
||||
@ -1215,6 +1221,10 @@
|
||||
<HasRegisterProc Value="True"/>
|
||||
<UnitName Value="ValEdit"/>
|
||||
</Item294>
|
||||
<Item295>
|
||||
<Filename Value="lazutf8classes.pas"/>
|
||||
<UnitName Value="lazutf8classes"/>
|
||||
</Item295>
|
||||
</Files>
|
||||
<LazDoc Paths="../docs/xml/lcl"/>
|
||||
<i18n>
|
||||
|
Loading…
Reference in New Issue
Block a user