int my_db_leftrd(t_mysh *mysh) { if (!(mysh->between[mysh->value.index_between])) return (at_exit_1("Expected file line after <<")); mysh->value.entree = open_file_read(mysh->between[mysh->value.index_between]); return (0); }
static int open_file(struct perf_data *data) { int fd; fd = perf_data__is_read(data) ? open_file_read(data) : open_file_write(data); data->file.fd = fd; return fd < 0 ? -1 : 0; }
void read_input_file( char *filename ) { FILE *input = open_file_read( filename ); int r, t_len, c_len; double count; t_dict = g_hash_table_new_full( &g_int_hash, &g_int_equal, NULL, destroy_word_count ); c_dict = g_hash_table_new_full( &g_int_hash, &g_int_equal, NULL, destroy_word_count ); symbols_dict = g_hash_table_new_full( &g_str_hash, &g_str_equal, free, free ); inv_symbols_dict = g_hash_table_new_full( &g_int_hash, &g_int_equal, NULL, NULL ); while( !feof( input ) ) { char *target_buff = NULL; char *context_buff = NULL; // Read the strings and count from the file r = fscanf( input, "%ms %ms %lf", &target_buff, &context_buff, &count ); // Reads potentially large strings from file // Shrink the strings to minimal memory, reject too large words if( r == 3 ) { t_len = strlen( target_buff ); c_len = strlen( context_buff ); if( t_len <= MAX_S && c_len <= MAX_S ) { //perra( "Target:\"%s\" Context:\"%s\"\n",target,context ); insert_into_index( t_dict, symbols_dict, inv_symbols_dict, target_buff, context_buff, t_len, c_len, count, &idc_t ); insert_into_index( c_dict, symbols_dict, inv_symbols_dict, context_buff, target_buff, c_len, t_len, count, &idc_c ); n_pairs += count; } else { perra( "Warning: Ignore (\"%s\",\"%s\")\n", target_buff, context_buff ); perra( "Word length (%d,%d) => max is %d\n", t_len, c_len, MAX_S ); } } free( target_buff ); free( context_buff ); target_buff = NULL; context_buff = NULL; } fclose( input ); }
void read_profiles_file( char *filename ) { FILE *input = open_file_read( filename ); char *target; char *context; double count; int idc, idt, r_read, nb_l = 2, *idc_copy; t_dict = g_hash_table_new_full( &g_str_hash, &g_str_equal, free, destroy_target_context ); c_dict = g_hash_table_new_full( &g_str_hash, &g_str_equal, free, free ); while( !feof( input ) && getc( input ) != '\n' ){ /* read-ignore header */ } while( !feof( input ) ) { target = malloc( (MAX_S + 1) * sizeof( char ) ); context = malloc( (MAX_S + 1) * sizeof( char ) ); r_read = fscanf( input, "%s\t%d\t%s\t%d\t%lf", target, &idt, context, &idc, &count ); if( r_read == 5 ) { if( print_originals ) { printf( "%s\t%s\t%lf\n", target, context, count ); } if( g_hash_table_lookup( c_dict, &idc ) == NULL ) { idc_copy = malloc( sizeof( int ) ); *idc_copy = idc; g_hash_table_insert( c_dict, idc_copy, context ); } store_t_c( t_dict, target, idt, idc, count ); } else { // buffers unused, can be freed free( target ); free( context ); if( r_read != -1 ) { // EOF fprintf( stderr, "WARNING: profiles may be corrupted line %d\n", nb_l ); break; // stop reading the file, there was an error or file ended } } while( getc(input) != '\n' && !feof(input) ){ /*read-ignore rest of line*/ } nb_l++; } fclose( input ); }
int main( int argc, char *argv[] ) { int argindex = treat_options( argc, argv ); if( argindex != argc - 2 ){ perr( "You must provide two filenames as arguments\n" ); usage(); } perr( "Reading input profiles into hashmap...\n" ); read_profiles_file( argv[ argindex + 1 ] ); perr( "Generalizing...\n" ); sim_file = open_file_read( argv[ argindex ] ); sim_index = get_index_column_name( sim_file, score_name ); if( sim_index < 4 ) { fprintf( stderr, "Column named \"%s\" not found!\nYou must ", score_name ); perr( "specify a valid -s option. Chose among SIM-FILE column headers.\n"); perr( "Remember that the 1st 4 fields cannot be used as scores.\n" ); usage(); } if( nb_threads > 1 ) { run_multi_threaded( &read_sim_and_generalize, nb_threads ); } else { perr( "Not using threads\n" ); read_sim_and_generalize_serial(); } // Clean and free to avoid memory leaks perr( "Finished, cleaning up...\n" ); fclose( sim_file ); g_hash_table_destroy( c_dict ); g_hash_table_destroy( t_dict ); if( t_filter ) { g_hash_table_destroy( t_filter ); } if( n_filter ) { g_hash_table_destroy( n_filter ); } if( c_filter ) { g_hash_table_destroy( c_filter ); } return 0; }
//at the moment only set up for log-binned angular weights. angwgt readangwgt(char *filename1pw) { angwgt awgt; FILE *ifpw; int headertot, linestot,i; if(!(ifpw = open_file_read(filename1pw))) { exit(1); //return NULL; } get_file_length(ifpw,&headertot,&linestot); assert(headertot==0); awgt.ntheta = linestot; awgt.thetacen = (real *) malloc(sizeof(real)*awgt.ntheta); awgt.thetamin = (real *) malloc(sizeof(real)*awgt.ntheta); awgt.thetamax = (real *) malloc(sizeof(real)*awgt.ntheta); awgt.angweight = (real *) malloc(sizeof(real)*awgt.ntheta); for(i=0;i<awgt.ntheta;i++) { #ifdef REALFLOAT fscanf(ifpw,"%e %e %e %e\n",&(awgt.thetacen[i]), &(awgt.thetamin[i]), &(awgt.thetamax[i]), &(awgt.angweight[i])); #else fscanf(ifpw,"%le %le %le %le\n",&(awgt.thetacen[i]), &(awgt.thetamin[i]), &(awgt.thetamax[i]), &(awgt.angweight[i])); #endif if(i==0) { awgt.dlog10theta = log10(awgt.thetamax[i]/awgt.thetamin[i]); } else { assert(fabs(log10(awgt.thetamax[i]/awgt.thetamin[i]) - awgt.dlog10theta) < 2.0e-6); assert(fabs(log10(awgt.thetacen[i]/awgt.thetacen[i-1]) - awgt.dlog10theta) < 2.0e-6); } } fclose(ifpw); awgt.log10thetamin = log10(awgt.thetamin[0]); return awgt; }