72 lines
1.2 KiB
ObjectPascal
72 lines
1.2 KiB
ObjectPascal
|
unit demo_stream;
|
||
|
|
||
|
{$mode objfpc}{$H+}
|
||
|
|
||
|
interface
|
||
|
|
||
|
uses
|
||
|
Classes, SysUtils;
|
||
|
|
||
|
type
|
||
|
Ts20 = string[20];
|
||
|
|
||
|
TdemoRec = record
|
||
|
aChar: Char;
|
||
|
anInt: integer;
|
||
|
aString: Ts20;
|
||
|
aDate: TDateTime;
|
||
|
end;
|
||
|
|
||
|
{ TdemoStream }
|
||
|
|
||
|
TdemoStream = class(TMemoryStream)
|
||
|
private
|
||
|
function GenerateRandomRec(num: integer): TdemoRec;
|
||
|
public
|
||
|
rec: TdemoRec;
|
||
|
constructor Create(numRecords: integer);
|
||
|
function RecAsString: string;
|
||
|
end;
|
||
|
|
||
|
implementation
|
||
|
|
||
|
uses math;
|
||
|
|
||
|
{ TdemoStream }
|
||
|
|
||
|
function TdemoStream.GenerateRandomRec(num: integer): TdemoRec;
|
||
|
begin
|
||
|
Result.aChar := Chr(65 + Random(26));
|
||
|
Result.aDate := TDateTime(RandomRange(100, trunc(Now)) + Random/10000);
|
||
|
Result.anInt := num;
|
||
|
Result.aString := Format('example string %d', [num]);
|
||
|
end;
|
||
|
|
||
|
constructor TdemoStream.Create(numRecords: integer);
|
||
|
var i: integer;
|
||
|
begin
|
||
|
// Initialize random number generator
|
||
|
Randomize;
|
||
|
|
||
|
// Call the inherited constructor
|
||
|
inherited Create;
|
||
|
|
||
|
|
||
|
Position := 0;
|
||
|
|
||
|
for i := 0 to numRecords-1 do
|
||
|
Write(GenerateRandomRec(i), SizeOf(TdemoRec));
|
||
|
end;
|
||
|
|
||
|
function TdemoStream.RecAsString: string;
|
||
|
begin
|
||
|
Read(rec, SizeOf(rec));
|
||
|
Result := Format(
|
||
|
'%5d %s %17s %s',
|
||
|
[rec.anInt, rec.aChar, rec.aString, FormatDateTime('dd-mm-yyyy:mm:ss', rec.aDate)]
|
||
|
);
|
||
|
end;
|
||
|
|
||
|
end.
|
||
|
|