Hello Glib users,
(I'm new in Glib, and I'm not a C expert :))
I would try to use Glib hash table to store many keys (strings) with its value (typedef unsigned char t_utype_rec_udatas[10];).// fill hash with data's
datastore = g_hash_table_new(g_str_hash, g_str_equal);
while (fgets(inputbuffer, 90, fp) != NULL) {
parse_utypeline(inputbuffer, udatakey, &udataidx); // parse line, store fields in udatakey and udataidx
utype_inc(udatakey, udataidx); // push to hash
total++;
}
Mar 23 18:13:09 basil myprog: udata store: 0x2371400
Mar 23 18:13:09 basil myprog: new udatakey: 'ABCD', udataidx: 0
Mar 23 18:13:09 basil myprog: store value: 1
Mar 23 18:13:09 basil myprog: 0. key: 'ABCD'
Mar 23 18:13:09 basil myprog: size of hash: 1
Mar 23 18:13:13 basil myprog: udata_get - udatakey: '', udataidx: 0
Mar 23 18:13:13 basil myprog: udata_get - size of hash: 1
Mar 23 18:13:13 basil myprog: udata_get - 0. key: '#020#001'
Mar 23 18:13:13 basil myprog: udata_get - udatakey doesn't exists in store: 0x2371400
gi = 0;
hkeys = g_hash_table_get_keys(datastore);
syslog(LOG_DEBUG, "udata_get - %d. key: '%s'", gi, hkeys->data);
Here is the code, what I use:
===%===
int utype_inc(char udatakey[15], int udata) {
t_utype_rec_udatas *utype_rec_udatas;
int i;
gboolean gb;
GList * hkeys = NULL;
int gi;
syslog(LOG_DEBUG, "utype store: %p", datastore);
gb = g_hash_table_contains(datastore, udatakey);
if (gb == 0) {
utype_rec_udatas = g_malloc0(sizeof *utype_rec_udatas);
for(i=0; i<10; i++) {
/*
* every item has derived like this:
* typedef unsigned char t_utype_rec_udatas[10];
* t_utype_rec_udatas *utype_rec_udatas;
*/
(*utype_rec_udatas)[i] = 0;
}
g_hash_table_insert(datastore, udatakey, utype_rec_udatas);
}
else {
utype_rec_udatas = g_hash_table_lookup(datastore, udatakey);
}
(*utype_rec_udatas)[udata]++;
gb = g_hash_table_contains(datastore, udatakey);
if (gb == 0) {
return -1;
}
else {
utype_rec_udatas = g_hash_table_lookup(datastore, udatakey);
return (*utype_rec_udatas)[udata];
}
}
int utype_get(char udatakey[15], int udata) {
t_utype_rec_udatas *utype_rec_udatas;
gboolean gb;
if (datastore == NULL) {
syslog(LOG_DEBUG, "store doesn't exists");
return -1;
}
gb = g_hash_table_contains(datastore, udatakey);
if (gb == 0) {
return -1;
}
else {
utype_rec_udatas = g_hash_table_lookup(datastore, udatakey);
return (*utype_rec_udatas)[udata];
}
}
===%===
int utype_inc(char udatakey[15], int udata) {
t_utype_rec_udatas *utype_rec_udatas;
int i;
gboolean gb;
GList * hkeys = NULL;
int gi;
syslog(LOG_DEBUG, "utype store: %p", datastore);
gb = g_hash_table_contains(datastore, udatakey);
if (gb == 0) {
utype_rec_udatas = g_malloc0(sizeof *utype_rec_udatas);
for(i=0; i<10; i++) {
/*
* every item has derived like this:
* typedef unsigned char t_utype_rec_udatas[10];
* t_utype_rec_udatas *utype_rec_udatas;
*/
(*utype_rec_udatas)[i] = 0;
}
g_hash_table_insert(datastore, udatakey, utype_rec_udatas);
}
else {
utype_rec_udatas = g_hash_table_lookup(datastore, udatakey);
}
(*utype_rec_udatas)[udata]++;
gb = g_hash_table_contains(datastore, udatakey);
if (gb == 0) {
return -1;
}
else {
utype_rec_udatas = g_hash_table_lookup(datastore, udatakey);
return (*utype_rec_udatas)[udata];
}
}
int utype_get(char udatakey[15], int udata) {
t_utype_rec_udatas *utype_rec_udatas;
gboolean gb;
if (datastore == NULL) {
syslog(LOG_DEBUG, "store doesn't exists");
return -1;
}
gb = g_hash_table_contains(datastore, udatakey);
if (gb == 0) {
return -1;
}
else {
utype_rec_udatas = g_hash_table_lookup(datastore, udatakey);
return (*utype_rec_udatas)[udata];
}
}
===%===
I'm totally confised, could anybody helps me, what em'I wrong?
Thanks:
a.
Thanks:
a.
_______________________________________________ gtk-list mailing list gtk-list@xxxxxxxxx https://mail.gnome.org/mailman/listinfo/gtk-list