-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdp_parser.c
More file actions
67 lines (65 loc) · 2.44 KB
/
sdp_parser.c
File metadata and controls
67 lines (65 loc) · 2.44 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
#include "sdp_parser.h"
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int sdp_parser_init(sdp_parser_t *parser, const char *source, size_t length) {
parser->source = source;
parser->cursor = source;
parser->eof = source + length;
parser->index = 0;
parser->type = 0;
parser->length = 0;
parser->state = SDP_PARSE_FIELD;
return 0;
}
int sdp_parser_execute(sdp_parser_t *parser, sdp_parser_setting_t *setting) {
int result;
while (parser->cursor < parser->eof) {
if (*parser->cursor == '=') {
if (parser->state == SDP_PARSE_FIELD) {
parser->type = parser->cursor[-1];
_try(setting->on_field_begin(parser, parser->type, parser->index));
parser->length = 0;
parser->state = SDP_PARSE_DESCRIBE;
} else {
parser->length++;
}
parser->cursor++;
} else if (isspace(*(unsigned char *)parser->cursor)) {
if (parser->length > 0 && parser->state == SDP_PARSE_DESCRIBE) {
_try(setting->on_describe(parser, parser->index, parser->cursor - parser->length, parser->length));
parser->index++;
parser->length = 0;
}
if (parser->cursor[0] == '\r' && parser->cursor[1] == '\n') {
_try(setting->on_field_end(parser, parser->type, parser->index));
parser->index = 0;
parser->state = SDP_PARSE_FIELD;
parser->cursor += 2;
} else if (parser->cursor[0] == '\r' || parser->cursor[0] == '\n') {
_try(setting->on_field_end(parser, parser->type, parser->index));
parser->state = SDP_PARSE_FIELD;
parser->index = 0;
parser->cursor++;
} else {
parser->cursor++;
}
} else {
parser->cursor++;
parser->length++;
}
}
if (parser->length > 0 && parser->state == SDP_PARSE_DESCRIBE) {
_try(setting->on_describe(parser, parser->index, parser->cursor - parser->length, parser->length));
parser->index++;
parser->length = 0;
}
if (parser->cursor == parser->eof && parser->state == SDP_PARSE_DESCRIBE) {
_try(setting->on_field_end(parser, parser->type, parser->index));
parser->index = 0;
parser->state = SDP_PARSE_FIELD;
}
_catch:
return result;
}