void gen_kw_fload(gen_kw_type * gen_kw , const char * filename) { const int size = gen_kw_config_get_data_size(gen_kw->config ); FILE * stream = util_fopen( filename , "r"); 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; 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); }
/** Will return 0.0 on invalid input, and set valid -> false. It is the responsibility of the calling scope to check valid. */ bool gen_kw_user_get(const gen_kw_type * gen_kw, const char * key , int report_step , state_enum state , double * value) { int index = gen_kw_config_get_index(gen_kw->config , key); if (index >= 0) { *value = gen_kw_config_transform(gen_kw->config , index , gen_kw->data[ index ] ); return true; } else { *value = 0.0; fprintf(stderr,"** Warning:could not lookup key:%s in gen_kw instance \n",key); return false; } }