blob: 32bede947b792b66932247343e3a9a63a82860a8 (
plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#include "../include/quickmedia/XpathTokenizer.h"
#include <stdlib.h>
#include <string.h>
void quickmedia_xpath_tokenizer_init(QuickMediaXpathTokenizer *self, const char *xpath) {
self->code = xpath;
self->identifier.data = NULL;
self->identifier.size = 0;
}
static int is_alpha(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static int is_num(char c) {
return c >= '0' && c <= '9';
}
static int is_alphanum(char c) {
return is_alpha(c) || is_num(c);
}
static const char* find_end_of_string(const char *str, char escape_symbol) {
int escape = 0;
while(*str != '\0') {
char c = *str;
if(c == '\\') {
escape = !escape;
} else if(c == escape_symbol) {
if(!escape)
return str;
} else {
escape = 0;
}
++str;
}
return str;
}
QuickMediaXpathToken quickmedia_xpath_tokenizer_next(QuickMediaXpathTokenizer *self) {
char c = *self->code;
if(c == '/') {
++self->code;
c = *self->code;
if(c == '/') {
++self->code;
return QUICKMEDIA_XPATH_TOKEN_CHILD_RECURSIVE;
}
return QUICKMEDIA_XPATH_TOKEN_CHILD;
} else if(is_alpha(c)) {
self->identifier.data = self->code;
++self->code;
while(is_alphanum(*self->code) || *self->code == '_' || *self->code == '-') {
++self->code;
}
self->identifier.size = self->code - self->identifier.data;
return QUICKMEDIA_XPATH_TOKEN_IDENTIFIER;
} else if(c == '[') {
++self->code;
return QUICKMEDIA_XPATH_TOKEN_OPEN_BRACKET;
} else if(c == ']') {
++self->code;
return QUICKMEDIA_XPATH_TOKEN_CLOSING_BRACKET;
} else if(c == '=') {
++self->code;
return QUICKMEDIA_XPATH_TOKEN_EQUAL;
} else if(c == '"' || c == '\'') {
char escape_symbol = c;
++self->code;
self->string.data = self->code;
self->code = find_end_of_string(self->string.data, escape_symbol);
if(*self->code == '\0') {
/* Reached end of xpath before end of string */
return QUICKMEDIA_XPATH_TOKEN_INVALID;
}
self->string.size = self->code - self->string.data;
++self->code;
return QUICKMEDIA_XPATH_TOKEN_STRING;
} else if(c == '\0') {
return QUICKMEDIA_XPATH_TOKEN_END_OF_FILE;
} else {
/* Invalid symbol @c */
return QUICKMEDIA_XPATH_TOKEN_INVALID;
}
}
int quickmedia_xpath_tokenizer_next_if(QuickMediaXpathTokenizer *self, QuickMediaXpathToken token) {
const char *restore_point = self->code;
if(quickmedia_xpath_tokenizer_next(self) == token)
return 0;
self->code = restore_point;
return -1;
}
char* quickmedia_xpath_tokenizer_copy_identifier(QuickMediaXpathTokenizer *self) {
char *result = malloc(self->identifier.size + 1);
result[self->identifier.size] = '\0';
memcpy(result, self->identifier.data, self->identifier.size);
return result;
}
char* quickmedia_xpath_tokenizer_copy_string(QuickMediaXpathTokenizer *self) {
return quickmedia_xpath_tokenizer_copy_identifier(self);
}
|