/** 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); }
void subst_list_filter_file(const subst_list_type * subst_list , const char * src_file , const char * target_file) { char * backup_file = NULL; buffer_type * buffer = buffer_fread_alloc( src_file ); buffer_fseek( buffer , 0 , SEEK_END ); /* Ensure that the buffer is a \0 terminated string. */ buffer_fwrite_char( buffer , '\0'); /*****************************************************************/ /* Backup file ?? */ if (util_same_file(src_file , target_file)) { char * backup_prefix = util_alloc_sprintf("%s-%s" , src_file , __func__); backup_file = util_alloc_tmp_file("/tmp" , backup_prefix , false); free(backup_prefix); } /* Writing backup file */ if (backup_file != NULL) { FILE * stream = util_fopen(backup_file , "w"); buffer_stream_fwrite_n( buffer , 0 , -1 , stream ); /* -1: Do not write the trailing \0. */ fclose(stream); } /* Doing the actual update */ subst_list_update_buffer(subst_list , buffer); /* Writing updated file */ { FILE * stream = util_mkdir_fopen(target_file , "w"); buffer_stream_fwrite_n( buffer , 0 , -1 , stream ); /* -1: Do not write the trailing \0. */ fclose(stream); } /* OK - all went hunka dory - unlink the backup file and leave the building. */ if (backup_file != NULL) { remove( backup_file ); free( backup_file ); } free(buffer); }
void buffer_stream_fwrite( const buffer_type * buffer , FILE * stream ) { buffer_stream_fwrite_n( buffer , 0 , buffer->content_size , stream ); }