/* Write all FONT records. */ void wbook_store_all_fonts(struct wbookctx *wbook) { int i; struct pkt *font; struct htbl *fonts; int key; int index; font = fmt_get_font(wbook->tmp_format); for (i = 1; i < 6; i++) { bw_append(wbook->biff, font->data, font->len); } pkt_free(font); fonts = hashtbl_new(wbook->formatcount + 1); /* For tmp_format */ index = 6; /* First user defined FONT */ key = fmt_gethash(wbook->tmp_format); hashtbl_insert(fonts, key, 0); /* Index of the default font */ /* User defined fonts */ for (i = 0; i < wbook->formatcount; i++) { int data; key = fmt_gethash(wbook->formats[i]); data = hashtbl_get(fonts, key); if (data >= 0) { /* FONT has already been used */ wbook->formats[i]->font_index = data; } else { /* Add a new FONT record */ hashtbl_insert(fonts, key, index); wbook->formats[i]->font_index = index; index++; font = fmt_get_font(wbook->formats[i]); bw_append(wbook->biff, font->data, font->len); pkt_free(font); } } hashtbl_destroy(fonts); }
//checks whether edge exists and inserts if not //ie insert if profile -> origprof doesn't exist yet void check_insert_edge(FILE *fp,char *profile,char *origprof) { int k1=0,k2=0; //*f1,*f2; //double ratio; char *diff,*copy,*brac; HASHTBL *hash = NULL; /* hash = graph[k1-1]; f1 = hashtbl_get(hash,profile); hash = graph[k2-1]; f2 = hashtbl_get(hash,origprof); ratio = ((double)*f2)/((double)*f1); // printf("ratio for %s and %s is %d/%d = %.2f\n",origprof,profile,*f2,*f1,ratio); */ copy = malloc(sizeof(char)*(strlen(profile)+strlen(origprof)+4)); // if (strlen(profile)+strlen(origprof) > (ARRAYSIZE*size-4)) //copy = resize(&size,strlen(profile)+strlen(origprof)+4,copy); sprintf(copy,"%s-> %s",profile,origprof); if (hashtbl_get(edges,copy)) return; hashtbl_insert(edges,copy,"1"); if (!(hash = hashtbl_create(HASHSIZE,NULL))) { fprintf(stderr, "ERROR: hashtbl_create() failed"); exit(EXIT_FAILURE); } diff = find_diff(hash,profile,origprof,&k1,&k2); brac = edge_label(hash,profile,origprof,k2); //color=gray%.0f,100-(ratio*100); if (strlen(brac) > 1) fprintf(fp,"\"%s\"-> \"%s\" [label=\" %s\\n%s \",fontsize=8];\n",profile,origprof,brac,diff); //if (VERBOSE) //printf("inserting edge %s\n",copy); free(copy); free(diff); free(brac); hashtbl_destroy(hash); }
//input first the fasta file, then the sample_1000.out file run on the fasta, then options int main(int argc, char *argv[]) { int i,total,notcommon,most; char **mostfreq; FILE *fp; OUTPUT = DEF_OUTPUT; INPUT = NULL; NATIVE = NULL; FILTER = 0; NOISE = DEF_NOISE; MIN_HEL_LEN = DEF_MIN_HEL_LEN; // THRESH_FREQ = DEF_THRESH_FREQ; THRESH_FREQ = 0; THRESH_COMMON = DEF_THRESH_COMMON; VERBOSE = 0; NUMFREQ = 0; NUMPROF = 0; PROF_FREQ = 0; // PROF_FREQ = DEF_PROF_FREQ; NUMSTRUCTS = 0; THRESH_STRUCT = DEF_THRESH_STRUCT; LENGTH = 0; STATS = 0; GRAPH = 1; REP_STRUCT = 0; if (argc < 3) { fprintf(stderr,"Not enough arguments\n"); exit(EXIT_FAILURE); } //OUTPUT = malloc(strlen(argv[1])+5); //sprintf(OUTPUT,"%s.dot",argv[1]); for (i = 3; i < argc; i++) { //printf("argv[%d] is %s\n",i,argv[i]); if (!strcmp(argv[i],"-f")) { FILTER = 1; if ((i + 1 <= argc - 1) && sscanf(argv[i+1],"%d",&NUMFREQ)) { // sscanf(argv[i+1],"%s",val); //printf("val is %s and argv %s\n",val,argv[i+1]); NUMFREQ = atoi(argv[i+1]); i++; } else NUMFREQ = DEF_NUMFREQ; } else if (!strcmp(argv[i],"-z")) { if ((i + 1 <= argc - 1) && sscanf(argv[i+1],"%d",&NOISE)) { NOISE = atoi(argv[i+1]); i++; } } else if (!strcmp(argv[i],"-h")) { if ((i + 1 <= argc - 1) && sscanf(argv[i+1],"%f",&THRESH_FREQ)) { THRESH_FREQ = atof(argv[i+1]); if (THRESH_FREQ < 0 || THRESH_FREQ > 100) { fprintf(stderr,"Error: invalid input %f for frequency threshold\n",THRESH_FREQ); THRESH_FREQ = DEF_THRESH_FREQ; } i++; } } else if (!strcmp(argv[i],"-c")) { if ((i + 1 <= argc - 1) && sscanf(argv[i+1],"%f",&THRESH_COMMON)) { THRESH_COMMON = atof(argv[i+1]); if (THRESH_COMMON < 0.0 || THRESH_COMMON > 100.0) { fprintf(stderr,"Error: invalid input %f for common threshold\n",THRESH_COMMON); THRESH_COMMON = DEF_THRESH_COMMON; } i++; } } else if (!strcmp(argv[i],"-q")) { //PRUNE = 1; if ((i + 1 <= argc - 1) && sscanf(argv[i+1],"%d",&NUMPROF)) { NUMPROF = atoi(argv[i+1]); i++; } } else if (!strcmp(argv[i],"-p")) { if ((i + 1 <= argc - 1) && sscanf(argv[i+1],"%f",&PROF_FREQ)) { PROF_FREQ = atof(argv[i+1]); i++; } } else if (!strcmp(argv[i],"-l")) { if ((i + 1 <= argc - 1) && sscanf(argv[i+1],"%d",&MIN_HEL_LEN)) { MIN_HEL_LEN = atoi(argv[i+1]); i++; } } else if (!strcmp(argv[i],"-s")) { if ((i + 1 <= argc - 1) && sscanf(argv[i+1],"%d",&NUMSTRUCTS)) { NUMSTRUCTS = atoi(argv[i+1]); i++; } } else if (!strcmp(argv[i],"-t")) { if ((i + 1 <= argc - 1) && sscanf(argv[i+1],"%f",&THRESH_STRUCT)) { THRESH_STRUCT = atof(argv[i+1]); i++; } } else if (!strcmp(argv[i],"-o")) { if (i + 1 <= argc - 1) { OUTPUT = argv[i+1]; i++; } } else if (!strcmp(argv[i],"-i")) { if (i + 1 <= argc - 1) { INPUT = argv[i+1]; i++; } } else if (!strcmp(argv[i],"-n")) { if (i + 1 <= argc - 1) { NATIVE = argv[i+1]; i++; } } else if (!strcmp(argv[i],"-v")) VERBOSE = 1; else if (!strcmp(argv[i],"-a")) STATS = 1; else if (!strcmp(argv[i],"-g")) GRAPH = 0; else if (!strcmp(argv[i],"-r")) REP_STRUCT = 1; } if (!(bp = hashtbl_create(HASHSIZE,NULL))) { fprintf(stderr, "ERROR: hashtbl_create() for bp failed"); exit(EXIT_FAILURE); } if (!(marginals = hashtbl_create(HASHSIZE,NULL))) { fprintf(stderr, "ERROR: hashtbl_create() for marginals failed"); exit(EXIT_FAILURE); } if (!(idhash = hashtbl_create(HASHSIZE,NULL))) { fprintf(stderr, "ERROR: hashtbl_create() for idhash failed"); exit(EXIT_FAILURE); } if (!(binary = hashtbl_create(HASHSIZE,NULL))) { fprintf(stderr, "ERROR: hashtbl_create() for binary failed"); exit(EXIT_FAILURE); } total = process_structs(argv[1],argv[2]); if (THRESH_FREQ==0) THRESH_FREQ = set_h_dropoff(marginals, H_START); if (VERBOSE) { printf("Threshold to find frequent helices: %.1f\%\n",THRESH_FREQ); printf("Maximum number of frequent helices: "); if (NUMFREQ == 0) puts("no limit"); else printf("%d\n",NUMFREQ); /* printf("Threshold to select frequent profiles: %.1f\%\n",PROF_FREQ); printf("Maximum number of profiles: "); if (NUMPROF == 0) puts("no limit"); else printf("%d\n",NUMPROF); */ printf("Number of structures processed: %d\n",NUMSTRUCTS); } printf("Total number of equivalence helix classes: %d\n",total-1); if (VERBOSE) print_all_helices(total); // make_graph(marginals,max,id,total,argv[1],fp); mostfreq = find_freq(total); printf("Total number of selected helices: %d\n",hashtbl_numkeys(freq)); notcommon = make_profiles(argv[2]); printf("Total number of profiles: %d\n",hashtbl_numkeys(cluster)); print_profiles(); if (PROF_FREQ == 0) { PROF_FREQ = set_h_dropoff(cluster,P_START); if (VERBOSE) printf("setting p to %.1f\n",PROF_FREQ); } most = select_profiles(mostfreq,notcommon)-1; printf("Total number of selected profiles: %d\n",hashtbl_numkeys(cluster)); if (hashtbl_numkeys(cluster) == 0) GRAPH = 0; if (hashtbl_numkeys(cluster) > 23 && GRAPH) { GRAPH = 0; printf("Total number of profiles above threshold %.1f is %d: disabling graph\n",PROF_FREQ,hashtbl_numkeys(cluster)); } if (REP_STRUCT) { //fp = fopen("structures.out","w"); //fprintf(fp,"Processing %s\n",argv[2]); find_consensus(); print_consensus(argv[1]); //print_cluster(argv[1]); //fclose(fp); } if (GRAPH) { fp = fopen(OUTPUT,"w"); insert_graph(fp,argv[1],most); fputs("}",fp); fclose(fp); } hashtbl_destroy(bp); hashtbl_destroy(marginals); hashtbl_destroy(idhash); hashtbl_destroy(binary); hashtbl_destroy(freq); hashtbl_destroy(cluster); hashtbl_destroy(bracket); hashtbl_destroy(infreq); return 0; }
//takes input profile and checks if in graph //if so, changes node shape to hexagon //if not, insert new vertex HASHTBL* process_input_profile(FILE *fp,HASHTBL *brac,char *fullprofile, int fullnum,char *profile,int numhelix, char *diff, int prob) { HASHTBL *hash, *temp=NULL; char *diff1,*bracket,*difftrip; int *val,k1=0,k2=0; KEY *parent,*next; if (!(temp = hashtbl_create(HASHSIZE,NULL))) { fprintf(stderr, "ERROR: hashtbl_create() failed"); exit(EXIT_FAILURE); } make_bracket_rep(brac,fullprofile); hashtbl_destroy(brac); profile = sort_input(profile,numhelix); //printf("(sorted) profile is %s with fullprofile %s and diff %s\n",profile,fullprofile,diff); if ((hash = graph[numhelix-1]) && (hashtbl_get(hash,profile))) { if (numhelix == fullnum) { //printf("case 1: full profile found in graph\n"); fullprofile = sort_input(fullprofile,numhelix); } else { //cannot use find_diff because fullprofile has helices not in table[] //puts("case 2: profile found in graph"); diff = insert_diff(temp,diff); bracket = edge_label(temp,profile,fullprofile,fullnum); fprintf(fp,"\"%s\"-> \"%s\" [label =\" %s\\n%s \",fontsize=8,style = dotted];\n",profile,fullprofile,diff,bracket); } } else { /* if (numhelix == fullnum) puts("case 3: full profile not found"); else puts("case 4: profile not found"); */ for (parent = find_parents(profile); parent; parent = next) { diff1 = find_diff(temp,parent->data,profile,&k1,&k2); if (numhelix != fullnum) { difftrip = insert_diff(temp,diff); diff1 = realloc(diff1,strlen(diff1)+strlen(difftrip)+4); //printf("for parent %s, diff1 is now %s, diff is %s and difftrip is %s\n",parent->data,diff1,diff,difftrip); sprintf(diff1,"%s\\n%s",diff1,difftrip); //printf("Diff is %s for parent %s of profile %s; diff %s for %s\n",diff1,parent->data,profile,difftrip,fullprofile); } bracket = edge_label(temp,parent->data,fullprofile,fullnum); fprintf(fp,"\"%s\"-> \"%s\" [label =\" %s\\n%s \",fontsize=8,style = dotted];\n",parent->data,fullprofile,bracket,diff1); next = parent->next; free(parent); } } //printf("%s has size %d and prob %d\n",fullprofile,fullnum,prob); fprintf(fp,"\"%s\" [shape = hexagon];\n",fullprofile); val = malloc(sizeof(int)*2); val[0] = fullnum; val[1] = prob; hashtbl_insert(input,fullprofile,val); hashtbl_destroy(temp); if (!(brac = hashtbl_create(HASHSIZE,NULL))) { fprintf(stderr, "ERROR: hashtbl_create() failed"); exit(EXIT_FAILURE); } return brac; }
//processes input file containing structures of interest, in triplet form //similar code to make_cluster: converts triplet to profile, void process_input(FILE *fp) { HASHTBL *halfbrac; FILE *file; char temp[100],tmp[ARRAYSIZE],*profile,*fullprofile,*diff; int i,j,k,*id,last=0,lastprob; int numhelix = 0,fullnum = 0,size = INIT_SIZE,size2 = INIT_SIZE,size3 = INIT_SIZE; if (!(halfbrac = hashtbl_create(HASHSIZE,NULL))) { fprintf(stderr, "ERROR: hashtbl_create() for halfbrac failed"); exit(EXIT_FAILURE); } if (!(input = hashtbl_create(HASHSIZE,NULL))) { fprintf(stderr, "ERROR: hashtbl_create() for input failed"); exit(EXIT_FAILURE); } //longest = hashtbl_get(max,"longest"); profile = malloc(sizeof(char)*ARRAYSIZE*size); fullprofile = malloc(sizeof(char)*ARRAYSIZE*size2); diff = malloc(sizeof(char)*ARRAYSIZE*size3); profile[0] = '\0'; fullprofile[0] = '\0'; diff[0] = '\0'; if (!(file = fopen(INPUT,"r"))) fprintf(stderr,"Cannot open %s\n",INPUT); while (fgets(temp,100,file)) { if (sscanf(temp,"%d %d %d",&i,&j,&k) == 3) { sprintf(tmp,"%d %d",i,j); id = hashtbl_get(bp,tmp); //printf("id is %d for %d %d %d\n",id,i,j,k); if (!id) id = process_native(i,j,k); if (*id != last) { sprintf(tmp,"%d",*id); if (hashtbl_get(freq,tmp)) { //is a freq helix, save to profile numhelix++; if (strlen(profile)+strlen(tmp) > (ARRAYSIZE*size-2)) profile = resize(&size,strlen(profile)+strlen(tmp)+1,profile); //printf("adding %d to profile\n",*id); sprintf(profile,"%s%s ",profile,tmp); } else { if (strlen(diff)+strlen(tmp)+2 > ARRAYSIZE*size3) diff = resize(&size3,strlen(diff)+strlen(tmp)+2,diff); sprintf(diff,"%s%s ",diff,tmp); //printf("printing diff %s\n",diff); } fullnum++; if (strlen(fullprofile)+strlen(tmp) > (ARRAYSIZE*size-2)) fullprofile = resize(&size2,strlen(fullprofile)+strlen(tmp)+2,fullprofile); sprintf(fullprofile,"%s%s ",fullprofile,tmp); //printf("helix %d added is %s\n",fullnum,tmp); last = *id; make_brackets(halfbrac,i,j,*id); } else //if id == last printf("helix %d %d %d is duplicate with id %d: process_input()\n",i,j,k,*id); } else if (sscanf(temp,"Structure %d (%d)",&i,&j) == 2) { if (strlen(fullprofile) == 0) { lastprob = j; continue; } //printf("profile is %s, fullprofile %s with diff %s\n\n",profile,fullprofile,diff); halfbrac = process_input_profile(fp,halfbrac,fullprofile,fullnum,profile,numhelix,diff,lastprob); numhelix = 0; fullnum = 0; profile[0] = '\0'; fullprofile[0] = '\0'; diff[0] = '\0'; lastprob = j; } } //printf("input profile is %s with fullprofile %s and diff %s\n",profile,fullprofile,diff); halfbrac = process_input_profile(fp,halfbrac,fullprofile,fullnum,profile,numhelix,diff,lastprob); free(profile); hashtbl_destroy(halfbrac); //finds edges between centroids find_centroid_edges(fp); }
//process input into three profiles Hs,Hu,Hn //s = selected profiles, u = unselected in sample,n=in native only //connects them, puts Hs in cluster with freq = 0 if not already there int process_one_input(FILE *fp) { HASHTBL *halfbrac; FILE *file; char temp[100],tmp[ARRAYSIZE],*profile,*fullprofile,*diff,*native,*diffn,*dup; int i,j,k,*id,last=0,insample; int numhelix = 0,fullnum = 0,natnum = 0; int size = INIT_SIZE,size2 = INIT_SIZE,size3 = INIT_SIZE,size4 = INIT_SIZE,size5 = INIT_SIZE; if (!(halfbrac = hashtbl_create(HASHSIZE,NULL))) { fprintf(stderr, "ERROR: hashtbl_create() for halfbrac failed"); exit(EXIT_FAILURE); } if (!(input = hashtbl_create(HASHSIZE,NULL))) { fprintf(stderr, "ERROR: hashtbl_create() for input failed"); exit(EXIT_FAILURE); } //longest = hashtbl_get(max,"longest"); profile = malloc(sizeof(char)*ARRAYSIZE*size); fullprofile = malloc(sizeof(char)*ARRAYSIZE*size2); diff = malloc(sizeof(char)*ARRAYSIZE*size3); native = malloc(sizeof(char)*ARRAYSIZE*size4); diffn = malloc(sizeof(char)*ARRAYSIZE*size5); profile[0] = '\0'; fullprofile[0] = '\0'; diff[0] = '\0'; native[0] = '\0'; diffn[0] = '\0'; if (!(file = fopen(INPUT,"r"))) fprintf(stderr,"Cannot open %s\n",INPUT); while (fgets(temp,100,file)) { // if (sscanf(temp,"Structure %d (%d)",&i,&prob) == 2) if (sscanf(temp,"%d %d %d",&i,&j,&k) == 3) { insample = 1; sprintf(tmp,"%d %d",i,j); id = hashtbl_get(bp,tmp); if (!id) { id = process_native(i,j,k); //printf("number in marginals %d\n",hashtbl_numkeys(marginals)); if (*id > hashtbl_numkeys(marginals)) insample = 0; } if (*id != last) { sprintf(tmp,"%d",*id); if (strlen(native)+strlen(tmp) > (ARRAYSIZE*size4-2)) native = realloc(native,ARRAYSIZE*(++size4)); sprintf(native,"%s%s ",native,tmp); natnum++; if (insample) { fullnum++; if (strlen(fullprofile)+strlen(tmp) > (ARRAYSIZE*size2-2)) fullprofile = realloc(fullprofile,ARRAYSIZE*(++size2)); sprintf(fullprofile,"%s%s ",fullprofile,tmp); if (hashtbl_get(freq,tmp)) { //is a freq helix, save to profile numhelix++; if (strlen(profile)+strlen(tmp) > (ARRAYSIZE*size-2)) profile = realloc(profile,ARRAYSIZE*(++size)); //printf("adding %d to profile\n",*id); sprintf(profile,"%s%s ",profile,tmp); } else { //if not freq record diff if (strlen(diff)+strlen(tmp)+2 > ARRAYSIZE*size3) diff = realloc(diff,ARRAYSIZE*(++size3)); sprintf(diff,"%s%s ",diff,tmp); //printf("printing diff %s\n",diff); } } else { if (strlen(diffn)+strlen(tmp)+2 > ARRAYSIZE*size5) diffn = realloc(diffn,ARRAYSIZE*(++size5)); sprintf(diffn,"%s%s ",diffn,tmp); } last = *id; make_brackets(halfbrac,i,j,*id); } else if (VERBOSE) printf("helix %d %d %d is duplicate with id %d: process_input()\n",i,j,k,*id); } } native = sort_input(native,natnum); //printf("native is now %s\n",native); make_bracket_rep(halfbrac,native); hashtbl_destroy(halfbrac); fullprofile = sort_input(fullprofile,fullnum); profile = sort_input(profile,numhelix); //printf("native %s, fullprofile %s, profile %s, diff %s, diffn %s\n",native,fullprofile,profile,diff,diffn); if (fullnum != natnum) make_edge_and_node(fp,fullprofile,native,diffn,natnum); if (numhelix != fullnum) make_edge_and_node(fp,profile,fullprofile,diff,fullnum); fprintf(fp,"\"%s\" [style = filled, fillcolor = gray60];\n",profile); sprintf(tmp,"%d ",numhelix); if (strlen(tmp)+strlen(profile) > ARRAYSIZE*size+1) profile = realloc(profile,ARRAYSIZE*(++size)); dup = mystrdup(profile); sprintf(profile,"%s%s",tmp,dup); id = hashtbl_get(cluster,profile); free(dup); if (!id) { id = malloc(sizeof(int)); *id = 0; hashtbl_insert(cluster,profile,id); //printf("inserting input %s into cluster\n",profile); } free(profile); return numhelix; }
//runs through nodes in cluster, finding their LCA's int insert_graph(FILE *fp,char *file,int gsize) { int i,*freq,k = 0,numkeys,total; char *profile; KEY *node; struct hashnode_s *begin; if (!(edges = hashtbl_create(HASHSIZE,NULL))) { fprintf(stderr, "ERROR: hashtbl_create() failed"); exit(EXIT_FAILURE); } fputs("digraph G {\n",fp); fprintf(fp,"\tlabel = \"%s\";\n",file); fprintf(fp,"\tpad = 0.5;\n"); fprintf(fp,"\tnodesep = 0.5;\n"); fprintf(fp,"\"\" [label=\"(0/%d)\"]\n",NUMSTRUCTS); //fputs("{ node [shape = plaintext]; ",fp); graphsize = gsize; if (INPUT) { i = process_one_input(fp); if (i > gsize) graphsize = i; } //printf("graphsize is %d for %s\n",*count,node->data); graph = malloc(sizeof(HASHTBL*)*(graphsize+1)); for (i = 0; i <= graphsize; i++) graph[i] = NULL; /*print ranks for (i = 0; i < *count; i++) { fprintf(fp,"%d",i+1); if (i == *count-1) fprintf(fp,"; }\n"); else fprintf(fp,"->"); } */ numkeys = hashtbl_numkeys(cluster); sums = malloc(sizeof(long)*numkeys); k = numkeys-1; profileID = malloc(sizeof(char*)*numkeys); modprofileID = malloc(sizeof(char*)*numkeys); most = 0; //for each profile, insert with freq for (node = hashtbl_getkeys(cluster); node; node = node->next) { //printf("node data is %s with k = %d\n",node->data,k); freq = hashtbl_get(cluster,node->data); //need to insert into graph if (freq) { if (most < *freq) most = *freq; profile = malloc(strlen(node->data)+1); sums[k] = insert_and_binary(node->data,profile,*freq); profileID[k] = node->data; modprofileID[k--] = profile; if (*freq > 0) fprintf(fp,"\"%s\" [shape = box];\n",profile); } else fprintf(stderr,"No entry for %s\n",node->data); //printf("for profile %s binary rep is %d with ID %d\n",node->data,sums[k+1],k+1); } make_key(); begin = insert_LCAs(fp,numkeys); //printf("inserted LCA's with numkeys %d\n",numkeys); add_infreq(begin); find_LCA_edges(fp,begin,numkeys); total = print_vertices(fp); printf("Total number of vertices: %d\n",total); if (INPUT) hashtbl_destroy(input); for (i = 0; i < graphsize; i++) { if (graph[i]) hashtbl_destroy(graph[i]); } free(sums); free(table); free(profileID); free(modprofileID); hashtbl_destroy(edges); return 0; }
int oph_server_conf_load(short unsigned int instance, HASHTBL **hashtbl) { if(!hashtbl){ pmesg(LOG_ERROR, __FILE__, __LINE__, "Null input parameter\n"); return OPH_SERVER_CONF_NULL_PARAM; } if( !(*hashtbl = hashtbl_create(OPH_SERVER_CONF_LOAD_SIZE, NULL)) ){ pmesg(LOG_ERROR, __FILE__, __LINE__, "Unable to create hash table\n"); return OPH_SERVER_CONF_ERROR; } FILE *file = fopen(OPH_SERVER_CONF_FILE_PATH, "r"); if(file == NULL) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Configuration file not found\n"); hashtbl_destroy(*hashtbl); *hashtbl = NULL; return OPH_SERVER_CONF_ERROR; } else { char buffer[OPH_SERVER_CONF_LINE_LEN] = {0}; char tmp_buffer[OPH_SERVER_CONF_LINE_LEN] = {0}; char *value = NULL; char *position = NULL, *position2 = NULL; const char *param_iter = NULL; int i = 0, j = 0; short int instance_flag = 0; char *instance_string = 0; short unsigned int instance_number = 0; while(!feof(file)) { //Retrieve instance header memset(tmp_buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer)); if (!fgets (buffer, sizeof(buffer), file)) { if(feof(file)){ fclose (file); return OPH_SERVER_CONF_SUCCESS; } else{ fclose (file); pmesg(LOG_ERROR, __FILE__, __LINE__, "Error while reading configuration file\n"); hashtbl_destroy(*hashtbl); return OPH_SERVER_CONF_ERROR; } } //Check if row is a comment j = 0; while(buffer[j] == ' ') j++; //Row is a comment if(buffer[j] == OPH_SERVER_CONF_COMMENT) continue; //Check if row is newline j = 0; while(buffer[j] == ' ') j++; //Row is a newline if(buffer[j] == '\n') continue; sscanf (buffer, "[%[^]]", tmp_buffer); if(instance_flag == 0){ //If row does not contain an instance ref then step to next row if (!(instance_string = strstr (tmp_buffer, OPH_SERVER_CONF_INSTANCE))){ continue; } else{ //Check instance number if(instance != 0){ instance_string += strlen(OPH_SERVER_CONF_INSTANCE); instance_number = (short unsigned int)strtol(instance_string, NULL, 10); if(instance_number == 0) continue; if(instance_number != instance) continue; } //If instance header is found read all lines until next one instance_flag = 1; } } else{ if (!strstr (tmp_buffer, OPH_SERVER_CONF_INSTANCE)){ //Parse row //Get param-value separator value = NULL; position = position2 = NULL; position = strchr(buffer, '='); if(position != NULL){ //Set = symbol to NULL position[0] = 0; position++; //If new line char is read, then remove it if((position2 = strchr(position, '\n')) != NULL){ position2[0] = 0; } //Check if param is allowed i = 0; param_iter = oph_server_conf_params[i++]; while(param_iter){ if(STRCMP(buffer, param_iter) == 0) break; param_iter = oph_server_conf_params[i++]; } if( param_iter == NULL ) { pmesg(LOG_DEBUG, __FILE__, __LINE__, "Configuration param not allowed %s\n", buffer); continue; } //Alloc value to be added to hash table value = strndup(position, OPH_SERVER_CONF_LINE_LEN); if(value == NULL){ pmesg(LOG_ERROR, __FILE__, __LINE__, "Error while copying param %s\n", buffer); fclose(file); hashtbl_destroy(*hashtbl); *hashtbl = NULL; return OPH_SERVER_CONF_ERROR; } if(hashtbl_insert(*hashtbl, buffer, value)){ pmesg(LOG_ERROR, __FILE__, __LINE__, "Error while loading param %s\n", buffer); fclose(file); free(value); hashtbl_destroy(*hashtbl); *hashtbl = NULL; return OPH_SERVER_CONF_ERROR; } pmesg(LOG_DEBUG, __FILE__, __LINE__, "LOADED PARAM: %s = %s \n", buffer, position); } else{ pmesg(LOG_DEBUG, __FILE__, __LINE__, "Configuration param corrupted %s\n", buffer); } } else{ //Found second instance ref, end cycle break; } } } fclose(file); } return OPH_SERVER_CONF_SUCCESS; }
/* * Function: * hash_create * Arguments: * (htbl) Pointer to a pointer to a hash_table structure. * Purpose: * This function is our most important function. From this * hash table, all other searches depend. This function will * grab all the definitions, parse them, also each functions * description, AND what it returns. It will build a new * 'chain_node' for each, and add it to it's proper 'bucket' * in our hash table. * Returns: * -1 - On error, call err_GetLastError for more information. * 0 - If everything went planned. */ int hashtbl_create( hashtbl **htbl ) { chain_node *tmp; char definition[CORE_MAX_DEF] = {0}; char description[CORE_MAX_DESC] = {0}; char returns[CORE_MAX_RET] = {0}; /* If this is still 0 they didn't call: core_start(); big no-no!!! */ if( !core_startup ) { err_setError( FUNC_HASHTBL_CREATE, FILE_HASH_C, ERR_CORE_STARTUP, __LINE__ ); return -1; } if( !htbl ) { err_setError( FUNC_HASHTBL_CREATE, FILE_HASH_C, ERR_NULL_PARAM, __LINE__ ); return -1; } *htbl = malloc( sizeof(**htbl) ); if( *htbl == NULL ) { err_setError( FUNC_HASHTBL_CREATE, FILE_HASH_C, ERR_MALLOC, __LINE__ ); return -1; } memset( *htbl, 0, sizeof(**htbl) ); /* If you mess w/ my data files you'll f**k this all up. Leave 'em alone */ while( fgets(definition, sizeof definition, def) != NULL && fgets(description, sizeof description, desc) != NULL && fgets(returns, sizeof returns, ret) != NULL ) { tmp = malloc( sizeof(*tmp) ); if( tmp == NULL ) goto error1; memset( tmp, 0, sizeof(*tmp) ); core_convertNewLines( description ); core_convertNewLines( returns ); tmp->name = malloc( (strlen(definition) + 1) * sizeof(*tmp->name) ); if( tmp->name == NULL ) { free( tmp ); goto error1; } tmp->description = malloc( (strlen(description) + 1) * sizeof(*tmp->description) ); if( tmp->description == NULL ) goto error; tmp->returns = malloc( (strlen(returns) + 1) * sizeof(*tmp->returns) ); if( tmp->returns == NULL ) goto error; strncpy( tmp->name, definition, strlen(definition) + 1 ); strncpy( tmp->description, description, strlen(description) + 1 ); strncpy( tmp->returns, returns, strlen(returns) + 1 ); if( core_parseDefinition(tmp->name, &tmp->name, &tmp->section, &tmp->prototype) ) goto error; core_convertNewLines( tmp->prototype ); if( hashtbl_add(*htbl, tmp) ) goto error; } return 0; error: if( tmp->description ) free( tmp->description ); if( tmp->returns ) free( tmp->returns ); if( tmp->name ) free( tmp->name ); free( tmp ); error1: hashtbl_destroy( htbl ); return -1; }
void activation_destroy(activation* act) { assert(act != NULL); hashtbl_destroy(act->env); free(act->env); }
//Procedure OPH_IO_SERVER_PROCEDURE_SUBSET int oph_io_server_run_subset_procedure(oph_metadb_db_row ** meta_db, oph_iostore_handler * dev_handle, oph_io_server_thread_status * thread_status, oph_query_arg ** args, HASHTBL * query_args) { if (!query_args || !dev_handle || !thread_status || !meta_db) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_NULL_INPUT_PARAM); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_NULL_INPUT_PARAM); return OPH_IO_SERVER_NULL_PARAM; } //Check if current DB is setted //TODO Improve how current DB is found if (thread_status->current_db == NULL || thread_status->device == NULL) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_NO_DB_SELECTED); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_NO_DB_SELECTED); return OPH_IO_SERVER_METADB_ERROR; } //Fetch function arguments char *function_args = hashtbl_get(query_args, OPH_QUERY_ENGINE_LANG_ARG_ARG); if (function_args == NULL) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MISSING_QUERY_ARGUMENT, OPH_QUERY_ENGINE_LANG_ARG_ARG); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MISSING_QUERY_ARGUMENT, OPH_QUERY_ENGINE_LANG_ARG_ARG); return OPH_IO_SERVER_EXEC_ERROR; } //Length used for new query unsigned int query_len = strlen(function_args) + 200; char **func_args_list = NULL; int func_args_num = 0; if (oph_query_parse_multivalue_arg(function_args, &func_args_list, &func_args_num)) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_MULTIVAL_PARSE_ERROR, OPH_QUERY_ENGINE_LANG_ARG_ARG); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_MULTIVAL_PARSE_ERROR, OPH_QUERY_ENGINE_LANG_ARG_ARG); return OPH_IO_SERVER_EXEC_ERROR; } //Check number of arguments if (func_args_num < 4 || func_args_num > 5) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_WRONG_PROCEDURE_ARG, OPH_IO_SERVER_PROCEDURE_SUBSET); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_WRONG_PROCEDURE_ARG, OPH_IO_SERVER_PROCEDURE_SUBSET); free(func_args_list); return OPH_IO_SERVER_EXEC_ERROR; } //Argument 0 should be a string with optionally a hierarchical name (a.b) //Remove leading/trailing spaces and match string if (oph_query_check_procedure_string(&(func_args_list[0]))) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_ARG_NO_STRING, func_args_list[0]); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_ARG_NO_STRING, func_args_list[0]); free(func_args_list); return OPH_IO_SERVER_EXEC_ERROR; } //Extract hierachical info char **in_frag_components = NULL; int in_frag_components_num = 0; if (oph_query_parse_hierarchical_args(func_args_list[0], &in_frag_components, &in_frag_components_num)) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_HIERARCHY_PARSE_ERROR, func_args_list[0]); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_HIERARCHY_PARSE_ERROR, func_args_list[0]); free(func_args_list); return OPH_IO_SERVER_PARSE_ERROR; } char *in_frag_name = NULL; //If DB is setted in frag name if (in_frag_components_num > 1) { //Check if db is the one used by the query if (STRCMP(thread_status->current_db, in_frag_components[0]) != 0) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_WRONG_DB_SELECTED); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_WRONG_DB_SELECTED); free(in_frag_components); free(func_args_list); return OPH_IO_SERVER_METADB_ERROR; } in_frag_name = in_frag_components[1]; } else { in_frag_name = in_frag_components[0]; } //Argument 1 should be a number long long id_start = 0; //Test number presence char *end = NULL; errno = 0; id_start = strtoll((char *) func_args_list[1], &end, 10); if ((errno != 0) || (end == (char *) func_args_list[1]) || (*end != 0)) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_ARG_NO_LONG, func_args_list[1]); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_ARG_NO_LONG, func_args_list[1]); free(in_frag_components); free(func_args_list); return OPH_IO_SERVER_EXEC_ERROR; } //Argument 2 should be a string //Remove leading/trailing spaces and match string if (oph_query_check_procedure_string(&(func_args_list[2]))) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_ARG_NO_STRING, func_args_list[2]); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_ARG_NO_STRING, func_args_list[2]); free(in_frag_components); free(func_args_list); return OPH_IO_SERVER_EXEC_ERROR; } //Argument 3 should be a string with optionally a hierarchical name (a.b) //Remove leading/trailing spaces and match string if (oph_query_check_procedure_string(&(func_args_list[3]))) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_ARG_NO_STRING, func_args_list[3]); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_ARG_NO_STRING, func_args_list[3]); free(in_frag_components); free(func_args_list); return OPH_IO_SERVER_EXEC_ERROR; } //Extract hierachical info char **out_frag_components = NULL; int out_frag_components_num = 0; if (oph_query_parse_hierarchical_args(func_args_list[3], &out_frag_components, &out_frag_components_num)) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_HIERARCHY_PARSE_ERROR, func_args_list[3]); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_HIERARCHY_PARSE_ERROR, func_args_list[3]); free(in_frag_components); free(func_args_list); return OPH_IO_SERVER_PARSE_ERROR; } char *out_frag_name = NULL; //If DB is setted in frag name if (out_frag_components_num > 1) { //Check if db is the one used by the query if (STRCMP(thread_status->current_db, out_frag_components[0]) != 0) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_WRONG_DB_SELECTED); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_WRONG_DB_SELECTED); free(in_frag_components); free(out_frag_components); free(func_args_list); return OPH_IO_SERVER_METADB_ERROR; } out_frag_name = out_frag_components[1]; } else { out_frag_name = out_frag_components[0]; } //Argument 4, if available, should be a string char where_flag = 0; if (func_args_num == 5) { //Remove leading/trailing spaces and match string if (oph_query_check_procedure_string(&(func_args_list[4]))) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_ARG_NO_STRING, func_args_list[4]); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_ARG_NO_STRING, func_args_list[4]); free(in_frag_components); free(out_frag_components); free(func_args_list); return OPH_IO_SERVER_EXEC_ERROR; } //If arg list 4 is set, then activate where flag where_flag = ((func_args_list[4])[0] != 0); } char new_query[query_len]; //Check if where is enabled if (where_flag) { snprintf(new_query, query_len - 1, "operation=create_frag_select;frag_name=%s;field=id_dim|%s;select_alias=|measure;from=%s;where=%s;sequential_id=%lld;", out_frag_name, func_args_list[2], in_frag_name, func_args_list[4], id_start); } else { snprintf(new_query, query_len - 1, "operation=create_frag_select;frag_name=%s;field=id_dim|%s;select_alias=|measure;from=%s;", out_frag_name, func_args_list[2], in_frag_name); } free(in_frag_components); free(out_frag_components); free(func_args_list); //Parse internal query HASHTBL *procedure_query_args = NULL; if (oph_query_parser(new_query, &procedure_query_args)) { pmesg(LOG_WARNING, __FILE__, __LINE__, "Unable to run query\n"); logging(LOG_WARNING, __FILE__, __LINE__, "Unable to run query\n"); } //Run create as select block if (oph_io_server_run_create_as_select_table(meta_db, dev_handle, thread_status->current_db, args, procedure_query_args)) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_DISPATCH_ERROR, "Create as Select"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_DISPATCH_ERROR, "Create as Select"); hashtbl_destroy(procedure_query_args); return OPH_IO_SERVER_EXEC_ERROR; } hashtbl_destroy(procedure_query_args); return OPH_IO_SERVER_SUCCESS; }