Jump to content

Ini in memory


Alek
 Share

Recommended Posts

UDFs for managing Ini format in memory.

They are very similar to the ini file functions (parameters and return values)

I found a set of function like these on the forum (i think) that used regexp, but ran into problems when using keys/values that containd symbols (like a file dir)

Bugs:

-Needs more testing...

#cs
_IniMem_Delete(ByRef $s_ini, $s_Section, $s_key = "")
_IniMem_Read($s_ini, $s_Section, $s_key, $s_default = "")
_IniMem_ReadSection($s_ini, $s_Section)
_IniMem_ReadSectionNames($s_ini)
_IniMem_RenameSection(ByRef $s_ini, $s_Old_Sec, $s_New_Sec, $i_Flag = 0)
_IniMem_Write(ByRef $s_ini, $s_Section, $s_key, $s_value)
_IniMem_WriteSection(ByRef $s_ini, $s_Section, $s_data, $i_Index = 1)
_IniMem_Cleanup(ByRef $s_ini)
#ce

; #FUNCTION# ==============================================================
; Name...........: _IniMem_Delete
; Description ...: Deletes a value from a standard format ini string
; Syntax.........: _IniMem_Delete(ByRef $s_ini, $s_Section, [$s_key])
; Parameters ....: $s_ini - The ini string
;                  $s_Section  - The section you want to delete
;                  $s_Key - The key you want to delete (default = "")
; Return values .: Success - 1
;                  Failure - 0 (cant find section or key)
; Author ........: Alek
; =========================================================================
Func _IniMem_Delete(ByRef $s_ini, $s_Section, $s_key = "")
    Local $s_temp_ini, $x, $i
    
    $s_temp_ini = StringSplit($s_ini, @CRLF)
    For $x = 1 To $s_temp_ini[0]
        If $s_temp_ini[$x] = "[" & $s_Section& "]" Then ExitLoop
    Next
    
    If $x > $s_temp_ini[0] Then Return 0
    If $s_key <> "" Then
        For $i = $x+1 To $s_temp_ini[0]
            If StringLeft($s_temp_ini[$i], 1) = "[" And StringRight($s_temp_ini[$i], 1) = "]" Then ExitLoop
            If StringLeft($s_temp_ini[$i], StringLen($s_key)) = $s_key Then 
                $s_temp_ini[$i] = ""
                ExitLoop
            EndIf
        Next
    Else
        For $i = $x To $s_temp_ini[0]
            If StringLeft($s_temp_ini[$i], 1) = "[" And StringRight($s_temp_ini[$i], 1) = "]" And $s_temp_ini[$i] <> "[" & $s_Section & "]" Then ExitLoop
            $s_temp_ini[$i] = ""
        Next
    EndIf

    $s_ini = ""
    For $x = 1 To $s_temp_ini[0]
        If $s_temp_ini[$x] = "" Then ContinueLoop
        If StringLeft($s_temp_ini[$x], 1) = "[" And StringRight($s_temp_ini[$x],1) = "]" And $x > 1 Then
            $s_ini &= @CRLF & $s_temp_ini[$x] & @CRLF
        Else
            $s_ini &= $s_temp_ini[$x] & @CRLF
        EndIf
    Next
    
    Return 1
EndFunc

; #FUNCTION# ==============================================================
; Name...........: _IniMem_Read
; Description ...: Reads a value from a standard format ini string
; Syntax.........: _IniMem_Read($s_ini, $s_Section, $s_key, [$s_default])
; Parameters ....: $s_ini - The ini string
;                  $s_Section  - The section you want to read
;                  $s_Key - The key you want to get
;                  $s_default - the return value when unable to read the key (default = "")
; Return values .: Success - value of the selected key
;                  Failure - $s_default
; Author ........: Alek
; =========================================================================
Func _IniMem_Read($s_ini, $s_Section, $s_key, $s_default = "")
    $s_ini = StringSplit($s_ini, @CRLF)
    For $x = 1 To $s_ini[0]
        If $s_ini[$x] = "[" & $s_Section & "]" Then ExitLoop
    Next

    If $x > $s_ini[0] Then Return $s_default
    
    For $i = $x+1 To $s_ini[0]
        If StringLeft($s_ini[$i],1) = "[" And StringRight($s_ini[$i],1) = "]" Then ExitLoop
        If $s_ini[$i] = "" Then ContinueLoop
        
        If StringLeft($s_ini[$i], StringLen($s_key)) = $s_key Then Return StringTrimLeft($s_ini[$i], StringLen($s_key) + 1)
    Next
    
    Return $s_default
EndFunc

; #FUNCTION# ==============================================================
; Name...........: _IniMem_ReadSection
; Description ...: Reads all key/value pairs from a section in a standard format ini string
; Syntax.........: _IniMem_ReadSection($s_ini, $s_Section)
; Parameters ....: $s_ini - The ini string
;                  $s_Section  - The section you want to read
; Return values .: Success - a 2 dimensional array where element[n][0] is the key and element[n][1] is the value, [0][0] is the number of elemnts
;                  Failure - [0][0] = 0 and sets error to 1
; Author ........: Alek
; =========================================================================
Func _IniMem_ReadSection($s_ini, $s_Section)
    Local $r_Array[1][2]
    $s_ini = StringSplit($s_ini, @CRLF)
    
    For $x = 1 To $s_ini[0]
        If $s_ini[$x] = "[" & $s_Section & "]" Then ExitLoop
    Next
    
    If $x > $s_ini[0] Then Return SetError(1)
    
    For $i = $x+1 To $s_ini[0]
        If StringLeft($s_ini[$i],1) = "[" And StringRight($s_ini[$i],1) = "]" Then ExitLoop
        If $s_ini[$i] = "" Then ContinueLoop
        
        ReDim $r_Array[UBound($r_Array, 1) + 1][2]
        $r_Array[UBound($r_Array, 1) - 1][0] = StringLeft($s_ini[$i], StringInStr($s_ini[$i], "=") - 1)
        $r_Array[UBound($r_Array, 1) - 1][1] = StringTrimLeft($s_ini[$i], StringInStr($s_ini[$i], "="))
        $r_Array[0][0] += 1
    Next
    
    Return $r_Array
EndFunc

; #FUNCTION# ==============================================================
; Name...........: _IniMem_ReadSectionNames
; Description ...: Reads all key/value pairs from a section in a standard format ini string
; Syntax.........: _IniMem_ReadSectionNames($s_ini)
; Parameters ....: $s_ini - The ini string
; Return values .: Success - an array of all section names in the INI string, [0] is the number of elemnts
;                  Failure - [0] = 0 and sets error to 1
; Author ........: Alek
; =========================================================================
Func _IniMem_ReadSectionNames($s_ini)
    Local $r_Array[1]
    $s_ini = StringSplit($s_ini,@CRLF)
    
    For $x = 1 To $s_ini[0]
        If $s_ini[$x] = "" Then ContinueLoop
        If StringLeft($s_ini[$x],1) = "[" And StringRight($s_ini[$x],1) = "]" Then
            ReDim $r_Array[UBound($r_Array, 1) + 1]
            $r_Array[UBound($r_Array, 1) - 1] = StringTrimLeft(StringTrimRight($s_ini[$x], 1), 1)
            $r_Array[0] += 1
        EndIf
    Next
    
    If $r_Array[0] = 0 Then SetError(1)
    
    Return $r_Array
EndFunc

; #FUNCTION# ==============================================================
; Name...........: IniMem_RenameSection
; Description ...: Renames a section in a standard format ini string
; Syntax.........:  _IniMem_RenameSection(ByRef $s_ini, $s_Old_Sec, $s_New_Sec, [$i_Flag])
; Parameters ....: $s_ini - The ini string
;                  $s_old_Sec - The section you want to rename
;                  $s_New_Sec - The new name of the section.
;                  $i_Flag - 0 Fail if "new section" already exists.
;                            1 Overwrite "new section". This will erase any existing keys in "new section"
; Return values .: Success - 1
;                  Failure - 0 (cant find old section)
; Author ........: Alek
; =========================================================================
Func _IniMem_RenameSection(ByRef $s_ini, $s_Old_Sec, $s_New_Sec, $i_Flag = 0)
    Local $s_temp_ini, $x, $i
    If $i_Flag = 0 And StringInStr($s_ini, "[" & $s_New_Sec & "]") Then Return 0
    
    ;$s_ini = StringReplace($s_ini,"[" & $s_Old_Sec & "]","[" & $s_New_Sec & "]", 1)
    $s_temp_ini = StringSplit($s_ini, @CRLF)

    For $x = 1 To $s_temp_ini[0]
        If $s_temp_ini[$x] = "[" & $s_Old_Sec & "]" Then
            $s_temp_ini[$x] = "[" & $s_New_Sec & "]"
            ExitLoop
        EndIf
    Next
    If $x > $s_temp_ini[0] Then Return 0
    
    If $i_Flag = 1 Then
        For $i = $x + 1 To $s_temp_ini[0]
            If $s_temp_ini[$x] = "[" & $s_New_Sec & "]" Then ExitLoop
        Next
        
        If $i > $s_temp_ini[0] Then Return 0
        
        For $x = $i+1 To $s_temp_ini[0]
            If StringLeft($s_temp_ini[$x], 1) = "[" And StringRight($s_temp_ini[$x], 1) = "]" Then ExitLoop
            $s_temp_ini[$x] = ""
        Next
    EndIf
    
    $s_ini = ""
    For $x = 1 To $s_temp_ini[0]
        If $s_temp_ini[$x] = "" Then ContinueLoop
        If StringLeft($s_temp_ini[$x], 1) = "[" And StringRight($s_temp_ini[$x],1) = "]" And $x > 1 Then
            $s_ini &= @CRLF & $s_temp_ini[$x] & @CRLF
        Else
            $s_ini &= $s_temp_ini[$x] & @CRLF
        EndIf
    Next
    
    Return 1
EndFunc

; #FUNCTION# ==============================================================
; Name...........: _IniMem_Write
; Description ...: Writes a value to a standard format ini string
; Syntax.........:  _IniMem_Write(ByRef $s_ini, $s_Section, $s_key, $s_value)
; Parameters ....: $s_ini - The ini string
;                  $s_Section - The section name in the ini string
;                  $s_Key - The key name in the in the ini string
;                  $s_Value - The value to write/change.
; Return values .: 1
; Author ........: Alek
; =========================================================================
Func _IniMem_Write(ByRef $s_ini, $s_Section, $s_key, $s_value)
    Local $s_temp_ini = StringSplit($s_ini, @CRLF)
    
    For $x = 1 To $s_temp_ini[0]
        If $s_temp_ini[$x] = "[" & $s_Section & "]" Then ExitLoop
    Next
    
    If $x > $s_temp_ini[0] Then
        ;$s_temp_ini[$s_temp_ini[0]] &= @CRLF & "[" & $s_Section & "]" & @CRLF & $s_key & "=" & $s_value
        $s_temp_ini[$s_temp_ini[0]] &= "[" & $s_Section & "]" & @CRLF & $s_key & "=" & $s_value ;looks better ;)
    Else
        For $i = $x+1 To $s_temp_ini[0]
            If StringLeft($s_temp_ini[$i], 1) = "[" And StringRight($s_temp_ini[$i],1) = "]" Then
                $x = $i-1
                $i = 0
                ExitLoop
            ElseIf StringLeft($s_temp_ini[$i], StringLen($s_key)) = $s_key Then
                $s_temp_ini[$i] = $s_key & "=" & $s_value
                ExitLoop
            EndIf
        Next
        
        If $i = 0 Then 
            $s_temp_ini[$x] &= @CRLF & $s_key & "=" & $s_value
        ElseIf $i > $s_temp_ini[0] Then
            $s_temp_ini[$s_temp_ini[0]] &= $s_key & "=" & $s_value
        EndIf
    EndIf
    
    $s_ini = ""
    For $x = 1 To $s_temp_ini[0]
        If $s_temp_ini[$x] = "" Then ContinueLoop
        If StringLeft($s_temp_ini[$x], 1) = "[" And StringRight($s_temp_ini[$x],1) = "]" And $x > 1 Then
            $s_ini &= @CRLF & $s_temp_ini[$x] & @CRLF
        Else
            $s_ini &= $s_temp_ini[$x] & @CRLF
        EndIf
    Next
    
    Return 1
EndFunc

; #FUNCTION# ==============================================================
; Name...........: _IniMem_WriteSection
; Description ...: Writes a section to a standard format ini string
; Syntax.........:  _IniMem_WriteSection(ByRef $s_ini, $s_Section, $s_data, [$i_Index])
; Parameters ....: $s_ini - The ini string
;                  $s_Section - The section name in the ini string
;                  $s_Data - The data to write. The data can either be a string or an array.
;                            If the data is a string, then each key=value pair must be delimited by @LF.
;                            If the data is an array, the array must be 2-dimensional and the second dimension must be 2 elements.
;                  $i_index - If an array is passed as data, this specifies the index to start writing from.
;                             By default, this is 1 so that the return value of IniReadSection() can be used immediately.
;                             For manually created arrays, this value may need to be different depending on how the array was created.
;                             This parameter is ignored if a string is passed as data.
; Return values .: Success - 1
;                  Failure - 0 (Invalid data format)
;                            Error - 1 The data array is not 2d
;                            Error - 2 The array is smaller then $i_Index
;                            Error - 3 The data array's second dimension is to small
; Author ........: Alek
; =========================================================================
Func _IniMem_WriteSection(ByRef $s_ini, $s_Section, $s_data, $i_Index = 1)
    Local $s_key, $s_value
    If IsArray($s_data) Then
        If UBound($s_data, 0) <> 2 Then Return SetError(1, Default, 0)
        If UBound($s_data, 1) - 1 > $i_Index Then Return SetError(2, Default, 0)
        If UBound($s_data, 2) < 2 Then Return SetError(3, Default, 0)
        
        For $x = $i_Index To UBound($s_data, 1) - 1
            _IniMem_Write($s_ini, $s_Section, $s_data[$x][0], $s_data[1])
        Next
    Else
        $s_data = StringSplit($s_data, @LF)
        If @error Then Return 0
        
        For $x = 1 To $s_data[0]
            $s_key = StringLeft($s_data[$x],StringInStr($s_data[$x],"=")-1)
            $s_value = StringTrimLeft($s_data[$x],StringInStr($s_data[$x],"="))
            If $s_key Or $s_value = "" Then ContinueLoop
            _IniMem_Write($s_ini, $s_Section, $s_key , $s_Value)
        Next
    EndIf
    Return 1
EndFunc

; #FUNCTION# ==============================================================
; Name...........: _IniMem_Cleanup
; Description ...: Cleans up a ini format string, will proberly add more features later
; Syntax.........:  _IniMem_Cleanup(ByRef $s_ini)
; Parameters ....: $s_ini - The ini string
; Return values .: 1
; Author ........: Alek
; =========================================================================
Func _IniMem_Cleanup(ByRef $s_ini)
    Local $s_temp_ini, $x
    $s_temp_ini = StringSplit($s_ini, @CRLF)
    
    $s_ini = ""
    For $x = 1 To $s_temp_ini[0]
        If $s_temp_ini[$x] = "" Then ContinueLoop
        If StringLeft($s_temp_ini[$x], 1) = "[" And StringRight($s_temp_ini[$x],1) = "]" And $x > 1 Then
            $s_ini &= @CRLF & $s_temp_ini[$x] & @CRLF
        Else
            $s_ini &= $s_temp_ini[$x] & @CRLF
        EndIf
    Next
    
    Return 1
EndFunc

[font="Impact"]Never fear, I is here.[/font]

Link to comment
Share on other sites

Interresting idea :P

It might be useful when dealing very often with IniRead/IniWrite so the file read/write is really a time factor.

My Pro's and Con's:

Pro: Having the ini file in memory can speed up the execution

Con: If you're using IniWrite, write to memory and the script crashes, the modified Ini values won't get written on the file and that info will be lost.

You've put alot of work into this UDF and I respect that; thank you for sharing this UDF with us.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

UDFs for managing Ini format in memory.

They are very similar to the ini file functions (parameters and return values)

I found a set of function like these on the forum (i think) that used regexp, but ran into problems when using keys/values that containd symbols (like a file dir)

Bugs:

-Needs more testing...

Were these`the ones you found?

http://www.autoitscript.com/forum/index.ph...mp;#entry530284

Because I believe they work fine with symbols in the keys\values (if I do say so myself :P )

Link to comment
Share on other sites

  • 10 years later...
On 8/13/2008 at 4:21 AM, Alek said:

Bugs:

Very useful script, I'm using it here. I found a bug:
In case of:

[SectionName]
textany=something
text=something else

Then the returned Key for for the Value "text" is wrong. It's like the function stuck on the Value "textany" because of the same initial string.

Link to comment
Share on other sites

Hello masvil, look at the last post date .... from 2008. AutoIt has changed a lot since then, so there could well be compatibility issues.

So you have revived an essentially dead topic, which is called necro posting ... and seriously frowned upon here.

The acceptable procedure, is to start a new topic of your own, and then if necessary, link back to the dead topic.

That's if it is at all relevant to do so, considering the age of the code.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

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...