1
0
mirror of synced 2024-11-15 03:23:51 +00:00
x2xmldatabinding/Units/XMLDataBindingHelpers.pas

162 lines
3.4 KiB
ObjectPascal
Raw Normal View History

2008-02-20 06:52:00 +00:00
unit XMLDataBindingHelpers;
interface
uses
Classes;
type
TStreamHelper = class(TObject)
private
FOwnership: TStreamOwnership;
FStream: TStream;
public
constructor Create(AStream: TStream; AOwnership: TStreamOwnership = soReference);
destructor Destroy; override;
2008-02-20 06:52:00 +00:00
function ReadString(ASize: Integer = -1): String;
function ReadInteger: Integer;
function ReadDateTime: TDateTime;
function ReadBoolean: Boolean;
2008-02-20 06:52:00 +00:00
procedure Write(const ASource: String);
procedure WriteLn(const ASource: String = '');
2008-03-09 20:36:27 +00:00
procedure WriteFmt(const ASource: String; const AParams: array of const);
procedure WriteLnFmt(const ASource: String; const AParams: array of const);
procedure WriteNamedFmt(const ASource: String; const AParams: array of const);
procedure WriteLnNamedFmt(const ASource: String; const AParams: array of const);
2008-02-20 06:52:00 +00:00
procedure WriteString(const ASource: String);
procedure WriteInteger(const ASource: Integer);
procedure WriteDateTime(const ASource: TDateTime);
procedure WriteBoolean(const ASource: Boolean);
end;
implementation
uses
SysUtils,
X2UtNamedFormat;
2008-02-20 06:52:00 +00:00
{ TStreamHelper }
constructor TStreamHelper.Create(AStream: TStream; AOwnership: TStreamOwnership);
begin
FOwnership := AOwnership;
FStream := AStream;
inherited Create;
2008-02-20 06:52:00 +00:00
end;
destructor TStreamHelper.Destroy;
2008-02-20 06:52:00 +00:00
begin
if FOwnership = soOwned then
FreeAndNil(FStream);
inherited;
end;
function TStreamHelper.ReadString(ASize: Integer): String;
var
iSize: Integer;
begin
if ASize = -1 then
iSize := ReadInteger
2008-02-20 06:52:00 +00:00
else
iSize := ASize;
SetLength(Result, iSize);
FStream.Read(PChar(Result)^, iSize);
end;
function TStreamHelper.ReadInteger: Integer;
2008-02-20 06:52:00 +00:00
begin
FStream.Read(Result, SizeOf(Integer));
end;
function TStreamHelper.ReadDateTime: TDateTime;
2008-02-20 06:52:00 +00:00
begin
FStream.Read(Result, SizeOf(TDateTime));
end;
function TStreamHelper.ReadBoolean: Boolean;
2008-02-20 06:52:00 +00:00
begin
FStream.Read(Result, SizeOf(Boolean));
end;
procedure TStreamHelper.Write(const ASource: String);
begin
FStream.Write(PChar(ASource)^, Length(ASource));
end;
procedure TStreamHelper.WriteLn(const ASource: String);
begin
Write(ASource + #13#10);
end;
2008-03-09 20:36:27 +00:00
procedure TStreamHelper.WriteFmt(const ASource: String; const AParams: array of const);
begin
Write(Format(ASource, AParams));
end;
procedure TStreamHelper.WriteLnFmt(const ASource: String; const AParams: array of const);
begin
WriteLn(Format(ASource, AParams));
end;
procedure TStreamHelper.WriteNamedFmt(const ASource: String; const AParams: array of const);
begin
Write(NamedFormat(ASource, AParams));
end;
procedure TStreamHelper.WriteLnNamedFmt(const ASource: String; const AParams: array of const);
begin
WriteLn(NamedFormat(ASource, AParams));
end;
2008-02-20 06:52:00 +00:00
procedure TStreamHelper.WriteString(const ASource: String);
var
iSize: Integer;
begin
iSize := Length(ASource);
WriteInteger(iSize);
FStream.Write(PChar(ASource)^, iSize);
end;
procedure TStreamHelper.WriteInteger(const ASource: Integer);
begin
FStream.Write(ASource, SizeOf(Integer));
end;
procedure TStreamHelper.WriteDateTime(const ASource: TDateTime);
begin
FStream.Write(ASource, SizeOf(TDateTime));
end;
procedure TStreamHelper.WriteBoolean(const ASource: Boolean);
begin
FStream.Write(ASource, SizeOf(Boolean));
end;
end.
2008-03-09 20:36:27 +00:00