Re: saving GArray contents to binary file
- From: edward hage <edha xs4all nl>
- To: gtk-app-devel-list gnome org
- Cc: Maciej Katafiasz <mnews22 wp pl>
- Subject: Re: saving GArray contents to binary file
- Date: Sun, 10 Oct 2004 23:11:35 +0200
Maciej Katafiasz wrote:
Dnia 03-10-2004, nie o godzinie 12:19 +0200, edward hage napisał(a):
I want to save a GArray of struct to a binairy file, but I get stuck somehow.
for (i=0; i<array->len; i++)
{
fwrite(&array[i], sizeof (typDing), 1, fp);
^^^^^^^^^
I don't think that's right thing to do, GArray is not C array
I also want to save structs with GArrays inside them, like:
typedef struct
{
gchar *name;
GArray *attached;
} typDing2;
How to save those?
you need to save them struct-by-struct, and then read it back
reconstructing GArray, in general. You could also do some GValue
transformation foo, but that would be probably more hassle than gain, so
unless you have 1000 different types with GArrays that can change at
runtime, better go easy route and just manually save it
HTH,
Maciej
I found a way to save GArray's to file, in fact a GArray is just a struct. All the data
about size is known in GRealArray which is used by all the g_array*** functions. This type
is casted to GArray which has less info.
So my idea to save GArray's is to cast it back to GRealArray, save the GRealArray with
array->data = NULL. The real data is stored after the GRealArray. A function is written to
save everything needed. A special function to read the array will read the GRealArray and
the data and put the data in the GRealArray, and cast it back to GArray:
typedef struct
{
guint8 *data;
guint len;
guint alloc;
guint elt_size;
guint zero_terminated : 1;
guint clear : 1;
} GRealArray;
GArray *save_array_to_file (GArray *farray, FILE *fp)
{
GRealArray *array = (GRealArray*) farray;
guint8 *data_begin;
data_begin = array -> data;
array -> data = NULL;
fwrite(array, sizeof(GRealArray),1, fp);
gint length = array ->elt_size * array ->len;
gint i;
for (i=0; i < length; i++)
{
fwrite(&(data_begin[i]),sizeof(guint8),1,fp);
}
return ((GArray *) array);
}
void read_array_from_file (GArray *farray, FILE *fp)
{
GRealArray *array = (GRealArray *) farray;
fread(array, sizeof(GRealArray),1, fp);
gint length = array ->elt_size * array ->len;
gint i;
guint8 *data_string= g_malloc(sizeof(guint8)*length);
for (i=0; i < length; i++)
{
fread(&(data_string[i]),sizeof(guint8),1,fp);
}
array -> data = data_string;
//attention: previous initialisation of input farray is overwritten (zero_terminated
bit, etc.)
// if farray not previously initialised then Segmentation fault will occur
}
MY QUESTIONS:
1) I want to save GArray's of structs with pointers to data, for which data in turn also
have pointers to other data. Is there a good way to save the whole data-tree at once to a
binary file?
2) Does anybody else already developed more secure or better (?) ways to save and load a
GArray? Now not so pretty is that in read_array_from_file the GArray *farray must already
be initialised with arbitrary values/ arbitrary because they get overwritten anyway by the
info from the file)
Greetings, Edward
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]