CIniFile: Difference between revisions
m
Sets _not_ Gets!
m (added structure) |
m (Sets _not_ Gets!) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
In [[Visual DataFlex]] you can freely use as many INI files as you require, however the two-level structure of INI files (sections and keys) means that there is often no need to use more than one. Here, for example, is a boot.ini file (usually found in the root directory of the system drive of Windows operating systems): | In [[Visual DataFlex]] you can freely use as many INI files as you require, however the two-level structure of INI files (sections and keys) means that there is often no need to use more than one. Here, for example, is a boot.ini file (usually found in the root directory of the system drive of Windows operating systems): | ||
<source lang="ini"> | |||
[boot loader] | |||
timeout=30 | |||
default=multi(0)disk(0)rdisk(0)partition(2)\WINDOWS | |||
[operating systems] | |||
multi(0)disk(0)rdisk(0)partition(2)\WINDOWS="Microsoft Windows XP Professional" /fastdetect /NoExecute=OptIn | |||
</source> | |||
As you can see, it defines two sections - [boot loader] and [operating systems] - each with different settings within them. | As you can see, it defines two sections - [boot loader] and [operating systems] - each with different settings within them. | ||
==Visual DataFlex Workspace files== | ==Visual DataFlex Workspace files== | ||
Another example are the workspace configuration files used by [[Visual DataFlex]] itself (by default named "Config.ws", showing that they don't have to use the .ini extension - see the | Another example are the workspace configuration files used by [[Visual DataFlex]] itself (by default named "Config.ws", showing that they don't have to use the .ini extension - see the article in "See Also" below), which must exist in the same directory the program is run from (usually the "Programs" subdirectory of the workspace). Here is an example: | ||
<source lang="ini"> | |||
[Workspace] | |||
Home=..\ | |||
AppSrcPath=.\AppSrc | |||
AppHTMLPath=.\AppHtml | |||
BitmapPath=.\Bitmaps | |||
IdeSrcPath=.\IdeSrc | |||
DataPath=.\Data | |||
DDSrcPath=.\DdSrc | |||
HelpPath=.\Help | |||
ProgramPath=.\Programs | |||
FileList=.\Data\Filelist.cfg | |||
Description=MyFirstProject | |||
</source> | |||
Here only a single section - [Workspace] - is defined, however the fact that an INI file can have as many sections as it needs means that you can use this very file to set up options for how you want your program to behave. | Here only a single section - [Workspace] - is defined, however the fact that an INI file can have as many sections as it needs means that you can use this very file to set up options for how you want your program to behave. | ||
Line 40: | Line 44: | ||
Here is an example where an additional section allowing the configuration of a database location and login has been added: | Here is an example where an additional section allowing the configuration of a database location and login has been added: | ||
<source lang="ini"> | |||
[Workspace] | |||
Home=..\ | |||
AppSrcPath=.\AppSrc | |||
AppHTMLPath=.\AppHtml | |||
BitmapPath=.\Bitmaps | |||
IdeSrcPath=.\IdeSrc | |||
DataPath=.\Data | |||
DDSrcPath=.\DdSrc | |||
HelpPath=.\Help | |||
ProgramPath=.\Programs | |||
FileList=.\Data\Filelist.cfg | |||
Description=MyFirstProject | |||
[Database Login] | |||
Server=big-iron.database-servers.local | |||
Login=itsme | |||
Password=letmein | |||
Database=Accounting | |||
Driver=MSSQLDRV | |||
</source> | |||
Combining these factors - the fact that there is probably no need to have more than one INI file, and the fact that [[Visual DataFlex]] requres one anyway - means that for most purposes you can just add what you need to the existing workspace configuration file. | Combining these factors - the fact that there is probably no need to have more than one INI file, and the fact that [[Visual DataFlex]] requres one anyway - means that for most purposes you can just add what you need to the existing workspace configuration file. | ||
Line 71: | Line 76: | ||
Here is one way of doing it ('''''note''' - this code is assumed to be in the oApplication object or a sub-class of the cApplication class; the only place this matters is in the "Set psFileName ..." line, where "Self" will refer to the oApplication object, and where the various properties are held''): | Here is one way of doing it ('''''note''' - this code is assumed to be in the oApplication object or a sub-class of the cApplication class; the only place this matters is in the "Set psFileName ..." line, where "Self" will refer to the oApplication object, and where the various properties are held''): | ||
<source lang="vdf"> | |||
String sServer sLogin sPW sDrv sDB | |||
Boolean bLogin | |||
Handle hoIni | |||
// Retrieve your application defaults into local variables | |||
Get psServer to sServer | |||
Get psLogin to sLogin | |||
Get psPassword to sPW | |||
Get psDriver to sDrv | |||
Get psDatabase to sDB | |||
Get pbDoLogin to bDB | |||
Get Create U_cIniFile to hoIni // Create INI file object | |||
// Make it use the workspace configuration file | |||
Set psFileName of hoIni to (psWorkspaceWSFile(phoWorkspace(Self))) | |||
// Read the information | |||
Move (ReadString(hoIni, "Database Login", "Server", sServer)) to sServer | |||
Move (ReadString(hoIni, "Database Login", "Login", sLogin)) to sLogin | |||
Move (ReadString(hoIni, "Database Login", "Password", sPW)) to sPW | |||
Move (ReadString(hoIni, "Database Login", "Driver", sDrv)) to sDrv | |||
Move (ReadString(hoIni, "Database Login", "Database", sDB)) to sDB | |||
Move (ReadString(hoIni, "Database Login", "DoLogin", bLogin)) to bLogin | |||
Send Destroy of hoIni // And Destroy it again | |||
// Set the properties to make the information persistent | |||
Set psServer to sServer | |||
Set psLogin to sLogin | |||
Set psPassword to sPW | |||
Set psDriver to sDrv | |||
Set psDatabase to sDB | |||
Set pbDoLogin to bDB | |||
</source> | |||
The above code will retrieve the values set in the second workspace file example above. Note that in that example, the "DoLogin" value is not present - the "''ReadString''" method of the cIniFile class lets you supply a default value to use if the setting is not present, and here we have used the bLogin variable, which was set from the pbDoLogin property, and which is then used to set that property again... so the fact that the setting is absent (not "''missing''" - it is deliberate!) means that the default property remains unchanged. | The above code will retrieve the values set in the second workspace file example above. Note that in that example, the "DoLogin" value is not present - the "''ReadString''" method of the cIniFile class lets you supply a default value to use if the setting is not present, and here we have used the bLogin variable, which was set from the pbDoLogin property, and which is then used to set that property again... so the fact that the setting is absent (not "''missing''" - it is deliberate!) means that the default property remains unchanged. | ||
Line 110: | Line 117: | ||
You could save some code by not retrieving the properties into local variables first, but using the properties as defaults directly: | You could save some code by not retrieving the properties into local variables first, but using the properties as defaults directly: | ||
<source lang="dataflex"> | |||
Move (ReadString(hoIni, "Database Login", "Server", psServer(Self))) to sServer | |||
Move (ReadString(hoIni, "Database Login", "Login", psLogin(Self))) to sLogin | |||
Move (ReadString(hoIni, "Database Login", "Password", psPassword(Self))) to sPW | |||
Move (ReadString(hoIni, "Database Login", "Driver", psDriver(Self))) to sDrv | |||
Move (ReadString(hoIni, "Database Login", "Database", psDatabase(Self))) to sDB | |||
Move (ReadString(hoIni, "Database Login", "DoLogin", pbDoLogin(Self))) to bLogin | |||
</source> | |||
Or even dispense with the local variables completely: | Or even dispense with the local variables completely: | ||
<source lang="dataflex"> | |||
Set psServer to (ReadString(hoIni, "Database Login", "Server", psServer(Self))) | |||
Set psLogin to (ReadString(hoIni, "Database Login", "Login", psLogin(Self))) | |||
Set psPassword to (ReadString(hoIni, "Database Login", "Password", psPassword(Self))) | |||
Set psDriver to (ReadString(hoIni, "Database Login", "Driver", psDriver(Self))) | |||
Set psDatabase to (ReadString(hoIni, "Database Login", "Database", psDatabase(Self))) | |||
Set pbDoLogin to (ReadString(hoIni, "Database Login", "DoLogin", pbDoLogin(Self))) | |||
</source> | |||
But some might consider that a bridge too far! | But some might consider that a bridge too far! |