trans_func_type * trans_func_fscanf_alloc( FILE * stream ) { trans_func_type * trans_func; char * func_name; func_name = util_fscanf_alloc_token(stream); if (func_name == NULL) { char * filename = "????"; #ifdef HAVE_FORK filename = util_alloc_filename_from_stream( stream ); #endif fprintf(stderr,"Problem at file:line: %s:%d \n", filename, util_get_current_linenr( stream )); util_abort("%s: could not locate name of transformation - aborting \n",__func__); } trans_func = trans_func_alloc( func_name ); arg_pack_fscanf( trans_func->params , stream ); free( func_name ); return trans_func; }
bool parser_fseek_string(const parser_type * parser , FILE * stream , const char * __string , bool skip_string, bool case_sensitive) { bool string_found = false; char * string = util_alloc_string_copy( __string ); if (!case_sensitive) util_strupr( string ); { long int initial_pos = ftell( stream ); /* Store the inital position. */ bool cont = true; if (strstr( string , parser->comment_start ) != NULL) util_abort("%s: sorry the string contains a comment start - will never find it ... \n"); /* A bit harsh ?? */ do { int c = fgetc( stream ); if (!case_sensitive) c = toupper( c ); /* Special treatment of quoters - does not properly handle escaping of the quoters. */ if (is_in_quoters( c , parser )) { long int quote_start_pos = ftell(stream); if (!fseek_quote_end( c , stream )) { fseek( stream , quote_start_pos , SEEK_SET); fprintf(stderr,"Warning: unterminated quotation starting at line: %d \n",util_get_current_linenr( stream )); fseek(stream , 0 , SEEK_END); } /* Now we are either at the first character following a terminated quotation, or at EOF. */ continue; } /* Special treatment of comments: */ if (c == parser->comment_start[0]) { /* OK - this might be the start of a comment - let us check further. */ bool comment_start = fgetc_while_equal( stream , &parser->comment_start[1] , false); if (comment_start) { long int comment_start_pos = ftell(stream) - strlen( parser->comment_start ); /* Start seeking for comment_end */ if (!util_fseek_string(stream , parser->comment_end , true , true)) { /* No end comment end was found - what to do about that?? The file is just positioned at the end - and the routine will exit at the next step - with a Warning. */ fseek( stream , comment_start_pos , SEEK_SET); fprintf(stderr,"Warning: unterminated comment starting at line: %d \n",util_get_current_linenr( stream )); fseek(stream , 0 , SEEK_END); } continue; /* Now we are at the character following a comment end - or at EOF. */ } } /*****************************************************************/ /* Now c is a regular character - and we can start looking for our string. */ if (c == string[0]) { /* OK - we got the first character right - lets try in more detail: */ bool equal = fgetc_while_equal( stream , &string[1] , case_sensitive); if (equal) { string_found = true; cont = false; } } if (c == EOF) cont = false; } while (cont); if (string_found) { if (!skip_string) fseek(stream , -strlen(string) , SEEK_CUR); /* Reposition to the beginning of 'string' */ } else fseek(stream , initial_pos , SEEK_SET); /* Could not find the string reposition at initial position. */ } free( string ); return string_found; }
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; }