* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / ext / syck / handler.c
blobd76a72e5cc28284e9e5a21fcb9baedab458bbcec
1 /*
2 * handler.c
4 * $Author$
6 * Copyright (C) 2003 why the lucky stiff
7 */
9 #include "ruby/ruby.h"
10 #include "syck.h"
12 SYMID
13 syck_hdlr_add_node( SyckParser *p, SyckNode *n )
15 SYMID id;
17 if ( ! n->id )
19 n->id = (p->handler)( p, n );
21 id = n->id;
23 if ( n->anchor == NULL )
25 syck_free_node( n );
27 return id;
30 SyckNode *
31 syck_hdlr_add_anchor( SyckParser *p, char *a, SyckNode *n )
33 SyckNode *ntmp = NULL;
35 n->anchor = a;
36 if ( p->bad_anchors != NULL )
38 SyckNode *bad;
39 if ( st_lookup( p->bad_anchors, (st_data_t)a, (void *)&bad ) )
41 if ( n->kind != syck_str_kind )
43 n->id = bad->id;
44 (p->handler)( p, n );
48 if ( p->anchors == NULL )
50 p->anchors = st_init_strtable();
52 if ( st_lookup( p->anchors, (st_data_t)a, (void *)&ntmp ) )
54 if ( ntmp != (void *)1 )
56 syck_free_node( ntmp );
59 st_insert( p->anchors, (st_data_t)a, (st_data_t)n );
60 return n;
63 void
64 syck_hdlr_remove_anchor( SyckParser *p, char *a )
66 char *atmp = a;
67 SyckNode *ntmp;
68 if ( p->anchors == NULL )
70 p->anchors = st_init_strtable();
72 if ( st_delete( p->anchors, (void *)&atmp, (void *)&ntmp ) )
74 if ( ntmp != (void *)1 )
76 syck_free_node( ntmp );
79 st_insert( p->anchors, (st_data_t)a, (st_data_t)1 );
82 SyckNode *
83 syck_hdlr_get_anchor( SyckParser *p, char *a )
85 SyckNode *n = NULL;
87 if ( p->anchors != NULL )
89 if ( st_lookup( p->anchors, (st_data_t)a, (void *)&n ) )
91 if ( n != (void *)1 )
93 S_FREE( a );
94 return n;
96 else
98 if ( p->bad_anchors == NULL )
100 p->bad_anchors = st_init_strtable();
102 if ( ! st_lookup( p->bad_anchors, (st_data_t)a, (void *)&n ) )
104 n = (p->bad_anchor_handler)( p, a );
105 st_insert( p->bad_anchors, (st_data_t)a, (st_data_t)n );
111 if ( n == NULL )
113 n = (p->bad_anchor_handler)( p, a );
116 if ( n->anchor )
118 S_FREE( a );
120 else
122 n->anchor = a;
125 return n;
128 void
129 syck_add_transfer( char *uri, SyckNode *n, int taguri )
131 if ( n->type_id != NULL )
133 S_FREE( n->type_id );
136 if ( taguri == 0 )
138 n->type_id = uri;
139 return;
142 n->type_id = syck_type_id_to_uri( uri );
143 S_FREE( uri );
146 char *
147 syck_xprivate( const char *type_id, int type_len )
149 char *uri = S_ALLOC_N( char, type_len + 14 );
150 uri[0] = '\0';
151 strcat( uri, "x-private:" );
152 strncat( uri, type_id, type_len );
153 return uri;
156 char *
157 syck_taguri( const char *domain, const char *type_id, int type_len )
159 char *uri = S_ALLOC_N( char, strlen( domain ) + type_len + 14 );
160 uri[0] = '\0';
161 strcat( uri, "tag:" );
162 strcat( uri, domain );
163 strcat( uri, ":" );
164 strncat( uri, type_id, type_len );
165 return uri;
168 int
169 syck_try_implicit( SyckNode *n )
171 return 1;