help with gconf basics



I need some help with gconf.

I have a command-line application that needs a mechanism for storing
key-value pairs. Gconf appears to fit the bill. It may be overkill for
this task, but that's OK. (The configuration values are read once, on
startup. I don't need callbacks to notify on change, etc.)

The gconf documentation says to use gconf_client_* instead of
gconf_engine_*, but doesn't say why. It appears to me that
gconf_client_* is for use in gtk applications. I'd rather not build in
a dependence on gtk just for this purpose.

I'm developing on Red Hat 7.3. The software I'm developing runs
on several of Unix-like platforms.

$ rpm -q GConf
GConf-1.0.9-4

$ rpm -q GConf-devel
GConf-devel-1.0.9-4

(I also run a fairly recent garnome out of my home directory, which
includes some gconf-2 stuff, but I don't think it's involved here.)

I created a gconf schema and installed it with gconftool-1:

$ cat tidal-schema
<gconfschemafile>
    <schemalist>
	<schema>
	    <key>/apps/tidal/adc/device</key>
	    <applyto>/apps/tidal/adc/device</applyto>
	    <owner>tidal</owner>
	    <type>string</type>
	    <locale name="C">
		<default>/dev/comed0</default>
		<short>Comedi device for a/d converter</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/adc/subdevice</key>
	    <applyto>/apps/tidal/adc/subdevice</applyto>
	    <owner>tidal</owner>
	    <type>int</type>
	    <locale name="C">
		<default>0</default>
		<short>Comedi subdevice for a/d converter</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/adc/range</key>
	    <applyto>/apps/tidal/adc/range</applyto>
	    <owner>tidal</owner>
	    <type>int</type>
	    <locale name="C">
		<default>0</default>
		<short>Comedi range for a/d converter</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/dac0/device</key>
	    <applyto>/apps/tidal/dac0/device</applyto>
	    <owner>tidal</owner>
	    <type>string</type>
	    <locale name="C">
		<default>/dev/comed0</default>
		<short>Comedi device for d/a converter 0</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/dac0/subdevice</key>
	    <applyto>/apps/tidal/dac0/subdevice</applyto>
	    <owner>tidal</owner>
	    <type>int</type>
	    <locale name="C">
		<default>1</default>
		<short>Comedi subdevice for d/a converter 0</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/dac0/range</key>
	    <applyto>/apps/tidal/dac0/range</applyto>
	    <owner>tidal</owner>
	    <type>int</type>
	    <locale name="C">
		<default>0</default>
		<short>Comedi range for d/a converter 0</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/dac1/device</key>
	    <applyto>/apps/tidal/dac1/device</applyto>
	    <owner>tidal</owner>
	    <type>string</type>
	    <locale name="C">
		<default>/dev/comed0</default>
		<short>Comedi device for d/a converter 0</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/dac1/subdevice</key>
	    <applyto>/apps/tidal/dac1/subdevice</applyto>
	    <owner>tidal</owner>
	    <type>int</type>
	    <locale name="C">
		<default>1</default>
		<short>Comedi subdevice for d/a converter 0</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/dac1/range</key>
	    <applyto>/apps/tidal/dac1/range</applyto>
	    <owner>tidal</owner>
	    <type>int</type>
	    <locale name="C">
		<default>0</default>
		<short>Comedi range for d/a converter 0</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/dac2/device</key>
	    <applyto>/apps/tidal/dac2/device</applyto>
	    <owner>tidal</owner>
	    <type>string</type>
	    <locale name="C">
		<default>/dev/comed0</default>
		<short>Comedi device for d/a converter 0</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/dac2/subdevice</key>
	    <applyto>/apps/tidal/dac2/subdevice</applyto>
	    <owner>tidal</owner>
	    <type>int</type>
	    <locale name="C">
		<default>1</default>
		<short>Comedi subdevice for d/a converter 0</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/dac2/range</key>
	    <applyto>/apps/tidal/dac2/range</applyto>
	    <owner>tidal</owner>
	    <type>int</type>
	    <locale name="C">
		<default>0</default>
		<short>Comedi range for d/a converter 0</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/dac3/device</key>
	    <applyto>/apps/tidal/dac3/device</applyto>
	    <owner>tidal</owner>
	    <type>string</type>
	    <locale name="C">
		<default>/dev/comed0</default>
		<short>Comedi device for d/a converter 0</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/dac3/subdevice</key>
	    <applyto>/apps/tidal/dac3/subdevice</applyto>
	    <owner>tidal</owner>
	    <type>int</type>
	    <locale name="C">
		<default>1</default>
		<short>Comedi subdevice for d/a converter 0</short>
	    </locale>
	</schema>
	<schema>
	    <key>/apps/tidal/dac3/range</key>
	    <applyto>/apps/tidal/dac3/range</applyto>
	    <owner>tidal</owner>
	    <type>int</type>
	    <locale name="C">
		<default>0</default>
		<short>Comedi range for d/a converter 0</short>
	    </locale>
	</schema>
    </schemalist>
</gconfschemafile>

It appears to work:

$ gconftool-1 --get /apps/tidal/adc/device
Type: string
List Type: *invalid*
Car Type: *invalid*
Cdr Type: *invalid*
Default Value: /dev/comed0
Owner: tidal
Short Desc: Comedi device for a/d converter
Long Desc: Unset

Just to be thorough:

$ gconftool-1 --get /apps/tidal/adc/devicex
No value set for `/apps/tidal/adc/devicex'

Now to the application. Here's the test program:

$ cat test.c
#include <stdio.h>
#include <stdlib.h>
#include <gconf/gconf.h>
#include <gconf/gconf-engine.h>

int
main(int argc, char *argv[])
{
    GConfEngine *engine;
    gchar *key;
    gchar *value;
    GError *err = NULL;

    key = argv[1];

    if (!gconf_init(argc, argv, &err)) {
	printf("bad init\n");
	exit(1);
    }

    engine = gconf_engine_get_default();
    if (!engine) {
	printf("bad engine\n");
	exit(1);
    }

    printf("key %s\n", key);
    value = gconf_engine_get_string(engine, key, &err);
    if (!value) {
	printf("error: %s\n", err->message);
	exit(1);
    } else {
	printf("%s %s\n", key, value);
    }

    gconf_engine_unref(engine);

    return 0;
}

$ cat Makefile
CC	= gcc -Wall -pedantic
CFLAGS	= -g `gconf-config-1 --cflags`

test:	test.o
	gcc -o test test.o `gconf-config-1 --libs`

$ gconf-config-1 --cflags
-I/usr/include/gconf/1 -I/usr/include/glib-1.2 -I/usr/lib/glib/include -I/usr/include/orbit-1.0

$ gconf-config-1 --libs
-L/usr/lib -lgconf-1 -loaf -lORBitCosNaming -lORBit -lIIOP -lORBitutil -lglib -lnsl -lm

When I run it on a valid (string-valued) key, I get a type mismatch:

$ ./test /apps/tidal/adc/device
key /apps/tidal/adc/device
error: Type mismatch:
 Expected string, got schema

When I run it on an invalid key, it dumps core:
$ ./test /apps/tidal/adc/devicex
./cmds: line 1: 11268 Segmentation fault      ./test /apps/tidal/adc/devicex

$ gdb ./test
GNU gdb Red Hat Linux (5.2-2)
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...
(gdb) run /apps/tidal/adc/devicex
Starting program: /home/sjenkins/gconf/test /apps/tidal/adc/devicex
key /apps/tidal/adc/devicex

Program received signal SIGSEGV, Segmentation fault.
0x08048785 in main (argc=2, argv=0xbffffa04) at test.c:30
30		printf("error: %s\n", err->message);
(gdb) print err
$1 = (GError *) 0x0
(gdb) quit

What am I missing?

Steve



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]