/** This function writes a filtered version of string to a file. */ void subst_list_filtered_fprintf(const subst_list_type * subst_list , const char * string , FILE * stream) { buffer_type * buffer = buffer_alloc( strlen(string) + 1 ); buffer_fwrite( buffer , string , 1 , strlen( string ) + 1); subst_list_update_buffer(subst_list , buffer); buffer_stream_fwrite_n( buffer , 0 , -1 , stream ); /* -1: The trailing \0 is not explicitly written to file. */ buffer_free(buffer); }
bool summary_write_to_buffer(const summary_type * summary , buffer_type * buffer, int report_step , state_enum state) { buffer_fwrite_int( buffer , SUMMARY ); if (summary->vector_storage) { double_vector_type * storage_vector = SELECT_VECTOR( summary , state ); double_vector_buffer_fwrite( storage_vector , buffer ); } else { int size = summary_config_get_data_size( summary->config ); buffer_fwrite( buffer , summary->data , sizeof * summary->data , size); } return true; }
bool buffer_search_replace( buffer_type * buffer , const char * old_string , const char * new_string) { const int shift = strlen( new_string ) - strlen( old_string ); bool match = buffer_strstr( buffer , old_string ); if (match) { size_t offset = buffer_get_offset( buffer ) + strlen( old_string ); if (shift != 0) buffer_memshift( buffer , offset , shift ); /** Search continues at the end of the newly inserted string - i.e. no room for recursions. */ buffer_fwrite( buffer , new_string , strlen( new_string ) , sizeof * new_string ); } return match; }
bool buffer_search_replace( buffer_type * buffer , const char * old_string , const char * new_string) { bool match = buffer_strstr( buffer , old_string ); if (match) { size_t offset = buffer_get_offset( buffer ) + strlen( old_string ); const int shift = strlen( new_string ) - strlen( old_string ); if (shift != 0) buffer_memshift( buffer , offset , shift ); buffer_fwrite( buffer , new_string , 1 , strlen(new_string)); buffer_terminate_char_ptr( buffer ); } return match; }
static void subst_list_eval_funcs____(const subst_list_type * subst_list , const parser_type * parser , buffer_type * buffer) { { int index; for (index = 0; index < vector_get_size( subst_list->func_data); index++) { const subst_list_func_type * subst_func = vector_iget_const( subst_list->func_data , index ); const char * func_name = subst_func->name; bool match; buffer_rewind( buffer ); do { size_t match_pos; match = buffer_strstr( buffer , func_name ); match_pos = buffer_get_offset( buffer ); if (match) { bool update = false; char * arg_start = buffer_get_data( buffer ); arg_start += buffer_get_offset( buffer ) + strlen( func_name ); if (arg_start[0] == '(') { /* We require that an opening paren follows immediately behind the function name. */ char * arg_end = strchr( arg_start , ')'); if (arg_end != NULL) { /* OK - we found an enclosing () pair. */ char * arg_content = util_alloc_substring_copy( arg_start, 1 , arg_end - arg_start - 1); stringlist_type * arg_list = parser_tokenize_buffer( parser , arg_content , true); char * func_eval = subst_list_func_eval( subst_func , arg_list ); int old_len = strlen(func_name) + strlen( arg_content) + 2; if (func_eval != NULL) { buffer_memshift( buffer , match_pos + old_len , strlen( func_eval ) - old_len); buffer_fwrite( buffer , func_eval , strlen( func_eval ) , sizeof * func_eval ); free( func_eval ); update = true; } free( arg_content ); stringlist_free( arg_list ); } } if (!update) buffer_fseek( buffer , match_pos + strlen( func_name ) , SEEK_SET); } } while (match); } } if (subst_list->parent != NULL) subst_list_eval_funcs____( subst_list->parent , parser , buffer ); }
void @TYPE@_vector_buffer_fwrite(const @TYPE@_vector_type * vector , buffer_type * buffer) { buffer_fwrite_int( buffer , vector->size ); buffer_fwrite( buffer , &vector->default_value , sizeof vector->default_value , 1 ); buffer_fwrite( buffer , vector->data , sizeof * vector->data , vector->size ); }
bool gen_kw_write_to_buffer(const gen_kw_type *gen_kw , buffer_type * buffer, int report_step, state_enum state) { const int data_size = gen_kw_config_get_data_size( gen_kw->config ); buffer_fwrite_int( buffer , GEN_KW ); buffer_fwrite(buffer , gen_kw->data , sizeof *gen_kw->data , data_size); return true; }
void buffer_replace_data(buffer_type * buffer , size_t offset , size_t old_size , const void * new_data , size_t new_size) { ssize_t shift = new_size - old_size; buffer_memshift( buffer , offset , shift ); buffer_fseek( buffer , offset , SEEK_SET ); buffer_fwrite( buffer , new_data , 1 , new_size ); }
/** Observe that this function writes a leading integer string length. */ void buffer_fwrite_string(buffer_type * buffer , const char * string) { buffer_fwrite_int( buffer , strlen( string )); /* Writing the length of the string */ buffer_fwrite(buffer , string , 1 , strlen( string ) + 1); /* Writing the string content ** WITH ** the terminating \0 */ }
void buffer_fwrite_double(buffer_type * buffer , double value) { buffer_fwrite(buffer , &value , sizeof value , 1); }
void buffer_fwrite_char(buffer_type * buffer , char value) { buffer_fwrite(buffer , &value , sizeof value , 1); }
void buffer_fwrite_time_t(buffer_type * buffer , time_t value) { buffer_fwrite(buffer , &value , sizeof value , 1); }
void buffer_fwrite_bool(buffer_type * buffer , bool value) { buffer_fwrite(buffer , &value , sizeof value , 1); }
void buffer_fwrite_int(buffer_type * buffer , int value) { buffer_fwrite(buffer , &value , sizeof value , 1); }
void buffer_fwrite_char_ptr(buffer_type * buffer , const char * string_ptr ) { buffer_fwrite(buffer , string_ptr , sizeof * string_ptr , strlen( string_ptr )); }