2005-02-03 16:18:22 +03:00
|
|
|
/*
|
|
|
|
* This file is part of NetSurf, http://netsurf.sourceforge.net/
|
|
|
|
* Licensed under the GNU General Public License,
|
|
|
|
* http://www.opensource.org/licenses/gpl-license
|
|
|
|
* Copyright 2005 Richard Wilson <info@tinct.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Central repository for URL data (implementation).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "netsurf/content/url_store.h"
|
2005-06-23 21:22:28 +04:00
|
|
|
#ifdef riscos
|
|
|
|
#include "netsurf/riscos/bitmap.h"
|
|
|
|
#endif
|
2005-02-03 16:18:22 +03:00
|
|
|
#include "netsurf/utils/url.h"
|
|
|
|
#include "netsurf/utils/log.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define ITERATIONS_BEFORE_TEST 32
|
|
|
|
#define MAXIMUM_URL_LENGTH 1024
|
|
|
|
|
|
|
|
struct hostname_data {
|
|
|
|
char *hostname; /** Hostname (lowercase) */
|
|
|
|
int hostname_length; /** Length of hostname */
|
|
|
|
struct url_data *url; /** URLs for this host */
|
|
|
|
struct hostname_data *previous; /** Previous hostname */
|
|
|
|
struct hostname_data *next; /** Next hostname */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct hostname_data *url_store_hostnames = NULL;
|
|
|
|
|
|
|
|
static struct hostname_data *url_store_find_hostname(const char *url);
|
|
|
|
static struct hostname_data *url_store_match_hostname(const char *url,
|
|
|
|
struct hostname_data *previous);
|
2005-04-27 04:01:17 +04:00
|
|
|
static char *url_store_match_scheme = NULL;
|
2005-02-03 16:18:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the hostname data for the specified URL. If no hostname
|
|
|
|
* data is currently available then it is created.
|
|
|
|
*
|
|
|
|
* \param url the url to find hostname data for
|
|
|
|
* \return the current hostname data, or NULL on error
|
|
|
|
*/
|
|
|
|
static struct hostname_data *url_store_find_hostname(const char *url) {
|
|
|
|
struct hostname_data *search;
|
|
|
|
struct hostname_data *result;
|
|
|
|
url_func_result res;
|
|
|
|
char *hostname;
|
|
|
|
int hostname_length;
|
|
|
|
int compare;
|
|
|
|
int fast_exit_counter = ITERATIONS_BEFORE_TEST;
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
assert(url);
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
res = url_host(url, &hostname);
|
2005-06-23 21:22:28 +04:00
|
|
|
if (res != URL_FUNC_OK) {
|
|
|
|
hostname = strdup("file:/"); /* for 'file:/' */
|
|
|
|
if (!hostname)
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-02-03 16:18:22 +03:00
|
|
|
hostname_length = strlen(hostname);
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* try to find a matching hostname fairly quickly */
|
|
|
|
for (search = url_store_hostnames; search; search = search->next) {
|
|
|
|
if ((fast_exit_counter <= 0) ||
|
|
|
|
(search->hostname_length == hostname_length)) {
|
|
|
|
compare = strcmp(hostname, search->hostname);
|
|
|
|
if (compare == 0) {
|
|
|
|
free(hostname);
|
|
|
|
return search;
|
|
|
|
} else if (compare < 0)
|
|
|
|
break;
|
|
|
|
fast_exit_counter = ITERATIONS_BEFORE_TEST;
|
|
|
|
} else {
|
|
|
|
fast_exit_counter--;
|
|
|
|
}
|
|
|
|
}
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* no hostname is available: create a new one */
|
2005-10-02 03:27:22 +04:00
|
|
|
result = calloc(1, sizeof(struct hostname_data));
|
2005-02-03 16:18:22 +03:00
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
result->hostname = hostname;
|
|
|
|
result->hostname_length = hostname_length;
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* simple case: no current hostnames */
|
|
|
|
if (!url_store_hostnames) {
|
|
|
|
url_store_hostnames = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* worst case scenario: the place we need to link is within the last
|
|
|
|
* section of the hostname list so we have no reference to work back
|
|
|
|
* from. rather than slowing with the very common case of searching,
|
|
|
|
* we take a speed hit for this case and simply move to the very end
|
|
|
|
* of the hostname list ready to work backwards. */
|
|
|
|
if (!search)
|
|
|
|
for (search = url_store_hostnames; search->next;
|
|
|
|
search = search->next);
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* we can now simply scan backwards as we know roughly where we need
|
|
|
|
* to link to (we either had an early exit from the searching so we
|
|
|
|
* know we're in the block following where we need to link, or we're
|
|
|
|
* at the very end of the list as we were in the last block.) */
|
|
|
|
while ((search) && (strcmp(hostname, search->hostname) < 0))
|
|
|
|
search = search->previous;
|
|
|
|
|
|
|
|
/* simple case: our new hostname is the first in the list */
|
|
|
|
if (!search) {
|
|
|
|
result->next = url_store_hostnames;
|
|
|
|
url_store_hostnames->previous = result;
|
|
|
|
url_store_hostnames = result;
|
|
|
|
return result;
|
|
|
|
}
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* general case: link in after the found hostname */
|
|
|
|
result->previous = search;
|
|
|
|
result->next = search->next;
|
|
|
|
if (search->next)
|
|
|
|
search->next->previous = result;
|
|
|
|
search->next = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the url data for the specified URL. If no url
|
|
|
|
* data is currently available then it is created.
|
|
|
|
*
|
|
|
|
* \param url a normalized url to find hostname data for
|
|
|
|
* \return the current hostname data, or NULL on error
|
|
|
|
*/
|
|
|
|
struct url_content *url_store_find(const char *url) {
|
|
|
|
struct hostname_data *hostname_data;
|
|
|
|
struct url_data *search;
|
|
|
|
struct url_data *result;
|
|
|
|
int url_length;
|
|
|
|
int compare;
|
|
|
|
int fast_exit_counter = ITERATIONS_BEFORE_TEST;
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
assert(url);
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* find the corresponding hostname data */
|
|
|
|
hostname_data = url_store_find_hostname(url);
|
|
|
|
if (!hostname_data)
|
|
|
|
return NULL;
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* move to the start of the leafname */
|
|
|
|
url_length = strlen(url);
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* try to find a matching url fairly quickly */
|
|
|
|
for (search = hostname_data->url; search; search = search->next) {
|
|
|
|
if ((fast_exit_counter <= 0) ||
|
2005-02-07 17:28:43 +03:00
|
|
|
(search->data.url_length == url_length)) {
|
|
|
|
compare = strcmp(url, search->data.url);
|
2005-02-03 16:18:22 +03:00
|
|
|
if (compare == 0)
|
|
|
|
return &search->data;
|
|
|
|
else if (compare < 0)
|
|
|
|
break;
|
|
|
|
fast_exit_counter = ITERATIONS_BEFORE_TEST;
|
|
|
|
} else {
|
|
|
|
fast_exit_counter--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no URL is available: create a new one */
|
2005-10-02 03:27:22 +04:00
|
|
|
result = calloc(1, sizeof(struct url_data));
|
2005-02-03 16:18:22 +03:00
|
|
|
if (!result)
|
|
|
|
return NULL;
|
2005-02-07 17:28:43 +03:00
|
|
|
result->data.url = malloc(url_length + 1);
|
|
|
|
if (!result->data.url) {
|
2005-02-03 16:18:22 +03:00
|
|
|
free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-02-07 17:28:43 +03:00
|
|
|
strcpy(result->data.url, url);
|
|
|
|
result->data.url_length = url_length;
|
2005-02-03 16:18:22 +03:00
|
|
|
result->data.requests = 0;
|
|
|
|
result->data.visits = 0;
|
|
|
|
result->parent = hostname_data;
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* simple case: no current URLs */
|
|
|
|
if (!hostname_data->url) {
|
|
|
|
hostname_data->url = result;
|
|
|
|
return &result->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* worst case scenario: the place we need to link is within the last
|
|
|
|
* section of the URL list so we have no reference to work back
|
|
|
|
* from. rather than slowing with the very common case of searching,
|
|
|
|
* we take a speed hit for this case and simply move to the very end
|
|
|
|
* of the URL list ready to work backwards. */
|
|
|
|
if (!search)
|
|
|
|
for (search = hostname_data->url; search->next;
|
|
|
|
search = search->next);
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* we can now simply scan backwards as we know roughly where we need
|
|
|
|
* to link to (we either had an early exit from the searching so we
|
|
|
|
* know we're in the block following where we need to link, or we're
|
|
|
|
* at the very end of the list as we were in the last block.) */
|
2005-02-07 17:28:43 +03:00
|
|
|
while ((search) && (strcmp(url, search->data.url) < 0))
|
2005-02-03 16:18:22 +03:00
|
|
|
search = search->previous;
|
|
|
|
|
|
|
|
/* simple case: our new hostname is the first in the list */
|
|
|
|
if (!search) {
|
|
|
|
result->next = hostname_data->url;
|
|
|
|
hostname_data->url->previous = result;
|
|
|
|
hostname_data->url = result;
|
|
|
|
return &result->data;
|
|
|
|
}
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* general case: link in after the found hostname */
|
|
|
|
result->previous = search;
|
|
|
|
result->next = search->next;
|
|
|
|
if (search->next)
|
|
|
|
search->next->previous = result;
|
|
|
|
search->next = result;
|
|
|
|
return &result->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the next hostname that matches a part of the specified URL.
|
|
|
|
*
|
|
|
|
* \param url a normalized url to find the next match for
|
|
|
|
* \param current the current hostname to search forward from, or NULL
|
|
|
|
* \return the next matching hostname, or NULL
|
|
|
|
*/
|
|
|
|
static struct hostname_data *url_store_match_hostname(const char *url,
|
|
|
|
struct hostname_data *current) {
|
|
|
|
url_func_result res;
|
|
|
|
char *hostname;
|
|
|
|
int hostname_length;
|
|
|
|
int compare;
|
|
|
|
bool www_test;
|
|
|
|
|
|
|
|
assert(url);
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
res = url_host(url, &hostname);
|
2005-06-23 21:22:28 +04:00
|
|
|
if (res != URL_FUNC_OK) {
|
|
|
|
hostname = strdup("file:/"); /* for 'file:/' */
|
|
|
|
if (!hostname)
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-02-03 16:18:22 +03:00
|
|
|
hostname_length = strlen(hostname);
|
|
|
|
www_test = strncmp(hostname, "www.", 4);
|
|
|
|
|
|
|
|
/* advance to the next hostname */
|
|
|
|
if (!current)
|
|
|
|
current = url_store_hostnames;
|
|
|
|
else
|
|
|
|
current = current->next;
|
|
|
|
|
|
|
|
/* skip past hostname data without URLs */
|
|
|
|
for (; current && (!current->url); current = current->next);
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
while (current) {
|
|
|
|
if (current->hostname_length >= hostname_length) {
|
|
|
|
compare = strncmp(hostname, current->hostname,
|
|
|
|
hostname_length);
|
|
|
|
if (compare == 0) {
|
|
|
|
free(hostname);
|
|
|
|
return current;
|
|
|
|
} else if ((compare < 0) && !www_test)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* special case: if hostname is not www then try it */
|
2005-04-27 04:01:17 +04:00
|
|
|
if (www_test && ((current->hostname_length - 4) >=
|
|
|
|
hostname_length) &&
|
2005-02-03 16:18:22 +03:00
|
|
|
(!strncmp(current->hostname, "www.", 4)) &&
|
|
|
|
(!strncmp(hostname, current->hostname + 4,
|
|
|
|
hostname_length))) {
|
|
|
|
free(hostname);
|
|
|
|
return current;
|
|
|
|
}
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* move to next hostname with URLs */
|
|
|
|
current = current->next;
|
|
|
|
for (; current && (!current->url); current = current->next);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(hostname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the complete URL for the next matched stored URL.
|
|
|
|
*
|
|
|
|
* \param url a normalized url to find the next match for
|
|
|
|
* \param reference internal reference (NULL for first call)
|
|
|
|
* \return the next URL that matches
|
|
|
|
*/
|
|
|
|
char *url_store_match(const char *url, struct url_data **reference) {
|
|
|
|
struct hostname_data *hostname;
|
|
|
|
struct url_data *search = NULL;
|
|
|
|
int scheme_length;
|
|
|
|
int url_length;
|
|
|
|
url_func_result res;
|
|
|
|
bool www_test;
|
|
|
|
|
|
|
|
assert(url);
|
|
|
|
|
|
|
|
if (!url_store_hostnames)
|
|
|
|
return NULL;
|
2005-04-27 04:01:17 +04:00
|
|
|
|
|
|
|
/* find the scheme and first URL, not necessarily matching */
|
2005-02-03 16:18:22 +03:00
|
|
|
if (!*reference) {
|
|
|
|
hostname = url_store_match_hostname(url, NULL);
|
|
|
|
if (!hostname)
|
|
|
|
return NULL;
|
2005-04-27 04:01:17 +04:00
|
|
|
if (url_store_match_scheme) {
|
|
|
|
free(url_store_match_scheme);
|
|
|
|
url_store_match_scheme = NULL;
|
|
|
|
}
|
|
|
|
res = url_scheme(url, &url_store_match_scheme);
|
|
|
|
if (res != URL_FUNC_OK)
|
|
|
|
return NULL;
|
2005-02-03 16:18:22 +03:00
|
|
|
} else {
|
|
|
|
search = *reference;
|
|
|
|
hostname = search->parent;
|
|
|
|
}
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-04-27 04:01:17 +04:00
|
|
|
scheme_length = strlen(url_store_match_scheme);
|
2005-02-03 16:18:22 +03:00
|
|
|
url_length = strlen(url);
|
2005-04-27 04:01:17 +04:00
|
|
|
www_test = (!strcmp(url_store_match_scheme, "http") &&
|
2005-02-03 16:18:22 +03:00
|
|
|
strncmp(url + 4 + 3, "www.", 4)); /* 'http' + '://' */
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* work through all our strings, ignoring the scheme and 'www.' */
|
|
|
|
while (hostname) {
|
|
|
|
|
|
|
|
/* get the next URL to test */
|
|
|
|
if (!search)
|
|
|
|
search = hostname->url;
|
|
|
|
else
|
|
|
|
search = search->next;
|
|
|
|
|
|
|
|
/* loop past end of list, or search */
|
|
|
|
if (!search) {
|
|
|
|
hostname = url_store_match_hostname(url, hostname);
|
|
|
|
if (!hostname)
|
|
|
|
return NULL;
|
2005-04-27 04:01:17 +04:00
|
|
|
} else if ((search->data.visits > 0) &&
|
|
|
|
(search->data.requests > 0)){
|
2005-02-03 16:18:22 +03:00
|
|
|
/* straight match */
|
2005-02-07 17:28:43 +03:00
|
|
|
if ((search->data.url_length >= url_length) &&
|
2005-04-27 04:01:17 +04:00
|
|
|
(!strncmp(search->data.url, url,
|
|
|
|
url_length))) {
|
2005-02-03 16:18:22 +03:00
|
|
|
*reference = search;
|
2005-02-07 17:28:43 +03:00
|
|
|
return search->data.url;
|
2005-02-03 16:18:22 +03:00
|
|
|
}
|
|
|
|
/* try with 'www.' inserted after the scheme */
|
2005-04-27 04:01:17 +04:00
|
|
|
if (www_test && ((search->data.url_length - 4) >=
|
|
|
|
url_length) &&
|
|
|
|
(!strncmp(search->data.url,
|
|
|
|
url_store_match_scheme,
|
|
|
|
scheme_length)) &&
|
|
|
|
(!strncmp(search->data.url + scheme_length + 3,
|
|
|
|
"www.", 4)) &&
|
2005-02-07 17:28:43 +03:00
|
|
|
(!strncmp(search->data.url + scheme_length + 7,
|
2005-02-03 16:18:22 +03:00
|
|
|
url + scheme_length + 3,
|
|
|
|
url_length - scheme_length - 3))) {
|
|
|
|
*reference = search;
|
2005-02-07 17:28:43 +03:00
|
|
|
return search->data.url;
|
2005-02-03 16:18:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-04-09 02:18:28 +04:00
|
|
|
return NULL;
|
2005-02-03 16:18:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a text string into one suitable for URL matching.
|
|
|
|
*
|
|
|
|
* \param text the text to search with
|
|
|
|
* \return URL matching string allocated on heap, or NULL on error
|
|
|
|
*/
|
|
|
|
char *url_store_match_string(const char *text) {
|
|
|
|
url_func_result res;
|
|
|
|
char *url;
|
|
|
|
|
|
|
|
assert(text);
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
res = url_normalize(text, &url);
|
|
|
|
if (res != URL_FUNC_OK)
|
|
|
|
return NULL;
|
2005-04-09 02:18:28 +04:00
|
|
|
|
2005-02-03 16:18:22 +03:00
|
|
|
/* drop the '/' from the end if it was added when normalizing */
|
|
|
|
if ((url[strlen(url) - 1] == '/') && (text[strlen(text) - 1] != '/'))
|
|
|
|
url[strlen(url) - 1] = '\0';
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2005-10-02 03:27:22 +04:00
|
|
|
* Loads the current contents of the URL store from disk
|
2005-02-03 16:18:22 +03:00
|
|
|
*
|
|
|
|
* \param file the file to load options from
|
|
|
|
*/
|
2005-04-09 02:18:28 +04:00
|
|
|
void url_store_load(const char *file) {
|
2005-02-03 16:18:22 +03:00
|
|
|
struct url_content *url;
|
|
|
|
char s[MAXIMUM_URL_LENGTH];
|
2005-02-08 04:06:03 +03:00
|
|
|
struct hostname_data *hostname;
|
|
|
|
struct url_data *result;
|
|
|
|
int urls;
|
|
|
|
int i;
|
|
|
|
int version;
|
2005-06-23 21:22:28 +04:00
|
|
|
int width, height, size;
|
2005-02-03 16:18:22 +03:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fopen(file, "r");
|
|
|
|
if (!fp) {
|
|
|
|
LOG(("Failed to open file '%s' for reading", file));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fgets(s, MAXIMUM_URL_LENGTH, fp))
|
|
|
|
return;
|
2005-02-08 04:06:03 +03:00
|
|
|
version = atoi(s);
|
2005-06-23 21:22:28 +04:00
|
|
|
if ((version < 100) || (version > 102))
|
2005-02-03 16:18:22 +03:00
|
|
|
return;
|
2005-04-09 02:18:28 +04:00
|
|
|
|
|
|
|
|
2005-02-08 04:06:03 +03:00
|
|
|
/* version 100 file is sequences of <url><visits><requests> */
|
|
|
|
if (version == 100) {
|
|
|
|
while (fgets(s, MAXIMUM_URL_LENGTH, fp)) {
|
|
|
|
if (s[strlen(s) - 1] == '\n')
|
|
|
|
s[strlen(s) - 1] = '\0';
|
|
|
|
url = url_store_find(s);
|
|
|
|
if (!url)
|
|
|
|
break;
|
|
|
|
if (!fgets(s, MAXIMUM_URL_LENGTH, fp))
|
|
|
|
break;
|
|
|
|
url->visits = atoi(s);
|
|
|
|
if (!fgets(s, MAXIMUM_URL_LENGTH, fp))
|
|
|
|
break;
|
|
|
|
url->requests = atoi(s);
|
|
|
|
}
|
2005-06-23 21:22:28 +04:00
|
|
|
} else if (version >= 101) {
|
|
|
|
/* version 102 is as 101, but with thumbnail information */
|
2005-04-27 04:01:17 +04:00
|
|
|
/* version 101 is as 100, but in hostname chunks, pre-sorted
|
|
|
|
* in reverse alphabetical order */
|
2005-02-08 04:06:03 +03:00
|
|
|
while (fgets(s, MAXIMUM_URL_LENGTH, fp)) {
|
|
|
|
if (s[strlen(s) - 1] == '\n')
|
|
|
|
s[strlen(s) - 1] = '\0';
|
|
|
|
hostname = url_store_find_hostname(s);
|
|
|
|
if (!hostname)
|
|
|
|
break;
|
|
|
|
if (!fgets(s, MAXIMUM_URL_LENGTH, fp))
|
|
|
|
break;
|
|
|
|
urls = atoi(s);
|
|
|
|
for (i = 0; i < urls; i++) {
|
|
|
|
if (!fgets(s, MAXIMUM_URL_LENGTH, fp))
|
|
|
|
break;
|
|
|
|
if (s[strlen(s) - 1] == '\n')
|
|
|
|
s[strlen(s) - 1] = '\0';
|
2005-10-02 03:27:22 +04:00
|
|
|
result = calloc(1, sizeof(struct url_data));
|
2005-02-08 04:06:03 +03:00
|
|
|
if (!result)
|
|
|
|
break;
|
|
|
|
result->data.url_length = strlen(s);
|
2005-04-27 04:01:17 +04:00
|
|
|
result->data.url = malloc(result->
|
|
|
|
data.url_length + 1);
|
2005-02-08 04:06:03 +03:00
|
|
|
if (!result->data.url) {
|
|
|
|
free(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
strcpy(result->data.url, s);
|
|
|
|
result->parent = hostname;
|
|
|
|
result->next = hostname->url;
|
|
|
|
if (hostname->url)
|
|
|
|
hostname->url->previous = result;
|
|
|
|
hostname->url = result;
|
|
|
|
if (!fgets(s, MAXIMUM_URL_LENGTH, fp))
|
|
|
|
break;
|
|
|
|
result->data.visits = atoi(s);
|
|
|
|
if (!fgets(s, MAXIMUM_URL_LENGTH, fp))
|
|
|
|
break;
|
|
|
|
result->data.requests = atoi(s);
|
2005-06-23 21:22:28 +04:00
|
|
|
if (version == 102) {
|
|
|
|
if (!fgets(s, MAXIMUM_URL_LENGTH, fp))
|
|
|
|
break;
|
|
|
|
size = atoi(s);
|
|
|
|
width = size & 65535;
|
|
|
|
height = size >> 16;
|
|
|
|
if (!fgets(s, MAXIMUM_URL_LENGTH, fp))
|
|
|
|
break;
|
|
|
|
#ifdef riscos
|
|
|
|
if (s[strlen(s) - 1] == '\n')
|
|
|
|
s[strlen(s) - 1] = '\0';
|
|
|
|
result->data.thumbnail =
|
|
|
|
bitmap_create_file(
|
|
|
|
width, height, s);
|
|
|
|
#endif
|
|
|
|
}
|
2005-02-08 04:06:03 +03:00
|
|
|
}
|
|
|
|
}
|
2005-02-03 16:18:22 +03:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the current contents of the URL store to disk
|
|
|
|
*
|
|
|
|
* \param file the file to load options from
|
|
|
|
*/
|
2005-04-09 02:18:28 +04:00
|
|
|
void url_store_save(const char *file) {
|
2005-02-03 16:18:22 +03:00
|
|
|
struct hostname_data *search;
|
|
|
|
struct url_data *url;
|
2005-02-08 04:06:03 +03:00
|
|
|
int url_count;
|
|
|
|
char *normal = NULL;
|
2005-06-23 21:22:28 +04:00
|
|
|
const char *thumb_file = "";
|
|
|
|
int thumb_size = 0;
|
2005-02-03 16:18:22 +03:00
|
|
|
FILE *fp;
|
2005-08-21 16:04:18 +04:00
|
|
|
#ifdef riscos
|
2005-06-23 21:22:28 +04:00
|
|
|
struct bitmap *bitmap;
|
2005-08-21 16:04:18 +04:00
|
|
|
#endif
|
2005-02-03 16:18:22 +03:00
|
|
|
|
|
|
|
fp = fopen(file, "w");
|
|
|
|
if (!fp) {
|
|
|
|
LOG(("Failed to open file '%s' for writing", file));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* file format version number */
|
2005-06-23 21:22:28 +04:00
|
|
|
fprintf(fp, "102\n");
|
2005-04-27 04:01:17 +04:00
|
|
|
for (search = url_store_hostnames; search && search->next;
|
|
|
|
search = search->next);
|
2005-02-08 04:06:03 +03:00
|
|
|
for (; search; search = search->previous) {
|
|
|
|
url_count = 0;
|
2005-02-07 17:28:43 +03:00
|
|
|
for (url = search->url; url; url = url->next)
|
2005-06-23 21:22:28 +04:00
|
|
|
if ((url->data.visits > 0) &&
|
2005-04-27 04:01:17 +04:00
|
|
|
(strlen(url->data.url) <
|
|
|
|
MAXIMUM_URL_LENGTH))
|
2005-02-08 04:06:03 +03:00
|
|
|
url_count++;
|
|
|
|
free(normal);
|
|
|
|
normal = url_store_match_string(search->hostname);
|
|
|
|
if ((url_count > 0) && (normal)) {
|
|
|
|
fprintf(fp, "%s\n%i\n", normal, url_count);
|
|
|
|
for (url = search->url; url->next; url = url->next);
|
|
|
|
for (; url; url = url->previous)
|
2005-06-23 21:22:28 +04:00
|
|
|
if ((url->data.visits > 0) &&
|
2005-04-27 04:01:17 +04:00
|
|
|
(strlen(url->data.url) <
|
2005-06-23 21:22:28 +04:00
|
|
|
MAXIMUM_URL_LENGTH)) {
|
|
|
|
#ifdef riscos
|
|
|
|
bitmap = url->data.thumbnail;
|
|
|
|
if (bitmap) {
|
|
|
|
thumb_size = bitmap->width |
|
|
|
|
(bitmap->height << 16);
|
|
|
|
thumb_file = bitmap->filename;
|
|
|
|
} else {
|
|
|
|
thumb_size = 0;
|
|
|
|
thumb_file = "";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
fprintf(fp, "%s\n%i\n%i\n%i\n%s\n",
|
2005-04-27 04:01:17 +04:00
|
|
|
url->data.url,
|
|
|
|
url->data.visits,
|
2005-06-23 21:22:28 +04:00
|
|
|
url->data.requests,
|
|
|
|
thumb_size,
|
|
|
|
thumb_file);
|
|
|
|
}
|
2005-02-08 04:06:03 +03:00
|
|
|
}
|
2005-02-03 16:18:22 +03:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dumps the currently stored URLs and hostnames to stderr.
|
|
|
|
*/
|
|
|
|
void url_store_dump(void) {
|
|
|
|
struct hostname_data *search;
|
|
|
|
struct url_data *url;
|
|
|
|
|
|
|
|
fprintf(stderr, "\nDumping hostname data:\n");
|
|
|
|
for (search = url_store_hostnames; search; search = search->next) {
|
|
|
|
fprintf(stderr, "\n");
|
2005-04-09 02:18:28 +04:00
|
|
|
fprintf(stderr, "%s", search->hostname);
|
2005-02-03 16:18:22 +03:00
|
|
|
fprintf(stderr, ":\n");
|
|
|
|
for (url = search->url; url; url = url->next) {
|
|
|
|
fprintf(stderr, " - ");
|
2005-04-09 02:18:28 +04:00
|
|
|
fprintf(stderr, "%s", url->data.url);
|
2005-02-03 16:18:22 +03:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\nEnd of hostname data.\n\n");
|
|
|
|
}
|
2005-06-23 21:22:28 +04:00
|
|
|
|
|
|
|
|
|
|
|
void url_store_add_thumbnail(const char *url, struct bitmap *bitmap) {
|
|
|
|
struct url_content *content;
|
|
|
|
|
|
|
|
content = url_store_find(url);
|
|
|
|
if (content) {
|
|
|
|
if (content->thumbnail)
|
|
|
|
bitmap_destroy(content->thumbnail);
|
|
|
|
content->thumbnail = bitmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bitmap *url_store_get_thumbnail(const char *url) {
|
|
|
|
struct url_content *content;
|
|
|
|
|
|
|
|
content = url_store_find(url);
|
|
|
|
if (content)
|
|
|
|
return content->thumbnail;
|
|
|
|
return NULL;
|
|
|
|
}
|