summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorMichael Drake <tlsa@netsurf-browser.org>2011-10-28 19:18:14 +0000
committerMichael Drake <tlsa@netsurf-browser.org>2011-10-28 19:18:14 +0000
commita112bfb6769fd393b0903f9262a8e1913407eda8 (patch)
treefdb541059a1643e5f322970c56f023091f4f2f9c /utils
parent110ba21d52b4b73cae7b82462dc057b3e3ef6d1a (diff)
downloadnetsurf-a112bfb6769fd393b0903f9262a8e1913407eda8.tar.gz
netsurf-a112bfb6769fd393b0903f9262a8e1913407eda8.tar.bz2
Function for adding fragment onto nsurl.
svn path=/trunk/netsurf/; revision=13087
Diffstat (limited to 'utils')
-rw-r--r--utils/nsurl.c60
-rw-r--r--utils/nsurl.h20
2 files changed, 79 insertions, 1 deletions
diff --git a/utils/nsurl.c b/utils/nsurl.c
index bb7b5068e..19ccc4374 100644
--- a/utils/nsurl.c
+++ b/utils/nsurl.c
@@ -1691,3 +1691,63 @@ nserror nsurl_defragment(const nsurl *url, nsurl **no_frag)
return NSERROR_OK;
}
+
+/* exported interface, documented in nsurl.h */
+nserror nsurl_refragment(const nsurl *url, lwc_string *frag, nsurl **new_url)
+{
+ int frag_len;
+ int base_len;
+ char *pos;
+
+ assert(url != NULL);
+ assert(frag != NULL);
+
+ /* Create NetSurf URL object */
+ *new_url = malloc(sizeof(nsurl));
+ if (*new_url == NULL) {
+ return NSERROR_NOMEM;
+ }
+
+ /* Find the change in length from url to new_url */
+ base_len = url->length;
+ if (url->fragment != NULL) {
+ base_len -= 1 + lwc_string_length(url->fragment);
+ }
+ frag_len = lwc_string_length(frag);
+
+ /* Set new_url's length */
+ (*new_url)->length = base_len + 1 /* # */ + frag_len;
+
+ /* Create new_url's url string */
+ (*new_url)->string = malloc((*new_url)->length + 1 /* \0 */);
+ if ((*new_url)->string == NULL) {
+ free(*new_url);
+ *new_url = NULL;
+ return NSERROR_NOMEM;
+ }
+
+ /* Set string */
+ pos = (*new_url)->string;
+ memcpy(pos, url->string, base_len);
+ pos += base_len;
+ *pos = '#';
+ memcpy(++pos, lwc_string_data(frag), frag_len);
+ pos += frag_len;
+ *pos = '\0';
+
+ /* Copy components */
+ (*new_url)->scheme = nsurl__component_copy(url->scheme);
+ (*new_url)->username = nsurl__component_copy(url->username);
+ (*new_url)->password = nsurl__component_copy(url->password);
+ (*new_url)->host = nsurl__component_copy(url->host);
+ (*new_url)->port = nsurl__component_copy(url->port);
+ (*new_url)->path = nsurl__component_copy(url->path);
+ (*new_url)->query = nsurl__component_copy(url->query);
+ (*new_url)->fragment = lwc_string_ref(frag);
+
+ /* Give the URL a reference */
+ (*new_url)->count = 1;
+
+ return NSERROR_OK;
+}
+
diff --git a/utils/nsurl.h b/utils/nsurl.h
index c708e0483..46496f212 100644
--- a/utils/nsurl.h
+++ b/utils/nsurl.h
@@ -212,7 +212,7 @@ nserror nsurl_join(const nsurl *base, const char *rel, nsurl **joined);
/**
* Create a NetSurf URL object without a fragment from a NetSurf URL
*
- * \param base NetSurf URL to create new NetSurf URL from
+ * \param url NetSurf URL to create new NetSurf URL from
* \param no_frag Returns new NetSurf URL without fragment
* \return NSERROR_OK on success, appropriate error otherwise
*
@@ -223,4 +223,22 @@ nserror nsurl_join(const nsurl *base, const char *rel, nsurl **joined);
*/
nserror nsurl_defragment(const nsurl *url, nsurl **no_frag);
+
+/**
+ * Create a NetSurf URL object, adding a fragment to an existing URL object
+ *
+ * \param url NetSurf URL to create new NetSurf URL from
+ * \param frag Fragment to add
+ * \param new_url Returns new NetSurf URL without fragment
+ * \return NSERROR_OK on success, appropriate error otherwise
+ *
+ * If return value != NSERROR_OK, nothing will be returned in new_url.
+ *
+ * It is up to the client to call nsurl_destroy when they are finished with
+ * the created object.
+ *
+ * Any fragment in url is replaced with frag in new_url.
+ */
+nserror nsurl_refragment(const nsurl *url, lwc_string *frag, nsurl **new_url);
+
#endif