Code-Beispiel
Arbeiten mit INI-Dateien
Lizenz: | Erster Autor: | Letzte Bearbeitung: |
k. A. | stephanbrunker | 05.11.2015 |
Wenn man bestimmte Parameter zwischenspeichern will, bietet sich eine INI-Datei an. Diese ist im Format als normale Textdatei gespeichert und ist wie folgt formatiert:
[sektion]
parameter=wert
[sektion2]
parameter=wert ;kommentar
usw.
Im Code sind nun eine ganze Menge Funktionen, die aber zum Teil nur innerhalb des Codes benötigt werden. Wichtig bei der Nutzung sind die folgenden:
- Eine Abfrage nach dem Wert zu einem Parameter in einer Sektion kann man mit der Funktion IniGetParam durchführen.
- Eine ganze Sektion erhält man mit IniGetSection
- Mit IniSetParam setzt man einen Wert
- Mit IniDeleteParam entfernt man eine Zeile
- Und mit IniGetSectionArray erhält man den Inhalt einer Sektion in zwei Arrays aus Strings: einmal die Parameter, im zweiten Array die Werte.
Alle Funktionen sind so geschrieben, das man für das Ergebnis (z.B. wert) einen leeren String an die Funktion übergibt, der dann gefüllt zurückgegeben wird. Das gleiche auch bei den Arrays, die allerdings mit ReDim dimensioniert sein müssen. Die Eingaben mit Dateipfad, Sektion und Parameter sind wohl selbsterklärend.
Streng genommen sollen bei Sektionen und Parametern Groß- und Kleinschreibung ignoriert werden, meine Version macht das jedoch nicht.
Die Funktionen InsStr(zum Einfügen eines Strings in einen anderen) und ReplStr(ersetzt Teil eines Strings) und die Definition für TRUE und FALSE gehören eigentlich unter die kleinen Helferlein, sind hier aber mit aufgeführt weil verwendet.
Hier jedenfalls der Code:
#Include Once "File.bi"
#Ifndef TRUE
#Define TRUE -1
#Define FALSE 0
#EndIf
'Insert a String in another
Function InsStr(ByRef s As String,ByVal po As Integer,ByRef ins As String) As String
Dim l As Integer = Len((s))
If po > l Then
Return s & ins
ElseIf po = 0 Then
Return ins & s
ElseIf po < 0 Then
Print "negativ einfügen nicht möglich":Sleep:End
EndIf
'divide string in two parts
Dim As String s1,s2
s1=Left(s,po-1)
s2=Right(s,l-po+1)
Return s1 & ins & s2
End Function
'Replace part of a String
Function ReplStr(ByRef s As String,ByVal po As Integer,ByVal rlen as Integer,ByRef ins As String) as String
Dim l As Integer = Len((s))
If po > l Then
Return s & ins
ElseIf po = 0 Then
Return ins & s
ElseIf po < 0 Then
Print "negativ einfügen nicht möglich":Sleep:End
EndIf
Dim as String s1,s2
s1=Left(s,po-1)
s2=Right(s,l-po+1-rlen)
Return s1 & ins & s2
End Function
Function IniStringGetSectionPos(ByRef inistring As String, ByRef section As String, ByRef startpos As ULong, ByRef endpos As ULong) As Integer
Dim As ULong index,i
Dim As ULong fclen = Len((inistring))
Dim As UByte flag
index = 1
For i = index To fclen
If inistring[i-1]=91 Then ' [ - Section title start
If flag = 1 Then 'end of section wanted - start of new section
endpos = i-1
Return TRUE
Else
index = i
End If
ElseIf inistring[i-1]=93 Then ' ] - Section title end
If Mid(inistring,index+1,i-index-1) = section Then
index = i
startpos = i+3 'skip Chr(13,10)
flag = 1
End If
End If
Next i
If flag = 1 Then
endpos = fclen 'end of section wanted - eof
Return TRUE
Else
startpos = fclen + 1
Return FALSE
EndIf
End Function
Function IniStringGetRowPos(ByRef inistring As String, ByRef param As String, ByRef startpos As ULong, ByRef endpos As ULong) As Integer
Dim As ULong i, index
Dim As ULong ep = endpos
Dim As UByte flag
Dim As ULong slen = Len((inistring))
index = startpos
For i = index To ep
If inistring[i-1] = 59 Then ' ; comment
flag = 1 'comment
ElseIf inistring[i-1] = 61 And flag = 0 Then ' = gefunden
If Mid(inistring,index,i-index) = param Then
startpos = index
flag = 2
End If
ElseIf inistring[i-1] = 13 And inistring[i] = 10 Then 'carriage return
If flag = 2 Then
endpos = i+1
Return TRUE
End If
flag = 0
index = i + 2
End If
Next i
Return FALSE
End Function
Function IniGetString (ByRef filepath As String, ByRef fc As String) As Integer
If Not FileExists(filepath) Then Return FALSE
Dim ff As ULong = FreeFile
Open filepath For Binary As #ff
Dim As ULong fclen = Lof(ff)
fc = Space(fclen)
Get #ff,1,fc
Close #ff
Return TRUE
End Function
Function IniPutString(ByRef filepath As String, ByRef fc As String) As Integer
Kill filepath
Dim ff As ULong = FreeFile
Open filepath For Binary As #ff
Put #ff,,fc
Close #ff
Return TRUE
End Function
Function IniGetSection (ByRef filepath As String,ByRef sectionwanted As String,ByRef sectioncontent As String) As Integer
Dim fc As String
If IniGetString(filepath,fc) = FALSE Then Return FALSE
Dim As ULong startpos, endpos
If IniStringGetSectionPos(fc,sectionwanted,startpos,endpos) = FALSE Then Return FALSE
sectioncontent = Mid(fc,startpos,endpos-startpos+1)
Return TRUE
End Function
Function IniStringGetParam(ByRef fc As String, ByRef section As String, ByRef param As String, ByRef wert As String) As Integer
Dim slen As ULong = Len((fc))
Dim As ULong i,j,index, startindex, endindex
If IniStringGetSectionPos(fc,section,startindex,endindex) = FALSE Then Return FALSE
If IniStringGetRowPos(fc,param,startindex,endindex) = FALSE Then Return FALSE
'divide param and content
index = startindex
slen = endindex
For i = index To slen
If fc[i-1] = 61 Then ' = gefunden
index = i + 1
ElseIf fc[i-1] = 13 And fc[i] = 10 Then 'carriage return
wert = Mid(fc,index,i-index)
Return TRUE
End If
Next i
Return FALSE
End Function
Function IniGetParam(ByRef filepath As String, ByRef section As String, ByRef param As String, ByRef wert As String) As Integer
Dim fc As String
If IniGetString(filepath,fc) = FALSE Then Return FALSE
If IniStringGetParam(fc, section, param,wert) = FALSE Then Return FALSE
Return TRUE
End Function
Function IniSetParam(ByRef filepath As String, ByRef section As String, ByRef param As String, ByRef wert As String) As Integer
Dim As String fc,fcbegin,fcend,fcsection
If IniGetString(filepath,fc) = FALSE Then Return FALSE 'can't open file
Dim As ULong startpos, endpos
If IniStringGetSectionPos(fc,section,startpos,endpos) = FALSE Then 'section doesn't exist - append section
fc &= "[" & section & "]" & Chr(13,10) & param & "=" & wert & Chr(13,10)
Else
If IniStringGetRowPos(fc,param,startpos,endpos) = FALSE Then 'row doesn't exist - append row to section
fc = InsStr(fc,endpos+1,param & "=" & wert & Chr(13,10))
Else
fc = ReplStr(fc,startpos,endpos-startpos+1,param & "=" & wert & Chr(13,10))
End If
End If
IniPutString(filepath,fc)
Return TRUE
End Function
Function IniDeleteParam(ByRef filepath As String, ByRef section As String, ByRef param As String) As Integer
Dim As String fc
If IniGetString(filepath,fc) = FALSE Then Return FALSE
Dim As ULong startpos, endpos
If IniStringGetSectionPos(fc,section,startpos,endpos) = FALSE Then Return FALSE
If IniStringGetRowPos(fc,param,startpos,endpos) = FALSE Then Return FALSE
fc = Left(fc,startpos-1) & Right(fc,Len((fc))-endpos) 'skip Chr(13,10)
IniPutString(filepath,fc)
Return TRUE
End Function
'put section in two arrays of params and contents
Function IniGetSectionArray(ByRef filepath As String, ByRef section As String,params() As String, werte() As String) As Integer
Dim sc As String
If IniGetSection(filepath,section,sc) = FALSE Then Return FALSE
Dim As ULong index = 1
Dim As ULong i
Dim As ULong arrayindex = 1
Dim As ULong slen = Len((sc))
Dim As UByte flag
For i = index To slen
If sc[i-1] = 59 Then ' ; comment
flag = 1
ElseIf sc[i-1] = 61 And flag = 0 Then ' = gefunden
ReDim Preserve params(1 To arrayindex)
ReDim Preserve werte(1 To arrayindex)
params(arrayindex) = Mid(sc,index,i-index)
index = i + 1
flag = 2
ElseIf sc[i-1] = 13 And sc[i] = 10 Then 'carriage return
If flag = 2 Then
werte(arrayindex) = Mid(sc,index,i-index)
arrayindex += 1
End If
flag = 0
index = i + 2
End If
Next i
Return TRUE
End Function
Zusätzliche Informationen und Funktionen |
- Das Code-Beispiel wurde am 05.11.2015 von stephanbrunker angelegt.
- Die aktuellste Version wurde am 05.11.2015 von stephanbrunker gespeichert.
|
|