[gq-commit] gq/src errorchain.c,1.13,1.14 errorchain.h,1.6,1.7
Status: Beta
Brought to you by:
sur5r
From: <sta...@us...> - 2003-10-05 13:38:12
|
Update of /cvsroot/gqclient/gq/src In directory sc8-pr-cvs1:/tmp/cvs-serv4105 Modified Files: errorchain.c errorchain.h Log Message: * Dropped the struct errmsgs altogether * now error_push allows for printf-style variable argument lists, very convenient: no more dealing with an extra message buffer for variable messages. Got rid of many such buffers * Many object types now have constructors and destructors of the form new_<type> and free_<type>. Use them instead of self-allocating and freeing memory as it was done before. Removed all such old style object handling * Got rid of most hand-knit linked list implementations, replaced them with GLists (errorchains, config->ldapservers, etc.) * Got rid of many, many fixed size buffers. These might have been problematic with variable length characters (as in the standard UTF-8 encoding) Index: errorchain.c =================================================================== RCS file: /cvsroot/gqclient/gq/src/errorchain.c,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** errorchain.c 4 Oct 2003 09:54:33 -0000 1.13 --- errorchain.c 5 Oct 2003 13:38:06 -0000 1.14 *************** *** 35,107 **** #include "i18n.h" #include "input.h" #include "../icons/bomb.xpm" ! struct errchain *chains = NULL; ! int error_new_context(char *title) { ! struct errchain *chain, *new_chain; ! int context = 0; ! ! new_chain = MALLOC(sizeof(struct errchain), "struct errchain"); ! strncpy(new_chain->title, title, MAX_ERRTITLE_SIZE); ! new_chain->title[MAX_ERRTITLE_SIZE] = '\0'; ! new_chain->msgs = NULL; ! new_chain->next = NULL; ! if(!chains) ! chains = new_chain; ! else { ! chain = chains; ! while(chain->next) { ! context = chain->context; ! chain = chain->next; ! } ! chain->next = new_chain; ! context = chain->context + 1; } ! new_chain->context = context; ! return(context); } ! void error_push_production(int context, char *msg) { struct errchain *chain; ! struct errmsgs *new_msg, *cur_msg; ! new_msg = MALLOC(sizeof(struct errmsgs), "struct errmsgs"); ! strncpy(new_msg->msg, msg, MAX_ERRMSG_SIZE); ! new_msg->msg[MAX_ERRMSG_SIZE] = '\0'; ! new_msg->next = NULL; /* plug into messagechain */ chain = error_chain_by_context(context); ! if(!chain->msgs) ! chain->msgs = new_msg; ! else { ! cur_msg = chain->msgs; ! while(cur_msg->next) ! cur_msg = cur_msg->next; ! cur_msg->next = new_msg; ! } } #ifdef DEBUG ! void error_push_debug(int context, char *msg, char *file, int line) { if (debug & GQ_DEBUG_ERROR_LINE) { ! int len = strlen(msg) + strlen(file) + 100; char *s = g_malloc(len); ! snprintf(s, len, "%s:%d %s", file, line, msg); ! error_push_production(context, s); g_free(s); } else { ! error_push_production(context, msg); } } #endif --- 35,145 ---- #include "i18n.h" #include "input.h" + #include "utf8-compat.h" #include "../icons/bomb.xpm" + static struct errchain *error_chain_by_context(int q); ! static GList *chains = NULL; ! static int error_context = 0; ! static struct errchain *new_errchain() { ! struct errchain *new_chain; ! new_chain = g_malloc0(sizeof(struct errchain)); ! new_chain->title = g_strdup(""); ! new_chain->messages = NULL; ! return new_chain; ! } ! static void free_errchain(struct errchain *chain) ! { ! if (chain) { ! g_free_if(chain->title); ! g_list_foreach(chain->messages, (GFunc) g_free, NULL); ! g_list_free(chain->messages); ! g_free(chain); } + } ! int error_new_context(const char *title) ! { ! struct errchain *new_chain; ! new_chain = new_errchain(); ! g_free_and_dup(new_chain->title, title); ! ! chains = g_list_append(chains, new_chain); ! new_chain->context = error_context++; ! ! return new_chain->context; } ! ! ! static void error_push_production_v(int context, const char *fmt, va_list ap) { struct errchain *chain; ! GString *str; ! int n, a; ! a = strlen(fmt) + 50; ! str = g_string_sized_new(a); /* used for glib1 compatibility */ ! /* I hope it is ok to repeatadly use ap like this */ ! ! do { ! n = vsnprintf(str->str, a - 1, fmt, ap); ! ! #ifdef HAVE_C99_SNPRINTF ! if (n > a - 1) { ! g_string_free(str, TRUE); ! a = n + 2; ! str = g_string_sized_new(a); ! } ! #else ! if (n == -1) { ! g_string_free(str, TRUE); ! a *= 2; ! str = g_string_sized_new(a); ! } ! #endif ! } while (n > (a - 1) || n == -1); /* plug into messagechain */ chain = error_chain_by_context(context); ! chain->messages = g_list_append(chain->messages, str->str); + g_string_free(str, FALSE); + } + + void error_push_production(int context, const char *fmt, ...) + { + va_list ap; + va_start(ap, fmt); + error_push_production_v(context, fmt, ap); + va_end(ap); } #ifdef DEBUG ! void error_push_debug(const char *file, int line, ! int context, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + if (debug & GQ_DEBUG_ERROR_LINE) { ! ! /* Is it allowed to change the fmt? */ ! ! int len = strlen(fmt) + strlen(file) + 100; char *s = g_malloc(len); ! snprintf(s, len, "%s:%d %s", file, line, fmt); ! error_push_production_v(context, s, ap); g_free(s); } else { ! error_push_production_v(context, fmt, ap); } + + va_end(ap); } #endif *************** *** 114,186 **** { char *error_msg; - char message[256]; ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg); if (error_msg != NULL && *error_msg != 0) { ! snprintf(message, sizeof(message), ! _("Additional error: %s"), error_msg); ! error_push(context, message); } } /* returns chain for requested context */ ! struct errchain *error_chain_by_context(int q) { struct errchain *chain; ! chain = chains; ! while(chain && chain->context != q) ! chain = chain->next; ! ! if(!chain) { ! fprintf(stderr, _("Oops! errorchain lookup error. Exiting...\n")); ! abort(); } ! ! return(chain); } ! void error_clear(int context) { struct errchain *chain; ! struct errmsgs *cur_msg, *old_msg; chain = error_chain_by_context(context); ! cur_msg = chain->msgs; ! while(cur_msg) { ! old_msg = cur_msg; ! cur_msg = cur_msg->next; ! FREE(old_msg, "struct errmsgs"); ! } ! chain->msgs = NULL; } ! void error_free(int context) { ! struct errchain *chain, *cur_chain; ! ! chain = error_chain_by_context(context); ! ! error_clear(context); ! ! /* free chain */ ! if(chain == chains) ! chains = chain->next; ! else { ! cur_chain = chains; ! while(cur_chain && cur_chain->next != chain) ! cur_chain = cur_chain->next; ! if(cur_chain) ! cur_chain->next = cur_chain->next->next; ! else { ! fprintf(stderr, _("Oops! errorchain free error. Exiting...\n")); ! abort(); ! } ! ! } ! FREE(chain, "struct errchain"); } --- 152,199 ---- { char *error_msg; ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg); if (error_msg != NULL && *error_msg != 0) { ! error_push(context, _("Additional error: %s"), error_msg); } } /* returns chain for requested context */ ! static struct errchain *error_chain_by_context(int q) { + GList *I; struct errchain *chain; ! for (I = chains ; I ; I = g_list_next(I)) { ! chain = I->data; ! if (chain->context == q) return chain; } ! fprintf(stderr, _("Oops! errorchain lookup error. Exiting...\n")); ! abort(); ! return NULL; /* make sure the compiler shuts up */ } ! static void error_free(int context) { struct errchain *chain; ! GList *I; chain = error_chain_by_context(context); + assert(chain); ! I = g_list_find(chains, chain); ! chains = g_list_remove(chains, I); + free_errchain(chain); } ! void error_clear(int context) { ! struct errchain *chain = error_chain_by_context(context); ! assert(chain); ! g_list_foreach(chain->messages, (GFunc) g_free, NULL); ! g_list_free(chain->messages); ! chain->messages = NULL; } *************** *** 191,199 **** GtkWidget *pixmap, *popupwin, *vbox, *vbox1, *hbox0, *hbox, *vbox2, *msg_label, *okbutton, *align; struct errchain *chain; ! struct errmsgs *cur_msg, *old_msg; chain = error_chain_by_context(context); ! if(chain->msgs) { popupwin = gtk_dialog_new(); gtk_widget_realize(popupwin); --- 204,213 ---- GtkWidget *pixmap, *popupwin, *vbox, *vbox1, *hbox0, *hbox, *vbox2, *msg_label, *okbutton, *align; struct errchain *chain; ! GList *msg; chain = error_chain_by_context(context); + assert(chain); ! if(chain->messages) { popupwin = gtk_dialog_new(); gtk_widget_realize(popupwin); *************** *** 228,237 **** /* show messages, freeing them as we go */ ! cur_msg = chain->msgs; ! while(cur_msg) { #if GTK_MAJOR >= 2 ! msg_label = gtk_label_new(cur_msg->msg); #else ! char *c = decoded_string(cur_msg->msg); msg_label = gtk_label_new(c); if (c) free(c); --- 242,252 ---- /* show messages, freeing them as we go */ ! for(msg = chain->messages ; msg ; msg = g_list_next(msg)) { ! char *m = msg->data; ! #if GTK_MAJOR >= 2 ! msg_label = gtk_label_new(m); #else ! char *c = decoded_string(m); msg_label = gtk_label_new(c); if (c) free(c); *************** *** 241,249 **** gtk_widget_show(msg_label); gtk_box_pack_start(GTK_BOX(vbox), msg_label, FALSE, FALSE, 0); ! old_msg = cur_msg; ! cur_msg = cur_msg->next; ! FREE(old_msg, "struct errmsgs"); } ! chain->msgs = NULL; vbox2 = GTK_DIALOG(popupwin)->action_area; --- 256,264 ---- gtk_widget_show(msg_label); gtk_box_pack_start(GTK_BOX(vbox), msg_label, FALSE, FALSE, 0); ! ! g_free(msg->data); } ! g_list_free(chain->messages); ! chain->messages = NULL; vbox2 = GTK_DIALOG(popupwin)->action_area; Index: errorchain.h =================================================================== RCS file: /cvsroot/gqclient/gq/src/errorchain.h,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** errorchain.h 15 Jul 2002 18:36:51 -0000 1.6 --- errorchain.h 5 Oct 2003 13:38:06 -0000 1.7 *************** *** 30,67 **** #include "config.h" ! int error_new_context(char *title); ! void error_push_production(int context, char *msg); #ifdef DEBUG ! void error_push_debug(int context, char *msg, char *file, int line); ! # define error_push(c, m) error_push_debug((c), (m), __FILE__, __LINE__) #else ! # define error_push(c, m) error_push_production((c), (m)) #endif - struct errchain *error_chain_by_context(int q); void error_flush(int context); void error_popup(char *title, char *message); void error_clear(int context); - void error_free(int context); void push_ldap_addl_error(LDAP *ld, int context); - #define MAX_ERRTITLE_SIZE 128 - #define MAX_ERRMSG_SIZE 256 - - struct errmsgs { - char msg[MAX_ERRMSG_SIZE]; - struct errmsgs *next; - }; - struct errchain { int context; ! char title[MAX_ERRTITLE_SIZE]; ! struct errmsgs *msgs; ! struct errchain *next; }; --- 30,57 ---- #include "config.h" ! int error_new_context(const char *title); ! void error_push_production(int context, const char *msg, ...) ! __attribute__ ((format (printf, 2, 3))); #ifdef DEBUG ! void error_push_debug(const char *file, int line, int context, const char *msg, ...); ! # define error_push(c, ...) error_push_debug(__FILE__, __LINE__, (c), __VA_ARGS__) #else ! # define error_push(c, ...) error_push_production((c), __VA_ARGS__) #endif void error_flush(int context); void error_popup(char *title, char *message); void error_clear(int context); void push_ldap_addl_error(LDAP *ld, int context); struct errchain { int context; ! char *title; ! GList *messages; }; |