WEBVTT_EXPORT void webvtt_release_node( webvtt_node **node ) { webvtt_uint i; webvtt_node *n; if( !node || !*node ) { return; } n = *node; if( webvtt_deref( &n->refs ) == 0 ) { if( n->kind == WEBVTT_TEXT ) { webvtt_release_string( &n->data.text ); } else if( WEBVTT_IS_VALID_INTERNAL_NODE( n->kind ) && n->data.internal_data ) { webvtt_release_stringlist( &n->data.internal_data->css_classes ); webvtt_release_string( &n->data.internal_data->annotation ); for( i = 0; i < n->data.internal_data->length; i++ ) { webvtt_release_node( n->data.internal_data->children + i ); } webvtt_free( n->data.internal_data->children ); webvtt_free( n->data.internal_data ); } webvtt_free( n ); } *node = 0; }
WEBVTT_INTERN void webvtt_delete_token( webvtt_cuetext_token **token ) { webvtt_start_token_data data; webvtt_cuetext_token *t; if( !token ) { return; } if( !*token ) { return; } t = *token; /** * Note that time stamp tokens do not need to free any internal data because * they do not allocate anything. */ switch( t->token_type ) { case START_TOKEN: data = t->start_token_data; webvtt_release_stringlist( &data.css_classes ); webvtt_release_string( &data.annotations ); webvtt_release_string( &t->tag_name ); break; case END_TOKEN: webvtt_release_string( &t->tag_name ); break; case TEXT_TOKEN: webvtt_release_string( &t->text ); break; } webvtt_free( t ); *token = 0; }
/** * Delete bytearray */ WEBVTT_INTERN void webvtt_delete_bytearray( webvtt_bytearray *pstr ) { if( pstr && *pstr ) { webvtt_free( *pstr ); *pstr = 0; } }
/** * Delete bytearray */ void webvtt_bytearray_delete( webvtt_bytearray *pstr ) { if( pstr && *pstr ) { webvtt_free( *pstr ); *pstr = 0; } }
void webvtt_delete_cue( webvtt_cue *pcue ) { if( pcue && *pcue ) { webvtt_cue cue = *pcue; *pcue = 0; webvtt_string_delete( cue->id ); webvtt_string_delete( cue->payload ); webvtt_free( cue ); } }
WEBVTT_EXPORT void webvtt_release_cue( webvtt_cue **pcue ) { if( pcue && *pcue ) { webvtt_cue *cue = *pcue; *pcue = 0; if( webvtt_deref( &cue->refs ) == 0 ) { webvtt_release_string( &cue->id ); webvtt_release_node( &cue->node_head ); webvtt_free( cue ); } } }
WEBVTT_INTERN webvtt_status webvtt_attach_node( webvtt_node *parent, webvtt_node *to_attach ) { webvtt_node **next = 0; webvtt_internal_node_data *nd = 0; if( !parent || !to_attach || !parent->data.internal_data ) { return WEBVTT_INVALID_PARAM; } nd = parent->data.internal_data; if( nd->alloc == 0 ) { next = (webvtt_node **)webvtt_alloc0( sizeof( webvtt_node * ) * 8 ); if( !next ) { return WEBVTT_OUT_OF_MEMORY; } nd->children = next; nd->alloc = 8; } if( nd->length + 1 >= ( nd->alloc / 3 ) * 2 ) { next = (webvtt_node **)webvtt_alloc0( sizeof( *next ) * nd->alloc * 2 ); if( !next ) { return WEBVTT_OUT_OF_MEMORY; } nd->alloc *= 2; memcpy( next, nd->children, nd->length * sizeof( webvtt_node * ) ); webvtt_free( nd->children ); nd->children = next; } nd->children[ nd->length++ ] = to_attach; webvtt_ref_node( to_attach ); return WEBVTT_SUCCESS; }
WEBVTT_INTERN static webvtt_status grow( webvtt_bytearray *pba, webvtt_uint need ) { static const webvtt_uint page = 0x1000; webvtt_uint n; webvtt_bytearray ba = *pba, new_ba; webvtt_uint grow = sizeof(*ba) + sizeof(webvtt_byte) * (ba->length + need); if( grow < page ) { n = page; do { n = n / 2; } while( n > grow ); n = n * 2; } else { n = page; do { n = n * 2; } while ( n < grow ); } new_ba = (webvtt_bytearray)webvtt_alloc( n ); if( !new_ba ) { return WEBVTT_OUT_OF_MEMORY; } new_ba->alloc = (n - sizeof(*ba)) / sizeof(webvtt_byte); new_ba->length = ba->length; new_ba->text = new_ba->array; memcpy( new_ba->text, ba->text, sizeof(webvtt_byte) * ba->length ); new_ba->text[ new_ba->length ] = 0; *pba = new_ba; webvtt_free( ba ); return WEBVTT_SUCCESS; }