Jump to content



Photo

UDF: _ini_to_dict


  • Please log in to reply
3 replies to this topic

#1 jftuga

jftuga

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 315 posts

Posted 24 May 2006 - 02:36 PM

AutoIt         
  ; ; creates a dictionary with the prefix $dict_ (in the Global scope) ; each $dict_ variable corresponds to a unique ini section, mapping it's keys to values ; returns the number of dictionaries created (aka the number of sections in the ini file) ; ; USE WITH CAUTION ; variable name creation will not work if section headers have characters other ; than letters, numbers, or the _ character.  However, spaces are removed. func _ini_to_dict($fname_ini_config)         local $ini_list = IniReadSectionNames( $fname_ini_config )     if @error then         MsgBox(0, "Error", "Could not read ini file: " & $fname_ini_config )         return -1     endif     local $i, $j     local $name, $dict     local $slots     local $key, $val     for $i=1 to $ini_list[0]         $slots = IniReadSection( $fname_ini_config, $ini_list[$i] )         $name = "dict_" & StringStripWS($ini_list[$i],8)         $dict = ObjCreate("Scripting.Dictionary")         for $j = 1 to $slots[0][0]             $key = $slots[$j][0]             $val = $slots[$j][1]             $dict.Add( $key, $val )         next         Assign($name, $dict, 2)     next     return $i-1 endfunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; example of how to run Dim $count Dim $key, $val $count = _ini_to_dict( "c:\Whatever.ini" ) MsgBox(0,"count", $count) if -1 = $count then; an error occured     exit endif ; change Whatever to a ini section header for $key in $dict_Whatever     $val = $dict_Whatever.Item($key)     MsgBox(0, $key, $val ) next

Edited by jftuga, 24 May 2006 - 03:08 PM.






#2 GEOSoft

GEOSoft

    Sure I'm senile. What's your excuse?

  • MVPs
  • 10,563 posts

Posted 24 May 2006 - 10:03 PM

AutoIt         
  ; ; creates a dictionary with the prefix $dict_ (in the Global scope) ; each $dict_ variable corresponds to a unique ini section, mapping it's keys to values ; returns the number of dictionaries created (aka the number of sections in the ini file) ; ; USE WITH CAUTION ; variable name creation will not work if section headers have characters other ; than letters, numbers, or the _ character.  However, spaces are removed. func _ini_to_dict($fname_ini_config)         local $ini_list = IniReadSectionNames( $fname_ini_config )     if @error then         MsgBox(0, "Error", "Could not read ini file: " & $fname_ini_config )         return -1     endif     local $i, $j     local $name, $dict     local $slots     local $key, $val     for $i=1 to $ini_list[0]         $slots = IniReadSection( $fname_ini_config, $ini_list[$i] )         $name = "dict_" & StringStripWS($ini_list[$i],8)         $dict = ObjCreate("Scripting.Dictionary")         for $j = 1 to $slots[0][0]             $key = $slots[$j][0]             $val = $slots[$j][1]             $dict.Add( $key, $val )         next         Assign($name, $dict, 2)     next     return $i-1 endfunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; example of how to run Dim $count Dim $key, $val $count = _ini_to_dict( "c:\Whatever.ini" ) MsgBox(0,"count", $count) if -1 = $count then; an error occured     exit endif ; change Whatever to a ini section header for $key in $dict_Whatever     $val = $dict_Whatever.Item($key)     MsgBox(0, $key, $val ) next

Nice but to avoid the problem you point to in
; USE WITH CAUTION
; variable name creation will not work if section headers have characters other
; than letters, numbers, or the _ character. However, spaces are removed.
Why not change
$name = "dict_" & StringStripWS($ini_list[$i],8)
to
$name = "dict_" & StringStripWS($ini_list[$i],3)
so you just strip leading and trailing WS. If you can't have any WS at all then after the strip use
If StringInStr($name,Chr(32) then $Name = StringReplace($Name,Chr(32),Chr(95))
which will replace the spaces with underscores. That way if someone had to do further conversion to re-write the ini they could use StringReplace to replace the underscores with spaces again.
GeorgeQuestion about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else."Old age and treachery will always overcome youth and skill!"

#3 jftuga

jftuga

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 315 posts

Posted 26 May 2006 - 07:07 PM

I added in you suggestions about space to underscore conversion in my ini_to_dict function. I also added a check for an empty section. Before this extra check, the function could return an array out of bounds error. Here is the updated ini_to_dict function. Use this one instead of the one listed in the first post.

-John

AutoIt         
func _ini_to_dict($fname_ini_config)        local $ini_list = IniReadSectionNames( $fname_ini_config )     if @error then         MsgBox(0, "Error", "Could not read ini file: " & $fname_ini_config )         return -1     endif     local $i, $j     local $name, $dict     local $slots     local $key, $val     for $i=1 to $ini_list[0]         $slots = IniReadSection( $fname_ini_config, $ini_list[$i] )         if 0 = ubound($slots) then             continueloop         endif         $name = "dict_" & StringStripWS($ini_list[$i],3)         If StringInStr($name,Chr(32)) then             $name = StringReplace($name, chr(32), chr(95) )         endif         $dict = ObjCreate("Scripting.Dictionary")         for $j = 1 to $slots[0][0]             $key = $slots[$j][0]             $val = $slots[$j][1]             $dict.Add( $key, $val )         next         Assign($name, $dict, 2)     next     return $i-1 endfunc

Edited by jftuga, 26 May 2006 - 07:14 PM.


#4 jftuga

jftuga

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 315 posts

Posted 26 May 2006 - 07:46 PM

OK, that one was not too good either. Once again...

-John

AutoIt         
func _ini_to_dict($fname_ini_config)        local $ini_list = IniReadSectionNames( $fname_ini_config )     if @error then         MsgBox(0, "Error", "Could not read ini file: " & $fname_ini_config )         return -1     endif     local $i, $j     local $name, $dict     local $slots     local $key, $val     for $i=1 to $ini_list[0]         $slots = IniReadSection( $fname_ini_config, $ini_list[$i] )         $name = "dict_" & StringStripWS($ini_list[$i],3)         If StringInStr($name,Chr(32)) then             $name = StringReplace($name, chr(32), chr(95) )         endif         $dict = ObjCreate("Scripting.Dictionary")         if ubound($slots) > 0 then             for $j = 1 to $slots[0][0]                 $key = $slots[$j][0]                 $val = $slots[$j][1]                 $dict.Add( $key, $val )             next         endif         Assign($name, $dict, 2)     next     return $i-1 endfunc

Edited by jftuga, 26 May 2006 - 07:46 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users