lazutils: added unit lazpasreadutil

git-svn-id: trunk@56209 -
This commit is contained in:
mattias 2017-10-26 14:58:46 +00:00
parent acd77c3bb0
commit fac364fd3b
6 changed files with 112 additions and 95 deletions

1
.gitattributes vendored
View File

@ -3168,6 +3168,7 @@ components/lazutils/lazloggerbase.pas svneol=native#text/pascal
components/lazutils/lazloggerdummy.pas svneol=native#text/pascal components/lazutils/lazloggerdummy.pas svneol=native#text/pascal
components/lazutils/lazloggerprofiling.pas svneol=native#text/pascal components/lazutils/lazloggerprofiling.pas svneol=native#text/pascal
components/lazutils/lazmethodlist.pas svneol=native#text/pascal components/lazutils/lazmethodlist.pas svneol=native#text/pascal
components/lazutils/lazpasreadutil.pas svneol=native#text/plain
components/lazutils/lazunicode.pas svneol=native#text/plain components/lazutils/lazunicode.pas svneol=native#text/plain
components/lazutils/lazutf16.pas svneol=native#text/pascal components/lazutils/lazutf16.pas svneol=native#text/pascal
components/lazutils/lazutf8.pas svneol=native#text/pascal components/lazutils/lazutf8.pas svneol=native#text/pascal

View File

@ -3,32 +3,7 @@
./runtests --format=plain --suite=TTestCompReaderWriterPas ./runtests --format=plain --suite=TTestCompReaderWriterPas
./runtests --format=plain --suite=TTestCompReaderWriterPas.TestBaseTypesMaxValues ./runtests --format=plain --suite=TTestCompReaderWriterPas.TestBaseTypesMaxValues
Working:
- boolean, set of boolean
- char, widechar, custom char, set of custom char
- integers, custom int, set of custom int
- strings, codepage system and UTF8
- float, currency
- enum, custom enum range
- set of enum, set of custom enum range
- variant: integers, boolean, string, floats, currency
- method
- persistent
- component children, use SetParentComponent or optional Parent:=
- collection
- IInterfaceComponentReference
- with ancestor
- ancestor: change ComponentIndex -> call SetChildPos
- reference foreign root, reference foreign component
- create components before setting properties to avoid having to set references
later
- inline component, csInline, call SetInline, inherited inline, inline on inherited
- TComponent.Left/Right via DesignInfo
- DefineProperties
ToDo: ToDo:
- OnWriteMethodProperty
- OnWriteStringProperty
- RegisterPascalProperties(aClass,@); - RegisterPascalProperties(aClass,@);
- enum: add unit, avoid nameclash with-do - enum: add unit, avoid nameclash with-do
- custom integer TColor, add unit, avoid nameclash with-do - custom integer TColor, add unit, avoid nameclash with-do
@ -52,8 +27,8 @@ unit TestCompReaderWriterPas;
interface interface
uses uses
Classes, SysUtils, typinfo, LazLoggerBase, LazUTF8, LazLogger, Classes, SysUtils, typinfo, LazLoggerBase, LazUTF8, LazLogger, CompWriterPas,
CompWriterPas, fpcunit, testregistry, CodeToolManager, LinkScanner, LazPasReadUtil, fpcunit, testregistry, CodeToolManager, LinkScanner,
CodeToolsStructs, CodeCache, BasicCodeTools, TestStdCodetools, TestGlobals, CodeToolsStructs, CodeCache, BasicCodeTools, TestStdCodetools, TestGlobals,
variants; variants;
@ -1985,47 +1960,6 @@ begin
end; end;
end; end;
type
{ TCSPReader }
TCSPReader = class(TReader)
public
procedure ReadProperties(Instance: TPersistent);
end;
{ TCSPReader }
procedure TCSPReader.ReadProperties(Instance: TPersistent);
begin
while not EndOfList do
ReadProperty(Instance);
end;
procedure ExecCustomLFM(Instance: TPersistent; const Data: array of string);
var
MemStream: TMemoryStream;
i: Integer;
s: String;
Reader: TCSPReader;
begin
MemStream:=TMemoryStream.Create;
Reader:=nil;
try
for i:=low(Data) to High(Data) do
begin
s:=Data[i];
MemStream.Write(s[1],length(s));
end;
MemStream.Position:=0;
Reader:=TCSPReader.Create(MemStream,1024);
Reader.ReadProperties(Instance);
finally
Reader.Free;
MemStream.Free;
end;
end;
procedure TTestCompReaderWriterPas.TestDefineProperites_ListOfStrings; procedure TTestCompReaderWriterPas.TestDefineProperites_ListOfStrings;
var var
ARoot: TSimpleControlWithStrings; ARoot: TSimpleControlWithStrings;
@ -2042,11 +1976,11 @@ begin
end; end;
Expected:=#7'Strings'#1#6#5'First'#6#6'Second'#0#0; Expected:=#7'Strings'#1#6#5'First'#6#6'Second'#0#0;
TestWriteDescendant('TestDefineProperites_ListOfStrings',ARoot,nil,[ TestWriteDescendant('TestDefineProperites_ListOfStrings',ARoot,nil,[
CSPDefaultExecCustomLFM+'(Lines,[#7''Strings''#1#6#5''First''#6#6''Second''#0#0]);', CSPDefaultExecCustomCSP+'(Lines,[#7''Strings''#1#6#5''First''#6#6''Second''#0#0]);',
'']); '']);
Lines2:=TStringList.Create; Lines2:=TStringList.Create;
ExecCustomLFM(Lines2,[Expected]); ExecCustomCSP(Lines2,[Expected]);
AssertEquals('read TStrings.Text',ARoot.Lines.Text,Lines2.Text); AssertEquals('read TStrings.Text',ARoot.Lines.Text,Lines2.Text);
finally finally

View File

@ -6,28 +6,35 @@
for details about the license. for details about the license.
***************************************************************************** *****************************************************************************
Component serialisation drivers for pascal. Component serialisation into Pascal.
Works: Author: Mattias Gaertner
- simple properties: integer, strings, events, ...
- nested components (e.g. the child controls of a form) Working:
- class properties (e.g. TControl.Font) - boolean, set of boolean
- char, widechar, custom char, set of custom char
- integers, custom int, set of custom int
- strings, codepage system and UTF8
- float, currency
- enum, custom enum range
- set of enum, set of custom enum range
- variant: integers, boolean, string, floats, currency
- method
- persistent
- component children, use SetParentComponent or optional Parent:=
- collection
- IInterfaceComponentReference
- with ancestor
- ancestor: change ComponentIndex -> call SetChildPos
- reference foreign root, reference foreign component
- create components before setting properties to avoid having to set references
later
- inline component, csInline, call SetInline, inherited inline, inline on inherited
- TComponent.Left/Right via DesignInfo
- DefineProperties
ToDo: ToDo:
- TCollection needs a typecast to the item class - RegisterPascalProperties(aClass,@);
- variants
- widestrings need special encoding conversions, but the driver does not
know, that a widestring is assigned
- what to do with DefineProperties?
- the 'with' can conflict
- circle dependencies:
Edit1:=TEdit.Create(Form1);
Edit1.AnchorSide[akLeft].Control:=Label1;
Label1:=TLabel.Create(Form1);
Label1.AnchorSide[akTop].Control:=Edit1;
- ChildPos
- Flags
- a reader
} }
unit CompWriterPas; unit CompWriterPas;
@ -47,7 +54,7 @@ const
CSPDefaultSignatureBegin = CSPDefaultSignature+' - Begin'; CSPDefaultSignatureBegin = CSPDefaultSignature+' - Begin';
CSPDefaultSignatureEnd = CSPDefaultSignature+' - End'; CSPDefaultSignatureEnd = CSPDefaultSignature+' - End';
CSPDefaultAccessClass = 'TPasStreamAccess'; CSPDefaultAccessClass = 'TPasStreamAccess';
CSPDefaultExecCustomLFM = 'ExecCustomLFM'; CSPDefaultExecCustomCSP = 'ExecCustomCSP';
CSPDefaultMaxColumn = 80; CSPDefaultMaxColumn = 80;
CWPSkipParentName = '-'; CWPSkipParentName = '-';
type type
@ -1255,7 +1262,7 @@ begin
FAssignOp:=':='; FAssignOp:=':=';
FSignature:=CSPDefaultSignature; FSignature:=CSPDefaultSignature;
FMaxColumn:=CSPDefaultMaxColumn; FMaxColumn:=CSPDefaultMaxColumn;
FExecCustomData:=CSPDefaultExecCustomLFM; FExecCustomData:=CSPDefaultExecCustomCSP;
FAccessClass:=CSPDefaultAccessClass; FAccessClass:=CSPDefaultAccessClass;
C:=TAccessComp.Create(nil); C:=TAccessComp.Create(nil);
FDefaultDefineProperties:=TMethod(@C.DefineProperties).Code; FDefaultDefineProperties:=TMethod(@C.DefineProperties).Code;

View File

@ -0,0 +1,71 @@
{
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Helper functions for component serialized Pascal.
Author: Mattias Gaertner
}
unit LazPasReadUtil;
{$mode objfpc}{$H+}
interface
uses
Classes;
{ ExecCustomCSP: Call Instance.DefineProperties with a list of properties in
TBinaryObjectWriter format. This function is used by the auto generated
Pascal of TCompWriterPas for custom DefineProperties. }
procedure ExecCustomCSP(Instance: TPersistent; const Data: array of string);
implementation
type
{ TCSPReader }
TCSPReader = class(TReader)
public
procedure ReadProperties(Instance: TPersistent);
end;
{ TCSPReader }
procedure TCSPReader.ReadProperties(Instance: TPersistent);
begin
while not EndOfList do
ReadProperty(Instance);
end;
procedure ExecCustomCSP(Instance: TPersistent; const Data: array of string);
var
MemStream: TMemoryStream;
i: Integer;
s: String;
Reader: TCSPReader;
begin
MemStream:=TMemoryStream.Create;
Reader:=nil;
try
for i:=low(Data) to High(Data) do
begin
s:=Data[i];
MemStream.Write(s[1],length(s));
end;
MemStream.Position:=0;
Reader:=TCSPReader.Create(MemStream,1024);
Reader.ReadProperties(Instance);
finally
Reader.Free;
MemStream.Free;
end;
end;
end.

View File

@ -16,7 +16,7 @@
<Description Value="Useful units for Lazarus packages."/> <Description Value="Useful units for Lazarus packages."/>
<License Value="Modified LGPL-2"/> <License Value="Modified LGPL-2"/>
<Version Major="1"/> <Version Major="1"/>
<Files Count="94"> <Files Count="95">
<Item1> <Item1>
<Filename Value="LazLoggerImpl.inc"/> <Filename Value="LazLoggerImpl.inc"/>
<Type Value="Include"/> <Type Value="Include"/>
@ -391,8 +391,12 @@
</Item93> </Item93>
<Item94> <Item94>
<Filename Value="compwriterpas.pas"/> <Filename Value="compwriterpas.pas"/>
<UnitName Value="ComponentStreamPas"/> <UnitName Value="CompWriterPas"/>
</Item94> </Item94>
<Item95>
<Filename Value="lazpasreadutil.pas"/>
<UnitName Value="lazpasreadutil"/>
</Item95>
</Files> </Files>
<LazDoc Paths="../../docs/xml/lazutils"/> <LazDoc Paths="../../docs/xml/lazutils"/>
<i18n> <i18n>

View File

@ -21,7 +21,7 @@ uses
StringHashList, TextStrings, Translations, TTCache, TTCalc, TTCMap, TTDebug, StringHashList, TextStrings, Translations, TTCache, TTCalc, TTCMap, TTDebug,
TTError, TTFile, TTGLoad, TTInterp, TTLoad, TTMemory, TTObjs, TTProfile, TTError, TTFile, TTGLoad, TTInterp, TTLoad, TTMemory, TTObjs, TTProfile,
TTRASTER, TTTables, TTTypes, UTF8Process, HTML2TextRender, Laz_AVL_Tree, TTRASTER, TTTables, TTTypes, UTF8Process, HTML2TextRender, Laz_AVL_Tree,
compwriterpas, LazarusPackageIntf; CompWriterPas, LazPasReadUtil, LazarusPackageIntf;
implementation implementation