summaryrefslogtreecommitdiff
path: root/amiga
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2014-05-26 23:43:36 +0100
committerVincent Sanders <vince@kyllikki.org>2014-05-26 23:52:36 +0100
commit00b6cfc57e27f8146d9b41ba8e63038a4f9df70e (patch)
treebadf71a43a81975098d3f1294073d8c76bc994ea /amiga
parent1f337f292d1c98c396d5f8d5d294f9ba13963586 (diff)
downloadnetsurf-00b6cfc57e27f8146d9b41ba8e63038a4f9df70e.tar.gz
netsurf-00b6cfc57e27f8146d9b41ba8e63038a4f9df70e.tar.bz2
rework path to url mapping functions to convert from and to nsurl
Diffstat (limited to 'amiga')
-rw-r--r--amiga/file.c6
-rw-r--r--amiga/gui.c160
-rw-r--r--amiga/icon.c6
-rwxr-xr-xamiga/misc.c224
-rw-r--r--amiga/misc.h5
5 files changed, 218 insertions, 183 deletions
diff --git a/amiga/file.c b/amiga/file.c
index acc1166e5..24c1fd902 100644
--- a/amiga/file.c
+++ b/amiga/file.c
@@ -76,7 +76,7 @@ static const ULONG ami_file_asl_mime_hook(struct Hook *mh,
void ami_file_open(struct gui_window_2 *gwin)
{
- char *temp, *temp2;
+ char *temp;
nsurl *url;
if(AslRequestTags(filereq,
@@ -93,9 +93,8 @@ void ami_file_open(struct gui_window_2 *gwin)
{
strlcpy(temp, filereq->fr_Drawer, 1024);
AddPart(temp, filereq->fr_File, 1024);
- temp2 = path_to_url(temp);
- if (nsurl_create(temp2, &url) != NSERROR_OK) {
+ if (netsurf_path_to_nsurl(temp, &url) != NSERROR_OK) {
warn_user("NoMemory", 0);
} else {
browser_window_navigate(gwin->bw,
@@ -108,7 +107,6 @@ void ami_file_open(struct gui_window_2 *gwin)
nsurl_unref(url);
}
- free(temp2);
FreeVec(temp);
}
}
diff --git a/amiga/gui.c b/amiga/gui.c
index 2c6104444..2592352b7 100644
--- a/amiga/gui.c
+++ b/amiga/gui.c
@@ -222,127 +222,6 @@ static void gui_window_place_caret(struct gui_window *g, int x, int y, int heigh
nsoptions_default[NSOPTION_##OPTION].value.i = VALUE
-/**
- * Generate a posix path from one or more component elemnts.
- *
- * If a string is allocated it must be freed by the caller.
- *
- * @param[in,out] str pointer to string pointer if this is NULL enough
- * storage will be allocated for the complete path.
- * @param[in,out] size The size of the space available if \a str not
- * NULL on input and if not NULL set to the total
- * output length on output.
- * @param[in] nemb The number of elements.
- * @param[in] ... The elements of the path as string pointers.
- * @return NSERROR_OK and the complete path is written to str
- * or error code on faliure.
- */
-static nserror amiga_vmkpath(char **str, size_t *size, size_t nelm, va_list ap)
-{
- const char *elm[16];
- size_t elm_len[16];
- size_t elm_idx;
- char *fname;
- size_t fname_len = 0;
-
- /* check the parameters are all sensible */
- if ((nelm == 0) || (nelm > 16)) {
- return NSERROR_BAD_PARAMETER;
- }
- if ((*str != NULL) && (size == NULL)) {
- /* if the caller is providing the buffer they must say
- * how much space is available.
- */
- return NSERROR_BAD_PARAMETER;
- }
-
- /* calculate how much storage we need for the complete path
- * with all the elements.
- */
- for (elm_idx = 0; elm_idx < nelm; elm_idx++) {
- elm[elm_idx] = va_arg(ap, const char *);
- /* check the argument is not NULL */
- if (elm[elm_idx] == NULL) {
- return NSERROR_BAD_PARAMETER;
- }
- elm_len[elm_idx] = strlen(elm[elm_idx]);
- fname_len += elm_len[elm_idx];
- }
- fname_len += nelm; /* allow for separators and terminator */
-
- /* ensure there is enough space */
- fname = *str;
- if (fname != NULL) {
- if (fname_len > *size) {
- return NSERROR_NOSPACE;
- }
- } else {
- fname = malloc(fname_len);
- if (fname == NULL) {
- return NSERROR_NOMEM;
- }
- }
-
- /* copy the first element complete */
- memmove(fname, elm[0], elm_len[0]);
- fname[elm_len[0]] = 0;
-
- /* add the remaining elements */
- for (elm_idx = 1; elm_idx < nelm; elm_idx++) {
- if (!AddPart(fname, elm[elm_idx], fname_len)) {
- break;
- }
- }
-
- *str = fname;
- if (size != NULL) {
- *size = fname_len;
- }
-
- return NSERROR_OK;
-}
-
-/**
- * Get the basename of a file using posix path handling.
- *
- * This gets the last element of a path and returns it.
- *
- * @param[in] path The path to extract the name from.
- * @param[in,out] str Pointer to string pointer if this is NULL enough
- * storage will be allocated for the path element.
- * @param[in,out] size The size of the space available if \a
- * str not NULL on input and set to the total
- * output length on output.
- * @return NSERROR_OK and the complete path is written to str
- * or error code on faliure.
- */
-static nserror amiga_basename(const char *path, char **str, size_t *size)
-{
- const char *leafname;
- char *fname;
-
- if (path == NULL) {
- return NSERROR_BAD_PARAMETER;
- }
-
- leafname = FilePart(path);
- if (leafname == NULL) {
- return NSERROR_BAD_PARAMETER;
- }
-
- fname = strdup(leafname);
- if (fname == NULL) {
- return NSERROR_NOMEM;
- }
-
- *str = fname;
- if (size != NULL) {
- *size = strlen(fname);
- }
- return NSERROR_OK;
-}
-
-
STRPTR ami_locale_langs(void)
{
@@ -778,11 +657,7 @@ static nsurl *gui_get_resource_url(const char *path)
else return NULL;
}
- raw = path_to_url(buf);
- if (raw != NULL) {
- nsurl_create(raw, &url);
- free(raw);
- }
+ netsurf_path_to_nsurl(buf, &url);
return url;
}
@@ -1036,7 +911,13 @@ static void gui_init2(int argc, char** argv)
DevNameFromLock(wbarg->wa_Lock,fullpath,1024,DN_FULLPATH);
AddPart(fullpath,wbarg->wa_Name,1024);
- if(!temp_homepage_url) temp_homepage_url = path_to_url(fullpath);
+ if(!temp_homepage_url) {
+ nsurl temp_url;
+ if (netsurf_path_to_nsurl(fullpath, &temp_url) == NSERROR_OK) {
+ temp_homepage_url = strcpy(nsurl_data(temp_url));
+ nsurl_unref(temp_url);
+ }
+ }
if(notalreadyrunning)
{
@@ -2378,7 +2259,6 @@ void ami_handle_appmsg(void)
int x, y;
struct WBArg *appwinargs;
STRPTR filename;
- char *urlfilename;
int i = 0;
while(appmsg=(struct AppMessage *)GetMsg(appport))
@@ -2410,9 +2290,8 @@ void ami_handle_appmsg(void)
appmsg->am_MouseX, appmsg->am_MouseY) == false)
{
nsurl *url;
- urlfilename = path_to_url(filename);
- if (nsurl_create(urlfilename, &url) != NSERROR_OK) {
+ if (netsurf_path_to_nsurl(filename, &url) != NSERROR_OK) {
warn_user("NoMemory", 0);
}
else
@@ -2440,17 +2319,14 @@ void ami_handle_appmsg(void)
}
nsurl_unref(url);
}
-
- free(urlfilename);
}
else
{
if(browser_window_drop_file_at_point(gwin->bw, x, y, filename) == false)
{
nsurl *url;
- urlfilename = path_to_url(filename);
- if (nsurl_create(urlfilename, &url) != NSERROR_OK) {
+ if (netsurf_path_to_nsurl(filename, &url) != NSERROR_OK) {
warn_user("NoMemory", 0);
}
else
@@ -2480,7 +2356,6 @@ void ami_handle_appmsg(void)
}
nsurl_unref(url);
}
- free(urlfilename);
}
}
FreeVec(filename);
@@ -2527,11 +2402,8 @@ void ami_handle_applib(void)
{
struct ApplicationOpenPrintDocMsg *applibopdmsg =
(struct ApplicationOpenPrintDocMsg *)applibmsg;
- char *tempurl;
-
- tempurl = path_to_url(applibopdmsg->fileName);
- error = nsurl_create(tempurl, &url);
+ error = netsurf_path_to_nsurl(applibopdmsg->fileName, &url);
if (error == NSERROR_OK) {
error = browser_window_create(BW_CREATE_HISTORY,
url,
@@ -2543,7 +2415,6 @@ void ami_handle_applib(void)
if (error != NSERROR_OK) {
warn_user(messages_get_errorcode(error), 0);
}
- free(tempurl);
}
break;
@@ -5206,16 +5077,9 @@ static struct gui_window_table amiga_window_table = {
.save_link = gui_window_save_link,
};
-/* amiga file handling operations */
-static struct gui_file_table amiga_file_table = {
- .mkpath = amiga_vmkpath,
- .basename = amiga_basename,
-};
static struct gui_fetch_table amiga_fetch_table = {
.filetype = fetch_filetype,
- .path_to_url = path_to_url,
- .url_to_path = url_to_path,
.get_resource_url = gui_get_resource_url,
};
@@ -5253,7 +5117,7 @@ int main(int argc, char** argv)
.clipboard = amiga_clipboard_table,
.download = amiga_download_table,
.fetch = &amiga_fetch_table,
- .file = &amiga_file_table,
+ .file = amiga_file_table,
.utf8 = amiga_utf8_table,
.search = amiga_search_table,
.search_web = &amiga_search_web_table,
diff --git a/amiga/icon.c b/amiga/icon.c
index 66a008faf..95fd685cd 100644
--- a/amiga/icon.c
+++ b/amiga/icon.c
@@ -144,14 +144,12 @@ bool amiga_icon_convert(struct content *c)
uint8 r, g, b, a;
ULONG offset;
const char *url;
- char *filename;
+ char *filename = NULL;
char *p;
ULONG trans, pals1;
struct ColorRegister *pal1;
- url = nsurl_access(content_get_url(c));
- filename = url_to_path(url);
-
+ netsurf_nsurl_to_path(content_get_url(c), &filename);
/* This loader will only work on local files, so fail if not a local path */
if(filename == NULL)
{
diff --git a/amiga/misc.c b/amiga/misc.c
index 3b4363ff5..d2b243a24 100755
--- a/amiga/misc.c
+++ b/amiga/misc.c
@@ -36,6 +36,7 @@
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/url.h"
+#include "utils/file.h"
#include "utils/utils.h"
void warn_user(const char *warning, const char *detail)
@@ -105,54 +106,91 @@ void die(const char *error)
exit(1);
}
-char *url_to_path(const char *url)
+/**
+ * Create a path from a nsurl using amiga file handling.
+ *
+ * @parm[in] url The url to encode.
+ * @param[out] path_out A string containing the result path which should
+ * be freed by the caller.
+ * @return NSERROR_OK and the path is written to \a path or error code
+ * on faliure.
+ */
+static nserror amiga_nsurl_to_path(struct nsurl *url, char **path_out)
{
- char *unesc, *slash, *colon, *url2;
-
- if (strncmp(url, "file://", SLEN("file://")) != 0)
- return NULL;
-
- url += SLEN("file://");
+ lwc_string *urlpath;
+ char *path;
+ bool match;
+ lwc_string *scheme;
+ nserror res;
+ char *colon;
+ char *slash;
+
+ if ((url == NULL) || (path_out == NULL)) {
+ return NSERROR_BAD_PARAMETER;
+ }
- if (strncmp(url, "localhost", SLEN("localhost")) == 0)
- url += SLEN("localhost");
+ scheme = nsurl_get_component(url, NSURL_SCHEME);
- if (strncmp(url, "/", SLEN("/")) == 0)
- url += SLEN("/");
+ if (lwc_string_caseless_isequal(scheme, corestring_lwc_file,
+ &match) != lwc_error_ok)
+ {
+ return NSERROR_BAD_PARAMETER;
+ }
+ lwc_string_unref(scheme);
+ if (match == false) {
+ return NSERROR_BAD_PARAMETER;
+ }
- if(*url == '\0')
- return NULL; /* file:/// is not a valid path */
+ urlpath = nsurl_get_component(url, NSURL_PATH);
+ if (urlpath == NULL) {
+ return NSERROR_BAD_PARAMETER;
+ }
- url2 = malloc(strlen(url) + 2);
- strcpy(url2, url);
+ res = url_unescape(lwc_string_data(urlpath) + 1, &path);
+ lwc_string_unref(urlpath);
+ if (res != NSERROR_OK) {
+ return res;
+ }
- colon = strchr(url2, ':');
+ colon = strchr(path, ':');
if(colon == NULL)
{
- if(slash = strchr(url2, '/'))
+ slash = strchr(path, '/');
+ if(slash)
{
*slash = ':';
}
else
{
- int len = strlen(url2);
- url2[len] = ':';
- url2[len + 1] = '\0';
+ int len = strlen(path);
+ path[len] = ':';
+ path[len + 1] = '\0';
}
}
- if(url_unescape(url2,&unesc) == NSERROR_OK)
- return unesc;
+ *path_out = path;
- return (char *)url2;
+ return NSERROR_OK;
}
-char *path_to_url(const char *path)
+/**
+ * Create a nsurl from a path using amiga file handling.
+ *
+ * Perform the necessary operations on a path to generate a nsurl.
+ *
+ * @param[in] path The path to convert.
+ * @param[out] url_out pointer to recive the nsurl, The returned url
+ * must be unreferenced by the caller.
+ * @return NSERROR_OK and the url is placed in \a url or error code on
+ * faliure.
+ */
+static nserror amiga_path_to_nsurl(const char *path, struct nsurl **url_out)
{
char *colon = NULL;
char *r = NULL;
char newpath[1024 + strlen(path)];
BPTR lock = 0;
+ nserror ret;
if(lock = Lock(path, MODE_OLDFILE))
{
@@ -162,13 +200,19 @@ char *path_to_url(const char *path)
else strlcpy(newpath, path, sizeof newpath);
r = malloc(strlen(newpath) + SLEN("file:///") + 1);
+ if (r == NULL) {
+ return NSERROR_NOMEM;
+ }
if(colon = strchr(newpath, ':')) *colon = '/';
strcpy(r, "file:///");
strcat(r, newpath);
- return r;
+ ret = nsurl_create(r, url_out);
+ free(r);
+
+ return ret;
}
/**
@@ -196,3 +240,133 @@ char *translate_escape_chars(const char *s)
ret[ii] = '\0';
return ret;
}
+
+/**
+ * Generate a posix path from one or more component elemnts.
+ *
+ * If a string is allocated it must be freed by the caller.
+ *
+ * @param[in,out] str pointer to string pointer if this is NULL enough
+ * storage will be allocated for the complete path.
+ * @param[in,out] size The size of the space available if \a str not
+ * NULL on input and if not NULL set to the total
+ * output length on output.
+ * @param[in] nemb The number of elements.
+ * @param[in] ... The elements of the path as string pointers.
+ * @return NSERROR_OK and the complete path is written to str
+ * or error code on faliure.
+ */
+static nserror amiga_vmkpath(char **str, size_t *size, size_t nelm, va_list ap)
+{
+ const char *elm[16];
+ size_t elm_len[16];
+ size_t elm_idx;
+ char *fname;
+ size_t fname_len = 0;
+
+ /* check the parameters are all sensible */
+ if ((nelm == 0) || (nelm > 16)) {
+ return NSERROR_BAD_PARAMETER;
+ }
+ if ((*str != NULL) && (size == NULL)) {
+ /* if the caller is providing the buffer they must say
+ * how much space is available.
+ */
+ return NSERROR_BAD_PARAMETER;
+ }
+
+ /* calculate how much storage we need for the complete path
+ * with all the elements.
+ */
+ for (elm_idx = 0; elm_idx < nelm; elm_idx++) {
+ elm[elm_idx] = va_arg(ap, const char *);
+ /* check the argument is not NULL */
+ if (elm[elm_idx] == NULL) {
+ return NSERROR_BAD_PARAMETER;
+ }
+ elm_len[elm_idx] = strlen(elm[elm_idx]);
+ fname_len += elm_len[elm_idx];
+ }
+ fname_len += nelm; /* allow for separators and terminator */
+
+ /* ensure there is enough space */
+ fname = *str;
+ if (fname != NULL) {
+ if (fname_len > *size) {
+ return NSERROR_NOSPACE;
+ }
+ } else {
+ fname = malloc(fname_len);
+ if (fname == NULL) {
+ return NSERROR_NOMEM;
+ }
+ }
+
+ /* copy the first element complete */
+ memmove(fname, elm[0], elm_len[0]);
+ fname[elm_len[0]] = 0;
+
+ /* add the remaining elements */
+ for (elm_idx = 1; elm_idx < nelm; elm_idx++) {
+ if (!AddPart(fname, elm[elm_idx], fname_len)) {
+ break;
+ }
+ }
+
+ *str = fname;
+ if (size != NULL) {
+ *size = fname_len;
+ }
+
+ return NSERROR_OK;
+}
+
+/**
+ * Get the basename of a file using posix path handling.
+ *
+ * This gets the last element of a path and returns it.
+ *
+ * @param[in] path The path to extract the name from.
+ * @param[in,out] str Pointer to string pointer if this is NULL enough
+ * storage will be allocated for the path element.
+ * @param[in,out] size The size of the space available if \a
+ * str not NULL on input and set to the total
+ * output length on output.
+ * @return NSERROR_OK and the complete path is written to str
+ * or error code on faliure.
+ */
+static nserror amiga_basename(const char *path, char **str, size_t *size)
+{
+ const char *leafname;
+ char *fname;
+
+ if (path == NULL) {
+ return NSERROR_BAD_PARAMETER;
+ }
+
+ leafname = FilePart(path);
+ if (leafname == NULL) {
+ return NSERROR_BAD_PARAMETER;
+ }
+
+ fname = strdup(leafname);
+ if (fname == NULL) {
+ return NSERROR_NOMEM;
+ }
+
+ *str = fname;
+ if (size != NULL) {
+ *size = strlen(fname);
+ }
+ return NSERROR_OK;
+}
+
+/* amiga file handling operations */
+static struct gui_file_table file_table = {
+ .mkpath = amiga_vmkpath,
+ .basename = amiga_basename,
+ .nsurl_to_path = amiga_nsurl_to_path,
+ .path_to_nsurl = amiga_path_to_nsurl,
+};
+
+struct gui_file_table *amiga_file_table = &file_table;
diff --git a/amiga/misc.h b/amiga/misc.h
index 60f249c2c..db559f78a 100644
--- a/amiga/misc.h
+++ b/amiga/misc.h
@@ -19,9 +19,10 @@
#ifndef AMIGA_MISC_H
#define AMIGA_MISC_H
+extern struct gui_file_table *amiga_file_table;
+
char *translate_escape_chars(const char *s);
int32 ami_warn_user_multi(const char *body, const char *opt1, const char *opt2, struct Window *win);
-char *url_to_path(const char *url);
-char *path_to_url(const char *path);
+
#endif