Sign in to follow this  
Followers 0
Valik

Scriptable config files

1 post in this topic

Here's something cute that I whipped up. It's undocumented, barely test, but functional. I'm sure some you can figure it out on your own, which is why I'm posting it now rather than after I document it (As I'm lazy and that might take awhile).

This exapnds variables in strings. I wrote it manually so I could use Ini files that look like this (NOTE: This is the INI file used in the test, so copy it and put it in the same directory as the script to test with, name it "Temp.ini"):

[Test]

Drive=C:\

Dir=Documents and Settings

Path=$(Drive)$(Dir)

Exe=Cool.exe

FullPath=$(Path)\$(Exe)

And here is the test script (Watch wordwrap):

Opt("MustDeclareVars", 1)

Global $__g_MapTable
Global $szPath = IniRead("Temp.ini", "Test", "Path", "ERROR")
Global $szExe = IniRead("Temp.ini", "Test", "Exe", "ERROR")
Global $szFullPath = IniRead("Temp.ini", "Test", "FullPath", "ERROR")
Global $szDrive = IniRead("Temp.ini", "Test", "Drive", "ERROR")
Global $szDir = IniRead("Temp.ini", "Test", "Dir", "ERROR")

Map_PushBack("Path", "szPath")
Map_PushBack("Exe", "szExe")
Map_PushBack("FullPath", "szFullPath")
Map_PushBack("Drive", "szDrive")
Map_PushBack("Dir", "szDir")

Map_ExpandString($szFullPath)
MsgBox(4096, "", $szFullPath)

; Begin Function Definitions
Func Map_ExpandString(ByRef $s)
    While StringInStr($s, "$(")
        Local $start = StringInStr($s, "$(")
        Local $end = StringInStr($s, ")")
        Local $i
        If Not $start Or Not $end Then Return
        If $end < $start Or StringInStr($s, ")", 0, 2) Then
            For $i = $start+2 To StringLen($s)
                If StringMid($s, $i, 1) = ")" Then ExitLoop
            Next
            $end = $i
        EndIf
        Local $key, $value
        $key = StringMid($s, $start+2, $end-$start-2)
        Map_GetValue($key, $value)
        If @error Then $value = $key
        $s = StringLeft($s, $start-1) & $value & StringRight($s, StringLen($s)-$end)
    Wend
EndFunc
    
Func SpecReDim(ByRef $array, $nNewSize)
    If $nNewSize < 1 Then   ; Invalid size, set @error and return
        SetError(1)
        Return
    EndIf
    Local $nArraySize = UBound($array, 1)   ; Get the size of the array
    If $nArraySize < 1 Then; It's not an array, so make it an array and return
        Dim $array[$nNewSize][2]
        Return
    EndIf
    Dim $aTmp[$nNewSize][2]   ; Temporary array used to store elements
    Local $nLowest   ; The most elements both arrays can have in common, prevents overflow in FOR loop
    If $nArraySize < $nNewSize Then   ; New size is bigger
        $nLowest = $nArraySize
    Else   ; New size is smaller, truncation will ocurr...
        $nLowest = $nNewSize    
    EndIf
    Local $i
    For $i = 0 To $nLowest-1   ; FOR loop to copy the contents
        $aTmp[$i][0] = $array[$i][0]
        $aTmp[$i][1] = $array[$i][1]
    Next
    $array = $aTmp   ; Set the original array to the newly resized array
EndFunc   ; SpecReDim()

Func Map_PushBack($key, $value)
    If Not IsArray($__g_MapTable) Then 
        Dim $__g_MapTable[1][2]
    Else
        SpecReDim($__g_MapTable, UBound($__g_MapTable, 1)+1)
    EndIf
    $__g_MapTable[UBound($__g_MapTable, 1)-1][0] = $key
    $__g_MapTable[UBound($__g_MapTable, 1)-1][1] = $value
EndFunc

Func Map_GetValue($key, ByRef $value)
    $value = ""
    If Not IsArray($__g_MapTable) Then Return SetError(2)
    Local $i
    For $i = 0 To UBound($__g_MapTable, 1)-1
        If $key = $__g_MapTable[$i][0] Then
            $value = Eval($__g_MapTable[$i][1])
            Return
        EndIf
    Next
    SetError(1)
EndFunc

Share this post


Link to post
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
Sign in to follow this  
Followers 0