[project @ 2004-03-27 23:18:52 by bursa]

Implement url_nice() and make save boxes use it to choose the default filename.

svn path=/import/netsurf/; revision=682
This commit is contained in:
James Bursa 2004-03-27 23:18:52 +00:00
parent 26f171382f
commit d5a81883e8
5 changed files with 116 additions and 11 deletions

View File

@ -9,12 +9,12 @@ Quit:Quit
Page:Page
PageInfo:Info...
Save:Save
SaveComp:Full save
Save:Save F3
SaveComp:Full save F3
Export:Export
ExportAs:Export as
Draw:Draw
Text:Text
Draw:Draw ^F3
Text:Text ^F3
Print:Print...
Selection:Selection
Copy:Copy to clipboard

View File

@ -9,11 +9,12 @@ Quit:Quitter
Page:Page
PageInfo:Info...
Save:Sauver
Save:Sauver F3
SaveComp:Full save F3
Export:Exporter
ExportAs:Exporter sous
Draw:Draw
Text:Text
Draw:Draw ^F3
Text:Text ^F3
Print:Imprimer...
Selection:Sélection
Copy:Copier vers le presse papier

View File

@ -20,6 +20,7 @@
#include "netsurf/riscos/theme.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/messages.h"
#include "netsurf/utils/url.h"
#include "netsurf/utils/utils.h"
@ -342,7 +343,8 @@ void ro_gui_menu_prepare_save(struct content *c)
{
char icon_buf[20] = "file_xxx";
const char *icon = icon_buf;
const char *name;
const char *name = "";
const char *nice;
switch (gui_current_save_type) {
case GUI_SAVE_SOURCE:
@ -368,6 +370,10 @@ void ro_gui_menu_prepare_save(struct content *c)
break;
}
if (c)
if ((nice = url_nice(c->url)))
name = nice;
ro_gui_set_icon_string(dialog_saveas, ICON_SAVE_ICON, icon);
ro_gui_set_icon_string(dialog_saveas, ICON_SAVE_PATH, name);
}

View File

@ -9,6 +9,7 @@
* URL parsing and joining (implementation).
*/
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
@ -20,7 +21,7 @@
#include "netsurf/utils/utils.h"
regex_t url_re, url_up_re;
regex_t url_re, url_up_re, url_nice_re;
/**
* Initialise URL routines.
@ -36,6 +37,10 @@ void url_init(void)
regcomp_wrapper(&url_up_re,
"/(|[^/]|[.][^./]|[^./][.]|[^/][^/][^/]+)/[.][.](/|$)",
REG_EXTENDED);
regcomp_wrapper(&url_nice_re,
"^([^.]{0,4}[.])?([^.][^.][.])?([^/?&;.=]*)"
"(=[^/?&;.]*)?[/?&;.]",
REG_EXTENDED);
}
@ -394,6 +399,95 @@ char *url_host(const char *url)
}
/**
* Attempt to find a nice filename for a URL.
*
* \param url an absolute URL
* \returns filename allocated on heap, or 0 on memory exhaustion
*/
char *url_nice(const char *url)
{
unsigned int i, j, k = 0, so;
unsigned int len;
const char *colon;
char buf[40];
char *result;
char *rurl;
int m;
regmatch_t match[10];
result = malloc(40);
if (!result)
return 0;
len = strlen(url);
assert(len != 0);
rurl = malloc(len + 1);
if (!rurl) {
free(result);
return 0;
}
/* reverse url into rurl */
for (i = 0, j = len - 1; i != len; i++, j--)
rurl[i] = url[j];
rurl[len] = 0;
/* prepare a fallback: always succeeds */
colon = strchr(url, ':');
if (colon)
url = colon + 1;
strncpy(result, url, 15);
result[15] = 0;
for (i = 0; result[i]; i++)
if (!isalnum(result[i]))
result[i] = '_';
/* append nice pieces */
j = 0;
do {
m = regexec(&url_nice_re, rurl + j, 10, match, 0);
if (m)
break;
if (match[3].rm_so != match[3].rm_eo) {
so = match[3].rm_so;
i = match[3].rm_eo - so;
if (15 < i) {
so = match[3].rm_eo - 15;
i = 15;
}
if (15 < k + i)
break;
if (k)
k++;
strncpy(buf + k, rurl + j + so, i);
k += i;
buf[k] = 160; /* nbsp */
}
j += match[0].rm_eo;
} while (j != len);
if (k == 0) {
free(rurl);
return result;
}
/* reverse back */
for (i = 0, j = k - 1; i != k; i++, j--)
result[i] = buf[j];
result[k] = 0;
for (i = 0; i != k; i++)
if (result[i] != (char) 0xa0 && !isalnum(result[i]))
result[i] = '_';
return result;
}
#ifdef TEST
@ -411,12 +505,15 @@ int main(int argc, char *argv[])
s = url_host(argv[i]);
if (s)
printf("<== '%s'\n", s);*/
if (1 != i) {
/* if (1 != i) {
s = url_join(argv[i], argv[1]);
if (s)
printf("'%s' + '%s' \t= '%s'\n", argv[1],
argv[i], s);
}
}*/
s = url_nice(argv[i]);
if (s)
printf("'%s'\n", s);
}
return 0;
}

View File

@ -16,5 +16,6 @@ void url_init(void);
char *url_normalize(const char *url);
char *url_join(const char *rel, const char *base);
char *url_host(const char *url);
char *url_nice(const char *url);
#endif