[Package Index | Mudlib Index | Effect Index]

File /obj/handlers/data.c

Universal data initialization handler. The data initializer can be used to initialize databases for use in other handlers. The database is defined in a user-provided text file, with a format similar to the virtual object compiler. The input file is converted to a temporary object, which allows fairly complicated expressions to be used as data values. The initializer can handle arrays and mappings (nested to any level (theoretically)), with a base element of any type, including mixed and classes.

Added in some code to allow it to compile lisp like files into a mapping as well.

To initialize a variable, assign it the value returned by compile_data() in the initializer. compile_data() takes an array of filenames as its only argument.

File format The data file uses the following keywords. Each keyword is followed by the required data. The data can be spread over multiple lines, following the same rules as for LPC code, but the keywords must be at the beginning of the line (preceded by optional whitespace).

::data:: < array | mapping > [ of ] ... < base >
This keyword defines the structure of the data. The word "of" is optional. "array" and "mapping" may be repeated as many times as desired. "base" is the base type of the data. For classes, it would be of the form "class ". For types other than classes, the base isn't really used at this time, but something needs to be there to keep the parser in line. Some examples:
::data:: array of mapping of array of int
::data:: mapping of mapping of mixed
::data:: mapping of array of class myclass
There should only be one ::data:: keyword in the list of files passed to compile_data(). Also, note that classes need to be defined before this statement. This can be done either with ::quote:: or ::#include::.
::item [,] ... :: [ value ]
This keyword defines the value for one item of the data. is repeated as often as necessary, given the structure declared in the ::data:: statement. For mappings, the index can be any valid mapping key. For arrays, the index can be either a number, or the strings i, i++, or ++i, for current index, current index (incrementing afterwards), or next index. The value can (and probably should) be omitted for classes, with the field values specified with the "->" keyword below. Examples (corresponding to the three ::data:: examples above):
::item 0, "item 1", 2:: 42
::item "a", "b":: ({ 1, "fred", ({ 2, 3 }) })
::item "x" i++::
::->::
This allows the fields of items of type class to be assigned individually. In general, the preceding ::item:: keyword should not have been given a value. The class must have been defined previously, either with an ::#include:: directive, or with the ::quote:: keyword. Examples:
::Quote::
class item_data {
    string *season;
    mixed  quant;
    string ob;
}

::Data:: mapping of class item_data

:item "white clover"::
::->season:: ({ "spring" })
::->quant:: ({ 3, 4 })
::->ob:: "/std/plant"
These statements set the season, quant, and ob fields of the mapping
::quote::
This keyword allows specific LPC statements to be inserted in the object that creates the database. To use this effectively requires a little understanding of the translation process. First, all lines associated with a given keyword are folded into one line. This means that using the "//" comment delimiter in a ::quote:: will cause the remainder of the quoted statements to be ignored. Second, the prototype of the function that returns the data isn't written until the ::data:: keyword is encountered. Therefore, any "global" statements (such as class definitions) should be included or quoted before the ::data:: line. The easiest way to see what's going on is to try a few examples and look at the resulting .c file (which is the first data file name with ".c" stuck on the end).

Written by Jeremy

Example

mixed data;
data = "/obj/handlers/data"->compile_data(({ "/save/file1.dat",
     "/save/file2.dat" }));
// This will catenate the two files into one, translate it, and return
// the data.  Of course, someone has to create the data files also.

Includes

This class includes the following files /include/data.h and /include/function.h

Class Index

Method index

Public Functions

These are functions that everyone can access.

compile_data
mixed compile_data(string * path)

Actualy compiles the files down. See the header file for a more detailed explaination

Parameters:
path - the files to parse


compile_file
void compile_file(string fname,
                  function call_back)

Compiles up the file into the useful soul format. It also tells the soul about it.

See the soul data files in /save/new_soul for details of the format for the soul files. The file has to come from the soul save directory or the call will not work.

Parameters:
fname - the name of the file to compile


to_diceint
mixed to_diceint(string str)

This will return a normal integer, or a dice class. The dice class allows for things of the form 5d6+10 or 3d11-10.

Returns:
the dice class


Classes

These are nice data types for dealing with... Data!