NetSurf
nsoption.h
Go to the documentation of this file.
1/*
2 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * \file
21 * Option reading and saving interface.
22 *
23 * Global options are defined in desktop/options.h
24 * Distinct target options are defined in ${TARGET}/options.h
25 *
26 * The implementation API is slightly compromised because it still has
27 * "global" tables for both the default and current option tables.
28 *
29 * The initialisation and read/write interfaces take pointers to an
30 * option table which would let us to make the option structure
31 * opaque.
32 *
33 * All the actual acessors assume direct access to a global option
34 * table (nsoptions). To avoid this the acessors would have to take a
35 * pointer to the active options table and be implemented as functions
36 * within nsoptions.c
37 *
38 * Indirect access would have an impact on performance of NetSurf as
39 * the expected option lookup cost is currently that of a simple
40 * dereference (which this current implementation keeps).
41 */
42
43#ifndef _NETSURF_UTILS_NSOPTION_H_
44#define _NETSURF_UTILS_NSOPTION_H_
45
46#include <stdio.h>
47#include <stdint.h>
48#include <stdbool.h>
49
50#include "utils/errors.h"
51
52/* allow targets to include any necessary headers of their own */
53#define NSOPTION_BOOL(NAME, DEFAULT)
54#define NSOPTION_STRING(NAME, DEFAULT)
55#define NSOPTION_INTEGER(NAME, DEFAULT)
56#define NSOPTION_UINT(NAME, DEFAULT)
57#define NSOPTION_COLOUR(NAME, DEFAULT)
58
59#include "desktop/options.h"
60#if defined(riscos)
61#include "riscos/options.h"
62#elif defined(nsgtk)
63#include "gtk/options.h"
64#elif defined(nsbeos)
65#include "beos/options.h"
66#elif defined(nsamiga)
67#include "amiga/options.h"
68#elif defined(nsframebuffer)
69#include "framebuffer/options.h"
70#elif defined(nsatari)
71#include "atari/options.h"
72#elif defined(nsmonkey)
73#include "monkey/options.h"
74#elif defined(nswin32)
75#include "windows/options.h"
76#endif
77
78#undef NSOPTION_BOOL
79#undef NSOPTION_STRING
80#undef NSOPTION_INTEGER
81#undef NSOPTION_UINT
82#undef NSOPTION_COLOUR
83
84
85
89
90#define DEFAULT_MARGIN_TOP_MM 10
91#define DEFAULT_MARGIN_BOTTOM_MM 10
92#define DEFAULT_MARGIN_LEFT_MM 10
93#define DEFAULT_MARGIN_RIGHT_MM 10
94#define DEFAULT_EXPORT_SCALE 0.7
95
96#ifndef DEFAULT_REFLOW_PERIOD
97/** Default reflow time in cs */
98#define DEFAULT_REFLOW_PERIOD 25
99#endif
100
101/** The options type. */
103 OPTION_BOOL, /**< Option is a boolean. */
104 OPTION_INTEGER, /**< Option is an integer. */
105 OPTION_UINT, /**< Option is an unsigned integer */
106 OPTION_STRING, /**< option is a heap allocated string. */
107 OPTION_COLOUR /**< Option is a netsurf colour. */
109
111 const char *key;
114 union {
115 bool b;
116 int i;
117 unsigned int u;
118 char *s;
119 const char *cs;
122};
123
124/* construct the option enumeration */
125#define NSOPTION_BOOL(NAME, DEFAULT) NSOPTION_##NAME,
126#define NSOPTION_STRING(NAME, DEFAULT) NSOPTION_##NAME,
127#define NSOPTION_INTEGER(NAME, DEFAULT) NSOPTION_##NAME,
128#define NSOPTION_UINT(NAME, DEFAULT) NSOPTION_##NAME,
129#define NSOPTION_COLOUR(NAME, DEFAULT) NSOPTION_##NAME,
130
132#include "desktop/options.h"
133#if defined(riscos)
134#include "riscos/options.h"
135#elif defined(nsgtk)
136#include "gtk/options.h"
137#elif defined(nsbeos)
138#include "beos/options.h"
139#elif defined(nsamiga)
140#include "amiga/options.h"
141#elif defined(nsframebuffer)
142#include "framebuffer/options.h"
143#elif defined(nsatari)
144#include "atari/options.h"
145#elif defined(nsmonkey)
146#include "monkey/options.h"
147#elif defined(nswin32)
148#include "windows/options.h"
149#endif
150 NSOPTION_LISTEND /* end of list */
152
153#undef NSOPTION_BOOL
154#undef NSOPTION_STRING
155#undef NSOPTION_INTEGER
156#undef NSOPTION_UINT
157#undef NSOPTION_COLOUR
158
159/**
160 * global active option table.
161 */
162extern struct nsoption_s *nsoptions;
163
164
165/**
166 * global default option table.
167 */
168extern struct nsoption_s *nsoptions_default;
169
170
171/**
172 * default setting callback.
173 */
175
176
177/**
178 * option generate callback
179 */
180typedef size_t(nsoption_generate_cb)(struct nsoption_s *option, void *ctx);
181
182
183/**
184 * flags to control option output in the generate call
185 */
187 /** Generate output for all options */
189 /** Generate output for options which differ from the default */
191};
192
193
194/**
195 * Initialise option system.
196 *
197 * @param set_default callback to allow the customisation of the default
198 * options.
199 * @param popts pointer to update to get options table or NULL.
200 * @param pdefs pointer to update to get default options table or NULL.
201 * @return The error status
202 */
203nserror nsoption_init(nsoption_set_default_t *set_default, struct nsoption_s **popts, struct nsoption_s **pdefs);
204
205
206/**
207 * Finalise option system
208 *
209 * Releases all resources allocated in the initialisation.
210 *
211 * @param opts the options table or NULL to use global table.
212 * @param defs the default options table to use or NULL to use global table
213 * return The error status
214 */
215nserror nsoption_finalise(struct nsoption_s *opts, struct nsoption_s *defs);
216
217
218/**
219 * Read choices file and set them in the passed table
220 *
221 * @param path The path to read the file from
222 * @param opts The options table to enerate values from or NULL to use global
223 * @return The error status
224 */
225nserror nsoption_read(const char *path, struct nsoption_s *opts);
226
227
228/**
229 * Generate options via acallback.
230 *
231 * iterates options controlled by flags calling a method for each matched option.
232 *
233 * @param cb Function called for each option to be output.
234 * @param ctx The context for the callback.
235 * @param flags Flags controlling option matching.
236 * @param opts The options table to enerate values from or NULL to use global.
237 * @param defs The default table to use or NULL to use global.
238 * @return The error status.
239 */
240nserror nsoption_generate(nsoption_generate_cb *cb, void *ctx, enum nsoption_generate_flags flags, struct nsoption_s *opts, struct nsoption_s *defs);
241
242
243/**
244 * Write options that have changed from the defaults to a file.
245 *
246 * The \a nsoption_dump can be used to output all entries not just
247 * changed ones.
248 *
249 * @param path The path to read the file from
250 * @param opts The options table to enerate values from or NULL to use global
251 * @param defs The default table to use or NULL to use global
252 * @return The error status
253 */
254nserror nsoption_write(const char *path, struct nsoption_s *opts, struct nsoption_s *defs);
255
256
257/**
258 * Write all options to a stream.
259 *
260 * @param outf The stream to write to
261 * @param opts The options table to enerate values from or NULL to use global
262 * @return The error status
263 */
264nserror nsoption_dump(FILE *outf, struct nsoption_s *opts);
265
266
267/**
268 * Process commandline and set options approriately.
269 *
270 * @param pargc Pointer to the size of the argument vector.
271 * @param argv The argument vector.
272 * @param opts The options table to enerate values from or NULL to use global
273 * @return The error status
274 */
275nserror nsoption_commandline(int *pargc, char **argv, struct nsoption_s *opts);
276
277
278/**
279 * Fill a buffer with an option using a format.
280 *
281 * The format string is copied into the output buffer with the
282 * following replaced:
283 * %k - The options key
284 * %t - The options type
285 * %V - value (HTML formatting)
286 * %v - value (plain formatting)
287 * %p - provenance either "user" or "default"
288 *
289 * @param string The buffer in which to place the results.
290 * @param size The size of the string buffer.
291 * @param option The option .
292 * @param fmt The format string.
293 * @return The number of bytes written to \a string or -1 on error
294 */
295int nsoption_snoptionf(char *string, size_t size, enum nsoption_e option, const char *fmt);
296
297
298/**
299 * Get the value of a boolean option.
300 *
301 * Gets the value of an option assuming it is a boolean type.
302 * @note option type is unchecked so care must be taken in caller.
303 */
304#define nsoption_bool(OPTION) (nsoptions[NSOPTION_##OPTION].value.b)
305
306
307/**
308 * Get the value of an integer option.
309 *
310 * Gets the value of an option assuming it is a integer type.
311 * @note option type is unchecked so care must be taken in caller.
312 */
313#define nsoption_int(OPTION) (nsoptions[NSOPTION_##OPTION].value.i)
314
315
316/**
317 * Get the value of an unsigned integer option.
318 *
319 * Gets the value of an option assuming it is a integer type.
320 * @note option type is unchecked so care must be taken in caller.
321 */
322#define nsoption_uint(OPTION) (nsoptions[NSOPTION_##OPTION].value.u)
323
324
325/**
326 * Get the value of a string option.
327 *
328 * Gets the value of an option assuming it is a string type.
329 * @note option type is unchecked so care must be taken in caller.
330 */
331#define nsoption_charp(OPTION) (nsoptions[NSOPTION_##OPTION].value.s)
332
333
334/**
335 * Get the value of a netsurf colour option.
336 *
337 * Gets the value of an option assuming it is a colour type.
338 * @note option type is unchecked so care must be taken in caller.
339 */
340#define nsoption_colour(OPTION) (nsoptions[NSOPTION_##OPTION].value.c)
341
342
343/** set a boolean option in the default table */
344#define nsoption_set_bool(OPTION, VALUE) nsoptions[NSOPTION_##OPTION].value.b = VALUE
345
346
347/** set an integer option in the default table */
348#define nsoption_set_int(OPTION, VALUE) nsoptions[NSOPTION_##OPTION].value.i = VALUE
349
350/** set an unsigned integer option in the default table */
351#define nsoption_set_uint(OPTION, VALUE) nsoptions[NSOPTION_##OPTION].value.u = VALUE
352
353
354/** set a colour option in the default table */
355#define nsoption_set_colour(OPTION, VALUE) nsoptions[NSOPTION_##OPTION].value.c = VALUE
356
357
358/**
359 * Set string option in specified table.
360 *
361 * Sets the string option to the value given freeing any resources
362 * currently allocated to the option. If the passed string is empty it
363 * is converted to the NULL value.
364 *
365 * @param opts The table to set option in
366 * @param option_idx The option
367 * @param s The string to set. This is used directly and not copied.
368 */
369nserror nsoption_set_tbl_charp(struct nsoption_s *opts, enum nsoption_e option_idx, char *s);
370
371/** set string option in default table */
372#define nsoption_set_charp(OPTION, VALUE) \
373 nsoption_set_tbl_charp(nsoptions, NSOPTION_##OPTION, VALUE)
374
375/** set string option in default table if currently unset */
376#define nsoption_setnull_charp(OPTION, VALUE) \
377 do { \
378 if (nsoptions[NSOPTION_##OPTION].value.s == NULL) { \
379 nsoption_set_tbl_charp(nsoptions, NSOPTION_##OPTION, VALUE); \
380 } else { \
381 free(VALUE); \
382 } \
383 } while (0)
384
385#endif
Option available on all platforms.
Error codes.
nserror
Enumeration of error codes.
Definition: errors.h:29
Option specific to RISC OS.
bool b
Definition: nsoption.h:115
const char * key
Definition: nsoption.h:111
union nsoption_s::@149 value
enum nsoption_type_e type
Definition: nsoption.h:113
const char * cs
Definition: nsoption.h:119
colour c
Definition: nsoption.h:120
unsigned int u
Definition: nsoption.h:117
char * s
Definition: nsoption.h:118
int key_len
Definition: nsoption.h:112
uint32_t colour
Colour type: XBGR.
Definition: types.h:35
static struct nsoption_s defaults[]
The table of compiled in default options.
Definition: nsoption.c:64
nserror nsoption_init(nsoption_set_default_t *set_default, struct nsoption_s **popts, struct nsoption_s **pdefs)
Initialise option system.
Definition: nsoption.c:608
struct nsoption_s * nsoptions_default
global default option table.
Definition: nsoption.c:46
size_t() nsoption_generate_cb(struct nsoption_s *option, void *ctx)
option generate callback
Definition: nsoption.h:180
nsoption_e
Definition: nsoption.h:131
@ NSOPTION_LISTEND
Definition: nsoption.h:150
nserror nsoption_set_tbl_charp(struct nsoption_s *opts, enum nsoption_e option_idx, char *s)
Set string option in specified table.
Definition: nsoption.c:1046
int nsoption_snoptionf(char *string, size_t size, enum nsoption_e option, const char *fmt)
Fill a buffer with an option using a format.
Definition: nsoption.c:930
nserror nsoption_generate(nsoption_generate_cb *cb, void *ctx, enum nsoption_generate_flags flags, struct nsoption_s *opts, struct nsoption_s *defs)
Generate options via acallback.
Definition: nsoption.c:746
nserror nsoption_dump(FILE *outf, struct nsoption_s *opts)
Write all options to a stream.
Definition: nsoption.c:832
nserror nsoption_read(const char *path, struct nsoption_s *opts)
Read choices file and set them in the passed table.
Definition: nsoption.c:696
struct nsoption_s * nsoptions
global active option table.
Definition: nsoption.c:45
@ OPTION_HTTP_PROXY_AUTH_NONE
Definition: nsoption.h:86
@ OPTION_HTTP_PROXY_AUTH_NTLM
Definition: nsoption.h:88
@ OPTION_HTTP_PROXY_AUTH_BASIC
Definition: nsoption.h:87
nserror() nsoption_set_default_t(struct nsoption_s *defaults)
default setting callback.
Definition: nsoption.h:174
nsoption_type_e
The options type.
Definition: nsoption.h:102
@ OPTION_STRING
option is a heap allocated string.
Definition: nsoption.h:106
@ OPTION_COLOUR
Option is a netsurf colour.
Definition: nsoption.h:107
@ OPTION_UINT
Option is an unsigned integer.
Definition: nsoption.h:105
@ OPTION_INTEGER
Option is an integer.
Definition: nsoption.h:104
@ OPTION_BOOL
Option is a boolean.
Definition: nsoption.h:103
nserror nsoption_commandline(int *pargc, char **argv, struct nsoption_s *opts)
Process commandline and set options approriately.
Definition: nsoption.c:856
nserror nsoption_write(const char *path, struct nsoption_s *opts, struct nsoption_s *defs)
Write options that have changed from the defaults to a file.
Definition: nsoption.c:786
nserror nsoption_finalise(struct nsoption_s *opts, struct nsoption_s *defs)
Finalise option system.
Definition: nsoption.c:663
nsoption_generate_flags
flags to control option output in the generate call
Definition: nsoption.h:186
@ NSOPTION_GENERATE_ALL
Generate output for all options.
Definition: nsoption.h:188
@ NSOPTION_GENERATE_CHANGED
Generate output for options which differ from the default.
Definition: nsoption.h:190
static nserror path(const struct redraw_context *ctx, const plot_style_t *pstyle, const float *p, unsigned int n, const float transform[6])
Plots a path.
Definition: plot.c:821