'*************************************************************
'* QIniFile - component
'* 2005 Nastase Eodor
'* Generated by RqWork v 7.0.0 Beta
'* eodor@3x.ro  http://rqwork.3x.ro
'*************************************************************

Declare Function GetWindowsDirectory Lib "kernel32.dll" Alias "GetWindowsDirectoryA" (lpBuffer As long,nSize As Long) As Long
Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (lpBuffer As long,nSize As Long) As Long
Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (nBufferLength As Long,lpBuffer As Long) As Long

Type QIniFile Extends QObject
     Private:
     Buffer   as QStringList
     Public:
     IniName  as string
     IniPath  as string
     WinDir   as string
     SysDir   as string
     TempDir  as string
     Exists   as integer
     
     With This
     
     Sub CreateFile
     Dim FI as QFileStream
     If FileExists(.IniPath + .IniName) then
        .Exists = 1
        GoTo Exit_Sub
     Else
        If .IniPath = "" then
           FI.Open(.WinDir + .IniName,65535)
           FI.Close
           .IniPath = .WinDir
           Else
           FI.Open(.IniPath + .IniName,65535)
           FI.Close
        End If
        .Exists = 1
     End If
     Exit_Sub:
     End Sub
     
     Function ReadString(Section as string,Key as string) as string
     Defstr s,k,l,b
     Defint i,ix
     Result = ""
     i = 0 : ix = -1 : l = "" : k = "" : b = ""
     .Buffer.LoadFromFile(.IniPath + .IniName)
     b = "["+ Section + "]"
     Do
       l = .Buffer.Item(i)
       If UCase$(l) = UCase$(b) then
          ix = i
          Exit Do
       End If
       Inc(i)
     Loop until i = .Buffer.ItemCount-1
     If ix = -1 then GoTo Exit_Function
     Do
       l = .Buffer.Item(ix +1)
       k = Mid$(l,1,Instr(l,"=")-1)
       If UCase$(k) = UCase$(Key) then
          Result =  Mid$(l,Instr(l,"=")+1,Len(l))
          Exit Do
       End If
       Inc(ix)
     Loop until Instr(l,"[") <> 0 or ix = .Buffer.ItemCount-1
     Exit_Function:
     End Function
     
     Sub ReadSection(Section as string,Buff as QStringList) ' pass buffer from outside object
     Defstr s,k,l,b
     Defint i,ix,se
     i = 0 : ix = -1 : l = "" : k = "" : b = ""
     .Buffer.LoadFromFile(.IniPath + .IniName)
     b = "["+ Section + "]"
     Do
       l = .Buffer.Item(i)
       If UCase$(l) = UCase$(b) then
          ix = i
          Exit Do
       End If
       Inc(i)
     Loop until i = .Buffer.ItemCount -1
     If ix = -1 then GoTo Exit_Sub
     For i = ix +1 to .Buffer.ItemCount -1
         l = .Buffer.Item(i)
         If Instr(l,"[") then
            se = i -1
            Exit For
         End If
     Next i
     If se = -1 then se = .Buffer.ItemCount -1
     For i = ix +1 to se
         l = .Buffer.Item(i)
         Buff.AddItems l
     Next i
     Exit_Sub:
     End Sub
     
     Function ReadInteger(Section as string,Key as string) as integer
     Defstr s,k,l,b
     Defint i,ix
     Result = 0
     i = 0 : ix = -1 : l = "" : k = "" : b = ""
     .Buffer.LoadFromFile(.IniPath + .IniName)
     b = "["+ Section + "]"
     Do
       l = .Buffer.Item(i)
       If UCase$(l) = UCase$(b) then
          ix = i
          Exit Do
       End If
       Inc(i)
     Loop until i = .Buffer.ItemCount-1
     If ix = -1 then GoTo Exit_Function
     Do
       l = .Buffer.Item(ix +1)
       k = Mid$(l,1,Instr(l,"=")-1)
       If UCase$(k) = UCase$(Key) then
          Result = val( Mid$(l,Instr(l,"=")+1,Len(l)) )
          Exit Do
       End If
       Inc(ix)
     Loop until Instr(l,"[") <> 0 or ix = .Buffer.ItemCount-1
     Exit_Function:
     End Function
     
     Function ReadFloat(Section as string,Key as string) as double
     Defstr s,k,l,b
     Defint i,ix
     Result = 0.0
     i = 0 : ix = -1 : l = "" : k = "" : b = ""
     .Buffer.LoadFromFile(.IniPath + .IniName)
     b = "["+ Section + "]"
     Do
       l = .Buffer.Item(i)
       If UCase$(l) = UCase$(b) then
          ix = i
          Exit Do
       End If
       Inc(i)
     Loop until i = .Buffer.ItemCount-1
     If ix = -1 then GoTo Exit_Function
     Do
       l = .Buffer.Item(ix +1)
       k = Mid$(l,1,Instr(l,"=")-1)
       If UCase$(k) = UCase$(Key) then
          Result = val( Mid$(l,Instr(l,"=")+1,Len(l)) )
          Exit Do
       End If
       Inc(ix)
     Loop until Instr(l,"[") <> 0 or ix = .Buffer.ItemCount-1
     Exit_Function:
     End Function
     
     Function WriteString(Section as string,Key as string,Value as string,Allow as integer) as integer
     Defstr s,l,b,k
     Defint i,ix,ke,se
     Result = 0
     ix = -1 : ke = -1 : se = -1 : l = "" : k = "" : b = ""
     If Allow = 0 then GoTo Exit_Function
     
     .Buffer.LoadFromFile(.IniPath + .IniName)
     b = "["+ Section + "]"
     For i = 0 to .Buffer.ItemCount -1
       l = LTrim$(RTrim$(.Buffer.Item(i)))
       If UCase$(l) = UCase$(b) then
          ix = i
          Exit For
       End If
     Next i

        If ix = -1 then 'Section  not exists
           .Buffer.AddItems "[" + Section + "]"
           .Buffer.AddItems Key + "=" + value
           .Buffer.SaveToFile(.IniPath + .IniName)
           Result = 1
        Else
           For i = ix + 1 to .Buffer.ItemCount -1
               l = .Buffer.Item(i)
               If Instr(l,"[") <> 0 then
                  se = i  ' last record in section
                  Exit For
               End If
           Next i
           If se = -1 then se = .Buffer.ItemCount -1
           For i = ix + 1 to .Buffer.ItemCount -1
               l = .Buffer.Item(i)
               k = Mid$(l,1,Instr(l,"=")-1)
               If UCase$(K) = UCase$(Key) then  'key exist
                  ke = i
                  Exit For
               End If
           Next i
           If ke <> -1 then
              .Buffer.Item(ke) = Key + "=" + value
              .Buffer.SaveToFile(.IniPath + .IniName)
              Result = 1
              Else
              .Buffer.InsertItem se,Key + "=" + value
              .Buffer.SaveToFile(.IniPath + .IniName)
              Result = 1
           End If
        End If
     Exit_Function:
     End Function

     Function WriteInteger(Section as string,Key as string,Value as integer,Allow as integer) as integer
     Defstr s,l,b,k
     Defint i,ix,ke,se
     Result = 0
     ix = -1 : ke = -1 : se = -1 : l = "" : k = "" : b = ""
     If Allow = 0 then GoTo Exit_Function

     .Buffer.LoadFromFile(.IniPath + .IniName)
     b = "["+ Section + "]"
     For i = 0 to .Buffer.ItemCount -1
       l = LTrim$(RTrim$(.Buffer.Item(i)))
       If UCase$(l) = UCase$(b) then
          ix = i
          Exit For
       End If
     Next i

        If ix = -1 then 'Section  not exists
           .Buffer.AddItems "[" + Section + "]"
           .Buffer.AddItems Key + "=" + str$(value)
           .Buffer.SaveToFile(.IniPath + .IniName)
           Result = 1
        Else
           For i = ix + 1 to .Buffer.ItemCount -1
               l = .Buffer.Item(i)
               If Instr(l,"[") <> 0 then
                  se = i  ' last record in section
                  Exit For
               End If
           Next i
           If se = -1 then se = .Buffer.ItemCount -1
           For i = ix + 1 to .Buffer.ItemCount -1
               l = .Buffer.Item(i)
               k = Mid$(l,1,Instr(l,"=")-1)
               If UCase$(K) = UCase$(Key) then  'key exist
                  ke = i
                  Exit For
               End If
           Next i
           If ke <> -1 then
              .Buffer.Item(ke) = Key + "=" + str$(value)
              .Buffer.SaveToFile(.IniPath + .IniName)
              Result = 1
              Else
              .Buffer.InsertItem se,Key + "=" + str$(value)
              .Buffer.SaveToFile(.IniPath + .IniName)
              Result = 1
           End If
        End If
     Exit_Function:
     End Function

     Function WriteFloat(Section as string,Key as string,Value as double,Allow as integer) as integer
     Defstr s,l,b,k
     Defint i,ix,ke,se
     Result = 0
     ix = -1 : ke = -1 : se = -1 : l = "" : k = "" : b = ""
     If Allow = 0 then GoTo Exit_Function

     .Buffer.LoadFromFile(.IniPath + .IniName)
     b = "["+ Section + "]"
     For i = 0 to .Buffer.ItemCount -1
       l = LTrim$(RTrim$(.Buffer.Item(i)))
       If UCase$(l) = UCase$(b) then
          ix = i
          Exit For
       End If
     Next i

        If ix = -1 then 'Section  not exists
           .Buffer.AddItems "[" + Section + "]"
           .Buffer.AddItems Key + "=" + str$(value)
           .Buffer.SaveToFile(.IniPath + .IniName)
           Result = 1
        Else
           For i = ix + 1 to .Buffer.ItemCount -1
               l = .Buffer.Item(i)
               If Instr(l,"[") <> 0 then
                  se = i  ' last record in section
                  Exit For
               End If
           Next i
           If se = -1 then se = .Buffer.ItemCount -1
           For i = ix + 1 to .Buffer.ItemCount -1
               l = .Buffer.Item(i)
               k = Mid$(l,1,Instr(l,"=")-1)
               If UCase$(K) = UCase$(Key) then  'key exist
                  ke = i
                  Exit For
               End If
           Next i
           If ke <> -1 then
              .Buffer.Item(ke) = Key + "=" + str$(value)
              .Buffer.SaveToFile(.IniPath + .IniName)
              Result = 1
              Else
              .Buffer.InsertItem se,Key + "=" + str$(value)
              .Buffer.SaveToFile(.IniPath + .IniName)
              Result = 1
           End If
        End If
     Exit_Function:
     End Function
     
     Sub GetIniPath
     Dim lpBuffer as string
     Dim size     as short
      lpBuffer = Space$(260)
      Size = GetWindowsDirectory(Varptr(lpBuffer),260)
      .WinDir = Left$(lpBuffer,Size) + "\"
      lpBuffer = Space$(260)
      Size = GetSystemDirectory(Varptr(lpBuffer),260)
      .SysDir = Left$(lpBuffer,Size) + "\"
      lpBuffer = Space$(260)
      Size = GetTempPath(260,Varptr(lpBuffer))
      .TempDir = Left$(lpBuffer,Size)
     End Sub
     
     Sub GetIniName
     .IniName = Mid$(Application.ExeName,1,RInstr(Application.ExeName,"."))
     .IniName = .IniName + "ini"
     End Sub

     End With
     
  Constructor
    GetIniPath
    IniName = ""
    IniPath = ""
  End Constructor
End Type
