| 1 | /* Deal with text INI files like smb.conf or WIN.INI */ | 
|---|
| 2 |  | 
|---|
| 3 | /* Available functions: */ | 
|---|
| 4 | /*    TRSetIni: Set/update value in a text INI file  */ | 
|---|
| 5 | /* More to be added */ | 
|---|
| 6 |  | 
|---|
| 7 | /*:VRX */ | 
|---|
| 8 | TRSetIni: procedure expose TempDir/* Set value in a text INI file */ | 
|---|
| 9 | /* Syntax: */ | 
|---|
| 10 | /*    ok = TRSetIni("[Section]","Key", "Value", "File") */ | 
|---|
| 11 | /* returns:      */ | 
|---|
| 12 | /*   1 = success */ | 
|---|
| 13 | /*   0 = failure */ | 
|---|
| 14 |  | 
|---|
| 15 | /* Check section, make sure it has trailing brackets */ | 
|---|
| 16 | SetSect = strip(arg(1)) | 
|---|
| 17 | if left(SetSect,1) <> '[' then SetSect = '['||SetSect | 
|---|
| 18 | if right(SetSect,1) <> ']' then SetSect = SetSect||']' | 
|---|
| 19 | USetSect = translate(SetSect) | 
|---|
| 20 |  | 
|---|
| 21 | /* Key */ | 
|---|
| 22 | SetVoc  = strip(arg(2)) | 
|---|
| 23 | USetVoc = translate(SetVoc) | 
|---|
| 24 |  | 
|---|
| 25 | /* Value */ | 
|---|
| 26 | SetVal  = arg(3) | 
|---|
| 27 |  | 
|---|
| 28 | /* INI file */ | 
|---|
| 29 | TINIfile = arg(4) | 
|---|
| 30 |  | 
|---|
| 31 | /* Temporary work file, FAT compliant */ | 
|---|
| 32 | if TempDir = "TEMPDIR" then TempDir = "" | 
|---|
| 33 | tempINI = SysTempFileName(TempDir||"tempINI.???") | 
|---|
| 34 | ok = SysFileDelete(tempINI) | 
|---|
| 35 |  | 
|---|
| 36 | /* Initialize variables */ | 
|---|
| 37 | TINI.0 = 0 | 
|---|
| 38 | I = 0 | 
|---|
| 39 | FoundSetSect = 0 | 
|---|
| 40 | DidUpdate = 0 | 
|---|
| 41 |  | 
|---|
| 42 | /* Read TINIfile into stem and update if the key already exists */ | 
|---|
| 43 | do until lines(TINIfile) = 0 | 
|---|
| 44 | I = I + 1 | 
|---|
| 45 | TINI.I = strip(linein(TINIfile)) | 
|---|
| 46 | if TINI.I = "" then do | 
|---|
| 47 | I = I - 1 | 
|---|
| 48 | iterate | 
|---|
| 49 | end | 
|---|
| 50 | if left(TINI.I,1) = '[' then do | 
|---|
| 51 | if FoundSetSect = 1 & DidUpdate = 0 then do | 
|---|
| 52 | /* We are through with the section, but our key was | 
|---|
| 53 | not there - let's add it */ | 
|---|
| 54 | J = I | 
|---|
| 55 | I = I + 1 | 
|---|
| 56 | TINI.I = TINI.J | 
|---|
| 57 | TINI.J = '09'x||setvoc' = 'setval | 
|---|
| 58 | DidUpdate = 1 | 
|---|
| 59 | end | 
|---|
| 60 | CurSection = translate(TINI.I) | 
|---|
| 61 | end | 
|---|
| 62 | if CurSection = USetSect then FoundSetSect = 1 | 
|---|
| 63 | parse var TINI.I voc '=' val | 
|---|
| 64 | ovoc = strip(voc) | 
|---|
| 65 | voc = strip(translate(voc),,'09'x) | 
|---|
| 66 | voc = strip(voc) | 
|---|
| 67 | if voc = usetvoc & FoundSetSect then do | 
|---|
| 68 | DidUpdate = 1 | 
|---|
| 69 | TINI.I = ovoc' = 'setval | 
|---|
| 70 | end | 
|---|
| 71 | end | 
|---|
| 72 | ok = stream(TINIfile,'c','close') | 
|---|
| 73 |  | 
|---|
| 74 | if FoundSetSect = 1 & DidUpdate = 0 then do | 
|---|
| 75 | /* We are through with the file, still in our section, but the key | 
|---|
| 76 | was not there - let's add it */ | 
|---|
| 77 | I = I + 1 | 
|---|
| 78 | TINI.I = '09'x||setvoc' = 'setval | 
|---|
| 79 | DidUpdate = 1 | 
|---|
| 80 | end | 
|---|
| 81 |  | 
|---|
| 82 | if FoundSetSect = 0 & DidUpdate = 0 then do | 
|---|
| 83 | /* We are through with the file, neither our section nor our key | 
|---|
| 84 | were there - let's add a new section with our key */ | 
|---|
| 85 | I = I + 1 | 
|---|
| 86 | TINI.I = SetSect | 
|---|
| 87 | I = I + 1 | 
|---|
| 88 | TINI.I = '09'x||setvoc' = 'setval | 
|---|
| 89 | DidUpdate = 1 | 
|---|
| 90 | end | 
|---|
| 91 |  | 
|---|
| 92 | /* Create a new file */ | 
|---|
| 93 | TINI.0 = I | 
|---|
| 94 | do I = 1 to TINI.0 | 
|---|
| 95 | if left(TINI.I,1) = '[' & I > 1 then do | 
|---|
| 96 | J = I - 1 | 
|---|
| 97 | if left(TINI.J,1) <> '#' then call charout tempINI, '0D0A'x | 
|---|
| 98 | end | 
|---|
| 99 | call lineout tempINI, TINI.I | 
|---|
| 100 | end | 
|---|
| 101 | ok = stream(tempINI,'c','close') | 
|---|
| 102 |  | 
|---|
| 103 | /* Update original and delete temporary */ | 
|---|
| 104 | Success = VRCopyFile(tempINI,TINIfile) | 
|---|
| 105 | if Success then ok = SysFileDelete(tempINI) | 
|---|
| 106 | return Success | 
|---|