[638] | 1 | /* Deal with text INI files like smb.conf or WIN.INI */
|
---|
| 2 |
|
---|
[1023] | 3 | /*
|
---|
| 4 | Copyright (C) 2007-2017 Herwig Bauernfeind for bww bitwise works GmbH.
|
---|
| 5 |
|
---|
| 6 | This program is free software: you can redistribute it and/or modify
|
---|
| 7 | it under the terms of the GNU General Public License as published by
|
---|
| 8 | the Free Software Foundation, either version 3 of the License, or
|
---|
| 9 | (at your option) any later version.
|
---|
| 10 |
|
---|
| 11 | This program is distributed in the hope that it will be useful,
|
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | GNU General Public License for more details.
|
---|
| 15 |
|
---|
| 16 | You should have received a copy of the GNU General Public License
|
---|
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 |
|
---|
[638] | 21 | /* Available functions: */
|
---|
| 22 | /* TRSetIni: Set/update value in a text INI file */
|
---|
| 23 | /* More to be added */
|
---|
| 24 |
|
---|
| 25 | /*:VRX */
|
---|
| 26 | TRSetIni: procedure expose TempDir/* Set value in a text INI file */
|
---|
| 27 | /* Syntax: */
|
---|
| 28 | /* ok = TRSetIni("[Section]","Key", "Value", "File") */
|
---|
| 29 | /* returns: */
|
---|
| 30 | /* 1 = success */
|
---|
| 31 | /* 0 = failure */
|
---|
| 32 |
|
---|
| 33 | /* Check section, make sure it has trailing brackets */
|
---|
| 34 | SetSect = strip(arg(1))
|
---|
| 35 | if left(SetSect,1) <> '[' then SetSect = '['||SetSect
|
---|
| 36 | if right(SetSect,1) <> ']' then SetSect = SetSect||']'
|
---|
| 37 | USetSect = translate(SetSect)
|
---|
| 38 |
|
---|
| 39 | /* Key */
|
---|
| 40 | SetVoc = strip(arg(2))
|
---|
| 41 | USetVoc = translate(SetVoc)
|
---|
| 42 |
|
---|
| 43 | /* Value */
|
---|
| 44 | SetVal = arg(3)
|
---|
| 45 |
|
---|
| 46 | /* INI file */
|
---|
| 47 | TINIfile = arg(4)
|
---|
| 48 |
|
---|
| 49 | /* Temporary work file, FAT compliant */
|
---|
| 50 | if TempDir = "TEMPDIR" then TempDir = ""
|
---|
| 51 | tempINI = SysTempFileName(TempDir||"tempINI.???")
|
---|
| 52 | ok = SysFileDelete(tempINI)
|
---|
| 53 |
|
---|
| 54 | /* Initialize variables */
|
---|
| 55 | TINI.0 = 0
|
---|
| 56 | I = 0
|
---|
| 57 | FoundSetSect = 0
|
---|
| 58 | DidUpdate = 0
|
---|
| 59 |
|
---|
| 60 | /* Read TINIfile into stem and update if the key already exists */
|
---|
| 61 | do until lines(TINIfile) = 0
|
---|
| 62 | I = I + 1
|
---|
| 63 | TINI.I = strip(linein(TINIfile))
|
---|
| 64 | if TINI.I = "" then do
|
---|
| 65 | I = I - 1
|
---|
| 66 | iterate
|
---|
| 67 | end
|
---|
| 68 | if left(TINI.I,1) = '[' then do
|
---|
| 69 | if FoundSetSect = 1 & DidUpdate = 0 then do
|
---|
| 70 | /* We are through with the section, but our key was
|
---|
| 71 | not there - let's add it */
|
---|
| 72 | J = I
|
---|
| 73 | I = I + 1
|
---|
| 74 | TINI.I = TINI.J
|
---|
| 75 | TINI.J = '09'x||setvoc' = 'setval
|
---|
| 76 | DidUpdate = 1
|
---|
| 77 | end
|
---|
| 78 | CurSection = translate(TINI.I)
|
---|
| 79 | end
|
---|
| 80 | if CurSection = USetSect then FoundSetSect = 1
|
---|
| 81 | parse var TINI.I voc '=' val
|
---|
| 82 | ovoc = strip(voc)
|
---|
| 83 | voc = strip(translate(voc),,'09'x)
|
---|
| 84 | voc = strip(voc)
|
---|
| 85 | if voc = usetvoc & FoundSetSect then do
|
---|
| 86 | DidUpdate = 1
|
---|
| 87 | TINI.I = ovoc' = 'setval
|
---|
| 88 | end
|
---|
| 89 | end
|
---|
| 90 | ok = stream(TINIfile,'c','close')
|
---|
| 91 |
|
---|
| 92 | if FoundSetSect = 1 & DidUpdate = 0 then do
|
---|
| 93 | /* We are through with the file, still in our section, but the key
|
---|
| 94 | was not there - let's add it */
|
---|
| 95 | I = I + 1
|
---|
| 96 | TINI.I = '09'x||setvoc' = 'setval
|
---|
| 97 | DidUpdate = 1
|
---|
| 98 | end
|
---|
| 99 |
|
---|
| 100 | if FoundSetSect = 0 & DidUpdate = 0 then do
|
---|
| 101 | /* We are through with the file, neither our section nor our key
|
---|
| 102 | were there - let's add a new section with our key */
|
---|
| 103 | I = I + 1
|
---|
| 104 | TINI.I = SetSect
|
---|
| 105 | I = I + 1
|
---|
| 106 | TINI.I = '09'x||setvoc' = 'setval
|
---|
| 107 | DidUpdate = 1
|
---|
| 108 | end
|
---|
| 109 |
|
---|
| 110 | /* Create a new file */
|
---|
| 111 | TINI.0 = I
|
---|
| 112 | do I = 1 to TINI.0
|
---|
| 113 | if left(TINI.I,1) = '[' & I > 1 then do
|
---|
| 114 | J = I - 1
|
---|
| 115 | if left(TINI.J,1) <> '#' then call charout tempINI, '0D0A'x
|
---|
| 116 | end
|
---|
| 117 | call lineout tempINI, TINI.I
|
---|
| 118 | end
|
---|
| 119 | ok = stream(tempINI,'c','close')
|
---|
| 120 |
|
---|
| 121 | /* Update original and delete temporary */
|
---|
| 122 | Success = VRCopyFile(tempINI,TINIfile)
|
---|
| 123 | if Success then ok = SysFileDelete(tempINI)
|
---|
| 124 | return Success
|
---|