const enkf_config_node_type * ensemble_config_user_get_node(const ensemble_config_type * config , const char * full_key, char ** index_key ) { const enkf_config_node_type * node = NULL; char ** key_list; int keys; int key_length = 1; int offset; *index_key = NULL; util_split_string(full_key , USER_KEY_JOIN_STRING , &keys , &key_list); while (node == NULL && key_length <= keys) { char * current_key = util_alloc_joined_string( (const char **) key_list , key_length , USER_KEY_JOIN_STRING ); if (ensemble_config_has_key(config , current_key)) node = ensemble_config_get_node(config , current_key); else key_length++; offset = strlen( current_key ); free( current_key ); } if (node != NULL) { if (offset < strlen( full_key )) *index_key = util_alloc_string_copy(&full_key[offset+1]); } util_free_stringlist(key_list , keys); return node; }
static void output_add_key( const ecl_sum_type * refcase , output_type * output , const char * qkey) { int tokens; double quantile; char ** tmp; char * sum_key; util_split_string( qkey , SUMMARY_JOIN , &tokens , &tmp); if (tokens == 1) util_exit("Hmmm - the key:%s is malformed - must be of the form SUMMARY_KEY:QUANTILE.\n",qkey); if (!util_sscanf_double( tmp[tokens - 1] , &quantile)) util_exit("Hmmmm - failed to interpret:%s as a quantile - must be a number (0,1).\n",tmp[tokens-1]); if (quantile <= 0 || quantile >= 1.0) util_exit("Invalid quantile value:%g - must be in interval (0,1)\n", quantile); sum_key = util_alloc_joined_string( (const char **) tmp , tokens - 1 , SUMMARY_JOIN); { stringlist_type * matching_keys = stringlist_alloc_new(); int i; ecl_sum_select_matching_general_var_list( refcase , sum_key , matching_keys ); for (i=0; i < stringlist_get_size( matching_keys ); i++) vector_append_owned_ref( output->keys , quant_key_alloc( stringlist_iget( matching_keys , i ) , quantile) , quant_key_free__ ); if (stringlist_get_size( matching_keys ) == 0) fprintf(stderr,"** Warning: No summary vectors matching:\'%s\' found?? \n", sum_key); stringlist_free( matching_keys ); } util_free_stringlist( tmp, tokens ); }
/** Will loose tagging .... */ int subst_list_add_from_string( subst_list_type * subst_list , const char * arg_string, bool append) { int error_count = 0; if (arg_string != NULL) { char ** key_value_list; int num_arg, iarg; util_split_string(arg_string , "," , &num_arg , &key_value_list); for (iarg = 0; iarg < num_arg; iarg++) { if (strchr(key_value_list[iarg] , '=') == NULL) //util_abort("%s: could not find \'=\' in argument string:%s \n",__func__ , key_value_list[iarg]); /* Could not find '=' in the argument string, this argument will be ignored, and the error_count will be increased by one. */ error_count += 1; else { char * key , * value; char * tmp = key_value_list[iarg]; int arg_length , value_length; while (isspace(*tmp)) /* Skipping initial space */ tmp++; arg_length = strcspn(tmp , " ="); key = util_alloc_substring_copy(tmp , 0 , arg_length); tmp += arg_length; while ((*tmp == ' ') || (*tmp == '=')) tmp++; value_length = strcspn(tmp , " "); value = util_alloc_substring_copy( tmp , 0 , value_length); /* Setting the argument */ if (append) subst_list_append_copy( subst_list , key , value , NULL); else subst_list_prepend_copy( subst_list , key , value , NULL); free(key); free(value); tmp += value_length; /* Accept only trailing space - any other character indicates a failed parsing. */ while (*tmp != '\0') { if (!isspace(*tmp)) util_abort("%s: something wrong with:%s - spaces are not allowed in key or value part.\n",__func__ , key_value_list[iarg]); tmp++; } } } util_free_stringlist(key_value_list , num_arg); } return error_count; }
stringlist_type * stringlist_alloc_from_split( const char * input_string , const char * sep ) { stringlist_type * slist = stringlist_alloc_new(); if (input_string != NULL) { char ** items; int num_items , i; util_split_string( input_string , sep , &num_items , &items); for ( i =0; i < num_items; i++) stringlist_append_copy( slist , items[i] ); util_free_stringlist( items , num_items ); } return slist; }
static void load_stations(vector_type * grav_stations , const char * filename) { printf("Loading from file:%s \n",filename); { int target_width; FILE * stream = util_fopen(filename , "r"); bool at_eof = false; /** When reading the first line we determine how many columns the file contains. */ { char * first_line = util_fscanf_alloc_line( stream , &at_eof); char ** token_list; util_split_string( first_line , " \t" , &target_width , &token_list); util_free_stringlist( token_list , target_width ); fseek( stream , 0 , SEEK_SET ); } while(!(at_eof)) { double x,y,d; double obs_gdiff , std_gdiff; char station_name[32]; int fscanf_return; if (target_width == 4) fscanf_return = fscanf(stream, "%s %lg %lg %lg", station_name , &x , &y , &d ); else fscanf_return = fscanf(stream, "%s %lg %lg %lg %lg %lg", station_name , &x , &y , &d , &obs_gdiff , &std_gdiff); if (fscanf_return == target_width) { grav_station_type * g = grav_station_alloc_new(station_name , x , y , d); if (target_width == 6) grav_station_add_obs( g , obs_gdiff , std_gdiff ); vector_append_owned_ref(grav_stations, g, grav_station_free__); } else at_eof = true; } fclose(stream); } }
const char * util_update_path_var(const char * variable, const char * value, bool append) { const char * current_value = getenv( variable ); if (current_value == NULL) /* The (path) variable is not currently set. */ util_setenv( variable , value ); else { bool update = true; { char ** path_list; int num_path; util_split_string( current_value , ":" , &num_path , &path_list); if (append) { int i; for (i = 0; i < num_path; i++) { if (util_string_equal( path_list[i] , value)) update = false; /* The environment variable already contains @value - no point in appending it at the end. */ } } else { if (util_string_equal( path_list[0] , value)) update = false; /* The environment variable already starts with @value. */ } util_free_stringlist( path_list , num_path ); } if (update) { char * new_value; if (append) new_value = util_alloc_sprintf("%s:%s" , current_value , value); else new_value = util_alloc_sprintf("%s:%s" , value , current_value); util_setenv( variable , new_value ); free( new_value ); } } return getenv( variable ); }
void util_path_split(const char *line , int *_tokens, char ***_token_list) { util_split_string( line , UTIL_PATH_SEP_STRING , _tokens , _token_list); }