forked from avar/YAML-Syck
-
Notifications
You must be signed in to change notification settings - Fork 16
/
handler.c
183 lines (164 loc) · 3.68 KB
/
handler.c
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* handler.c
*
* $Author: why $
* $Date: 2005-09-20 13:21:06 +0800 (二, 20 9 2005) $
*
* Copyright (C) 2003 why the lucky stiff
*/
#include "syck.h"
SYMID
syck_hdlr_add_node( SyckParser *p, SyckNode *n )
{
SYMID id;
if ( ! n->id )
{
n->id = (p->handler)( p, n );
}
id = n->id;
if ( n->anchor == NULL )
{
syck_free_node( n );
}
return id;
}
SyckNode *
syck_hdlr_add_anchor( SyckParser *p, char *a, SyckNode *n )
{
SyckNode *ntmp = NULL;
if (n->anchor != NULL) {
/*
* Note: An error should be returned here. But this is better than
* not checking at all because it will abort the program with a
* memory corruption error.
* Happens if you have two anchors after each other or an anchor
* before an alias
* */
return n;
}
n->anchor = a;
if ( p->bad_anchors != NULL )
{
SyckNode *bad;
if ( st_lookup( p->bad_anchors, (st_data_t)a, (st_data_t *)&bad ) )
{
if ( n->kind != syck_str_kind )
{
n->id = bad->id;
(p->handler)( p, n );
}
}
}
if ( p->anchors == NULL )
{
p->anchors = st_init_strtable();
}
if ( st_lookup( p->anchors, (st_data_t)a, (st_data_t *)&ntmp ) )
{
if ( ntmp != (void *)1 )
{
syck_free_node( ntmp );
}
}
st_insert( p->anchors, (st_data_t)a, (st_data_t)n );
return n;
}
void
syck_hdlr_remove_anchor( SyckParser *p, char *a )
{
char *atmp = a;
SyckNode *ntmp;
if ( p->anchors == NULL )
{
p->anchors = st_init_strtable();
}
if ( st_delete( p->anchors, (st_data_t *)&atmp, (st_data_t *)&ntmp ) )
{
if ( ntmp != (void *)1 )
{
syck_free_node( ntmp );
}
}
st_insert( p->anchors, (st_data_t)a, (st_data_t)1 );
}
SyckNode *
syck_hdlr_get_anchor( SyckParser *p, char *a )
{
SyckNode *n = NULL;
if ( p->anchors != NULL )
{
if ( st_lookup( p->anchors, (st_data_t)a, (st_data_t *)&n ) )
{
if ( n != (void *)1 )
{
S_FREE( a );
return n;
}
else
{
if ( p->bad_anchors == NULL )
{
p->bad_anchors = st_init_strtable();
}
if ( ! st_lookup( p->bad_anchors, (st_data_t)a, (st_data_t *)&n ) )
{
n = (p->bad_anchor_handler)( p, a );
st_insert( p->bad_anchors, (st_data_t)a, (st_data_t)n );
}
}
}
}
if ( n == NULL )
{
n = (p->bad_anchor_handler)( p, a );
}
if ( n->anchor )
{
S_FREE( a );
}
else
{
n->anchor = a;
}
return n;
}
void
syck_add_transfer( char *uri, SyckNode *n, int taguri )
{
if ( n->type_id != NULL )
{
S_FREE( n->type_id );
}
if ( taguri == 0 )
{
n->type_id = uri;
return;
}
n->type_id = syck_type_id_to_uri( uri );
S_FREE( uri );
}
char *
syck_xprivate( char *type_id, int type_len )
{
char *uri = S_ALLOC_N( char, type_len + 14 );
uri[0] = '\0';
strcat( uri, "x-private:" );
strncat( uri, type_id, type_len );
return uri;
}
char *
syck_taguri( const char *domain, const char *type_id, int type_len )
{
char *uri = S_ALLOC_N( char, strlen( domain ) + type_len + 14 );
uri[0] = '\0';
strcat( uri, "tag:" );
strcat( uri, domain );
strcat( uri, ":" );
strncat( uri, type_id, type_len );
return uri;
}
int
syck_try_implicit( SyckNode *n )
{
return 1;
}