udfArrayGetIniValue
str udfArrayGetIniValue (arr, str, str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayGetIniValue (arrIni, strSection, strKey)
If strKey == "" Then Return ""
If ArrInfo (arrIni, 2) < 32 Then Return ""
intItemLast = ArrInfo (arrIni, 1) - 1
If intItemLast < 0 Then Return ""
intSearchOpt = 2
If intItemLast > 3 ; This value should be adjusted to the point, where binary search will be faster than linear search.
   ArraySort (arrIni, @ASCENDING, 1)
   intSearchOpt = 3
EndIf
intRow = ArraySearch (arrIni, strKey, intSearchOpt, 1, 0)
If intRow == -1 Then Return ""
If 0 == StriCmp (arrIni[intRow, 0], strSection) Then Return arrIni[intRow, 2]
; Linear search forward.
intRowLin = intRow
While intRowLin < intItemLast
   intRowLin = intRowLin + 1
   intRowLin = ArraySearch (arrIni, strKey, 2, 1, intRowLin)
   If intRowLin == -1 Then Break
   If 0 == StriCmp (arrIni[intRowLin, 0], strSection) Then Return arrIni[intRowLin, 2]
EndWhile
If intSearchOpt == 2 Then Return ""
; Linear search backward.
intRowLin = intRow
While intRowLin > 0
   intRowLin = intRowLin - 1
   If arrIni[intRowLin, 1] != strKey Then Break
   If 0 == StriCmp (arrIni[intRowLin, 0], strSection) Then Return arrIni[intRowLin, 2]
EndWhile
Return ""
;..........................................................................................................................................
; This UDF "udfArrayGetIniValue" returns the value for the given key of the given section of a dim-2 "ini-style" array.
;
; Parameters:
;
;   arrIni      ... The dim-2 array with at least 3 columns.
;                   Column 0 holds the section name.
;                   Column 1 holds the key name.
;                   Column 2 holds the value of the key.
;
;   strSection  ... The section name.
;
;   strKey      ... The key name.
;
; Example:
;
;   The dim-2 array ... not sorted ...       The dim-2 array ... sorted ...
;
;   +----+----------+-------+-----------+    +----+----------+-s-asc-+-----------+
;   |    | c0       | c1    | c2        |    |    | c0       | c1    | c2        |
;   +----+----------+-------+-----------+    +----+----------+-------+-----------+
;   | r0 | COLORS   | CON   | 128,0,128 |    | r0 | KEYWORDS | About | 1         |
;   | r1 | COLORS   | EXT   | 255,0,255 |    | r1 | DUP_1    | About | 11        |
;   | r2 | KEYWORDS | About | 1         |    | r2 | DUP_2    | About | 222       |
;   | r3 | KEYWORDS | Abs   | 2         |    | r3 | KEYWORDS | Abs   | 2         |
;   | r4 | KEYWORDS | Acos  | 3         |    | r4 | KEYWORDS | Acos  | 3         |
;   | r5 |          | Key   | Value     |    | r5 | COLORS   | CON   | 128,0,128 |
;   | r6 | DUP_1    | About | 11        |    | r6 | COLORS   | EXT   | 255,0,255 |
;   | r7 | DUP_2    | About | 222       |    | r7 |          | Key   | Value     |
;   +----+----------+-------+-----------+    +----+----------+-------+-----------+
;
;..........................................................................................................................................
; (c)Detlev Dalitz.20110405.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

arrIni = ArrDimension (8, 3)
arrIni[0, 0] = "COLORS"
arrIni[0, 1] = "CON"
arrIni[0, 2] = "128,0,128"
arrIni[1, 0] = "COLORS"
arrIni[1, 1] = "EXT"
arrIni[1, 2] = "255,0,255"
arrIni[2, 0] = "KEYWORDS"
arrIni[2, 1] = "About"
arrIni[2, 2] = "1"
arrIni[3, 0] = "KEYWORDS"
arrIni[3, 1] = "Abs"
arrIni[3, 2] = "2"
arrIni[4, 0] = "KEYWORDS"
arrIni[4, 1] = "Acos"
arrIni[4, 2] = "3"
arrIni[5, 0] = ""
arrIni[5, 1] = "Key"
arrIni[5, 2] = "Value"
arrIni[6, 0] = "DUP_1"
arrIni[6, 1] = "About"
arrIni[6, 2] = "11"
arrIni[7, 0] = "DUP_2"
arrIni[7, 1] = "About"
arrIni[7, 2] = "222"


:Test1
strSection1 = "KEYWORDS"
strKey1 = "About"
strValue1 = udfArrayGetIniValue (arrIni, strSection1, strKey1) ; "1"


:Test2
strSection2 = "DUP_1"
strKey2 = "About"
strValue2 = udfArrayGetIniValue (arrIni, strSection2, strKey2) ; "11"


:Test3
strSection3 = "colors"
strKey3 = "con"
strValue3 = udfArrayGetIniValue (arrIni, strSection3, strKey3) ; "128,0,128"


:Test4
strSection4 = ""
strKey4 = "Key"
strValue4 = udfArrayGetIniValue (arrIni, strSection4, strKey4) ; "Value"


:Test5
strSection5 = "colors"
strKey5 = ""
strValue5 = udfArrayGetIniValue (arrIni, strSection5, strKey5) ; ""

:CANCEL
Exit