Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBC] Using INI Files The 'Delphi' Way (No DLL)

Author
Message
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 19th May 2007 05:29 Edited at: 19th May 2007 05:36
Reading/Writing To INI Files

For DB Classic Enhanced & Un-Enhanced Versions


I know there are DLL's for DBPro to do this already, but I thought I'd do something for those of you using the non-enhanced version of DBC - where you can't call DLL's.

When you see how easy it is to use, you might like to give it a go even if you can use DLL's!

Anyway, as a Delphi programmer I think the method Delphi uses to do this is quite nice so I decided to emulate it - hence the reason the functions are coded a bit strangely. I doubt creating and modifying INI files could be any easier than this though...

For anyone who doesn't know, an INI file is basically a text file that your programs can write data to and read from. This data can be scores, setup info, window positions - anything... as long as it's a string,an integer or a float.

So, here's the very first version of TDK_Man's INI File Functions - as there's probably room for improvement!

Essentially, there are seven functions in the set.

Creating The INI File

INI_Create()

This creates an INI file. The only parameter is the INI filename - for example:

INI_Create("MyINIFile.INI")

This creates the INI file only if one doesn't already exist. It does nothing if one with the same name does already exist, so you should always use it before using any of the other functions.


Writing Data To The INI File

There are three functions for writing:

INI_WriteString()
INI_WriteInteger()
INI_WriteFloat()

These are for writing Strings, Integers and Floats and each one has four parameters. Eg:

INI_WriteString(INIFilename$,SectionName$,EntryName$,DataItem$)
INI_WriteInteger(INIFilename$,SectionName$,EntryName$,DataItem)
INI_WriteFloat(INIFilename$,SectionName$,EntryName$,DataItem#)

INIFilename$ is the name of the INI file (the same as that used in the INI_Create() function).

SectionName$ is the section name in the INI file. If you use 'General' for this parameter, then a section will appear in the INI file like so:

[General]

EntryName$ and DataItem$/DataItem/DataItem#

These are the entry name and data items. The data items have to be the correct type depending on the function and can be string, integer or float.

So, when using INI_WriteString() if you have 'Name' for the entry name and 'Fred' for DataItem$ you get a line in the INI file like this:

Name=Fred

The function call for the whole of this would simply be:

INI_WriteString("MyINIFile.INI","General","Name","Fred")

Note: Obviously, you can use variables instead of literals - as long as they are of the correct type.

To add an integer entry called 'Player Health' to the General Section of the INI with a value of 50 you would use:

INI_WriteInteger("MyINIFile.INI","General","Player Health",50)

Remember, if you use anything other than 'General' for the section name, then another section will be added with that name and the data placed into that section.

Pretty easy so far eh?


Reading Data From The INI File

There are three functions for reading:

INI_ReadString()
INI_ReadInteger()
INI_ReadFloat()

These are for reading Strings, Integers and Floats and each one has four parameters. Eg:

INI_ReadString(INIFilename$,SectionName$,EntryName$,Default$)
INI_ReadInteger(INIFilename$,SectionName$,EntryName$,Default)
INI_ReadFloat(INIFilename$,SectionName$,EntryName$,Default#)

The first three parameters are identical to the INI_Write functions, but the fourth is a little different.

As you can see it's called 'Default', (but for the respective data types).

The reason for this is that you may want to add something to your program at a later date, and adding more read INI lines to your program can be a problem when users already have an INI file - without the corresponding data entries your new program will try to read in.

The Default parameter, used when reading solves this problem.

Essentially, when you use the INI_Read functions, if the supplied Section and/or Data Entry exists then the stored data is read in. If they don't exist, then the default value you supply is used and the entry is added to the INI file.

Without it, the load process would fall over!

The next time the functions are used, the entry will be present and will be loaded instead of the default value. Cool eh?

So, to load the camera's X position in from the Camera section of the INI file we would simply use:

CamPosX# = INI_ReadFloat("MyINIFile.INI","Camera","X Position",2500.0)

If an 'X Position' entry exists in a 'Camera' section, then the float value there would be loaded into CamPosX#. If it didn't, then that section and entry would be created and CamPosX# would be set to the supplied default float value of 2500.0.

There's an attached zip including the functions file and a couple of basic demos - one for writing and one for reading.

If your program uses the functions, you must remember to have the following line at the start:

#Include "INIFiles.dba"

along with the two array DIMS.

And that's it! Your comments welcome on approximately three hours of work!

TDK_Man

Attachments

Login to view attachments
Jack
19
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 19th May 2007 05:39
This functions are really useful.
Nice indeed

[/center]
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 19th May 2007 13:35
Nice, I was thinking of doing something a little similar, but slightly different too lol

TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 19th May 2007 18:45
Here's an example of using all the INI functions in your own programs:



Just make sure that the file INIFIles.dba is in the same folder as your project DBA file.

You don't have to put the read lines in the same order as the write lines either.

Finally, you can have procedures that read from and write to different parts of the same file - just create different sections.

TDK_Man

Welder1
17
Years of Service
User Offline
Joined: 28th Jan 2007
Location: Illinois
Posted: 22nd Aug 2007 01:11
Very usefull. Thanks for sharing this!!

Login to post a reply

Server time is: 2024-05-09 07:12:15
Your offset time is: 2024-05-09 07:12:15