📄 membuffer.c
字号:
free( m->buf ); membuffer_init( m );}/************************************************************************* Function : membuffer_assign** Parameters :* INOUT membuffer* m ; buffer whose memory is to be allocated and * assigned.* IN const void* buf ; source buffer whose contents will be copied* IN size_t buf_len ; length of the source buffer** Description : Allocate memory to membuffer* m and copy the contents * of the in parameter IN const void* buf.** Return : int ;* UPNP_E_SUCCESS* UPNP_E_OUTOF_MEMORY** Note :************************************************************************/intmembuffer_assign( INOUT membuffer * m, IN const void *buf, IN size_t buf_len ){ int return_code; assert( m != NULL ); // set value to null if( buf == NULL ) { membuffer_destroy( m ); return 0; } // alloc mem return_code = membuffer_set_size( m, buf_len ); if( return_code != 0 ) { return return_code; } // copy memcpy( m->buf, buf, buf_len ); m->buf[buf_len] = 0; // null-terminate m->length = buf_len; return 0;}/************************************************************************* Function : membuffer_assign_str** Parameters :* INOUT membuffer* m ; buffer to be allocated and assigned* IN const char* c_str ; source buffer whose contents will be * copied** Description : Wrapper function for membuffer_assign()** Return : int ;* UPNP_E_SUCCESS* UPNP_E_OUTOF_MEMORY** Note :************************************************************************/intmembuffer_assign_str( INOUT membuffer * m, IN const char *c_str ){ return membuffer_assign( m, c_str, strlen( c_str ) );}/************************************************************************* Function : membuffer_append** Parameters :* INOUT membuffer* m ; buffer whose memory is to be appended.* IN const void* buf ; source buffer whose contents will be * copied* IN size_t buf_len ; length of the source buffer** Description : Invokes function to appends data from a constant buffer * to the buffer ** Return : int ;** Note :************************************************************************/intmembuffer_append( INOUT membuffer * m, IN const void *buf, IN size_t buf_len ){ assert( m != NULL ); return membuffer_insert( m, buf, buf_len, m->length );}/************************************************************************* Function : membuffer_append_str** Parameters :* INOUT membuffer* m ; buffer whose memory is to be appended.* IN const char* c_str ; source buffer whose contents will be * copied** Description : Invokes function to appends data from a constant string * to the buffer ** Return : int ;** Note :************************************************************************/intmembuffer_append_str( INOUT membuffer * m, IN const char *c_str ){ return membuffer_insert( m, c_str, strlen( c_str ), m->length );}/************************************************************************* Function : membuffer_insert** Parameters :* INOUT membuffer* m ; buffer whose memory size is to be increased * and appended.* IN const void* buf ; source buffer whose contents will be * copied* IN size_t buf_len ; size of the source buffer* int index ; index to determine the bounds while movinf the data** Description : Allocates memory for the new data to be inserted. Does* memory management by moving the data from the existing memory to * the newly allocated memory and then appending the new data.** Return : int ;** Note :************************************************************************/intmembuffer_insert( INOUT membuffer * m, IN const void *buf, IN size_t buf_len, int index ){ int return_code; assert( m != NULL ); if( index < 0 || index > ( int )m->length ) return UPNP_E_OUTOF_BOUNDS; if( buf == NULL || buf_len == 0 ) { return 0; } // alloc mem return_code = membuffer_set_size( m, m->length + buf_len ); if( return_code != 0 ) { return return_code; } // insert data // move data to right of insertion point memmove( m->buf + index + buf_len, m->buf + index, m->length - index ); memcpy( m->buf + index, buf, buf_len ); m->length += buf_len; m->buf[m->length] = 0; // null-terminate return 0;}/************************************************************************* Function : membuffer_delete** Parameters :* INOUT membuffer* m ; buffer whose memory size is to be decreased* and copied to the odified location* IN int index ; index to determine bounds while moving data* IN size_t num_bytes ; number of bytes that the data needs to * shrink by** Description : Shrink the size of the buffer depending on the current * size of the bufer and te input parameters. Move contents from the * old buffer to the new sized buffer.** Return : void ;** Note :************************************************************************/voidmembuffer_delete( INOUT membuffer * m, IN int index, IN size_t num_bytes ){ int return_value; int new_length; size_t copy_len; assert( m != NULL ); if( m->length == 0 ) { return; } assert( index >= 0 && index < ( int )m->length ); // shrink count if it goes beyond buffer if( index + num_bytes > m->length ) { num_bytes = m->length - ( size_t ) index; copy_len = 0; // every thing at and after index purged } else { // calc num bytes after deleted string copy_len = m->length - ( index + num_bytes ); } memmove( m->buf + index, m->buf + index + num_bytes, copy_len ); new_length = m->length - num_bytes; return_value = membuffer_set_size( m, new_length ); // trim buffer assert( return_value == 0 ); // shrinking should always work // don't modify until buffer is set m->length = new_length; m->buf[new_length] = 0;}/************************************************************************* Function : membuffer_detach** Parameters :* INOUT membuffer* m ; buffer to be returned and updated. ** Description : Detaches current buffer and returns it. The caller* must free the returned buffer using free().* After this call, length becomes 0.** Return : char* ;* a pointer to the current buffer** Note :************************************************************************/char *membuffer_detach( INOUT membuffer * m ){ char *buf; assert( m != NULL ); buf = m->buf; // free all membuffer_initialize( m ); return buf;}/************************************************************************* Function : membuffer_attach** Parameters :* INOUT membuffer* m ; buffer to be updated* IN char* new_buf ; source buffer which will be assigned to the * buffer to be updated* IN size_t buf_len ; length of the source buffer ** Description : Free existing memory in membuffer and assign the new * buffer in its place.** Return : void ;** Note : 'new_buf' must be allocted using malloc or realloc so* that it can be freed using free()************************************************************************/voidmembuffer_attach( INOUT membuffer * m, IN char *new_buf, IN size_t buf_len ){ assert( m != NULL ); membuffer_destroy( m ); m->buf = new_buf; m->length = buf_len; m->capacity = buf_len;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -