From 033b5d815a19a05441a99cdc744a6a8752d191cc Mon Sep 17 00:00:00 2001 From: Chris Young Date: Sun, 21 Mar 2010 13:32:59 +0000 Subject: Move code which extracts the filename from a given path into frontend. svn path=/trunk/netsurf/; revision=10139 --- amiga/misc.c | 15 ++++++++++++++- beos/beos_gui.cpp | 20 ++++++++++++++++++++ content/fetchers/fetch_curl.c | 35 +++-------------------------------- framebuffer/misc.c | 20 ++++++++++++++++++++ gtk/gtk_gui.c | 20 ++++++++++++++++++++ riscos/gui.c | 36 ++++++++++++++++++++++++++++++++++++ utils/utils.h | 2 +- windows/misc.c | 20 ++++++++++++++++++++ 8 files changed, 134 insertions(+), 34 deletions(-) diff --git a/amiga/misc.c b/amiga/misc.c index cee6aa688..f492fcf8a 100755 --- a/amiga/misc.c +++ b/amiga/misc.c @@ -1,5 +1,5 @@ /* - * Copyright 2008, 2009 Chris Young + * Copyright 2008-2010 Chris Young * * This file is part of NetSurf, http://www.netsurf-browser.org/ * @@ -24,6 +24,7 @@ #include "utils/messages.h" #include #include +#include "utils/utils.h" void warn_user(const char *warning, const char *detail) { @@ -83,6 +84,18 @@ char *path_to_url(const char *path) return r; } +/** + * Return the filename part of a full path + * + * \param path full path and filename + * \return filename (will be freed with free()) + */ + +char *filename_from_path(char *path) +{ + return strdup(FilePart(path)); +} + /** * returns a string without escape chars or |M chars. * (based on remove_underscores from utils.c) diff --git a/beos/beos_gui.cpp b/beos/beos_gui.cpp index 3f83a8dcb..6c05bc401 100644 --- a/beos/beos_gui.cpp +++ b/beos/beos_gui.cpp @@ -1160,3 +1160,23 @@ static void *myrealloc(void *ptr, size_t len, void *pw) { return realloc(ptr, len); } + +/** + * Return the filename part of a full path + * + * \param path full path and filename + * \return filename (will be freed with free()) + */ + +char *filename_from_path(char *path) +{ + char *leafname; + + leafname = strrchr(path, '/'); + if (!leafname) + leafname = path; + else + leafname += 1; + + return strdup(leafname); +} diff --git a/content/fetchers/fetch_curl.c b/content/fetchers/fetch_curl.c index e11829b46..ca2d86845 100644 --- a/content/fetchers/fetch_curl.c +++ b/content/fetchers/fetch_curl.c @@ -1219,36 +1219,9 @@ fetch_curl_post_convert(struct form_successful_control *control) for (; control; control = control->next) { if (control->file) { char *leafname = 0; -#ifdef riscos - char *temp; - int leaflen; - - temp = strrchr(control->value, '.'); - if (!temp) - temp = control->value; /* already leafname */ - else - temp += 1; - - leaflen = strlen(temp); - - leafname = malloc(leaflen + 1); - if (!leafname) { - LOG(("malloc failed")); - continue; - } - memcpy(leafname, temp, leaflen + 1); - - /* and s/\//\./g */ - for (temp = leafname; *temp; temp++) - if (*temp == '/') - *temp = '.'; -#else - leafname = strrchr(control->value, '/') ; - if (!leafname) - leafname = control->value; - else - leafname += 1; -#endif + + leafname = filename_from_path(control->value); + /* We have to special case filenames of "", so curl * a) actually attempts the fetch and * b) doesn't attempt to open the file "" @@ -1288,9 +1261,7 @@ fetch_curl_post_convert(struct form_successful_control *control) control->value)); free(mimetype); } -#ifdef riscos free(leafname); -#endif } else { code = curl_formadd(&post, &last, diff --git a/framebuffer/misc.c b/framebuffer/misc.c index 20c676324..c5f367019 100644 --- a/framebuffer/misc.c +++ b/framebuffer/misc.c @@ -44,3 +44,23 @@ char *url_to_path(const char *url) { return strdup(url + 5); } + +/** + * Return the filename part of a full path + * + * \param path full path and filename + * \return filename (will be freed with free()) + */ + +char *filename_from_path(char *path) +{ + char *leafname; + + leafname = strrchr(path, '/'); + if (!leafname) + leafname = path; + else + leafname += 1; + + return strdup(leafname); +} diff --git a/gtk/gtk_gui.c b/gtk/gtk_gui.c index 163972fc7..0251e173f 100644 --- a/gtk/gtk_gui.c +++ b/gtk/gtk_gui.c @@ -920,3 +920,23 @@ uint32_t gtk_gui_gdkkey_to_nskey(GdkEventKey *key) key->keyval); } } + +/** + * Return the filename part of a full path + * + * \param path full path and filename + * \return filename (will be freed with free()) + */ + +char *filename_from_path(char *path) +{ + char *leafname; + + leafname = strrchr(path, '/'); + if (!leafname) + leafname = path; + else + leafname += 1; + + return strdup(leafname); +} diff --git a/riscos/gui.c b/riscos/gui.c index 64276b36b..a1c1980e2 100644 --- a/riscos/gui.c +++ b/riscos/gui.c @@ -2417,3 +2417,39 @@ void PDF_Password(char **owner_pass, char **user_pass, char *path) /*TODO:this waits to be written, until then no PDF encryption*/ *owner_pass = NULL; } + +/** + * Return the filename part of a full path + * + * \param path full path and filename + * \return filename (will be freed with free()) + */ + +char *filename_from_path(char *path) +{ + char *leafname; + char *temp; + int leaflen; + + temp = strrchr(path, '.'); + if (!temp) + temp = path; /* already leafname */ + else + temp += 1; + + leaflen = strlen(temp); + + leafname = malloc(leaflen + 1); + if (!leafname) { + LOG(("malloc failed")); + continue; + } + memcpy(leafname, temp, leaflen + 1); + + /* and s/\//\./g */ + for (temp = leafname; *temp; temp++) + if (*temp == '/') + *temp = '.'; + + return leafname; +} diff --git a/utils/utils.h b/utils/utils.h index c6b07c6b6..f58a2632b 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -108,5 +108,5 @@ query_id query_user(const char *query, const char *detail, const query_callback *cb, void *pw, const char *yes, const char *no); void query_close(query_id); void PDF_Password(char **owner_pass, char **user_pass, char *path); - +char *filename_from_path(char *path); #endif diff --git a/windows/misc.c b/windows/misc.c index 5eebaa383..002297665 100644 --- a/windows/misc.c +++ b/windows/misc.c @@ -50,3 +50,23 @@ char *url_to_path(const char *url) { return strdup(url + 5); } + +/** + * Return the filename part of a full path + * + * \param path full path and filename + * \return filename (will be freed with free()) + */ + +char *filename_from_path(char *path) +{ + char *leafname; + + leafname = strrchr(path, '\\'); + if (!leafname) + leafname = path; + else + leafname += 1; + + return strdup(leafname); +} -- cgit v1.2.3