From 6879c02a332824ff63ce0b56de36996d2f0d48cf Mon Sep 17 00:00:00 2001 From: James Bursa Date: Sun, 6 Jun 2004 19:39:17 +0000 Subject: [project @ 2004-06-06 19:39:17 by bursa] Add and improve error handling of save_complete. Save HTML as UTF-8 to prevent encoding issues. svn path=/import/netsurf/; revision=931 --- riscos/save.c | 25 ++- riscos/save_complete.c | 522 +++++++++++++++++++++++++++++-------------------- riscos/save_complete.h | 2 +- riscos/window.c | 3 - 4 files changed, 323 insertions(+), 229 deletions(-) diff --git a/riscos/save.c b/riscos/save.c index 22278bf9c..ba636435d 100644 --- a/riscos/save.c +++ b/riscos/save.c @@ -35,7 +35,7 @@ extern struct content *save_content; typedef enum { LINK_ACORN, LINK_ANT, LINK_TEXT } link_format; -static void ro_gui_save_complete(struct content *c, char *path); +static bool ro_gui_save_complete(struct content *c, char *path); static void ro_gui_save_object_native(struct content *c, char *path); static bool ro_gui_save_link(struct content *c, link_format format, char *path); @@ -166,7 +166,8 @@ void ro_gui_save_datasave_ack(wimp_message *message) break; case GUI_SAVE_COMPLETE: - ro_gui_save_complete(c, path); + if (!ro_gui_save_complete(c, path)) + return; break; case GUI_SAVE_DRAW: @@ -236,13 +237,17 @@ void ro_gui_save_datasave_ack(wimp_message *message) /** * Prepare an application directory and save_complete() to it. + * + * \param c content of type CONTENT_HTML to save + * \param path path to save as + * \return true on success, false on error and error reported */ #define WIDTH 64 #define HEIGHT 64 #define SPRITE_SIZE (16 + 44 + ((WIDTH / 2 + 3) & ~3) * HEIGHT / 2) -void ro_gui_save_complete(struct content *c, char *path) +bool ro_gui_save_complete(struct content *c, char *path) { char buf[256]; FILE *fp; @@ -258,7 +263,7 @@ void ro_gui_save_complete(struct content *c, char *path) LOG(("xosfile_create_dir: 0x%x: %s", error->errnum, error->errmess)); warn_user("SaveError", error->errmess); - return; + return false; } /* Save !Run file */ @@ -267,7 +272,7 @@ void ro_gui_save_complete(struct content *c, char *path) if (!fp) { LOG(("fopen(): errno = %i", errno)); warn_user("SaveError", strerror(errno)); - return; + return false; } fprintf(fp, "Filer_Run .index\n"); fclose(fp); @@ -276,7 +281,7 @@ void ro_gui_save_complete(struct content *c, char *path) LOG(("xosfile_set_type: 0x%x: %s", error->errnum, error->errmess)); warn_user("SaveError", error->errmess); - return; + return false; } /* Create !Sprites */ @@ -288,7 +293,7 @@ void ro_gui_save_complete(struct content *c, char *path) area = thumbnail_initialise(34, 34, os_MODE8BPP90X90); if (!area) { warn_user("NoMemory", 0); - return; + return false; } sprite_header = (osspriteop_header *)(area + 1); strncpy(sprite_header->name, appname + 1, 12); @@ -305,12 +310,14 @@ void ro_gui_save_complete(struct content *c, char *path) LOG(("xosspriteop_save_sprite_file: 0x%x: %s", error->errnum, error->errmess)); warn_user("SaveError", error->errmess); - return; + return false; } - save_complete(c, path); + return save_complete(c, path); } + + void ro_gui_save_object_native(struct content *c, char *path) { os_error *error; diff --git a/riscos/save_complete.c b/riscos/save_complete.c index bdf8682db..4ad1668b6 100644 --- a/riscos/save_complete.c +++ b/riscos/save_complete.c @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -33,24 +34,24 @@ regex_t save_complete_import_re; /** An entry in save_complete_list. */ struct save_complete_entry { - char *url; /**< Fully qualified URL, as per url_join output */ - int ptr; /**< Pointer to object's location in memory */ - struct save_complete_entry *next; /**< Next entry in list */ + struct content *content; + struct save_complete_entry *next; /**< Next entry in list */ }; /** List of urls seen and saved so far. */ -static struct save_complete_entry *save_complete_list; +static struct save_complete_entry *save_complete_list = 0; -static void save_complete_html(struct content *c, const char *path, - bool index); -static void save_imported_sheets(struct content *c, const char *path); +static bool save_complete_html(struct content *c, const char *path, + bool index); +static bool save_imported_sheets(struct content *c, const char *path); static char * rewrite_stylesheet_urls(const char *source, unsigned int size, int *osize, const char *base); -static int rewrite_document_urls(xmlDoc *doc, const char *base); -static int rewrite_urls(xmlNode *n, const char *base); -static void rewrite_url(xmlNode *n, const char *attr, const char *base); -static void save_complete_add_url(const char *url, int id); -static int save_complete_find_url(const char *url); +static bool rewrite_document_urls(xmlDoc *doc, const char *base); +static bool rewrite_urls(xmlNode *n, const char *base); +static bool rewrite_url(xmlNode *n, const char *attr, const char *base); +static bool save_complete_list_add(struct content *content); +static struct content * save_complete_list_find(const char *url); +static bool save_complete_list_check(struct content *content); /** @@ -58,64 +59,89 @@ static int save_complete_find_url(const char *url); * * \param c CONTENT_HTML to save * \param path directory to save to (must exist) + * \return true on success, false on error and error reported */ -void save_complete(struct content *c, const char *path) +bool save_complete(struct content *c, const char *path) { - save_complete_list = 0; + bool result; - save_complete_html(c, path, true); + result = save_complete_html(c, path, true); - /* free save_complete_list */ + /* free save_complete_list */ while (save_complete_list) { struct save_complete_entry *next = save_complete_list->next; - free(save_complete_list->url); free(save_complete_list); save_complete_list = next; } + + return result; } + /** * Save an HTML page with all dependencies, recursing through imported pages. * - * \param c CONTENT_HTML to save - * \param path directory to save to (must exist) + * \param c CONTENT_HTML to save + * \param path directory to save to (must exist) + * \param index true to save as "index" + * \return true on success, false on error and error reported */ -void save_complete_html(struct content *c, const char *path, bool index) +bool save_complete_html(struct content *c, const char *path, bool index) { char spath[256]; unsigned int i; - htmlParserCtxtPtr toSave; + htmlParserCtxtPtr parser; + os_error *error; if (c->type != CONTENT_HTML) - return; + return false; + + if (save_complete_list_check(c)) + return true; - /* save stylesheets, ignoring the base sheet */ - for (i = 1; i != c->data.html.stylesheet_count; i++) { + /* save stylesheets, ignoring the base sheet */ + for (i = 1; i != c->data.html.stylesheet_count; i++) { struct content *css = c->data.html.stylesheet_content[i]; char *source; int source_len; - if (!css) - continue; + if (!css) + continue; + if (save_complete_list_check(css)) + continue; - save_complete_add_url(css->url, (int) css); + if (!save_complete_list_add(css)) { + warn_user("NoMemory", 0); + return false; + } - save_imported_sheets(css, path); + if (!save_imported_sheets(css, path)) + return false; - if (i == 1) continue; /* don't save