Next: , Previous: Defining Function Sets, Up: Functions


2.4.3 Function Look Up Example

The following very simple function look up method makes use of an array of function definitions list. The list is assumed to have one extra element for which the name field is NULL, marking the end of list.

The look up function uses sequential search to identify the variable.

static int
select_function(const char *f, unsigned length, const void *context,
                const struct x1f4_function_type **function)
{
    int status = X1f4_E4_PARSE_ERROR;
    const struct x1f4_function_type *function_data;

    function_data = context;
    if (function_data) {
        while (function_data->name) {
            if (length == function_data->length
                && !memcmp((void *) f, function_data->name, length)) {
                break;
            }
            function_data++;
        }
        if (function_data->name) {
            status = 0;
            *function = function_data;
        }
    }

    return status;
}

Setting up the expression parser becomes:

    struct x1f4_attributes_type attributes;

    ...

    attributes.function_set.get = select_function;
    attributes.function_set.context = <function definitions array>;

    ...

    status = x1f4_init_expression(..., ..., ..., &attributes);

See x1f4_init_expression.