Visual Basic 6 function "ini_exist_chapter"

Go back

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

Attribute VB_Name = "modIniExistChapter"
' These functions are 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

''
' Returns all chapters in an INI-file
' Useful for simple INI-files but chapters must be unique
' @param    string  inidata         The INI contents (could be file_get_contents(filename))
' @param    array   chapters        By reference; will be filled with all chapters
' @param    boolean case_sensitive  When false, all chapters are lower case, otherwise case remains
' @return   integer                 The amount of chapters
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function ini_list_chapters(ByVal inidata As String, ByRef chapters() As String, Optional ByVal case_sensitive As Boolean = False) As Integer
    Dim line As Variant, cnt As Integer, ret() As String
    For Each line In Split(inidata, vbLf)
        ' Removes a Cariage Return if exists
        If Right(line, 1) = vbCr Then line = Left(line, Len(line) - 1)
        ' Removes additional whitespace around the line
        line = Trim(line)
        ' Checks if this is a chapter
        If Left(line, 1) = "[" And Right(line, 1) = "]" Then
            ReDim Preserve ret(0 To cnt)
            If case_sensitive Then
                ret(cnt) = Trim(Mid(line, 2, Len(line) - 2))
            Else
                ret(cnt) = Trim(LCase(Mid(line, 2, Len(line) - 2)))
            End If
            cnt = cnt + 1
        End If
    Next
    
    ' Returns the values
    chapters = ret
    ini_list_chapters = cnt
End Function

''
' Returns if a specific INI chapter exists
' @param    string  inidata         The INI contents (could be file_get_contents(filename))
' @param    string  chapter         The chapter name
' @param    boolean case_sensitive  When false, all chapters and keys are lower case, otherwise case remains
' @return   boolean                 True when it exists, false otherwise
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function ini_exist_chapter(ByVal inidata As String, ByVal chapter As String, Optional ByVal case_sensitive As Boolean = False) As Boolean
    Dim i As Integer, chaps() As String
    i = ini_list_chapters(inidata, chaps(), case_sensitive)
    If i = 0 Then Exit Function ' No chapters found at all
    If Not case_sensitive Then chapter = LCase(chapter)
    For i = LBound(chaps) To UBound(chaps)
        If chaps(i) = chapter Then ini_exist_chapter = True: Exit Function
    Next i
End Function