diff options
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/netsurf.c | 4 | ||||
-rw-r--r-- | desktop/options.c | 157 | ||||
-rw-r--r-- | desktop/options.h | 40 |
3 files changed, 178 insertions, 23 deletions
diff --git a/desktop/netsurf.c b/desktop/netsurf.c index a35788ba8..724b57415 100644 --- a/desktop/netsurf.c +++ b/desktop/netsurf.c @@ -3,7 +3,7 @@ * Licensed under the GNU General Public License, * http://www.opensource.org/licenses/gpl-license * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net> - * Copyright 2003 James Bursa <bursa@users.sourceforge.net> + * Copyright 2004 James Bursa <bursa@users.sourceforge.net> */ #include <stdbool.h> @@ -50,8 +50,6 @@ int main(int argc, char** argv) void netsurf_init(int argc, char** argv) { stdout = stderr; - options_init(&OPTIONS); - options_read(&OPTIONS, NULL); gui_init(argc, argv); fetch_init(); cache_init(); diff --git a/desktop/options.c b/desktop/options.c new file mode 100644 index 000000000..5b1981b6e --- /dev/null +++ b/desktop/options.c @@ -0,0 +1,157 @@ +/* + * 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 2003 Phil Mellor <monkeyson@users.sourceforge.net> + * Copyright 2003 John M Bell <jmb202@ecs.soton.ac.uk> + * Copyright 2004 James Bursa <bursa@users.sourceforge.net> + */ + +/** \file + * Option reading and saving (implementation). + * + * Options are stored in the format key:value, one per line. For bool options, + * value is "0" or "1". + */ + +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include "netsurf/desktop/options.h" +#include "netsurf/utils/log.h" +#include "netsurf/utils/utils.h" + +#ifdef riscos +#include "netsurf/riscos/options.h" +#else +#define EXTRA_OPTION_DEFINE +#define EXTRA_OPTION_TABLE +#endif + + +/** An HTTP proxy should be used. */ +bool option_http_proxy = false; +/** Hostname of proxy. */ +char *option_http_proxy_host = 0; +/** Proxy port. */ +int option_http_proxy_port = 8080; +EXTRA_OPTION_DEFINE + + +struct { + const char *key; + enum { OPTION_BOOL, OPTION_INTEGER, OPTION_STRING } type; + void *p; +} option_table[] = { + { "http_proxy", OPTION_BOOL, &option_http_proxy }, + { "http_proxy_host", OPTION_STRING, &option_http_proxy_host }, + { "http_proxy_port", OPTION_INTEGER, &option_http_proxy_port }, + EXTRA_OPTION_TABLE +}; + +#define option_table_entries (sizeof option_table / sizeof option_table[0]) + + +/** + * Read options from a file. + * + * \param path name of file to read options from + * + * Option variables corresponding to lines in the file are updated. Missing + * options are unchanged. If the file fails to open, options are unchanged. + */ + +void options_read(const char *path) +{ + char s[100]; + FILE *fp; + + fp = fopen(path, "r"); + if (!fp) { + LOG(("failed to open file '%s'", path)); + return; + } + + while (fgets(s, 100, fp)) { + char *colon, *value; + unsigned int i; + + if (s[0] == 0 || s[0] == '#') + continue; + colon = strchr(s, ':'); + if (colon == 0) + continue; + s[strlen(s) - 1] = 0; /* remove \n at end */ + *colon = 0; /* terminate key */ + value = colon + 1; + + for (i = 0; i != option_table_entries; i++) { + if (strcasecmp(s, option_table[i].key) != 0) + continue; + + switch (option_table[i].type) { + case OPTION_BOOL: + *((bool *) option_table[i].p) = + value[0] == '1'; + break; + + case OPTION_INTEGER: + *((int *) option_table[i].p) = + atoi(value); + break; + + case OPTION_STRING: + free(*((char **) option_table[i].p)); + *((char **) option_table[i].p) = + strdup(value); + break; + } + break; + } + } + + fclose(fp); +} + + +/** + * Save options to a file. + * + * \param path name of file to write options to + * + * Errors are ignored. + */ + +void options_write(const char *path) +{ + unsigned int i; + FILE *fp; + + fp = fopen(path, "w"); + if (!fp) { + LOG(("failed to open file '%s' for writing", path)); + return; + } + + for (i = 0; i != option_table_entries; i++) { + fprintf(fp, "%s:", option_table[i].key); + switch (option_table[i].type) { + case OPTION_BOOL: + fprintf(fp, "%c", *((bool *) option_table[i].p) ? + '1' : '0'); + break; + + case OPTION_INTEGER: + fprintf(fp, "%i", *((int *) option_table[i].p)); + break; + + case OPTION_STRING: + if (*((char **) option_table[i].p)) + fprintf(fp, "%s", *((char **) option_table[i].p)); + break; + } + fprintf(fp, "\n"); + } + + fclose(fp); +} diff --git a/desktop/options.h b/desktop/options.h index 415279b36..3c2acced9 100644 --- a/desktop/options.h +++ b/desktop/options.h @@ -3,32 +3,32 @@ * Licensed under the GNU General Public License, * http://www.opensource.org/licenses/gpl-license * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net> + * Copyright 2004 James Bursa <bursa@users.sourceforge.net> */ +/** \file + * Option reading and saving (interface). + * + * Non-platform specific options can be added by editing this file and + * netsurf/desktop/options.c + * + * Platform specific options should be added in the platform options.h. + * + * The following types of options are supported: + * - bool (OPTION_BOOL) + * - int (OPTION_INTEGER) + * - char* (OPTION_STRING) (must be allocated on heap, may be 0, free before + * assigning a new value) + */ #ifndef _NETSURF_DESKTOP_OPTIONS_H_ #define _NETSURF_DESKTOP_OPTIONS_H_ -struct options; - -#include "netsurf/riscos/options.h" - -struct options -{ - /* global options */ - int http; - char* http_proxy; - int http_port; +extern bool option_http_proxy; +extern char *option_http_proxy_host; +extern int option_http_proxy_port; - /* platform specific options */ - PLATFORM_OPTIONS -}; - -extern struct options OPTIONS; - -void options_init(struct options* opt); -void options_write(struct options*, char* filename); -void options_read(struct options*, char* filename); +void options_read(const char *path); +void options_write(const char *path); #endif - |