Visual Basic 6 function "ini_value_set"

Go back

Below you'll find the source for the Visual Basic 6 function ini_value_set.

Attribute VB_Name = "modIniValueSet"
' This function is downloaded from:
' http://www.stefanthoolen.nl/archive/vb6-functions/
' 
' You may freely distribute this file but please leave all comments, including this one, in it.
' 
' @Author Stefan Thoolen <mail@stefanthoolen.nl>

Option Explicit

''
' Sets an INI-value
' @param    string  inidata         By reference, the INI contents (will be modified)
' @param    string  chapter         The chapter name
' @param    string  key             The key name
' @param    string  value           The new value
' @param    boolean case_sensitive  When false, all chapters and keys are lower case, otherwise case remains
' @param    integer index           An optional index of the key, if a key can exist multiple times. If it's -1 (or above the amount of keys) a new key will be added
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Sub ini_value_set(ByRef inidata As String, ByVal chapter As String, ByVal key As String, ByVal value As String, Optional ByVal case_sensitive As Boolean = False, Optional ByVal index As Integer = 0)
    Dim line As Variant, in_chapter As Boolean, done As Boolean, p() As String, ret As String
    Dim crlf As String, orig_line As String, ind As Integer
    
    crlf = vbCrLf ' At default, we use CrLf, but when we detect no Cr then we only use Lf
    
    For Each line In Split(inidata, vbLf)
        orig_line = line & vbLf
        ' Removes a Cariage Return if exists
        If Right(line, 1) = vbCr Then line = Left(line, Len(line) - 1) Else crlf = vbLf
        ' Removes additional whitespace around the line
        line = Trim(line)
        ' Checks if this is a chapter
        If Left(line, 1) = "[" And Right(line, 1) = "]" And Not done Then
            ' We were in the right chapter but didn't found the key so we add it
            If in_chapter Then
                If Right(ret, Len(crlf) * 2) = crlf & crlf Then
                    ret = Left(ret, Len(ret) - Len(crlf)) & key & "=" & value & crlf & crlf
                Else
                    ret = ret & key & "=" & value & crlf
                End If
                done = True
            End If
            
            ' We are not in the right chapter
            in_chapter = False
            ' Except if we are in the right chapter
            If (case_sensitive And Trim(Mid(line, 2, Len(line) - 2)) = chapter) _
            Or (Not case_sensitive And Trim(LCase(Mid(line, 2, Len(line) - 2))) = LCase(chapter)) Then
                in_chapter = True
            End If
        ElseIf Left(line, 1) <> ";" And line <> "" And in_chapter And Not done Then
            ' Key found
            p = Split(line, "=", 2)
            If (case_sensitive And Trim(p(LBound(p))) = key) _
            Or (Not case_sensitive And Trim(LCase(p(LBound(p)))) = LCase(key)) Then
                If ind = index Then done = True: orig_line = p(LBound(p)) & "=" & value & crlf
                ind = ind + 1
            End If
        End If
        ret = ret & orig_line
    Next
    
    ' Still not added, lets add it at the end
    If Not done Then
        If in_chapter Then
            If Right(ret, Len(crlf) * 2) = crlf & crlf Then
                ret = Left(ret, Len(ret) - Len(crlf)) & key & "=" & value & crlf & crlf
            Else
                ret = ret & key & "=" & value & crlf
            End If
            done = True
        Else
            ret = ret & "[" & chapter & "]" & crlf
            ret = ret & key & "=" & value & crlf
            done = True
        End If
    End If
    
    inidata = ret
End Sub