1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
TMyData = Class( TComponent )
PUBLIC
FieldInteger: Integer;
FieldString: String;
FieldCurr: Currency;
StringList: TStringList;
PUBLISHED
property A: Integer read FieldInteger write FieldInteger;
property B: String read FieldString write FieldString;
property C: Currency read FieldCurr write FieldCurr;
property D: TStringList read StringList write StringList;
end;
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
d : TMyData;
s : TFileStream;
i : Integer;
x : Integer;
y : Integer;
begin
s := TFileStream.Create( './properties.dat', fmCreate );
d := TMyData.Create(nil);
d.FieldInteger := 1969;
d.FieldString := 'Eleonore ma chaine.';
d.FieldCurr := 123456.789;
d.StringList := TStringList.Create;
for i := 1 to 36826 do
d.StringList.Add( StringOfChar( Chr(64+Random(26)), 1+Random(10) ) );
s.WriteComponent(d);
// Taille des données brutes
x := 0;
for i := 0 to d.StringList.Count-1 do
x := x + Length( d.StringList[i] ) + 1; // +1 = Il faut bien compter un séparateur OU une longueur
x := x + Length( d.FieldString ) + 1;
x := x + SizeOf( d.FieldInteger );
x := x + SizeOf( d.FieldCurr );
FreeAndNil(s);
FreeAndNil(d);
// Taille du fichier final
y := FileSize( './properties.dat' );
ShowMessage( Format( 'Inflation: %d octets , %2.2f %%', [y-x,100-((x/y)*100)] ) );
end;
procedure TForm1.Button2Click(Sender: TObject);
var
d : TMyData;
s : TFileStream;
c : TMyData;
begin
Try
s := TFileStream.Create( './properties.dat', fmOpenRead );
d := TMyData.Create(nil);
d.StringList := TStringList.Create;
s.ReadComponent(d);
ShowMessage( Format( '| %s | %d | %d | %m |', [d.FieldString,d.FieldInteger,d.StringList.Count,d.FieldCurr] ) + #13 + d.StringList.Text );
Try
c := TMyData.Create(nil);
c.StringList := TStringList.Create;
// CRASH !
s.ReadComponent(c);
Except
s.Position := 0;
// OK !
s.ReadComponent(c);
// La preuve !
ShowMessage( Format( '| %s | %d | %d | %m |', [c.FieldString,c.FieldInteger,c.StringList.Count,c.FieldCurr] ) + #13 + c.StringList.Text );
end;
Finally
FreeAndNil(s);
FreeAndNil(d);
FreeAndNil(c);
end;
end;
end. |
Partager