(view source code of readini.rex as plain text)
/* Check command line arguments */
Parse Arg ini section key dummy
If dummy <> "" Then Call Syntax
If key = "" Then Call Syntax
ucsection = Translate( Strip( section, "B", '"' ) )
uckey = Translate( Strip( key, "B", '"' ) )
/* Initialize RexxUtil */
Call RxFuncAdd "sysloadfuncs", "RexxUtil", "sysloadfuncs"
Call sysloadfuncs
/* Check if INI file exists */
Call sysfiletree ini, "file.", "FO"
If file.0 <> 1 Then Do
Say
Say "File not found!"
Call Syntax
End
/* Reset temporary variables */
sectok = 0
sectfound = 0
keyfound = 0
/* Search the INI file line by line */
Call LineIn ini, 1, 0
Do Until Lines( ini ) = 0
line = LineIn( ini )
ucline = Translate( line )
Select
When Pos( "["||ucsection||"]", ucline ) > 0 Then Do
sectok = 1
sectfound = 1
End
When SubStr( Strip( line ), 1, 1 ) = "[" Then sectok = 0
When Pos( "=", line ) > 0 Then Do
If sectok = 0 Then Iterate
Parse Value line With thiskey"="thisvalue
thiskey = Translate( Strip( Strip( thiskey, "B", '"' ) ) )
If thiskey = uckey Then Do
keyfound = 1
thisvalue = Strip( Strip( thisvalue, "B", '"' ) )
Say
Say '"'||Strip( ini, "B", '"' )||'"'
Say "["||section||"]"
Say key||"="||thisvalue
Exit 0
End
End
Otherwise Nop
End
End
/* If you reached this part of the code the key wasn't found */
Say
/* The section does not exist */
If sectfound = 0 Then Do
Say "Section not found!"
Exit 2
End
/* The section was found but the key does not exist */
If keyfound = 0 Then Do
Say "Key not found!"
Exit 3
End
/* Key was found but value was empty */
Exit 0
Syntax:
Say
Say "ReadINI.rex, Version 1.00"
Say "Read a value from the specified INI file"
Say
Say 'Usage: <REXX> READINI.REXX "ini_file" "section" "key"'
Say
Say 'Where: "<REXX>" is your Rexx interpreter:'
Say " - Windows: REGINA.EXE or REXX.EXE,"
Say " - OS/2: don't specify, just rename script to *.cmd"
Say ' "ini_file" is the file name of the INI file to be read'
Say ' "section" is the section name, without the brackets'
Say ' "key" is the key whose value must be read'
Say
Say "Example: if MYPROG.INI looks like this:"
Say "[Section 1]"
Say "Key1=Value 1"
Say "[Section 2]"
Say "Key2=Value 2"
Say
Say 'Then the command: READINI.REX "MYPROG.INI" "section 2" "key2"'
Say "should return: Value 2"
Say
Say "Written by Rob van der Woude"
Say "http://www.robvanderwoude.com"
Exit 1
Return
page last modified: 2024-04-16; loaded in 0.0056 seconds