Imports System
Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Text.RegularExpressions

Public Class clsReg

    Inherits System.ComponentModel.Component
    Private Declare Function GetPrivateProfileString Lib "kernel32" _
       Alias "GetPrivateProfileStringW" ( _
       <MarshalAs(UnmanagedType.LPWStr)> ByVal lpApplicationName As String, _
       <MarshalAs(UnmanagedType.LPWStr)> ByVal lpKeyName As String, _
       <MarshalAs(UnmanagedType.LPWStr)> ByVal lpDefault As String, _
       <MarshalAs(UnmanagedType.LPWStr)> ByVal lpReturnedString As StringBuilder, _
       ByVal nSize As UInt32, _
       <MarshalAs(UnmanagedType.LPWStr)> ByVal lpFileName As String) As UInt32

    Private Declare Function WritePrivateProfileString Lib "kernel32" _
        Alias "WritePrivateProfileStringW" ( _
        <MarshalAs(UnmanagedType.LPWStr)> ByVal lpApplicationName As String, _
        <MarshalAs(UnmanagedType.LPWStr)> ByVal lpKeyName As String, _
        <MarshalAs(UnmanagedType.LPWStr)> ByVal lpString As String, _
        <MarshalAs(UnmanagedType.LPWStr)> ByVal lpFileName As String) As UInt32


    Private ReadOnly _sectionNameRegex As Regex = New Regex("(?<=\[).*(?=\])")
    Private ReadOnly _settingRegex As Regex = New Regex("(?<key>\w+)[^\w]=[^\w](?<value>\w+)")
    Private ReadOnly _settings As Dictionary(Of String, Dictionary(Of String, Object))
    Private ReadOnly _settingsKey As Dictionary(Of String, String)

    '***********************************************************
    'Purpose      : Retrieves a value from an INI file.
    'Parameters   : pProFile      ,I  ,String    ,INI file path
    '               pSection      ,I  ,String    ,Section name in the INI file
    '               pKey          ,I  ,String    ,Key name in the specified section
    'Return Value : Returns the value associated with the specified key.
    'Note         :
    '***********************************************************
    Public Function fGetIniValueOLD(ByVal pProFile As String, ByVal pSection As String, ByVal pKey As String) As String
        Dim wBuf As StringBuilder = New StringBuilder(256)
        Dim wnum As UInt32
        Dim wPath As String = 0
        Dim intCnt As Integer = 0

        wPath = pProFile

        wnum = GetPrivateProfileString(pSection, pKey, "", wBuf, Convert.ToUInt32(wBuf.Capacity), wPath)

        If wBuf.Length = 0 Then fGetIniValueOLD = "" : Exit Function

        fGetIniValueOLD = wBuf.ToString
    End Function

    Public Function fGetIniValue(ByVal pProFile As String, ByVal pSection As String, ByVal pKey As String) As String

        Dim sr As StreamReader = New StreamReader(pProFile)
        Dim sectionName = pSection
        Dim result As String = ""

        Dim a As String
        Dim st As String()
        Dim i As Integer = 0
        Dim isSection As Boolean = False

        Try

            Do
                a = sr.ReadLine


                If a <> String.Empty Then


                    If a.Contains(pSection) Then
                        isSection = True
                    End If


                    st = a.Split(New Char() {"="c})

                    If st.Length = 2 Then
                        If st(0) = pKey And isSection = True Then
                            Return st(1)
                        End If
                    End If
                End If

            Loop Until a Is Nothing

            sr.Close()

            Return result



        Catch ex As Exception
            Return ""
        End Try

    End Function

    Public Function GetValueOrDefault(Of T)(ByVal section As String, ByVal key As String) As T
        If Not _settings.ContainsKey(section) Then
            Throw New KeyNotFoundException(section)
        End If

        If Not _settings(section).ContainsKey(key) Then
            Throw New KeyNotFoundException(key)
        End If

        Try
            Dim result As T = CType(_settings(section)(key), T)
            Return result
        Catch
            Return Nothing
        End Try
    End Function

    '***********************************************************
    'Purpose      : Retrieves a value from an INI file.
    'Parameters   : pProFile      ,I  ,String    ,INI file path
    '               pSection      ,I  ,String    ,Section name in the INI file
    '               pKey          ,I  ,String    ,Key name in the specified section
    '               pBuf          ,I  ,String    ,Buffer to receive the retrieved value
    '               bRet          ,O  ,Boolean   ,True if successful, False otherwise
    'Return Value : Returns True if the value was successfully retrieved; otherwise, False.
    'Note         :
    '***********************************************************
    Public Function fPutIniValue(ByVal pProFile As String, ByVal pSection As String, ByVal pKey As String, ByVal pBuf As String) As Boolean
        Dim wPath As String
        Dim wnum As UInt32

        wPath = pProFile
        wnum = WritePrivateProfileString(pSection, pKey, pBuf, wPath)

        fPutIniValue = False
    End Function
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

    Public Sub New()
        MyBase.new()
    End Sub
End Class
