NetSurf
generics.c
Go to the documentation of this file.
1/*
2 * Copyright 2010 John-Mark Bell <jmb@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#include <stdlib.h>
20
21#include "utils/http/generics.h"
23
24/**
25 * Destructor for an item list
26 *
27 * \param list List to destroy
28 */
30{
31 while (list != NULL) {
32 http__item *victim = list;
33
34 list = victim->next;
35
36 victim->free(victim);
37 }
38}
39
40/**
41 * Parse a list of items
42 *
43 * \param input Pointer to current input byte. Updated on exit.
44 * \param itemparser Pointer to function to parse list items
45 * \param first Pointer to first item, or NULL.
46 * \param items Pointer to location to receive on-heap parameter list.
47 * \return NSERROR_OK on success,
48 * NSERROR_NOMEM on memory exhaustion,
49 * NSERROR_NOT_FOUND if no items could be parsed
50 *
51 * The returned list is owned by the caller
52 *
53 * \note Ownership of the \a first item is passed to this function.
54 */
55nserror http___item_list_parse(const char **input,
56 http__itemparser itemparser, http__item *first,
58{
59 const char *pos = *input;
60 const char separator = *pos;
61 http__item *item;
62 http__item *list = first;
63 nserror error = NSERROR_OK;
64
65 /* 1*( <separator> <item> ) */
66
67 while (*pos == separator) {
68 pos++;
69
70 http__skip_LWS(&pos);
71
72 error = itemparser(&pos, &item);
73 if (error == NSERROR_OK) {
74 if (list != NULL)
75 item->next = list;
76
77 list = item;
78
79 http__skip_LWS(&pos);
80 } else if (error != NSERROR_NOT_FOUND) {
81 /* Permit <separator> LWS <separator> */
82 break;
83 }
84 }
85
86 if (error != NSERROR_OK && error != NSERROR_NOT_FOUND) {
88 } else if (list == NULL) {
89 error = NSERROR_NOT_FOUND;
90 } else {
91 error = NSERROR_OK;
92 *items = list;
93 *input = pos;
94 }
95
96 return error;
97}
98
99
static html_css_fetcher_item * items
Definition: css_fetcher.c:65
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NOT_FOUND
Requested item not found.
Definition: errors.h:34
@ NSERROR_OK
No error.
Definition: errors.h:30
nserror http___item_list_parse(const char **input, http__itemparser itemparser, http__item *first, http__item **items)
Parse a list of items.
Definition: generics.c:55
void http___item_list_destroy(http__item *list)
Destructor for an item list.
Definition: generics.c:29
#define http__item_list_destroy(l)
Definition: generics.h:46
nserror(* http__itemparser)(const char **input, http__item **item)
Type of an item parser.
Definition: generics.h:42
void http__skip_LWS(const char **input)
Skip past linear whitespace in input.
Definition: primitives.c:31
Representation of an item.
Definition: generics.h:29
void(* free)(struct http__item *self)
Item destructor.
Definition: generics.h:32
struct http__item * next
Next item in list, or NULL.
Definition: generics.h:30