int handle_need_rng(void) { int status; normal("Configuring random number generator\n"); // turn off automatic abort on gsl error gsl_set_error_handler_off(); status = ggen_rng_init(&rng); if(status) { error("Failed to initialize RNG\n"); return 1; } if(rngfname) { info("Using %s as RNG state file\n",rngfname); status = ggen_rng_load(&rng,rngfname); if(status == 1) warning("RNG State file not found, will continue anyway\n"); else if(status != 0) { error("Reading RNG State from file failed.\n"); return 1; } } normal("RNG configured\n"); return 0; }
int handle_second_lvl(int argc,char **argv,struct first_lvl_cmd *fl, struct second_lvl_cmd *sl) { int status = 0; // check for help if(ask_help || (argc == 0 && sl->nargs != 0)) { if(sl->help != NULL) print_help(sl->help); else print_first_lvl_help(fl); return 0; } // check number of arguments if(argc != sl->nargs) { info("Expected %u arguments, found %u\n",sl->nargs,argc); error("Wrong number of arguments\n"); return 1; } // open input if(fl->flags & NEED_INPUT) { normal("Configuring input\n"); if(infname) { info("Using %s as input file\n",infname); infile = fopen(infname,"r"); if(!infile) { warning("failed to open file %s for graph input, using stdin instead\n",infname); infile = stdin; infname = NULL; } } else infile = stdin; status = ggen_read_graph(&g,infile); if(infname) fclose(infile); if(status) { error("Failed to read graph\n"); goto free_ing; } normal("Input configured and graph read\n"); } // load rng if(fl->flags & NEED_RNG) { normal("Configuring random number generator\n"); // turn off automatic abort on gsl error gsl_set_error_handler_off(); status = ggen_rng_init(&rng); if(status) { error("Failed to initialize RNG\n"); goto free_ing; } if(rngfname) { info("Using %s as RNG state file\n",rngfname); status = ggen_rng_load(&rng,rngfname); if(status == 1) warning("RNG State file not found, will continue anyway\n"); else if(status != 0) { error("Reading RNG State from file failed.\n"); goto free_rng; } } normal("RNG configured\n"); } // set name if((fl->flags & NEED_NAME) && name == NULL) { name = "newproperty"; info("Property name needed, using %s as default\n",name); } // set type if((fl->flags & NEED_TYPE) && ptype == -1) { ptype = VERTEX_PROPERTY; info("Property type needed, using VERTEX as default\n"); } // output is a bit different from input: // a command can have its output redirected even // if it does not generate a graph // need_output tells us if a resulting graph needs // to be wrote, not if the output can be redirected normal("Configuring output\n"); if(outfname) { info("Opening %s for writing\n",outfname); outfile = fopen(outfname,"w"); if(!outfile) { warning("Failed to open file %s for output, using stdout instead\n",outfname); outfile = stdout; outfname = NULL; } } else outfile = stdout; normal("Ouput configured\n"); // launch cmd status = sl->fn(argc,argv); if(status) { error("Command Failed\n"); goto err; } if(fl->flags & NEED_OUTPUT) { normal("Printing graph\n"); if(fl->flags & IS_GRAPH_P) status = ggen_write_graph(g_p,outfile); else status = ggen_write_graph(&g,outfile); if(status) { error("Writing graph failed\n"); goto free_outg; } else normal("Graph printed\n"); } if((fl->flags & NEED_RNG) && rngfname) { normal("Saving RNG state\n"); status = ggen_rng_save(&rng,rngfname); if(status) { error("RNG saving failed\n"); } else normal("RNG Saved\n"); } free_outg: if(outfname) fclose(outfile); if(fl->flags & IS_GRAPH_P) { igraph_destroy(g_p); free(g_p); } err: free_rng: if(fl->flags & NEED_RNG) gsl_rng_free(rng); free_ing: if(fl->flags & NEED_INPUT) igraph_destroy(&g); return status; }