Jump to content

Update a configuration file: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
Line 646:
 
But given that the NAMELIST protocol is available without difficulty, why would a hard-core fortranner bother for a Fortran programme?
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
 
Type ConfigData
favouriteFruit As String
needsPeeling As Boolean
seedsRemoved As Boolean
numberOfBananas As UInteger
numberOfStrawberries As UInteger
End Type
 
Sub updateConfigData(fileName As String, cData As ConfigData)
Dim fileNum As Integer = FreeFile
Open fileName For Input As #fileNum
If err > 0 Then
Print "File could not be opened"
Sleep
End
End If
Dim tempFileName As String = "temp_" + fileName
Dim fileNum2 As Integer = FreeFile
Open tempFileName For Output As #fileNum2
Dim As Boolean hadFruit, hadPeeling, hadSeeds, hadBananas, hadStrawberries '' all false by default
Dim As String ln
While Not Eof(fileNum)
Line Input #fileNum, ln
If ln = "" OrElse Left(ln, 1) = "#" Then
Print #fileNum2, ln
Continue While
End If
ln = Trim(LTrim(ln, ";"), Any !" \t")
If ln = "" Then Continue While
If UCase(Left(ln, 14)) = "FAVOURITEFRUIT" Then
If hadFruit Then Continue While
hadFruit = True
Print #fileNum2, "FAVOURITEFRUIT " + cData.favouriteFruit
ElseIf UCase(Left(ln, 12)) = "NEEDSPEELING" Then
If hadPeeling Then Continue While
hadPeeling = True
If cData.needsPeeling Then
Print #fileNum2, "NEEDSPEELING"
Else
Print #fileNum2, "; NEEDSPEELING"
End If
ElseIf UCase(Left(ln, 12)) = "SEEDSREMOVED" Then
If hadSeeds Then Continue While
hadSeeds = True
If cData.seedsRemoved Then
Print #fileNum2, "SEEDSREMOVED"
Else
Print #fileNum2, "; SEEDSREMOVED"
End If
ElseIf UCase(Left(ln, 15)) = "NUMBEROFBANANAS" Then
If hadBananas Then Continue While
hadBananas = True
Print #fileNum2, "NUMBEROFBANANAS " + Str(cData.numberOfBananas)
ElseIf UCase(Left(ln, 20)) = "NUMBEROFSTRAWBERRIES" Then
If hadStrawberries Then Continue While
hadStrawberries = True
Print #fileNum2, "NUMBEROFSTRAWBERRIES " + Str(cData.numberOfStrawBerries)
End If
Wend
 
If Not hadFruit Then
Print #fileNum2, "FAVOURITEFRUIT " + cData.favouriteFruit
End If
If Not hadPeeling Then
If cData.needsPeeling Then
Print #fileNum2, "NEEDSPEELING"
Else
Print #fileNum2, "; NEEDSPEELING"
End If
End If
If Not hadSeeds Then
If cData.seedsRemoved Then
Print #fileNum2, "SEEDSREMOVED"
Else
Print #fileNum2, "; SEEDSREMOVED"
End If
End If
If Not hadBananas Then
Print #fileNum2, "NUMBEROFBANANAS " + Str(cData.numberOfBananas)
End If
If Not hadStrawberries Then
Print #fileNum2, "NUMBEROFSTRAWBERRIES " + Str(cData.numberOfStrawBerries)
End If
 
Close #fileNum : Close #fileNum2
Kill(fileName)
Name (tempFileName fileName)
End Sub
 
Dim fileName As String = "config2.txt"
Dim cData As ConfigData
With cData
.favouriteFruit = "banana"
.needsPeeling = False
.seedsRemoved = True
.numberOfBananas = 1024
.numberOfStrawberries = 62000
End With
 
updateConfigData fileName, cData
Print
Print "Press any key to quit"
Sleep</lang>
 
The contents of config2.txt after updating are:
{{out}}
<pre>
# This is a configuration file in standard configuration file format
#
# Lines begininning with a hash or a semicolon are ignored by the application
# program. Blank lines are also ignored by the application program.
 
# The first word on each non comment line is the configuration option.
# Remaining words or numbers on the line are configuration parameter
# data fields.
 
# Note that configuration option names are not case sensitive. However,
# configuration parameter data is case sensitive and the lettercase must
# be preserved.
 
# This is a favourite fruit
FAVOURITEFRUIT banana
 
# This is a boolean that should be set
; NEEDSPEELING
 
# This boolean is commented out
SEEDSREMOVED
 
# How many bananas we have
NUMBEROFBANANAS 1024
NUMBEROFSTRAWBERRIES 62000
</pre>
 
=={{header|Go}}==
9,490

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.