[gq-commit] gq/src state.c,1.2,1.3 state.h,1.1,1.2
Status: Beta
Brought to you by:
sur5r
From: <sta...@us...> - 2003-10-11 20:26:10
|
Update of /cvsroot/gqclient/gq/src In directory sc8-pr-cvs1:/tmp/cvs-serv1010 Modified Files: state.c state.h Log Message: * Support for string values, rudimentary, untested support for lists of strings * Support for hierarchic storage of values (very registry-like, uahhh), using the "." as a path separator * Support to allow for removal of state names * Cosmetic changes (I'm using Java conventions, sometimes) Index: state.c =================================================================== RCS file: /cvsroot/gqclient/gq/src/state.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** state.c 8 Oct 2003 22:51:53 -0000 1.2 --- state.c 11 Oct 2003 20:26:07 -0000 1.3 *************** *** 67,70 **** --- 67,72 ---- }; + typedef void (*sv_free_list_func)(void); + struct state_value { enum state_value_type type; *************** *** 74,78 **** GList *list_val; } val; ! void (*free_list_element)(void *); }; --- 76,80 ---- GList *list_val; } val; ! sv_free_list_func free_list_element; }; *************** *** 131,135 **** } ! static gboolean ghr_free_value(char *key, struct state_value *v, gpointer ud) { g_free_if(key); free_state_value(v); --- 133,138 ---- } ! static gboolean ghr_free_value(char *key, struct state_value *v, gpointer ud) ! { g_free_if(key); free_state_value(v); *************** *** 140,144 **** static gboolean ghr_free_entity(char *key, ! struct state_entity *e, gpointer ud) { g_free_if(key); free_state_entity(e); --- 143,148 ---- static gboolean ghr_free_entity(char *key, ! struct state_entity *e, gpointer ud) ! { g_free_if(key); free_state_entity(e); *************** *** 146,150 **** } ! void free_state_entity(struct state_entity *e) { assert(e); g_hash_table_foreach_remove(e->values, (GHRFunc) ghr_free_value, NULL); --- 150,155 ---- } ! void free_state_entity(struct state_entity *e) ! { assert(e); g_hash_table_foreach_remove(e->values, (GHRFunc) ghr_free_value, NULL); *************** *** 155,178 **** ! struct state_entity *lookup_entity(const char *entity_name) { struct state_entity *e; ! if (!entities) { ! entities = g_hash_table_new(g_str_hash, g_str_equal); } ! e = g_hash_table_lookup(entities, entity_name); if (e) { return e; } ! e = new_state_entity(); ! g_hash_table_insert(entities, g_strdup(entity_name), e); return e; } ! int lookup_state_value_get_int(const char *state_name, ! const char *value_name, ! int def) { struct state_value *val; --- 160,222 ---- ! static struct state_entity *lookup_entity_with_root(GHashTable *root, ! const char *entity_name, ! gboolean create) { struct state_entity *e; + const char *c; ! c = strchr(entity_name, '.'); /* UTF-8 OK: we do not use non-ASCII ! entity names */ ! if (c) { ! /* look for subentities! */ ! char *d = g_strdup(entity_name); ! char *f = d + (c - entity_name); ! *f = 0; ! f++; ! ! e = g_hash_table_lookup(root, d); ! ! if (!e) { ! if (create) { ! e = new_state_entity(); ! g_hash_table_insert(root, g_strdup(d), e); ! } else { ! return NULL; ! } ! } ! ! e = lookup_entity_with_root(e->entities, f, create); ! return e; } ! e = g_hash_table_lookup(root, entity_name); if (e) { return e; } ! if (create) { ! e = new_state_entity(); ! g_hash_table_insert(root, g_strdup(entity_name), e); ! } return e; } ! gboolean exists_entity(const char *entity_name) ! { ! return lookup_entity_with_root(entities, entity_name, FALSE) != NULL; ! } ! ! struct state_entity *lookup_entity(const char *entity_name) ! { ! if (!entities) { ! entities = g_hash_table_new(g_str_hash, g_str_equal); ! } ! ! return lookup_entity_with_root(entities, entity_name, TRUE); ! } ! ! int state_value_get_int(const char *state_name, ! const char *value_name, ! int def) { struct state_value *val; *************** *** 215,218 **** --- 259,347 ---- } + const char *state_value_get_string(const char *state_name, + const char *value_name, + const char *def) + { + struct state_value *val; + struct state_entity *e = lookup_entity(state_name); + assert(e); + assert(e->values); + + val = g_hash_table_lookup(e->values, value_name); + if (val) { + if (val->type == SV_char) { + return val->val.char_val; + } + } else { + if (def) { + val = new_state_value(SV_char); + val->val.char_val = g_strdup(def); + g_hash_table_insert(e->values, g_strdup(value_name), val); + } + } + return def; + } + + void state_value_set_string(const char *state_name, + const char *value_name, + const char *c) + { + struct state_value *val; + struct state_entity *e = lookup_entity(state_name); + assert(e); + assert(e->values); + + val = g_hash_table_lookup(e->values, value_name); + if (val) { + if (val->type == SV_char) { + g_free_and_dup(val->val.char_val, c); + } + } else { + val = new_state_value(SV_char); + g_free_and_dup(val->val.char_val, c); + g_hash_table_insert(e->values, g_strdup(value_name), val); + } + } + + static void rm_entity_with_root(GHashTable *root, + const char *entity_name) + { + struct state_entity *e; + const char *c; + + c = strchr(entity_name, '.'); /* UTF-8 OK: we do not use non-ASCII + entity names */ + if (c) { + /* look for subentities! */ + char *d = g_strdup(entity_name); + char *f = d + (c - entity_name); + *f = 0; + f++; + + e = g_hash_table_lookup(root, d); + + if (!e) { + return; + } + + rm_entity_with_root(e->entities, f); + return; + } + + e = g_hash_table_lookup(root, entity_name); + if (e) { + free_state_entity(e); + g_hash_table_remove(root, entity_name); + } + } + + void rm_value(const char *state_name) + { + if (entities != NULL) { + rm_entity_with_root(entities, state_name); + } + } + + static void width_height_state(GtkWidget *w, GtkAllocation *allocation, *************** *** 228,233 **** GdkWindow *win = w->window; if (win && config->restore_window_positions) { ! int x = lookup_state_value_get_int(name, "x", -1); ! int y = lookup_state_value_get_int(name, "y", -1); if (x >= 0 && y >= 0) gdk_window_move(win, x, y); --- 357,362 ---- GdkWindow *win = w->window; if (win && config->restore_window_positions) { ! int x = state_value_get_int(name, "x", -1); ! int y = state_value_get_int(name, "y", -1); if (x >= 0 && y >= 0) gdk_window_move(win, x, y); *************** *** 258,263 **** if (config->restore_window_sizes) { ! w = lookup_state_value_get_int(name, "width", w); ! h = lookup_state_value_get_int(name, "height", h); } --- 387,392 ---- if (config->restore_window_sizes) { ! w = state_value_get_int(name, "width", w); ! h = state_value_get_int(name, "height", h); } *************** *** 309,312 **** --- 438,443 ---- int len = strlen(value_name) + 80; char *msg = g_malloc(len); + GList *l; + snprintf(msg, len, "<state-value name='%s' type='%s'>\n", value_name, detokenize(token_value_type_names, v->type)); *************** *** 323,330 **** if (v->val.char_val) { config_write(wc, v->val.char_val); } break; case SV_list: ! abort(); break; default: --- 454,466 ---- if (v->val.char_val) { config_write(wc, v->val.char_val); + config_write(wc, "\n"); } break; case SV_list: ! wc->indent++; ! for ( l = v->val.list_val ; l ; l = g_list_next(l)) { ! config_write_string(wc, l->data, "list-item"); ! } ! wc->indent--; break; default: *************** *** 487,491 **** static void entity_entityE(struct parser_context *ctx, ! struct tagstack_entry *e) { struct state_entity *parent = peek_tag(ctx->stack, 1)->data; --- 623,627 ---- static void entity_entityE(struct parser_context *ctx, ! struct tagstack_entry *e) { struct state_entity *parent = peek_tag(ctx->stack, 1)->data; *************** *** 506,531 **** } static void state_valueE(struct parser_context *ctx, ! struct tagstack_entry *e) { struct state_entity *ent = peek_tag(ctx->stack, 1)->data; const char *n = NULL; - enum state_value_type t = 0; int i; for (i = 0 ; e->attrs[i] ; i += 2) { if (strcmp("name", e->attrs[i]) == 0) { n = e->attrs[i+1]; - } else if (strcmp("type", e->attrs[i]) == 0) { - t = tokenize(token_value_type_names, e->attrs[i+1]); } } ! if (n != NULL && t != 0) { ! struct state_value *v = new_state_value(t); char *ep; ! switch (t) { ! case SV_int: *(v->val.int_val) = --- 642,689 ---- } + static void state_valuesS(struct parser_context *ctx, + struct tagstack_entry *e) + { + /* struct state_entity *ent = peek_tag(ctx->stack, 1)->data; */ + int i; + enum state_value_type t = 0; + + for (i = 0 ; e->attrs[i] ; i += 2) { + if (strcmp("type", e->attrs[i]) == 0) { + t = tokenize(token_value_type_names, e->attrs[i+1]); + } + } + + if (t != 0) { + struct state_value *v = new_state_value(t); + e->data = v; + e->free_data = (free_func) free_state_value; + } else { + /** FIXME: report error **/ + } + + + } + static void state_valueE(struct parser_context *ctx, ! struct tagstack_entry *e) { struct state_entity *ent = peek_tag(ctx->stack, 1)->data; const char *n = NULL; int i; + struct state_value *v = (struct state_value *) e->data; for (i = 0 ; e->attrs[i] ; i += 2) { if (strcmp("name", e->attrs[i]) == 0) { n = e->attrs[i+1]; } } ! if (n != NULL && v->type != 0) { char *ep; ! assert(v); ! ! switch (v->type) { case SV_int: *(v->val.int_val) = *************** *** 536,540 **** break; case SV_list: ! abort(); break; default: --- 694,698 ---- break; case SV_list: ! v->free_list_element = (sv_free_list_func) g_free; break; default: *************** *** 544,547 **** --- 702,721 ---- g_hash_table_insert(ent->values, g_strdup(n), v); + + e->data = NULL; + e->free_data = NULL; + } + } + + static void list_itemE(struct parser_context *ctx, + struct tagstack_entry *e) + { + struct state_value *v = peek_tag(ctx->stack, 1)->data; + + if (v->type == SV_list) { + v->val.list_val = g_list_append(v->val.list_val, + g_strdup(e->cdata)); + } else { + /* FIXME: report error */ } } *************** *** 565,570 **** { "state-value", 0, ! NULL, state_valueE, { "entity", NULL }, }, { --- 739,749 ---- { "state-value", 0, ! state_valuesS, state_valueE, { "entity", NULL }, + }, + { + "list-item", 0, + NULL, list_itemE, + { "state-value", NULL }, }, { Index: state.h =================================================================== RCS file: /cvsroot/gqclient/gq/src/state.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** state.h 5 Oct 2003 22:15:31 -0000 1.1 --- state.h 11 Oct 2003 20:26:07 -0000 1.2 *************** *** 34,39 **** --- 34,60 ---- const char *name, int w, int h); + void init_state(); void save_state(); + + /* Never use non-ASCII entity names */ + struct state_entity *lookup_entity(const char *entity_name); + void rm_value(const char *state_name); + int state_value_get_int(const char *state_name, + const char *value_name, + int def); + void state_value_set_int(const char *state_name, + const char *value_name, + int n); + + const char *state_value_get_string(const char *state_name, + const char *value_name, + const char *def); + void state_value_set_string(const char *state_name, + const char *value_name, + const char *c); + + gboolean exists_entity(const char *entity_name) ; + #endif |