Jump to content

Update a configuration file: Difference between revisions

(Added FreeBASIC)
Line 150:
 
FileCopy, %A_scriptdir%\rosettaconfig.tmp, %A_scriptdir%\rosettaconfig.txt, 1</lang>
 
=={{header|BASIC}}==
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|VB-DOS|1.0}}
{{works with|QB64|1.1}}
{{works with|PDS|7.1}}
 
This is just the generic procedure that does the magic to modify a configuration file. In the remarks are the details of the parameters needed to modify or add a variable and its value. This solution is not tied to the requested task, but open to any configuration file. Based in the parameters, you can just give this procedure the name of the file, the variable name, the value for the variable, the separator between the variable and its value in the file, and if the variable should be commented. The full program is somewhat long in order to allow the user to modify or add the variables. In order to save space, the full program has been added as a file so the user can test it directly.
<lang qbasic>
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' Read a Configuration File V1.0 '
' '
' Developed by A. David Garza Marín in VB-DOS for '
' RosettaCode. December 2, 2016. '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
 
' Register for values located
TYPE regVarValue
VarName AS STRING * 20
VarType AS INTEGER ' 1=String, 2=Integer, 3=Real, 4=Comment
VarValue AS STRING * 30
END TYPE
 
' Var
DIM rVarValue() as regVarValue
 
SUB doModifyValueFromConfFile (WhichFile AS STRING, WhichVariable AS STRING, WhatValue AS STRING, Separator AS STRING, ToComment AS INTEGER)
' To see details of parameters, please see doModifyArrValueFromConfFile
doModifyArrValueFromConfFile WhichFile, WhichVariable, 1, WhatValue, Separator, ToComment
END SUB
 
SUB doModifyArrValueFromConfFile (WhichFile AS STRING, WhichVariable AS STRING, WhichIndex AS INTEGER, WhatValue AS STRING, Separator AS STRING, ToComment AS INTEGER)
' Parameters:
' WhichFile: The name of the Configuration File. It can include the full path.
' WhichVariable: The name of the variable to be modified or added to the conf file.
' WhichIndex: The index number of the element to be modified in a matrix (Default=1)
' WhatValue: The new value to set in the variable specified in WhichVariable.
' Separator: The separator between the variable name and its value in the conf file. Defaults to a space " ".
' ToComment: A value to set or remove the comment mode of a variable: -1=Toggle to Comment, 0=Toggle to not comment, 1=Leave as it is.
 
' Var
DIM iFile AS INTEGER, iFile2 AS INTEGER, iError AS INTEGER
DIM iMod AS INTEGER, iIsComment AS INTEGER
DIM sLine AS STRING, sDummyFile AS STRING, sChar AS STRING
 
' If conf file doesn't exists, create one.
iError = 0
iMod = 0
IF NOT FileExists%(WhichFile) THEN
iError = CreateConfFile%(WhichFile)
END IF
 
IF NOT iError THEN ' File exists or it was created
Separator = RTRIM$(LTRIM$(Separator))
IF Separator = "" THEN
Separator = " " ' Defaults to Space
END IF
sDummyFile = GetDummyFile$(WhichFile)
 
' It is assumed a text file
iFile = FREEFILE
OPEN WhichFile FOR INPUT AS #iFile
 
iFile2 = FREEFILE
OPEN sDummyFile FOR OUTPUT AS #iFile2
 
' Goes through the file to find the variable
DO WHILE NOT EOF(iFile)
LINE INPUT #iFile, sLine
sLine = RTRIM$(LTRIM$(sLine))
sChar = LEFT$(sLine, 1)
iIsComment = (sChar = ";")
IF iIsComment THEN ' Variable is commented
sLine = LTRIM$(MID$(sLine, 2))
END IF
 
IF sChar <> "#" AND LEN(sLine) > 0 THEN ' Is not a comment?
IF IsItTheVariableImLookingFor%(sLine, WhichVariable) THEN
sLine = NewValueForTheVariable$(WhichVariable, WhichIndex, WhatValue, Separator)
iMod = True
IF ToComment = True THEN
sLine = "; " + sLine
END IF
ELSEIF iIsComment THEN
sLine = "; " + sLine
END IF
 
END IF
 
PRINT #iFile2, sLine
LOOP
 
' Reviews if a modification was done, if not, then it will
' add the variable to the file.
IF NOT iMod THEN
sLine = NewValueForTheVariable$(WhichVariable, 1, WhatValue, Separator)
PRINT #iFile2, sLine
END IF
CLOSE iFile2, iFile
 
' Removes the conf file and sets the dummy file as the conf file
KILL WhichFile
NAME sDummyFile AS WhichFile
END IF
 
END SUB
 
FUNCTION FileExists% (WhichFile AS STRING)
' Var
DIM iFile AS INTEGER
DIM iItExists AS INTEGER
SHARED iErr AS INTEGER
 
ON ERROR GOTO FileError
iFile = FREEFILE
iErr = 0
OPEN WhichFile FOR BINARY AS #iFile
IF iErr = 0 THEN
iItExists = LOF(iFile) > 0
CLOSE #iFile
 
IF NOT iItExists THEN
KILL WhichFile
END IF
END IF
ON ERROR GOTO 0
FileExists% = iItExists
 
END FUNCTION
 
FUNCTION GetDummyFile$ (WhichFile AS STRING)
' Var
DIM i AS INTEGER, j AS INTEGER
 
' Gets the path specified in WhichFile
i = 1
DO
j = INSTR(i, WhichFile, "\")
IF j > 0 THEN i = j + 1
LOOP UNTIL j = 0
 
GetDummyFile$ = LEFT$(WhichFile, i - 1) + "$dummyf$.tmp"
END FUNCTION
 
FUNCTION IsItTheVariableImLookingFor% (TextToAnalyze AS STRING, WhichVariable AS STRING)
' Var
DIM sVar AS STRING, sDT AS STRING, sDV AS STRING
DIM iSep AS INTEGER
 
sDT = UCASE$(RTRIM$(LTRIM$(TextToAnalyze)))
sDV = UCASE$(RTRIM$(LTRIM$(WhichVariable)))
iSep = INSTR(sDT, "=")
IF iSep = 0 THEN iSep = INSTR(sDT, " ")
IF iSep > 0 THEN
sVar = RTRIM$(LEFT$(sDT, iSep - 1))
ELSE
sVar = sDT
END IF
 
' It will return True or False
IsItTheVariableImLookingFor% = (sVar = sDV)
END FUNCTION
 
FUNCTION NewValueForTheVariable$ (WhichVariable AS STRING, WhichIndex AS INTEGER, WhatValue AS STRING, Separator AS STRING)
' Var
DIM iItem AS INTEGER, iItems AS INTEGER, iFirstItem AS INTEGER
DIM i AS INTEGER, iCount AS INTEGER, iHMV AS INTEGER
DIM sLine AS STRING, sVar AS STRING, sVar2 AS STRING
SHARED rVarValue() AS regVarValue
 
IF IsItAnArray%(WhichVariable) THEN
iItems = HowManyElementsInTheArray(WhichVariable)
iFirstItem = FindVarPosArr%(WhichVariable, 1)
ELSE
iItems = 1
iFirstItem = FindVarPos%(WhichVariable)
END IF
iItem = FindVarPosArr%(WhichVariable, WhichIndex)
sLine = ""
sVar = UCASE$(WhichVariable)
iHMV = UBOUND(rVarValue)
 
IF iItem > 0 THEN
i = iFirstItem
DO
sVar2 = UCASE$(RTRIM$(rVarValue(i).VarName))
IF sVar = sVar2 THEN ' Does it found an element of the array?
iCount = iCount + 1
IF LEN(sLine) > 0 THEN ' Add a comma
sLine = sLine + ", "
END IF
IF i = iItem THEN
sLine = sLine + WhatValue
ELSE
sLine = sLine + RTRIM$(rVarValue(i).VarValue)
END IF
END IF
i = i + 1
LOOP UNTIL i > iHMV OR iCount = iItems
 
sLine = WhichVariable + Separator + sLine
ELSE
sLine = WhichVariable + Separator + WhatValue
END IF
 
NewValueForTheVariable$ = sLine
END FUNCTION
 
FUNCTION IsItAnArray% (WhichVariable AS STRING)
' Returns if a Variable is an Array
IsItAnArray% = (HowManyElementsInTheArray%(WhichVariable) > 1)
 
END FUNCTION
 
FUNCTION HowManyElementsInTheArray% (WhichVariable AS STRING)
' Var
DIM i AS INTEGER, iHMV AS INTEGER, iCount AS INTEGER, iPos AS INTEGER, iQuit AS INTEGER
DIM sVar AS STRING, sVal AS STRING, sWV AS STRING
SHARED rVarValue() AS regVarValue
 
' Looks for a variable name and returns its value
iHMV = UBOUND(rVarValue)
sWV = UCASE$(LTRIM$(RTRIM$(WhichVariable)))
sVal = ""
 
' Look for all instances of WhichVariable in the
' list. This is because elements of an array will not alwasy
' be one after another, but alternate.
FOR i = 1 TO iHMV
sVar = UCASE$(RTRIM$(rVarValue(i).VarName))
IF sVar = sWV THEN
iCount = iCount + 1
END IF
NEXT i
 
HowManyElementsInTheArray = iCount
END FUNCTION
 
FUNCTION FindVarPosArr% (WhichVariable AS STRING, WhichIndex AS INTEGER)
' Var
DIM i AS INTEGER, iHMV AS INTEGER, iCount AS INTEGER, iPos AS INTEGER
DIM sVar AS STRING, sVal AS STRING, sWV AS STRING
SHARED rVarValue() AS regVarValue
 
' Looks for a variable name and returns its position
iHMV = UBOUND(rVarValue)
sWV = UCASE$(LTRIM$(RTRIM$(WhichVariable)))
sVal = ""
iCount = 0
DO
i = i + 1
sVar = UCASE$(RTRIM$(rVarValue(i).VarName))
IF sVar = sWV THEN
iCount = iCount + 1
IF iCount = WhichIndex THEN
iPos = i
END IF
END IF
LOOP UNTIL i >= iHMV OR iPos > 0
 
FindVarPosArr% = iPos
END FUNCTION
 
</lang>
 
<pre>
 
</pre>
 
=={{header|C}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.