This is a simple example of how to lookup information in an INI file using Matrix1 Utilities
CharacterData = 1
Make Lookup From INI CharacterData, "Character.ini"
TotalCharacters = Val( Lookup$( CharacterData, "Characters/Count", "0" ) )
For i = 1 to TotalCharacters
Print Lookup$( CharacterData, "Character" + Str$(i) + "/Name", "Unnamed" )
Next i
Print "Press any key to exit"
Wait Key
These are extra functions that you may find useful when reading information that requires constraints
Lookup#( iLookupId, sKey$, fDefault#, fMin#, fMax# )
Get a number from a lookup table and clamp it in range.
For example: If you need a number between 0 and 10
and the lookup (or INI) field specifies 1000.
the function will return 10. If -2 was received, it will
return 0. If the field does not exist it will return the
default parameter value
Lookup( iLookupId, sKey$, iDefault, iMin, iMax )
This is an integer version of the Lookup# function
LookupBool( iLookupId, sKey$ )
DBPRO will not clamp boolean values between 0 and 1. This
lookup function can only return 0 or 1. If any lookup value
is below zero, then zero is returned. If any value is higher
than zero, then you will get back a value of 1.
LookupColour( iIni, sColourKey$, iDefault )
This function will return a colour value from a comma seperated
RGB or ARGB field. For example, a string with "255,255,255" will
cause the function to return an integer value representing white.
A value of "128,255,0,0" will return an integer value representing
a semi-transparent red. If you use only two numbers, such as "128,255"
then the function will use the first to represent the alpha
transparency, and the latter will represent a grade of grey.
CurrentLookupByte( iLookupId )
CurrentLookupBool( iLookupId )
CurrentLookupValue( iLookupId )
These three functions return numeric values from the current lookup string. The current lookup is set using First Lookup, Next Lookup and other similar functions outlined in the Matrix1 utility 31 documentation.
LookupP( iLookup, sKey$,fDefX#,fDefY#,fDefZ#,fMin#, fMax# )
This function requires the use of the attached UDT called
PosType. It will store the position specified in a comma
seperated string, such as '0,10,20', and store it in the
X#, Y# & Z# fields of the NewPos UDT variable, which you
must initiate before using this function.
The function will also limit the position to a bounding
area so that your positions do not go off course. There
is also a default position if the location was not specified
Note that if only 1 or 2 numbers are specified, only x or x & y
values are obtained respectively. The missing information is
defaulted to your default specification.
LookupPos( iLookup, sKey$,fDefX#,fDefY#,fDefZ#, arrayPtr, index )
Similar to LookupP, this time the function stores the position in a UDT array of the PosType class, which you need to pass a pointer for, using the Get ArrayPtr function. Note that if only 1 or 2 numbers are specified, only x or x & y values are obtained respectively. The missing information is defaulted to your default specification.
The functions:
//==========================================================
` Get a number from a lookup table and clamp it in range.
` For example: If you need a number between 0 and 10
` and the lookup (or INI) field specifies 1000.
` the function will return 10. If -2 was received, it will
` return 0. If the field does not exist it will return the
` default parameter value
Function Lookup#( iLookupId, sKey$, fDefault#, fMin#, fMax# )
r# = Val( Lookup$( iLookupId, sKey$, Str$(fDefault#) ) )
r# = Clamp( r#, fMin#, fMax#)
Endfunction r#
//==========================================================
` DBPRO will not clamp boolean values between 0 and 1. This
` lookup function can only return 0 or 1. If any lookup value
` is below zero, then zero is returned. If any value is higher
` than zero, then you will get back a value of 1.
Function LookupBool( iLookupId, sKey$ )
r = Val( Lookup$( iLookupId, sKey$, "0" ) )
r = Clamp( r, 0, 1)
Endfunction r
//==========================================================
` This is an integer version of the Lookup# function
Function Lookup( iLookupId, sKey$, iDefault, iMin, iMax )
r = IntVal( Lookup$( iLookupId, sKey$, Str$(iDefault) ) )
r = Clamp( r, iMin, iMax)
Endfunction r
//==========================================================
` This function will return a colour value from a comma seperated
` RGB or ARGB field. For example, a string with "255,255,255" will
` cause the function to return an integer value representing white.
` A value of "128,255,0,0" will return an integer value representing
` a semi-transparent red. If you use only two numbers, such as "128,255"
` then the function will use the first to represent the alpha
` transparency, and the latter will represent a grade of grey.
Function LookupColour( iIni, sColourKey$, iDefault )
s$ = Lookup$( iIni, sColourKey$, "0" )
iColour = iDefault
If s$ = "0"
ExitFunction iDefault
Else
If InStr( s$, ",") > 0
Split String s$, ","
Select Split Count()
Case 2
iColour = Rgb( Val(Split Word$(1)), Val(Split Word$(2)), Val(Split Word$(2)), Val(Split Word$(2)) )
Endcase
Case 3
iColour = Rgb( 255, Val(Split Word$(1)), Val(Split Word$(2)), Val(Split Word$(3)) )
Endcase
Case 4
iColour = Rgb( Val(Split Word$(1)), Val(Split Word$(2)), Val(Split Word$(3)), Val(Split Word$(4)) )
Endcase
Case Default
ExitFunction iDefault
Endcase
Endselect
Else
iColour = Val(s$)
Endif
Endif
Endfunction iColour
//==========================================================
` This will convert the current lookup value to a byte; which
` will be returned as a value equal or greater than 0, up to
` the value of 255. The current lookup field is set using
` First Lookup, Next Lookup or similar functions outlined in
` the Matrix1 utilities
Function CurrentLookupByte( iLookupId )
Local r as Byte
r = Clamp( Val( Lookup Current Value$( iLookupId ) ), 0, 255)
Endfunction r
//==========================================================
` Similar to the CurrentLookupByte function, only a 0 or 1 is returned
Function CurrentLookupBool( iLookupId )
Local r as Byte
r = Clamp( Val( Lookup Current Value$( iLookupId ) ), 0, 1)
Endfunction r
//==========================================================
` Returns the lookup string as a floating point value
Function CurrentLookupValue( iLookupId )
r# = Val( Lookup Current Value$( iLookupId ) )
Endfunction r#
//==========================================================
` This function requires the use of the attached UDT called
` PosType. It will store the position specified in a comma
` seperated string, such as '0,10,20', and store it in the
` X#, Y# & Z# fields of the NewPos UDT variable, which you
` must initiate before using this function.
` The function will also limit the position to a bounding
` area so that your positions do not go off course. There
` is also a default position if the location was not specified
`
` Note that if only 1 or 2 numbers are specified, only x or x & y
` values are obtained respectively. The missing information is
` defaulted to your default specification.
Function LookupP( iLookup, sKey$,fDefX#,fDefY#,fDefZ#,fMin#, fMax# )
Split CSV String Lookup$( iLookup, sKey$, "0,0,0")
iWords = Split Count()
If 1 <= iWords
NewPos.x# = Max( fMin#, Min( fMax# , Val( Split Word$(1) ) ) )
Else
NewPos.x# = fDefX#
Endif
If 2 <= iWords
NewPos.y# = Max( fMin#, Min( fMax# , Val( Split Word$(2) ) ) )
Else
NewPos.y# = fDefY#
Endif
If 3 <= iWords
NewPos.z# = Max( fMin#, Min( fMax# , Val( Split Word$(3) ) ) )
Else
NewPos.z# = fDefZ#
Endif
EndFunction
//==========================================================
` Similar to LookupP, this time the function stores the position
` in a UDT array of the PosType class, which you need to pass a
` pointer for, using the Get ArrayPtr function.
` Note that if only 1 or 2 numbers are specified, only x or x & y
` values are obtained respectively. The missing information is
` defaulted to your default specification.
Function LookupPos( iLookup, sKey$,fDefX#,fDefY#,fDefZ#, arrayPtr, index )
Link Array npos(), arrayPtr
iCount = Array Count( npos() )
If index < 0 or npos > iCount
` Error message here
Endif
Split CSV String Lookup$( iLookup, sKey$, "0,0,0")
iWords = Split Count()
If 1 <= iWords
npos(index).x# = Val( Split Word$(1) )
Else
npos(index).x# = fDefX#
Endif
If 2 <= iWords
npos(index).y# = Val( Split Word$(2) )
Else
npos(index).y# = fDefY#
Endif
If 3 <= iWords
npos(index).z# = Val( Split Word$(3) )
Else
npos(index).z# = fDefZ#
Endif
Unlink Array npos()
EndFunction arrayPtr
PosType UDT:
Type PosType
x#
y#
z#
Endtype
Global NewPos as PosType