/* Deal with text INI files like smb.conf or WIN.INI */ /* Available functions: */ /* TRSetIni: Set/update value in a text INI file */ /* More to be added */ /*:VRX */ TRSetIni: procedure expose TempDir/* Set value in a text INI file */ /* Syntax: */ /* ok = TRSetIni("[Section]","Key", "Value", "File") */ /* returns: */ /* 1 = success */ /* 0 = failure */ /* Check section, make sure it has trailing brackets */ SetSect = strip(arg(1)) if left(SetSect,1) <> '[' then SetSect = '['||SetSect if right(SetSect,1) <> ']' then SetSect = SetSect||']' USetSect = translate(SetSect) /* Key */ SetVoc = strip(arg(2)) USetVoc = translate(SetVoc) /* Value */ SetVal = arg(3) /* INI file */ TINIfile = arg(4) /* Temporary work file, FAT compliant */ if TempDir = "TEMPDIR" then TempDir = "" tempINI = SysTempFileName(TempDir||"tempINI.???") ok = SysFileDelete(tempINI) /* Initialize variables */ TINI.0 = 0 I = 0 FoundSetSect = 0 DidUpdate = 0 /* Read TINIfile into stem and update if the key already exists */ do until lines(TINIfile) = 0 I = I + 1 TINI.I = strip(linein(TINIfile)) if TINI.I = "" then do I = I - 1 iterate end if left(TINI.I,1) = '[' then do if FoundSetSect = 1 & DidUpdate = 0 then do /* We are through with the section, but our key was not there - let's add it */ J = I I = I + 1 TINI.I = TINI.J TINI.J = '09'x||setvoc' = 'setval DidUpdate = 1 end CurSection = translate(TINI.I) end if CurSection = USetSect then FoundSetSect = 1 parse var TINI.I voc '=' val ovoc = strip(voc) voc = strip(translate(voc),,'09'x) voc = strip(voc) if voc = usetvoc & FoundSetSect then do DidUpdate = 1 TINI.I = ovoc' = 'setval end end ok = stream(TINIfile,'c','close') if FoundSetSect = 1 & DidUpdate = 0 then do /* We are through with the file, still in our section, but the key was not there - let's add it */ I = I + 1 TINI.I = '09'x||setvoc' = 'setval DidUpdate = 1 end if FoundSetSect = 0 & DidUpdate = 0 then do /* We are through with the file, neither our section nor our key were there - let's add a new section with our key */ I = I + 1 TINI.I = SetSect I = I + 1 TINI.I = '09'x||setvoc' = 'setval DidUpdate = 1 end /* Create a new file */ TINI.0 = I do I = 1 to TINI.0 if left(TINI.I,1) = '[' & I > 1 then do J = I - 1 if left(TINI.J,1) <> '#' then call charout tempINI, '0D0A'x end call lineout tempINI, TINI.I end ok = stream(tempINI,'c','close') /* Update original and delete temporary */ Success = VRCopyFile(tempINI,TINIfile) if Success then ok = SysFileDelete(tempINI) return Success