Serialize/stream (read/write) configuration settings
Store application settings in a Delphi class and serialize it binary to a file
Code submitted by Jens borrisholt. Text by Zarko Gajic.
Learn how to save published properties of a class into a stream/file (binary serialization) and read back. Use it to store Application Specific configuration data.
While using INI files to store Application Specific configuration data is a piece of cake in Delphi, INI files are simple text files-and everyone cocould mess up the information inside using a simple notepad.
Have a custom object for application configuration
Your (simple) application can have a custom class "tsettings" exposing properties and methods to help you manage your application on a more OOP level.
The properties of the settings class cocould hold information like form position, location of the database, most recently used resources or anything else that is required for your application to work ..
Serializing/streaming a class
The "settingsu" unit holds a custom tsettings class exposing 3 properties: propertyint (integer value), propertystring (string value) and propertydate (tdatetime value ).
The tcustomsettings as a base class defines 4 streaming/serializing methods to save the object and load the object from/to a stream or a file.
Savetostream and loadfromstream uses Delphi's rtti along with the treader and twiter class to save any published properties of the tcustomset=descendant.
Unit settingsu;
Interface
Uses classes;
{$ M +}
Type
Tcustomsettings = Class
Public
Procedure loadfromstream (const stream: tstream );
Procedure loadfromfile (const filename: string );
Procedure savetostream (const stream: tstream );
Procedure savetofile (const filename: string );
End;
Tsettings = Class (tcustomsettings)
Private
Fpropertystring: string;
Fpropertydate: tdatetime;
Fpropertyint: integer;
Published
Property propertyint: integer read fpropertyint write fpropertyint;
Property propertystring: String read fpropertystring write fpropertystring;
Property propertydate: tdatetime read fpropertydate write fpropertydate;
End;
VaR
Settings: tsettings;
Implementation
Uses typinfo, sysutils;
{Tsettings}
Procedure tcustomsettings. loadfromfile (const filename: string );
VaR
Stream: tstream;
Begin
Stream: = tfilestream. Create (filename, fmopenread or fmsharedenywrite );
Try
Loadfromstream (Stream );
Finally
Stream. Free;
End;
End;
Procedure tcustomsettings. loadfromstream (const stream: tstream );
VaR
Reader: treader;
Propname, propvalue: string;
Begin
Reader: = Treader. Create (stream, $ fff );
Stream. Position: = 0;
Reader. readlistbegin;
While not reader. endoflist do
Begin
Propname: = reader. readstring;
Propvalue: = reader. readstring;
Setpropvalue (self, propname, propvalue );
End;
Freeandnil (Reader );
End;
Procedure tcustomsettings. savetofile (const filename: string );
VaR
Stream: tstream;
Begin
Stream: = tfilestream. Create (filename, fmcreate );
Try
Savetostream (Stream );
Finally
Stream. Free;
End;
End;
Procedure tcustomsettings. savetostream (const stream: tstream );
VaR
Propname, propvalue: string;
CNT: integer;
Lpropinfo: ppropinfo;
Lpropcount: integer;
Lproplist: pproplist;
Lproptype: pptypeinfo;
Writer: twriter;
Begin
Lpropcount: = getproplist (ptypeinfo (classinfo), lproplist );
Writer: = twriter. Create (stream, $ fff );
Stream. Size: = 0;
Writer. writelistbegin;
For CNT: = 0 to lpropcount-1 do
Begin
Lpropinfo: = lproplist ^ [CNT];
Lproptype: = lpropinfo ^. proptype;
If lproptype ^. Kind = tkmethod then continue;
Propname: = lpropinfo. Name;
Propvalue: = getpropvalue (self, lpropinfo );
Writer. writestring (propname );
Writer. writestring (propvalue );
End;
Writer. writelistend;
Freeandnil (writer );
End;
Initialization
Settings: = tsettings. Create;
Finalization
Freeandnil (settings );
End.
Using the tsettings class is easy:
Uses settingsu;
...
Begin
Settings. propertyint: = 1;
Settings. propertystring: = 'string ';
Settings. propertydate: = now;
Showmessage ('settings. property2: '+ settings. propertystring );
// Save the object
Settings. savetofile ('settings. dmp ');
Settings. propertyint: = 2;
Settings. propertystring: = 'string2 ';
Settings. propertydate: = now + 200;
Showmessage ('settings. property2: '+ settings. propertystring );
// Now load saved state
Settings. loadfromfile ('settings. dmp ');
Showmessage ('settings. property2: '+ settings. propertystring );
End;
Note: The sample code above uses "settings. DMP "-but you can name the File whatever you want and give it any extension you like (just make sure not use registered/well known extensions (like PDF, doc, XML or similar ). you can use, for example, "MyApp. settings ".
Therefore you only have to call two methods: savetofile to save your configuration class and loadfromfile to load the configuration back when needed (application startup ).
Note that, in order to use your settings, you need to derive your class from the provided tsettings-and make sure that you place all the properties you want to stream in the published section-only published properties will be searialized.