Visual Basic 6 function "ini_list_chapters"

Go back

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

Attribute VB_Name = "modIniListChapters"
' 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

''
' 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