jftuga Posted May 24, 2006 Share Posted May 24, 2006 (edited) expandcollapse popup; ; 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 May 24, 2006 by jftuga Admin_Popup, show computer info or launch shellRemote Manager, facilitates connecting to RDP / VNCProc_Watch, reprioritize cpu intensive processesUDF: _ini_to_dict, transforms ini file entries into variablesUDF: monitor_resolutions, returns resolutions of multiple monitorsReport Computer Problem, for your IT help deskProfile Fixer, fixes a 'missing' AD user profile Link to comment Share on other sites More sharing options...
GEOSoft Posted May 24, 2006 Share Posted May 24, 2006 expandcollapse popup; ; 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 ) nextNice 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. George Question 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!" Link to comment Share on other sites More sharing options...
jftuga Posted May 26, 2006 Author Share Posted May 26, 2006 (edited) 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 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 May 26, 2006 by jftuga Admin_Popup, show computer info or launch shellRemote Manager, facilitates connecting to RDP / VNCProc_Watch, reprioritize cpu intensive processesUDF: _ini_to_dict, transforms ini file entries into variablesUDF: monitor_resolutions, returns resolutions of multiple monitorsReport Computer Problem, for your IT help deskProfile Fixer, fixes a 'missing' AD user profile Link to comment Share on other sites More sharing options...
jftuga Posted May 26, 2006 Author Share Posted May 26, 2006 (edited) OK, that one was not too good either. Once again... -John 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 May 26, 2006 by jftuga robertocm 1 Admin_Popup, show computer info or launch shellRemote Manager, facilitates connecting to RDP / VNCProc_Watch, reprioritize cpu intensive processesUDF: _ini_to_dict, transforms ini file entries into variablesUDF: monitor_resolutions, returns resolutions of multiple monitorsReport Computer Problem, for your IT help deskProfile Fixer, fixes a 'missing' AD user profile Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now