Next: Numerical Utilities Up: Programming Utilities Previous: Argument Processing

Global Variables

This module provides support for defining global variables in such a way that there is a lookup table of global variable names. This table can be used to provide reading of global variables from files at runtime, or to allow the user to query and modify global variable values during program execution. In order to use this facility, the programmer must have (either directly or indirectly)


#include  <globals.h>
in every program file that refers to a global variable. The global variables must be defined in a file called global_variables.h which has the following form:

START_GLOBALS
    DEF_GLOBAL(        Mine_1, BOOLEAN,  TRUE )
    DEF_GLOBAL(        Mine_2, int,      3 )
    DEF_GLOBAL(        Mine_3, Real,     2.5 )
    DEF_GLOBAL(        Mine_4, Colour,   RED )
    DEF_GLOBAL5(       Mine_5, Surfprop,
                       0.3, 0.6, 0.6, 40.0, 1.0 )
    DEF_GLOBAL3(       Mine_6, Vector,   1.0, 0.0, 0.0 )
    DEF_GLOBAL3(       Mine_7, Point,    2.0, 2.0, 2.0 )
    DEF_GLOBAL_STRING( Mine_8, "Initial value" )
END_GLOBALS
The first argument of each of these macros is the name of the global variable. The second argument is the type of the global variable. Note that only the types in the previous example are supported. The final argument or arguments are the initial values of the global variable.

Finally, one needs to create the lookup table in one program file, usually the main program file, as follows:


#define  GLOBALS_LOOKUP_NAME  my_globals
#include  <globals.h>
which creates a global variable lookup table called my_globals (any name may be chosen for this table). Relevant functions for global variables are:


public  Status  input_globals_file(
    int             n_globals_lookup,
    global_struct   globals_lookup[],
    char            filename[] )

Inputs global variable assignments from the specified file. Assignments are of the form variable = value ;. The lookup table and its size are passed to this function. An example follows:


    if( input_globals_file( SIZEOF_STATIC_ARRAY(my_globals),
           my_globals, filename ) != OK )
    {
        print( "Error reading globals file.\n" );
    }


public  Status  get_global_variable(
    int              n_globals_lookup,
    global_struct    globals_lookup[],
    char             variable_name[],
    char             value[] )

Creates a string containing the value of the global variable matching the specified name, variable_name. For example, if the global variable my_variable is of type integer,


    if( get_global_variable( SIZEOF_STATIC_ARRAY(my_globals),
            my_globals, "my_variable", value_string ) == OK )
    {
        print( "Value is: %s\n", value_string );
    }
is equivalent to:

    print( "Value is: %d\n", my_variable );


public  Status  set_global_variable(
    int              n_globals_lookup,
    global_struct    globals_lookup[],
    char             variable_name[],
    char             value_to_set[] )

Sets the value of the global variable matching the specified name, variable_name, to the value specified by the ascii string value_to_set. For example, if the global variable my_variable is of type integer,


    if( set_global_variable( SIZEOF_STATIC_ARRAY(my_globals),
            my_globals, "my_variable", "45" ) != OK )
    {
        print( "Error setting variable.\n" );
    }
is equivalent to

    my_variable = 45;


public  Status  set_or_get_global_variable(
    int              n_globals_lookup,
    global_struct    globals_lookup[],
    char             input_str[],
    char             variable_name[],
    char             value_string[] )

If the input_str is of the form ``variable = value'', then the corresponding global variable is set to the value, and the value is passed back in ascii form in the value_string argument. Otherwise, the form of input_str is simply ``variable'' and the corresponding global variable is not changed, but its current value is passed back in the value_string argument. This routine is useful in many programs to read input lines from the user, allowing the user to query and set global variable values during program execution.



Next: Numerical Utilities Up: Programming Utilities Previous: Argument Processing


david@pet.mni.mcgill.ca
Fri Feb 17 15:40:04 EST 1995