6 * Copyright (C) 2003 why the lucky stiff
13 * Node allocation functions
16 syck_alloc_node( enum syck_kind_tag type
)
20 s
= S_ALLOC( SyckNode
);
31 syck_free_node( SyckNode
*n
)
33 syck_free_members( n
);
34 if ( n
->type_id
!= NULL
)
39 if ( n
->anchor
!= NULL
)
53 m
= S_ALLOC( struct SyckMap
);
57 m
->keys
= S_ALLOC_N( SYMID
, m
->capa
);
58 m
->values
= S_ALLOC_N( SYMID
, m
->capa
);
60 n
= syck_alloc_node( syck_map_kind
);
72 s
= S_ALLOC( struct SyckSeq
);
76 s
->items
= S_ALLOC_N( SYMID
, s
->capa
);
78 n
= syck_alloc_node( syck_seq_kind
);
90 s
= S_ALLOC( struct SyckStr
);
93 s
->style
= scalar_none
;
95 n
= syck_alloc_node( syck_str_kind
);
102 syck_new_str( const char *str
, enum scalar_style style
)
104 return syck_new_str2( str
, strlen( str
), style
);
108 syck_new_str2( const char *str
, long len
, enum scalar_style style
)
112 n
= syck_alloc_str();
113 n
->data
.str
->ptr
= S_ALLOC_N( char, len
+ 1 );
114 n
->data
.str
->len
= len
;
115 n
->data
.str
->style
= style
;
116 memcpy( n
->data
.str
->ptr
, str
, len
);
117 n
->data
.str
->ptr
[len
] = '\0';
123 syck_replace_str( SyckNode
*n
, char *str
, enum scalar_style style
)
125 syck_replace_str2( n
, str
, strlen( str
), style
);
129 syck_replace_str2( SyckNode
*n
, char *str
, long len
, enum scalar_style style
)
131 if ( n
->data
.str
!= NULL
)
133 S_FREE( n
->data
.str
->ptr
);
134 n
->data
.str
->ptr
= NULL
;
135 n
->data
.str
->len
= 0;
137 n
->data
.str
->ptr
= S_ALLOC_N( char, len
+ 1 );
138 n
->data
.str
->len
= len
;
139 n
->data
.str
->style
= style
;
140 memcpy( n
->data
.str
->ptr
, str
, len
);
141 n
->data
.str
->ptr
[len
] = '\0';
145 syck_str_blow_away_commas( SyckNode
*n
)
149 go
= n
->data
.str
->ptr
;
150 end
= go
+ n
->data
.str
->len
;
151 while ( *(++go
) != '\0' )
155 n
->data
.str
->len
-= 1;
156 memmove( go
, go
+ 1, end
- go
);
163 syck_str_read( SyckNode
*n
)
166 return n
->data
.str
->ptr
;
170 syck_new_map( SYMID key
, SYMID value
)
174 n
= syck_alloc_map();
175 syck_map_add( n
, key
, value
);
181 syck_map_empty( SyckNode
*n
)
185 ASSERT( n
->data
.list
!= NULL
);
187 S_FREE( n
->data
.pairs
->keys
);
188 S_FREE( n
->data
.pairs
->values
);
192 m
->keys
= S_ALLOC_N( SYMID
, m
->capa
);
193 m
->values
= S_ALLOC_N( SYMID
, m
->capa
);
197 syck_map_add( SyckNode
*map
, SYMID key
, SYMID value
)
202 ASSERT( map
!= NULL
);
203 ASSERT( map
->data
.pairs
!= NULL
);
208 if ( m
->idx
> m
->capa
)
211 S_REALLOC_N( m
->keys
, SYMID
, m
->capa
);
212 S_REALLOC_N( m
->values
, SYMID
, m
->capa
);
215 m
->values
[idx
] = value
;
219 syck_map_update( SyckNode
*map1
, SyckNode
*map2
)
221 struct SyckMap
*m1
, *m2
;
222 long new_idx
, new_capa
;
223 ASSERT( map1
!= NULL
);
224 ASSERT( map2
!= NULL
);
226 m1
= map1
->data
.pairs
;
227 m2
= map2
->data
.pairs
;
228 if ( m2
->idx
< 1 ) return;
233 while ( new_idx
> new_capa
)
235 new_capa
+= ALLOC_CT
;
237 if ( new_capa
> m1
->capa
)
240 S_REALLOC_N( m1
->keys
, SYMID
, m1
->capa
);
241 S_REALLOC_N( m1
->values
, SYMID
, m1
->capa
);
243 for ( new_idx
= 0; new_idx
< m2
->idx
; m1
->idx
++, new_idx
++ )
245 m1
->keys
[m1
->idx
] = m2
->keys
[new_idx
];
246 m1
->values
[m1
->idx
] = m2
->values
[new_idx
];
251 syck_map_count( SyckNode
*map
)
253 ASSERT( map
!= NULL
);
254 ASSERT( map
->data
.pairs
!= NULL
);
255 return map
->data
.pairs
->idx
;
259 syck_map_assign( SyckNode
*map
, enum map_part p
, long idx
, SYMID id
)
263 ASSERT( map
!= NULL
);
277 syck_map_read( SyckNode
*map
, enum map_part p
, long idx
)
281 ASSERT( map
!= NULL
);
290 return m
->values
[idx
];
295 syck_new_seq( SYMID value
)
299 n
= syck_alloc_seq();
300 syck_seq_add( n
, value
);
306 syck_seq_empty( SyckNode
*n
)
310 ASSERT( n
->data
.list
!= NULL
);
312 S_FREE( n
->data
.list
->items
);
316 s
->items
= S_ALLOC_N( SYMID
, s
->capa
);
320 syck_seq_add( SyckNode
*arr
, SYMID value
)
325 ASSERT( arr
!= NULL
);
326 ASSERT( arr
->data
.list
!= NULL
);
331 if ( s
->idx
> s
->capa
)
334 S_REALLOC_N( s
->items
, SYMID
, s
->capa
);
336 s
->items
[idx
] = value
;
340 syck_seq_count( SyckNode
*seq
)
342 ASSERT( seq
!= NULL
);
343 ASSERT( seq
->data
.list
!= NULL
);
344 return seq
->data
.list
->idx
;
348 syck_seq_assign( SyckNode
*seq
, long idx
, SYMID id
)
352 ASSERT( map
!= NULL
);
359 syck_seq_read( SyckNode
*seq
, long idx
)
363 ASSERT( seq
!= NULL
);
366 return s
->items
[idx
];
370 syck_free_members( SyckNode
*n
)
372 if ( n
== NULL
) return;
377 if ( n
->data
.str
!= NULL
)
379 S_FREE( n
->data
.str
->ptr
);
380 n
->data
.str
->ptr
= NULL
;
381 n
->data
.str
->len
= 0;
382 S_FREE( n
->data
.str
);
388 if ( n
->data
.list
!= NULL
)
390 S_FREE( n
->data
.list
->items
);
391 S_FREE( n
->data
.list
);
397 if ( n
->data
.pairs
!= NULL
)
399 S_FREE( n
->data
.pairs
->keys
);
400 S_FREE( n
->data
.pairs
->values
);
401 S_FREE( n
->data
.pairs
);
402 n
->data
.pairs
= NULL
;