LQ Newbie
Registered: Jul 2006
Posts: 1
Rep:
|
GLib problems with g_tree_insert()
Hi all,
I'm rebuilding a server written in C and using open-source GLib.
In particular, after initializing a tree, I would like to full it using the function g_tree_insert(), using a string as key and a struct as data. Here is the function:
void caricaValori(void) {
extern struct dbase head_db;
extern int revoche;
extern mpz_t espowa;
struct row *r1;
unsigned char str_xlow[DIM_STR_SERIALE],str_xhigh[DIM_STR_SERIALE],str_y[DIM_STR_Y],str_y_inv[DIM_STR_Y_INV],intestazione[DIM_INTESTAZIONE], str_in[DIM_STR_IN];
FILE *fd;
#ifdef DEBUG
printf("CARICA_VALORI: ... ");
#endif
strcpy(str_xlow,"");
strcpy(str_xhigh,"");
strcpy(str_y,"");
strcpy(str_y_inv,"");
revoche=0;
if((fd = fopen(nomefile1,"r")) == NULL) {perror("Impossibile aprire il file db in lettura");raise(SIGINT);}
if(fgets(intestazione,DIM_INTESTAZIONE,fd)==NULL) {perror("Niente da leggere nel file db in lettura");}
do {
fscanf(fd,"%s %s %s %s\n",str_xlow,str_xhigh,str_y,str_y_inv);
r1=(struct row*)malloc(sizeof(struct row));
if(strlen(str_xlow)!=0 && strlen(str_xhigh)!=0 && strlen(str_y)!=0 && strlen(str_y_inv)!=0) {
mpz_init_set_str(r1->stat.xlow,str_xlow,BASE);
mpz_init_set_str(r1->stat.xhigh,str_xhigh,BASE); // setta il parametro 1 dalla striga 2 con base BASE
mpz_init_set_str(r1->stat.y,str_y,BASE); // setta il parametro 1 dalla striga 2 con base BASE
mpz_init_set_str(r1->stat.y_inv,str_y_inv,BASE);
mpz_init(r1->stat.z);
r1->stat.valid_z=0; // z non �valido
if(head_db.tqh_first == NULL) { // TODO forse non importa
TAILQ_INSERT_HEAD(&head_db, r1, rows);
revoche++;
}
else {
TAILQ_INSERT_TAIL(&head_db, r1, rows);
revoche++;
}
}
else {
#ifdef DEBUG
printf("DB vuoto\n");
#endif
if(mpz_cmp_ui(espowa,1)!=0) {
perror("Valori di espowa non coerenti con DB vuoto (espowa!=1)");
raise(SIGINT);
}
if(revocaSeriale("0")<1) {
perror("Errore nella revoca di 0");
raise(SIGINT);
}
else revoche++;
break;
}
} while (!feof(fd));
fclose(fd);
if(mpz_cmp_ui(head_db.tqh_first->stat.xhigh,0)!=0 && mpz_cmp_ui(espowa,1)==0) {
perror("Valori di espowa non coerenti con DB pieno (espowa=1)");
raise(SIGINT);
}
r1 = head_db.tqh_first;
while(r1 != NULL) {
mpz_get_str (str_xlow, BASE, r1->stat.xlow);
mpz_get_str (str_xhigh, BASE, r1->stat.xhigh);
strcpy(str_in, "");
strncat(str_in,str_xlow,strlen(str_xlow));
strncat(str_in,str_xhigh,strlen(str_xhigh));
//printf("%s\n", str_in);
g_tree_insert(tree, str_in, r1);
r1 = r1->rows.tqe_next;
}
g_tree_foreach(tree, (GTraverseFunc)iter_all, NULL);
}
This function opens a text file, reads it, fulls a list with all values. Then it is supposed to fill up the tree reading all those values contained in the list, but when I look up the tree with g_tree_foreach, I see that I can find in the tree only the last value, as if the previous values were somehow overwritten. I need instead to fill up the tree with all the values read from the list (the list contains all the values correctly). I'm using GNU MP too.
Please, can anyone help me?
Thanks in advance and sorry for my bad english!
Marco
|