-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_parser_attribute_reader.c
More file actions
89 lines (67 loc) · 1.89 KB
/
Copy pathhtml_parser_attribute_reader.c
File metadata and controls
89 lines (67 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "html_parser_mem.h"
#include "html_parser_text.h"
#include "html_parser_attribute_reader.h"
#include "html_parser_attribute_list.h"
#include "html_parser_attribute_rep.h"
/* static function prototypes */
static Text_T strip(Text_T *txt);
static Attr_rep_T attribute(Text_T *tag_chunk);
static char *attribute_name(Text_T *tag_chunk);
static char *attribute_value(Text_T *tag_chunk);
Attr_list_T Attribute_reader(Text_T *tag_chunk)
{
Attr_list_T attr_list;
attr_list = Attr_list_list();
while (tag_chunk->len > 0)
attr_list = Attr_list_enqueue(attr_list, attribute(tag_chunk));
return attr_list;
}
static Attr_rep_T attribute(Text_T *tag_chunk)
{
char *name, *value;
Attr_rep_T new;
name = attribute_name(tag_chunk);
value = attribute_value(tag_chunk);
new =Attr_rep_new(name, value);
/* not a great solution, but... */
FREE(value);
return new;
}
static char *attribute_name(Text_T *tag_chunk)
{
int end;
Text_T name;
end = Text_chr(*tag_chunk, 1, 0, '=');
name = Text_sub(*tag_chunk, 1, end);
strip(&name);
/* progress tag_chunk */
*tag_chunk = Text_sub(*tag_chunk, end + 1, 0);
return Text_get(NULL, -1, name);
}
static char *attribute_value(Text_T *tag_chunk)
{
int start, end;
Text_T value;
start = Text_upto(*tag_chunk, 1, 0, Text_nonwhite);
/* Is the value enclosed by quotes? */
if (Text_chr(*tag_chunk, start, start + 1, '"')) {
start++;
end = Text_chr(*tag_chunk, start + 1, 0, '"');
} else {
end = Text_chr(*tag_chunk, start, 0, ' ');
}
value = Text_sub(*tag_chunk, start, end);
/* progress tag_chunk */
if (end == 0)
*tag_chunk = Text_sub(*tag_chunk, 0, 0);
else
*tag_chunk = Text_sub(*tag_chunk, Text_upto(*tag_chunk, end + 1, 0,
Text_nonwhite), 0);
return Text_get(NULL, -1, value);
}
static Text_T strip(Text_T *txt)
{
*txt = Text_sub(*txt, Text_upto(*txt, 1, 0, Text_nonwhite),
Text_rupto(*txt, 1, 0, Text_nonwhite) + 1);
return *txt;
}