Resources Example (XForms (Forms Library) - Version 1.2 (Rev. 4) A Graphical User Interface Toolkit for X)

Up: Current Support [Contents][Index]

#### 34.1.1 Resources Example

Let us assume that you have an application named `myapp` and it accepts the options `-foo` level and `-bar` plus a filename. The proper way to initialize the Forms Library is as follows

    FL_CMD_OPT cmdopt[] = {
      {"-foo", "*.foo", XrmoptionSepArg, 0     },
      {"-bar", ".bar",  XrmoptionNoArg,  "True"}
    };

    int foolevel, ifbar;
    int deftrue;      /* can only be set thru resources */

    FL_resource res[] = {
      {"foo",     "FooCLASS", FL_INT,  &foolevel, "0"},
      {"bar",     "BarCLASS", FL_BOOL, &ifbar,    "0"},
      {"deftrue", "Whatever", FL_BOOL, &deftrue,  "1"}
    };

    int main(int argc, char *argv[]) {
        fl_initialize(&argc, argv ,"MyappClass", cmdopt, 2);
        fl_get_app_resources(res, 3);
        if (argc == 1)   /* missing filename */
            fprintf(stderr, "Usage %s: [-foo level][-bar] "
                    "filename\n","myapp");
        /* rest of the program */
    }

After this both variables `foolevel` and `ifbar` are set either through resource files or command line options, with the command line options overriding those set in the resource files. In case neither the command line nor the resource files specified the options, the default value string is converted.

There is another routine, a resource routine of the lowest level in XForms, which might be useful if a quick-and-dirty option needs to be read:

    const char *fl_get_resource(const char *res_name,
                                const char *res_class,
                                FL_RTYPE type, char *defval,
                                void *val, int nbytes);

`res_name` and `res_class` must be complete resource specifications (minus the application name) and should not contain wildcards of any kind. The resource will be converted according to the type and result stored in `type`, which is an integer of type `FL_RTYPE`. `nbytes` is used only if the resource type is `FL_STRING`. The function returns the string representation of the resource value. If a value of `FL_NONE` is passed for `type` the resource is not converted and the pointer `val` is not dereferenced.

There is also a routine that allows the application program to set resources programmatically:

    void fl_set_resource(const char *string, const char *value);

where `string` and `value` are a resource-value pair. The string can be a fully qualified resource name (minus the application name) or a resource class.

Routines `fl_set_resource()` and `fl_get_resource()` can be used to store and retrieve arbitrary strings and values and may be useful to pass data around.

Up: Current Support [Contents][Index]