Classe lectrice de fichiers ini sans utiliser les apis

Description

Suite à l'échec de la version précédente de cette source, j'ai changer de direction. Voici un lecteur de fichier Ini qui permet d'en énumérer les section et les clées et de lire une clée. Pour l'énumération, le principe est particulié :
le programme lit le fichier à la recherce de sections/clées
déclenche un évènement pour dire qu'une section/clée à été trouvée
Donc, plus besoins de charger le fichier en mémoire!!! Durant l'énumération, si c'est la clée que vous voullez, vous pouvez arrêter la procédure d'énumération.

Source / Exemple :


'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'À METTRE DANS UNE CLASSE APPELLÉE cIniFile
'¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
Option Explicit

Public Enum IniErrors: IniErrUnKnown = 0: IniErrFileNotFound = 1: IniErrFileEmpty = 2: IniErrFileBusy = 3: End Enum
Public Enum IniResults: IniResOK = 0: IniResStopped = 1: IniResError = 2: IniSectionNotFound = 3: IniKeyNotFound = 4: End Enum

Public Event OnSectionFound(strSectionName As String, bContinue As Boolean)
Public Event OnKeyFound(strKeyName As String, strKeyValue As String, strSectionName As String, bContinue As Boolean)
Public Event OnFinish(intWhy As IniResults)
Public Event OnError(intErrNumber As IniErrors)

Public IniFile As String
Private m_FileHandle As Integer
Private strLine As String
Private strCurrentSection As String
Private strCurrentKey As String
Private strCurrentKeyValue As String

Private m_Busy As Boolean
Private intJobType As Integer

Private strKeyToFind As String
Private bKeyFound As Boolean
Private bSectionFound As Boolean

Private strSectionToFind As String
Private bInSection As Boolean
Private bOnlyOnce As Boolean
Private Function pLeftStr(strData As String, lngLength As Long, Optional bError As Boolean = False) As String 'Protégé contre les erreurs
If Len(strData) < lngLength Then bError = True: Exit Function
pLeftStr = Left$(strData, lngLength)
End Function

Public Property Get FileHandle() As Long
FileHandle = m_FileHandle
End Property

Public Property Get Busy() As Boolean
Busy = m_Busy
End Property

Public Sub IniEnumSections()
intJobType = 1
Core True, False
End Sub

Public Sub IniEnumAll()
intJobType = 3
Core True, True
End Sub

Public Function IniReadKey(strSection As String, strKey As String) As String
intJobType = 4
strKeyToFind = strKey
strSectionToFind = strSection
Core True, True
End Function

Public Sub IniEnumKeys(strSection As String, Optional bOnlyInTheFirst As Boolean = False)
intJobType = 2
bOnlyOnce = bOnlyInTheFirst
strSectionToFind = strSection
Core True, True
End Sub

Private Sub DoSection(bContinue As Boolean)
Select Case intJobType
Case 1 'Énumération des sections
RaiseEvent OnSectionFound(strCurrentSection, bContinue)
Case 2
bInSection = False
    If (bOnlyOnce And Not bInSection) Or (Not bOnlyOnce) Then
    If StrComp(strSectionToFind, strCurrentSection, vbTextCompare) = 0 Then bInSection = True: bSectionFound = True
    End If
Case 3 'Énumération complète
RaiseEvent OnSectionFound(strCurrentSection, bContinue)
Case 4
    If StrComp(strSectionToFind, strCurrentSection, vbTextCompare) = 0 Then bInSection = True
End Select
End Sub

Private Sub DoKey(bContinue As Boolean)
Select Case intJobType
Case 2 'Énumération des clées
If bInSection Then RaiseEvent OnKeyFound(strCurrentKey, strCurrentKeyValue, strCurrentSection, bContinue)
Case 3 'Énumération complète
RaiseEvent OnKeyFound(strCurrentKey, strCurrentKeyValue, strCurrentSection, bContinue)
Case 4
If bInSection = True Then
    If StrComp(strKeyToFind, strCurrentKey, vbTextCompare) = 0 Then
    RaiseEvent OnKeyFound(strCurrentKey, strCurrentKeyValue, strCurrentSection, bContinue)
    bKeyFound = True
    bContinue = False
    End If
End If
End Select
End Sub

Private Sub Core(Optional bCallDoSection As Boolean = True, Optional bCallDoKey As Boolean = True, Optional bKeyMustHaveValue As Boolean = False)
'On Error Resume Next
Dim lngPos As Long, bErr As Boolean, bContinue As Boolean, astrKey() As String, bOk As Boolean
If m_Busy Then
RaiseEvent OnError(IniErrFileBusy)
Exit Sub
End If
m_Busy = True
m_FileHandle = FreeFile
Open IniFile For Input As #m_FileHandle

If Err.Number = 53 Then 'Fichier non trouvé
RaiseEvent OnError(IniErrFileNotFound)
Err.Clear: bErr = True
End If

If FileLen(IniFile) = 0 Then 'Fichier vide
RaiseEvent OnError(IniErrFileEmpty)
Err.Clear: bErr = True
End If
bContinue = True
Do While (Not EOF(m_FileHandle)) And (Not bErr) And bContinue
Line Input #m_FileHandle, strLine 'Lit
strLine = Trim$(strLine) 'Fait le trimage gauche et droite
    If (Not Len(strLine) = 0) And (Not pLeftStr(strLine, 1) = ";") Then
    lngPos = InStr(1, strLine, "]")
        
        If pLeftStr(strLine, 1) = "[" And (Not Len(strLine) < 3) And (Not lngPos = 0) Then 'Dans une section
        strCurrentSection = Mid$(strLine, 2, lngPos - 2)
            If bCallDoSection = True Then
            DoSection bContinue
            End If
        Else
            If (Not strCurrentSection = "") And bCallDoKey Then
            'Dans une clée valide
            astrKey = Split(strLine, "=", 2)
                If UBound(astrKey) = 1 Then
                strCurrentKey = astrKey(0)
                strCurrentKeyValue = astrKey(1)
                ElseIf Not bKeyMustHaveValue Then
                strCurrentKey = astrKey(0)
                strCurrentKeyValue = ""
                End If
            DoKey bContinue
            End If
        End If
    End If
DoEvents
Loop

If bErr Then
RaiseEvent OnFinish(2)
bOk = True
ElseIf bContinue = False And intJobType <> 4 Then
RaiseEvent OnFinish(1)
bOk = True
End If

Close #m_FileHandle
If Not bOk Then
    Select Case intJobType
    Case 1
    RaiseEvent OnFinish(0)
    Case 2
    If bSectionFound Then
    RaiseEvent OnFinish(0)
    Else
    RaiseEvent OnFinish(3)
    End If
    Case 3
    RaiseEvent OnFinish(0)
    Case 4
    If Not bInSection Then
    RaiseEvent OnFinish(3)
    ElseIf Not bKeyFound Then
    RaiseEvent OnFinish(4)
    Else
    RaiseEvent OnFinish(0)
    End If
    End Select
End If
strCurrentSection = ""
strCurrentKey = ""
strCurrentKeyValue = ""
m_Busy = False
intJobType = 0
strKeyToFind = ""
strSectionToFind = False
bInSection = False
bSectionFound = False
bOnlyOnce = False
bKeyFound = False
End Sub

'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Fonctionnement
'¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
Open "The Zip"

Conclusion :


Alors? Est-ce mieux?

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.