static bool custom_kw_config_setup__(custom_kw_config_type * config, const char * result_file) { FILE * stream = util_fopen__(result_file, "r"); if (stream != NULL) { bool read_ok = true; config->key_definition_file = util_alloc_string_copy(result_file); int counter = 0; char key[128]; char value[128]; int read_count; while ((read_count = fscanf(stream, "%s %s", key, value)) != EOF) { if (read_count == 1) { fprintf(stderr ,"[%s] Warning: Key: '%s:%s' is missing value in file: '%s'\n", __func__, config->name, key, result_file); read_ok = false; break; } if (custom_kw_config_has_key(config, key)) { fprintf(stderr ,"[%s] Warning: Key: '%s:%s' already defined!\n", __func__, config->name, key); } else { hash_insert_int(config->custom_keys, key, counter++); hash_insert_int(config->custom_key_types, key, util_sscanf_double(value, NULL)); } } fclose(stream); return read_ok; } return false; }
static bool custom_kw_config_read_data__(const custom_kw_config_type * config, const char * result_file, stringlist_type * result) { FILE * stream = util_fopen__(result_file, "r"); if (stream != NULL) { bool read_ok = true; stringlist_clear(result); stringlist_iset_ref(result, hash_get_size(config->custom_keys) - 1, NULL); hash_type * read_keys = hash_alloc(); char key[128]; char value[128]; int read_count; while ((read_count = fscanf(stream, "%s %s", key, value)) != EOF) { if (read_count == 1) { fprintf(stderr ,"[%s] Warning: Key: '%s:%s' missing value in file: %s!\n", __func__, config->name, key, result_file); read_ok = false; break; } if (custom_kw_config_has_key(config, key)) { if (hash_has_key(read_keys, key)) { fprintf(stderr ,"[%s] Warning: Key: '%s:%s' has appeared multiple times. Only the last occurrence will be used!\n", __func__, config->name, key); } hash_insert_int(read_keys, key, 1); int index = custom_kw_config_index_of_key(config, key); stringlist_iset_copy(result, index, value); } else { fprintf(stderr ,"[%s] Warning: Key: '%s:%s' not in the available set. Ignored!\n", __func__, config->name, key); } } fclose(stream); if (read_ok) { read_ok = hash_key_list_compare(read_keys, config->custom_keys); } return read_ok; } return false; }
bool gen_kw_fload(gen_kw_type * gen_kw , const char * filename) { FILE * stream = util_fopen__( filename , "r"); if (stream) { const int size = gen_kw_config_get_data_size(gen_kw->config ); bool readOK = true; /* First try reading all the data as one long vector. */ { int index = 0; while ((index < size) && readOK) { double value; if (fscanf(stream,"%lg" , &value) == 1) gen_kw->data[index] = value; else readOK = false; index++; } } /* OK - rewind and try again with interlaced key + value pairs. Observe that we still require that ALL the elements in the gen_kw instance are set, i.e. it is not allowed to read only some of the keywords; but the ordering is not relevant. The code will be fooled (and give undefined erronous results) if the same key appears several times. Be polite! */ if (!readOK) { int counter = 0; readOK = true; util_fseek( stream , 0 , SEEK_SET ); while ((counter < size) && readOK) { char key[128]; double value; int fscanf_return = fscanf(stream , "%s %lg" , key , &value); if (fscanf_return == 2) { int index = gen_kw_config_get_index(gen_kw->config , key); if (index >= 0) gen_kw->data[index] = value; else util_abort("%s: key:%s not recognized as part of GEN_KW instance - error when reading file:%s \n",__func__ , key , filename); counter++; } else { util_abort("%s: failed to read (key,value) pair at line:%d in file:%s \n",__func__ , util_get_current_linenr( stream ) , filename); readOK = false; } } } if (!readOK) util_abort("%s: failed loading from file:%s \n",__func__ , filename); fclose(stream); return true; } else return false; }