Welcome to PenUltima Online. Click to login or register.

File: CFGfile.em

Description: Functions for accessing configuration files.
Last Modified: 10/27/2007

AppendConfigFileElem( filename, elemtype, elemkey, properties )
Parameters:
Name Type
filename String
elemtype String
elemkey Int or String
properties Array of Arrays {propname,value}, or array of structs { name, value }
Explanation
Appends an element to the config file specified. Properties must be an array of structures, where each element is a structure containing a name member first, and a value member second.
filename uses normal package descriptor format (see ReadConfigFile).
elemtype is the string before the key in the config file, i.e. 'MyType' in the following:
MyType 0x34F
{
    MyProp 3.1
}
elemkey is 0x34F in the above example. It can be a string or integer (as in that case)
properties must be an array of arrays with the following format:
var props := array;
var a := {"MyProp1",1};
var b := {"MyProp2",2};
props.append(a);
props.append(b);
AppendConfigFileElem( ":mypkg:mycfg", "MyType", 0x34F, props );
Return values
1 on success
Errors
"Unable to find package X"
"Poorly formed config file descriptor: X"
"Invalid parameter type"
FindConfigElem( config_file, key )
Parameters:
Name Type
config_file ConfigFileRef
key String or Integer
Explanation
Finds a Config Element matching 'key' in the given config_file. In the following sample entry, 'applepie' is the key:
Dessert applepie
{
    Cost 8
    Calories 1004
    MadeLike   grandma
    Ingredient flour
    Ingredient butter
    Ingredient apples
    Deliciousness 3.6
}
Return values
A Config Element Reference on success
Errors
"Parameter 0 must be a Config File"
"Param 1 must be an Integer or a String"
"Element not found"
GetConfigInt( element, property_name )
Parameters:
Name Type
element ConfigElementRef
property_name String
Explanation
Gets a piece of Integer data from a config file element reference. For example, to get the value of 'Calories' in the following element:
Dessert applepie
{
    Cost 8
    Calories 1004
    MadeLike   grandma
    Ingredient flour
    Ingredient butter
    Ingredient apples
    Deliciousness 3.6
}
Use: 'var cals := GetConfigInt(element,"Calories");'
Return values
An Integer value for the property
Errors
"Invalid type in config file! (internal error)"
"Property not found"
"Invalid parameter type"
GetConfigIntKeys( config_file )
Parameters:
Name Type
config_file ConfigFileRef
Explanation
Gets an array of only all the Integer keys in the config file.
Return values
An array of integers
Errors
"GetConfigIntKeys param 0 must be a Config File"
GetConfigMaxIntKey( config_file )
Parameters:
Name Type
config_file ConfigFileRef
Explanation
Gets the value of the highest value integer key in the config file. This is useful if all the keys are sequentially numbered and need to be looped over.
Return values
The maximum integer key in the file
Errors
"Parameter 0 must be a Config File"
GetConfigReal( element, property_name )
Parameters:
Name Type
element ConfigElementRef
property_name String
Explanation
Gets a piece of Real (float) data from a config file element reference. For example, to get the value of 'Deliciousness' in the following element:
Dessert applepie
{
    Cost 8
    Calories 1004
    MadeLike   grandma
    Ingredient flour
    Ingredient butter
    Ingredient apples
    Deliciousness 3.6
}
Use: 'var yum := GetConfigReal(element,"Deliciousness");'
Return values
An Real value for the property
Errors
"Invalid type in config file! (internal error)"
"Property not found"
"Invalid parameter type"
GetConfigString( element, property_name )
Parameters:
Name Type
element ConfigElementRef
property_name String
Explanation
Gets a piece of String data from a config file element reference. For example, to get the value of 'MadeLike' in the following element:
Dessert applepie
{
    Cost 8
    Calories 1004
    MadeLike   grandma
    Ingredient flour
    Ingredient butter
    Ingredient apples
    Deliciousness 3.6
}
Use: 'var madelike := GetConfigString(element,"MadeLike");'
Return values
An Real value for the property
Errors
"Invalid type in config file! (internal error)"
"Property not found"
"Invalid parameter type"
GetConfigStringArray( element, property_name )
Parameters:
Name Type
element ConfigElementRef
property_name String
Explanation
For elements with multiple occurrences of a given property, returns an array containing each occurrence.
For example, to get all the ingredients in the following element:
Dessert applepie
{
    Cost 8
    Calories 1004
    MadeLike   grandma
    Ingredient flour
    Ingredient butter
    Ingredient apples
    Deliciousness 3.6
}
Use: 'var ingredients := GetConfigStringArray(element,"Ingredient");'
'ingredients' now contains 'flour', 'butter', and 'apples'.
Value of the properties are always returned as strings. If the values are ints or reals, use CInt() or CDbl() to cast the values to the correct type.
Return values
An array of strings.
Errors
"Invalid parameter type"
GetConfigStringDictionary( element, property_name )
Parameters:
Name Type
element ConfigElementRef
property_name String
Explanation
Works similarly to GetConfigStringArray() but will take the first word on the line and use it for the key.
For example, to get all the ingredients in the following element:
Dessert applepie
{
    Cost 8
    Calories 1004
    MadeLike   grandma
    Ingredient flour 1 kg
    Ingredient butter 2 kg
    Ingredient apples 3 kg
    Deliciousness 3.6
}
Use: 'var ingredients := GetConfigStringDictionary(element,"Ingredient");'
'ingredients' now contains ['flour'] = '1 kg', ['butter'] = '2 kg', and ['apples'] = '3 kg'.
Value of the properties are always returned as strings. If the values are ints or reals, use CInt() or CDbl() to cast the values to the correct type.
Return values
Dictionary with Strings
Errors
"Invalid parameter type"
GetConfigStringKeys( config_file )
Parameters:
Name Type
config_file ConfigFileRef
Explanation
Gets an array of all the String keys, AND integer keys (returned as strings) in the config file.
Return values
An array of strings
Errors
"GetConfigStringKeys param 0 must be a Config File"
ListConfigElemProps( element )
Parameters:
Name Type
element ConfigElementRef
Explanation
Gets an array of the element's property-names (as unique strings)
Return values
An array of strings
Errors
"Invalid parameter type"
LoadTusScpFile( filename )
Parameters:
Name Type
filename String, see explaination
Explanation
Very Old Function - may not work any longer!
    // 
    // Load a TUS .SCP file as a config file
    //
    // Filename must be alphanumeric only.  The file must exist
    // in pol/import/tus.  So LoadTusScpFile( "grayacct" ) will
    // try to load "/pol/import/tus/grayacct.scp"
    // 
    // Header properties, if any, in cfgfile[0]
    // Other elements numbered sequentially from 1
    // The part in brackets ("[WORLDITEM 04000]") will appear in two ways:
    //   1) as a "_key" property
    //   2) as a property itself (name=WORLDITEM, value=0x4000)
    //
    //
    // So,if you had the following as the first element
    //   [WORLDITEM 04000]
    //   SERIAL=080
    //   P=1,2,3
    // And 'var tusfile' was the result of LoadTusScpFile, then
    // tusfile[1]._key      is "WORLDITEM 04000"
    // tusfile[1].worlditem is 0x4000
    // tusfile[1].serial    is 0x80
    // tusfile[1].p         is "1,2,3"
    //
Return values
A Config File Reference
Errors
"Invalid parameter type"
"Filename cannot include path information or special characters"
"File not found"
ReadConfigFile(filename)
Parameters:
Name Type
filename String (not including '.cfg')
Explanation
Reads a given config file and returns a reference to it for use with other functions in this EM file.
Note, takes the usual package filespec: '::cfgfile' is in /pol/config, '::regions/cfgfile' is in /pol/regions, ':*:cfgfile' is a combination of all configs named 'cfgfile' in all pacakages, and ':pkgname:cfgfile' is a specific cfgfile inside a package. If only 'cfgfile' is passed, POL looks for it first in /pol/config and then in each package sequentially.
Return values
A ConfigFileRef on success.
Errors
"Unable to find package X"
"Poorly formed config file descriptor: X"
"Config file not found"
"Invalid parameter type"
UnloadConfigFile( filename )
Parameters:
Name Type
filename String
Explanation
Unloads a config file from memory. If other scripts reference the config file specified, it will remain in memory until they no longer reference it. Scripts which call ReadConfigFile() after this, however, will re-read the config file and use the newer version.
Has no effect on most core-read config files like itemdesc.cfg, npcdesc.cfg, etc (See ReloadConfiguation in polsys.em)
filename uses the normal package descriptor format (see ReadConfigFile)
Return values
Integer number of files unloaded
Errors
"Unable to find package X"
"Poorly formed config file descriptor: X"
"Invalid parameter"