Serializing a glib data type

A

akappa

Hi,
I want to serialize a glib data type (an hash), but I don't want to
access directly to the fields of the various structs, since it's an
opaque data type: there is one "more-or-less standard way to do it"
that it's more efficient than read all the fields with the iterator,
saves them on file and successively recreating the hash putting them
once per time?

Thanks in advice.
 
M

Mark McIntyre

Hi,
I want to serialize a glib data type (an hash), but I don't want to
access directly to the fields of the various structs, since it's an
opaque data type: there is one "more-or-less standard way to do it"
that it's more efficient than read all the fields with the iterator,
saves them on file and successively recreating the hash putting them
once per time?

This sounds like an algorithm question. Try asking in
comp.programming? Alternatively, doesn't the data type provide some
accessor functions?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Malcolm McLean

akappa said:
Hi,
I want to serialize a glib data type (an hash), but I don't want to
access directly to the fields of the various structs, since it's an
opaque data type: there is one "more-or-less standard way to do it"
that it's more efficient than read all the fields with the iterator,
saves them on file and successively recreating the hash putting them
once per time?
This is something that C doesn't do well. All objects ought to have
"serialise" methods built in at a fundamental level. In C of course you've
got the problem of what to do with shared pointers.
glib is open source, so by looking at the hash structure, you ought to be
able to write a serialise / deserialise pair that executes quite a bit
faster than rebuilding the table. However the objects in the table would
also need serialise methods.
glib might already have such methods, but I doubt it, because it adds so
much complexity to the basic hash interface.
 
F

Flash Gordon

akappa wrote, On 19/08/07 16:58:
Hi,
I want to serialize a glib data type (an hash), but I don't want to
access directly to the fields of the various structs, since it's an
opaque data type: there is one "more-or-less standard way to do it"
that it's more efficient than read all the fields with the iterator,
saves them on file and successively recreating the hash putting them
once per time?

C itself provides no mechanism to automatically do what you want. glib
might, but for that you need to ask where GLib is topical, such as one
of the gnome mailing lists http://mail.gnome.org/
 
A

akappa

At the first, thanks to all that have replied to my question :)

Malcolm McLean said:
glib is open source, so by looking at the hash structure, you ought to be
able to write a serialise / deserialise pair that executes quite a bit
faster than rebuilding the table.

You're of course right, but I don't want to °break° the datatype's
opaqueness:
The GHashTable struct is an opaque data structure to represent a Hash Table.
It should only be accessed via the following functions.

Unfortunately, glib (seems to) doesn't have no native way to
marshalling their owns data type...

BTW, I'm sorry to be a bit OT in this group, next time I should select
with much care the group :)
 
C

CBFalconer

akappa said:
I want to serialize a glib data type (an hash), but I don't want to
access directly to the fields of the various structs, since it's an
opaque data type: there is one "more-or-less standard way to do it"
that it's more efficient than read all the fields with the iterator,
saves them on file and successively recreating the hash putting them
once per time?

There is no iterator in the C language. Try comp.lang.c++.
 
K

Keith Thompson

CBFalconer said:
There is no iterator in the C language. Try comp.lang.c++.

C doesn't have linked lists or binary trees either, but they can be
built in standard C. He's not talking about a C++ iterator; he's
talking about an "interator" provided by glib, which is a C library.
 
C

CBFalconer

Keith said:
C doesn't have linked lists or binary trees either, but they can be
built in standard C. He's not talking about a C++ iterator; he's
talking about an "interator" provided by glib, which is a C library.

OK, but GNU stuff is no more on-topic here than Windoze junk.
 
J

Justin Spahr-Summers

The 'library' is not defined in the C standard. Unless you post
the C-standard code involved here the question remains off-topic.

Although he's working with the library, he's asking a fairly general
question pertaining to the capabilities of C as a language: whether
it's possible to serialize a structure without knowing the structure
of its fields in a way that will work even if the structure layout is
changed.

In short, the answer is no because fwrite() and fread() only work on
raw bytes, and there's no telling what the internal representations
for the fields present in the structure are, or whether there are any
padding bytes, etc. It seems, however, that the library you're using
provides *a* solution, even if it's not the optimal one. Luckily, if
you're doing file I/O, that's probably the real bottleneck, as opposed
to preparing the data for output and reconstructing it from input.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top