void test_filter_file1() { subst_list_type * subst_list = subst_list_alloc( NULL ); test_work_area_type * work_area = test_work_area_alloc("subst_list/filter1"); { FILE * stream = util_fopen("template" , "w"); fprintf(stream , "<KEY1>\n<KEY2>\n<KEY3>\n<KEY4>\n"); fclose(stream); } subst_list_append_copy( subst_list , "<KEY1>" , "Value1" , NULL); subst_list_append_copy( subst_list , "<KEY2>" , "Value2" , NULL); subst_list_append_copy( subst_list , "<KEY3>" , "Value3" , NULL); subst_list_append_copy( subst_list , "<KEY4>" , "Value4" , NULL); subst_list_filter_file( subst_list , "template" , "target"); { FILE * stream = util_fopen("target" , "r"); char s1[128],s2[128],s3[128],s4[128]; test_assert_int_equal( 4 , fscanf( stream , "%s %s %s %s" , s1,s2,s3,s4)); fclose(stream); test_assert_string_equal( s1 , "Value1"); test_assert_string_equal( s2 , "Value2"); test_assert_string_equal( s3 , "Value3"); test_assert_string_equal( s4 , "Value4"); } test_work_area_free( work_area ); subst_list_free( subst_list ); }
void test_filter_file2() { subst_list_type * subst_list = subst_list_alloc( NULL ); test_work_area_type * work_area = test_work_area_alloc("subst_list/filter2"); { FILE * stream = util_fopen("template" , "w"); fprintf(stream , "MAGIC_PRINT magic-list.txt <ERTCASE> __MAGIC__"); fclose(stream); } subst_list_append_copy( subst_list , "<QC_PATH>" , "QC" , NULL); subst_list_append_copy( subst_list , "__MAGIC__" , "MagicAllTheWayToWorkFlow" , NULL); subst_list_append_copy( subst_list , "<CASE>" , "SUPERcase" , NULL); subst_list_append_copy( subst_list , "<ERTCASE>" , "default" , NULL); subst_list_filter_file( subst_list , "template" , "target"); { char * target_string = util_fread_alloc_file_content( "target" , NULL ); test_assert_string_equal( target_string , "MAGIC_PRINT magic-list.txt default MagicAllTheWayToWorkFlow"); free( target_string ); } test_work_area_free( work_area ); subst_list_free( subst_list ); }
void gen_kw_filter_file(const gen_kw_type * gen_kw , const char * target_file) { const char * template_file = gen_kw_config_get_template_file(gen_kw->config); if (template_file != NULL) { const int size = gen_kw_config_get_data_size(gen_kw->config ); int ikw; for (ikw = 0; ikw < size; ikw++) { const char * key = gen_kw_config_get_tagged_name(gen_kw->config , ikw); subst_list_append_owned_ref(gen_kw->subst_list , key , util_alloc_sprintf("%g" , gen_kw_config_transform( gen_kw->config , ikw , gen_kw->data[ikw] )) , NULL); } /* If the target_file already exists as a symbolic link the symbolic link is removed before creating the target file. The is to ensure against existing symlinks pointing to a common file outside the realization root. */ if (util_is_link( target_file )) remove( target_file ); subst_list_filter_file( gen_kw->subst_list , template_file , target_file); } else util_abort("%s: internal error - tried to filter gen_kw instance without template file.\n",__func__); }
bool workflow_try_compile( workflow_type * script , const subst_list_type * context) { if (util_file_exists( script->src_file )) { const char * src_file = script->src_file; char * tmp_file = NULL; bool update = false; if (context != NULL) { tmp_file = util_alloc_tmp_file("/tmp" , "ert-workflow" , false ); update = subst_list_filter_file( context , script->src_file , tmp_file ); if (update) { script->compiled = false; src_file = tmp_file; } else { remove( tmp_file ); free( tmp_file ); tmp_file = NULL; } } { time_t src_mtime = util_file_mtime( script->src_file ); if (script->compiled) { if (util_difftime_seconds( src_mtime , script->compile_time ) > 0 ) return true; else { // Script has been compiled succesfully, but then changed afterwards. // We try to recompile; if that fails we are left with 'nothing'. } } } { // Try to compile config_parser_type * config_compiler = workflow_joblist_get_compiler( script->joblist ); script->compiled = false; workflow_clear( script ); { config_content_type * content = config_parse( config_compiler , src_file , WORKFLOW_COMMENT_STRING , WORKFLOW_INCLUDE , NULL , CONFIG_UNRECOGNIZED_ERROR , true ); if (config_content_is_valid( content )) { int cmd_line; for (cmd_line = 0; cmd_line < config_content_get_size(content); cmd_line++) { const config_content_node_type * node = config_content_iget_node( content , cmd_line ); const char * jobname = config_content_node_get_kw( node ); const workflow_job_type * job = workflow_joblist_get_job( script->joblist , jobname ); cmd_type * cmd = cmd_alloc( job , config_content_node_get_stringlist( node )); workflow_add_cmd( script , cmd ); } script->compiled = true; } else workflow_store_error( script , config_content_get_errors( content )); config_content_free( content ); } } if (tmp_file != NULL) { if (script->compiled) remove( tmp_file ); free( tmp_file ); } } // It is legal to remove the script after successfull compilation but // then the context will not be applied at subsequent invocations. return script->compiled; }
/** This function does search-replace on a file inplace. */ void subst_list_update_file(const subst_list_type * subst_list , const char * file) { subst_list_filter_file( subst_list , file , file ); }