Jump to content

_IniString Functions (was Ini_InMem)


ResNullius
 Share

Recommended Posts

  • Moderators

Your expression pattern is... well, it's not right.

Your example also will fail or return bad data if that section doesn't have the "key" but a later section does.

Global $gs_Ini = ""
$gs_Ini &= "[Section1]" & @CRLF
$gs_Ini &= "RandomKey=Apple" & @CRLF
$gs_Ini &= "[Section2]" & @CRLF
$gs_Ini &= "DontReadThisKeyInThisKey=Opps Failure!" & @CRLF

Global $gs_ReturnData = _IniString_Read($gs_Ini, "Section1", "DontReadThisKeyInThisKey")
ConsoleWrite($gs_ReturnData & @CRLF)

Func _IniString_Read($sIni, $sSection, $sKey, $sDefault = "")
    ;by Arcker
    ; On 08/04/2011
    ; Only one regexp to get the value !
    ; execution < 1 ms
    Local $sSect = StringRegExp($sIni, "(?i)(?s)(?m)(?:.*)(\["& $sSection &"\].*?^"&$sKey&"=)(.+?$)",1) ; Get entire $sSection section as string.
    If @error Then
        Return $sDefault
    Else
        Return StringStripCR($sSect[1])
    EndIf

EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

mmm didn't think about it, the "cross-section" value. Will try to add some check.

For the pattern, what is "not right" ? I've just started to use pattern so i've to learn a lot.

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

corrected version, but doesn't support if a section is commented out between two sections.

need to escape lines beginning with #

Global $gs_Ini = ""
$gs_Ini &= "[Section1]" & @CRLF
$gs_Ini &= "RandomKey=Apple" & @CRLF
$gs_Ini &= "[Section2]" & @CRLF
$gs_Ini &= "DontReadThisKeyInThisKey=Failure" & @CRLF
$gs_Ini &= "#[Section3]" & @CRLF
$gs_Ini &= "DontReadThisKeyInThisKeyPlz=I do what I want" & @CRLF
$gs_Ini &= "[Section4]" & @CRLF
$gs_Ini &= "DontReadThisKeyInThisKeyPlz=Failure 3" & @CRLF
Global $gs_ReturnData = _IniString_Read($gs_Ini, "Section1", "RandomKey")
ConsoleWrite($gs_ReturnData & @CRLF)
Global $gs_ReturnData = _IniString_Read($gs_Ini, "Section1", "DontReadThisKeyInThisKey")
ConsoleWrite($gs_ReturnData & @CRLF)
Global $gs_ReturnData = _IniString_Read($gs_Ini, "Section3", "DontReadThisKeyInThisKeyPlz")
ConsoleWrite($gs_ReturnData & @CRLF)

Func _IniString_Read($sIni, $sSection, $sKey, $sDefault = "")
    ;by Arcker
    ; On 08/04/2011
    ; Only one regexp to get the value !
    ; execution < 1 ms
    Local $sSect = StringRegExp($sIni, "(?i)(?s)(?m)(?:.*)(\["& $sSection &"\].[^\[]*?"&$sKey&"=)(.+?$)",1) ; Get entire $sSection section as string.
    If @error Then
        Return $sDefault
    Else
        Return StringStripCR($sSect[1])
    EndIf

EndFunc

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

  • 1 year later...

I believe I've found a bug. For the key named "DownloadURL32" the UDF will match the "DownloadURL32", "URL32" and "32" (well, the UDF will match string of characters right to left).

I'll post the fix if I'm successful

[RemoteSupport]
DownloadURL32=1234

$INI_RemoteSupport_DownloadURL32 = _IniString_Read($tmpGuardianINI, "RemoteSupport", "DownloadURL32", "")
$INI_RemoteSupport_DownloadURL32 = _IniString_Read($tmpGuardianINI, "RemoteSupport", "URL32", "")
$INI_RemoteSupport_DownloadURL32 = _IniString_Read($tmpGuardianINI, "RemoteSupport", "32", "")

All return the value "1234"

Edited by lowray1975
Link to comment
Share on other sites

  • 5 years later...

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