Jump to content

Enumerate INI Sections


Matt @ MPCS
 Share

Recommended Posts

I converted an old VB application to AutoIt and required this function. I found it useful, hopefully someone else will too.

Func INISectionEnum( $IniFile )
  
   Local $i, $tmpLine, $file, $strSections
  
   If( Not FileExists( $IniFile ) ) Then
      SetError( -1 )
      Return 0
   EndIf
  
   $file = FileOpen( $IniFile, 0 )
  
   If( $file = -1 ) then 
      SetError( -2 )
      Return 0
   EndIf
  
   While 1
      $tmpLine = FileReadLine( $file )
        
      If( @error = -1 ) Then
         ExitLoop
      EndIf
        
      If( StringLeft( $tmpLine, 1 ) = "[" ) Then
           
         $i = StringInStr( $tmpLine, "]" )
           
         If( $i = 0 ) Then
            ContinueLoop
         EndIf
           
         If( $strSections = "" ) Then
            $strSections = StringMid( $tmpLine, 2, ($i - 2) ) 
         Else
            $strSections = $strSections & "|" & StringMid( $tmpLine, 2, ($i - 2) ) 
         EndIf
        
      EndIf
     
   Wend

   If( $strSections = "" ) Then
      SetError( 1 )
      Return 0
   EndIf
  
  Return StringSplit( $strSections, "|" )

EndFunc

Func INIKeyEnum( $IniFile, $Section )
   
   Local $file, $tmpLine, $numLine, $strKeys
   
   If( Not FileExists( $IniFile ) ) Then
      SetError( -1 )
      Return 0
   EndIf

   $file = FileOpen( $IniFile, 0 )
   
   If( $file = -1 ) then 
      SetError( -2 )
      Return 0
   EndIf
   
   $numLine = 0
   
   While 1
      
      $numLine = $numLine + 1
      
      $tmpLine = FileReadLine( $file )
      
      If( @error = -1 ) Then
         ExitLoop
      EndIf
        
      If( StringLeft( $tmpLine, (StringLen( $Section ) + 2) ) = "[" & $Section & "]" ) Then
           
         $i = $numLine + 1
         
         While 1
            
            $tmpLine = FileReadLine( $file, $i )
            
            If( (@error = -1) Or (StringLeft( $tmpLine, 1 ) = "[") ) Then
               ExitLoop
            EndIf

            If( StringStripWS( $tmpLine, 8 ) = "" ) Then ContinueLoop
            
            If( $strKeys = "" ) Then
               $strKeys = StringLeft( $tmpLine, StringInStr( $tmpLine, "=" ) - 1 )
            Else
               $strKeys = $strKeys & "|" & StringLeft( $tmpLine, StringInStr( $tmpLine, "=" ) - 1 )
            EndIf
           
            $i = $i + 1

         Wend

         Return StringSplit( $strKeys, "|" )
         
      EndIf
   
   Wend
   
EndFunc

It isn't rocket science but hopefully somone will find it useful. Thanks guys!

*** Matt @ MPCS

EDIT: Standardized the code.

EDIT2: Updated code to add new function.

Edited by Matt @ MPCS
Link to comment
Share on other sites

I got used to that while writing C++ code; if it bothers you and you want to use the code it won't hurt to remove the parents. I think the only statement that it doesn't wor on is the for statement.

*** Matt @ MPCS

Link to comment
Share on other sites

@bcording

I didn't see it posted, so I wrote my own. It would have been nice to have instead of doing it myself. It is amazing the similarities between them. You do a few conditionals I don't think should be/need to be done, but if it works then it works.

FYI: Technically the official name for your 'SectionEntries' is 'Keys'. This was defined by Microsoft and is the industry standard.

*** Matt @ MPCS

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...