From dc65374aa6ccd54fd4b61cacb7bd57b7d756043d Mon Sep 17 00:00:00 2001 From: John Tytgat Date: Sat, 5 Jun 2004 15:03:59 +0000 Subject: [project @ 2004-06-05 15:03:59 by joty] Created content_set_status() : one centralised place where status_message in the content struct is updated in a fail safe way. svn path=/import/netsurf/; revision=928 --- content/content.c | 26 ++++++++++++++++++++++++-- content/content.h | 1 + content/fetchcache.c | 8 ++++---- render/html.c | 30 ++++++++++++++---------------- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/content/content.c b/content/content.c index be4331884..a002c04fd 100644 --- a/content/content.c +++ b/content/content.c @@ -13,8 +13,10 @@ */ #include -#include +#include +#include #include +#include #include "netsurf/utils/config.h" #include "netsurf/content/content.h" #include "netsurf/css/css.h" @@ -183,7 +185,7 @@ struct content * content_create(char *url) c->source_data = 0; c->source_size = 0; c->mime_type = 0; - strcpy(c->status_message, messages_get("Loading")); + content_set_status(c, messages_get("Loading")); user_sentinel = xcalloc(1, sizeof(*user_sentinel)); user_sentinel->callback = 0; user_sentinel->p1 = user_sentinel->p2 = 0; @@ -229,6 +231,26 @@ void content_set_type(struct content *c, content_type type, } +/** + * Updates content with new status. + * + * The textual status contained in the content is updated with given string. + * + * \param status_message new textual status + */ +void content_set_status(struct content *c, const char *status_message, ...) +{ + va_list ap; + int len; + + va_start(ap, status_message); + if ((len = vsnprintf(c->status_message, sizeof(c->status_message), status_message, ap)) < 0 + || len >= sizeof(c->status_message)) + c->status_message[sizeof(c->status_message) - 1] = '\0'; + va_end(ap); +} + + /** * Process a block of source data. * diff --git a/content/content.h b/content/content.h index 5c36559da..13c4092ce 100644 --- a/content/content.h +++ b/content/content.h @@ -169,6 +169,7 @@ content_type content_lookup(const char *mime_type); struct content * content_create(char *url); void content_set_type(struct content *c, content_type type, const char *mime_type, const char *params[]); +void content_set_status(struct content *c, const char *status_message, ...); void content_process_data(struct content *c, char *data, unsigned long size); void content_convert(struct content *c, unsigned long width, unsigned long height); void content_revive(struct content *c, unsigned long width, unsigned long height); diff --git a/content/fetchcache.c b/content/fetchcache.c index a56da83f9..ed958882e 100644 --- a/content/fetchcache.c +++ b/content/fetchcache.c @@ -171,13 +171,13 @@ void fetchcache_callback(fetch_msg msg, void *p, char *data, unsigned long size) case FETCH_DATA: LOG(("FETCH_DATA")); if (c->total_size) - sprintf(c->status_message, + content_set_status(c, messages_get("RecPercent"), human_friendly_bytesize(c->source_size + size), human_friendly_bytesize(c->total_size), (unsigned int) ((c->source_size + size) * 100.0 / c->total_size)); else - sprintf(c->status_message, + content_set_status(c, messages_get("Received"), human_friendly_bytesize(c->source_size + size)); content_broadcast(c, CONTENT_MSG_STATUS, msg_data); @@ -186,9 +186,9 @@ void fetchcache_callback(fetch_msg msg, void *p, char *data, unsigned long size) case FETCH_FINISHED: LOG(("FETCH_FINISHED")); - sprintf(c->status_message, messages_get("Converting"), - c->source_size); c->fetch = 0; + content_set_status(c, messages_get("Converting"), + c->source_size); content_broadcast(c, CONTENT_MSG_STATUS, msg_data); content_convert(c, c->width, c->height); break; diff --git a/render/html.c b/render/html.c index ab772e5d5..722fcc5ba 100644 --- a/render/html.c +++ b/render/html.c @@ -185,7 +185,7 @@ int html_convert(struct content *c, unsigned int width, unsigned int height) /* convert xml tree to box tree */ LOG(("XML to box")); - sprintf(c->status_message, messages_get("Processing")); + content_set_status(c, messages_get("Processing")); content_broadcast(c, CONTENT_MSG_STATUS, data); xml_to_box(html, c); /*box_dump(c->data.html.layout->children, 0);*/ @@ -198,7 +198,7 @@ int html_convert(struct content *c, unsigned int width, unsigned int height) xmlFreeDoc(document); /* layout the box tree */ - sprintf(c->status_message, messages_get("Formatting")); + content_set_status(c, messages_get("Formatting")); content_broadcast(c, CONTENT_MSG_STATUS, data); LOG(("Layout document")); layout_document(c->data.html.layout->children, width, @@ -210,11 +210,10 @@ int html_convert(struct content *c, unsigned int width, unsigned int height) if (c->active == 0) { c->status = CONTENT_STATUS_DONE; - sprintf(c->status_message, messages_get("Done")); + content_set_status(c, messages_get("Done")); } else { c->status = CONTENT_STATUS_READY; - sprintf(c->status_message, messages_get("FetchObjs"), - c->active); + content_set_status(c, messages_get("FetchObjs"), c->active); } return 0; @@ -424,7 +423,7 @@ void html_find_stylesheets(struct content *c, xmlNode *head) /* complete the fetches */ while (c->active != 0) { if (c->active != last_active) { - sprintf(c->status_message, messages_get("FetchStyle"), + content_set_status(c, messages_get("FetchStyle"), c->active); content_broadcast(c, CONTENT_MSG_STATUS, msg_data); last_active = c->active; @@ -434,7 +433,7 @@ void html_find_stylesheets(struct content *c, xmlNode *head) } if (c->error) { - sprintf(c->status_message, "Warning: some stylesheets failed to load"); + content_set_status(c, "Warning: some stylesheets failed to load"); content_broadcast(c, CONTENT_MSG_STATUS, msg_data); } } @@ -457,7 +456,7 @@ void html_convert_css_callback(content_msg msg, struct content *css, c->data.html.stylesheet_content[i] = 0; c->active--; c->error = 1; - sprintf(c->status_message, messages_get("NotCSS")); + content_set_status(c, messages_get("NotCSS")); content_broadcast(c, CONTENT_MSG_STATUS, data); content_remove_user(css, html_convert_css_callback, c, (void*)i); } @@ -478,7 +477,7 @@ void html_convert_css_callback(content_msg msg, struct content *css, break; case CONTENT_MSG_STATUS: - snprintf(c->status_message, 80, messages_get("FetchStyle2"), + content_set_status(c, messages_get("FetchStyle2"), c->active, css->status_message); content_broadcast(c, CONTENT_MSG_STATUS, data); break; @@ -585,7 +584,7 @@ void html_object_callback(content_msg msg, struct content *object, c->data.html.object[i].content = 0; c->active--; c->error = 1; - sprintf(c->status_message, messages_get("BadObject")); + content_set_status(c, messages_get("BadObject")); content_broadcast(c, CONTENT_MSG_STATUS, data); content_remove_user(object, html_object_callback, c, (void*)i); break; @@ -606,13 +605,13 @@ void html_object_callback(content_msg msg, struct content *object, c->data.html.object[i].content = 0; c->active--; c->error = 1; - snprintf(c->status_message, 80, - messages_get("ObjError"), data.error); + content_set_status(c, messages_get("ObjError"), + data.error); content_broadcast(c, CONTENT_MSG_STATUS, data); break; case CONTENT_MSG_STATUS: - snprintf(c->status_message, 80, messages_get("FetchObjs2"), + content_set_status(c, messages_get("FetchObjs2"), c->active, object->status_message); /* content_broadcast(c, CONTENT_MSG_STATUS, 0); */ break; @@ -685,12 +684,11 @@ void html_object_callback(content_msg msg, struct content *object, /* all objects have arrived */ content_reformat(c, c->available_width, 0); c->status = CONTENT_STATUS_DONE; - sprintf(c->status_message, messages_get("Done")); + content_set_status(c, messages_get("Done")); content_broadcast(c, CONTENT_MSG_DONE, data); } if (c->status == CONTENT_STATUS_READY) - sprintf(c->status_message, messages_get("FetchObjs"), - c->active); + content_set_status(c, messages_get("FetchObjs"), c->active); } -- cgit v1.2.3