Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for readini.rex

(view source code of readini.rex as plain text)

  1. /* Check command line arguments */
  2. Parse Arg ini section key dummy
  3. If dummy <> "" Then Call Syntax
  4. If key   =  "" Then Call Syntax
  5. ucsection = Translate( Strip( section, "B", '"' ) )
  6. uckey     = Translate( Strip( key, "B", '"' ) )
  7.  
  8. /* Initialize RexxUtil */
  9. Call RxFuncAdd "sysloadfuncs", "RexxUtil", "sysloadfuncs"
  10. Call sysloadfuncs
  11.  
  12. /* Check if INI file exists */
  13. Call sysfiletree ini, "file.", "FO"
  14. If file.0 <> 1 Then Do
  15. 	Say
  16. 	Say "File not found!"
  17. 	Call Syntax
  18. End
  19.  
  20. /* Reset temporary variables */
  21. sectok    = 0
  22. sectfound = 0
  23. keyfound  = 0
  24.  
  25. /* Search the INI file line by line */
  26. Call LineIn ini, 1, 0
  27. Do Until Lines( ini ) = 0
  28. 	line   = LineIn( ini )
  29. 	ucline = Translate( line )
  30. 	Select
  31. 		When Pos( "["||ucsection||"]", ucline ) >  0  Then Do
  32. 			sectok    = 1
  33. 			sectfound = 1
  34. 		End
  35. 		When SubStr( Strip( line ), 1, 1 )  = "[" Then sectok = 0
  36. 		When Pos( "=", line ) > 0 Then Do
  37. 			If sectok = 0 Then Iterate
  38. 			Parse Value line With thiskey"="thisvalue
  39. 			thiskey = Translate( Strip( Strip( thiskey, "B", '"' ) ) )
  40. 			If thiskey = uckey Then Do
  41. 				keyfound  = 1
  42. 				thisvalue = Strip( Strip( thisvalue, "B", '"' ) )
  43. 				Say
  44. 				Say '"'||Strip( ini, "B", '"' )||'"'
  45. 				Say "["||section||"]"
  46. 				Say key||"="||thisvalue
  47. 				Exit 0
  48. 			End
  49. 		End
  50. 		Otherwise Nop
  51. 	End
  52. End
  53.  
  54. /* If you reached this part of the code the key wasn't found */
  55. Say
  56.  
  57. /* The section does not exist */
  58. If sectfound = 0 Then Do
  59. 	Say "Section not found!"
  60. 	Exit 2
  61. End
  62.  
  63. /* The section was found but the key does not exist */
  64. If keyfound = 0 Then Do
  65. 	Say "Key not found!"
  66. 	Exit 3
  67. End
  68.  
  69. /* Key was found but value was empty */
  70. Exit 0
  71.  
  72. Syntax:
  73. 	Say
  74. 	Say "ReadINI.rex,  Version 1.00"
  75. 	Say "Read a value from the specified INI file"
  76. 	Say
  77. 	Say 'Usage:  <REXX>  READINI.REXX  "ini_file"  "section"  "key"'
  78. 	Say
  79. 	Say 'Where:  "<REXX>"   is your Rexx interpreter:'
  80. 	Say "                   - Windows:  REGINA.EXE or REXX.EXE,"
  81. 	Say "                   - OS/2:     don't specify, just rename script to *.cmd"
  82. 	Say '        "ini_file" is the file name of the INI file to be read'
  83. 	Say '        "section"  is the section name, without the brackets'
  84. 	Say '        "key"      is the key whose value must be read'
  85. 	Say
  86. 	Say "Example: if MYPROG.INI looks like this:"
  87. 	Say "[Section 1]"
  88. 	Say "Key1=Value 1"
  89. 	Say "[Section 2]"
  90. 	Say "Key2=Value 2"
  91. 	Say
  92. 	Say 'Then the command: READINI.REX "MYPROG.INI" "section 2" "key2"'
  93. 	Say "should return:    Value 2"
  94. 	Say
  95. 	Say "Written by Rob van der Woude"
  96. 	Say "http://www.robvanderwoude.com"
  97. 	Exit 1
  98. Return
  99.  

page last modified: 2024-02-26; loaded in 0.0171 seconds