Contributed by: Doug Rickman
This is a generic subroutine to handle initiallization and initial loading of an ini file under OS/2. The routine is intended to simplify code reuse. For example by setting the name of the program and the name of the ini file to variables set in the calling routine the entire subroutine can be copied from program to program and only the names of the keys and their default values need editing. The key names and the defaults are in an easy to edit form and location. Chores such as detecting a missing key or some defects in an existing ini are handled.
/* INISetup - subroutine for .ini set up and configuration. */
/* To use edit the variable KeysList, adding key names and default values. */
/* This routine will do the following: */
/* see if the necessary ini exists */
/* if not it will be created, */
/* if it does exist it will be checked to make sure all the necessary */
/* keys are present, */
/* missing keys will be created, */
/* all of the keys are loaded into memory. */
/* */
/* To add a key simply modify the variable KeyList and give default */
/* value for the new key. */
/* Format Usage: */
/* Numeric key values can not be quoted. */
/* Blanks are not permitted in keys. */
/* Keys and values are delimited by the string ';' */
/* */
/* If INISetup detects a key which does not match the list in KeysList it */
/* will automatically backup the existing ini and create a new file with the */
/* defaults. */
/* */
/* INISetup will load into memory all the values of all keys using variable */
/* names equal to the names of the keys. */
/* */
/* It is assumed the calling routine will have set the variables "MasterM" */
/* and "INIFile". MasterM is the name of the program, such as "Import" and */
/* INIFile is the name of the ini file to be used. I use two lines of code */
/* in the main routine of all my programs such as: */
/* MasterM='Import' */
/* INIFile=MasterM'.ini' */
/* This way I can set the name of the program once and simply copy blocks of */
/* code between programs and not worry about editing the sysini() calls. */
/* */
/* I use two calls to the REXXLIB library, dosdel() and doscopy(). The */
/* former deletes a file and the latter copies a file. The functionality */
/* can be obtained by other mechanisms if you don't have REXXLIB. */
/* REXXLIB is a product of Quercus, http://www.quercus-sys.com/ */
/* */
/* Doug Rickman Jan 21, 2000 */
/* List the keys and default values that shall exist in the ini file. */
KeysList= 'InputFile' "*.*" ';',
'OutputFile' '*.elas' ';',
'InitialElement' 0 ';',
'LastElement' 0 ';',
'InitialLine' 0 ';',
'LastLine' 0 ';',
'OutputFormat' "FLAT_BIL" ';',
'ByteOrder' "SGI" ';',
'PrintHeader' "YES" ';',
'ChannelsList' "1 2 3" ';'
do i = 1
parse var KeysList MasterKeysList.i MasterKeysValues.i ';' KeysList
MasterKeysList.i = strip(MasterKeysList.i)
MasterKeysValues.i = strip(MasterKeysValues.i)
if KeysList='' then leave i
end i
MasterKeysList.0=i
/* does INI exist? */
rc = syssearchpath('..',INIFile)
if rc = ' ' then do
/* ini file does not exist here, make one */
rc=CreateINI()
rc=sysini(INIFile,MasterM,'ALL:','IniKeysList.')
rc=LoadINIValues()
return 1
end
/* But if INI does exist .... */
/* First check to be sure ini contains values for all keys. */
rc=sysini(INIFile,MasterM,'ALL:','IniKeysList.')
select
/* Does the number of variables in IniKeysList.0 = MasterKeysList.0? */
when IniKeysList.0 = MasterKeysList.0 then do
CheckSum=0
do i = 1 to IniKeysList.0
do j = 1 to MasterKeysList.0
if IniKeysList.i = MasterKeysList.j then do
/* say 'Found ' i IniKeysList.i */
CheckSum=CheckSum+1
leave j
end
end j
end i
if CheckSum<>MasterKeysList.0 then do
/* Something is wrong with the ini. Backit up and create a new one. */
txt='Something is wrong with your existing INI file. I will back it',
'up as 'INIFile'.backup and create a new one.'
response=VpMessageBox(window,'Problems, problems, problems!',txt)
rc=stream(INIFile,'c' 'close')
rc=dosdel(INIFile'.backup')
rc=doscopy(INIFile,INIFile'.backup')
rc=dosdel(INIFile)
rc=CreateINI()
rc=sysini(INIFile,MasterM,'ALL:','IniKeysList.')
rc=LoadINIValues()
end /* if CheckSum<>MasterKeysList.0 then ... */
else nop /* The correct number and the correct keys are present. Situation A-OK. */
end /* when IniKeysList.0 = MasterKeysList.0 then ... */
when IniKeysList.0 > MasterKeysList.0 then do
/* Something is wrong with the ini. Backit up and create a new one. */
txt='Something is wrong with your existing INI file. I will back it',
'up as 'INIFile'.backup and create a new one.'
response=VpMessageBox(window,'Problems, problems, problems!',txt)
rc=stream(INIFile,'c' 'close')
rc=dosdel(INIFile'.backup')
rc=doscopy(INIFile,INIFile'.backup')
rc=dosdel(INIFile)
rc=CreateINI()
rc=sysini(INIFile,MasterM,'ALL:','IniKeysList.')
rc=LoadINIValues()
end
when IniKeysList.0 < MasterKeysList.0 then do
/* Missing one or more keys, find and add them. */
do j = 1 to MasterKeysList.0
Found='NO'
do i = 1 to IniKeysList.0
if IniKeysList.i = MasterKeysList.j then do
Found='YES'
leave i
end
end i
if Found='YES' then nop
else /* Create the key and give it a value. */
rc=sysini(INIFile,MasterM,MasterKeysList.j,MasterKeysValues.i)
end j
end /* when ... */
otherwise say 'How did I get to here in IniSetup on such a beautiful day? Doug goofed again.'
end /* select */
rc=LoadINIValues()
return 1
LoadINIValues:
/* Now place the ini values into memory. */
do i = 1 to IniKeysList.0
rc=value(IniKeysList.i,sysini(INIFile,MasterM,IniKeysList.i))
end i
in = InputFile /* Special case for my own needs, D. Rickman */
out = OutputFile /* Special case for my own needs, D. Rickman */
return 1
CreateINI:
do i = 1 to MasterKeysList.0
rc=sysini(INIFile,MasterM,MasterKeysList.i,MasterKeysValues.i)
end i
return 1