📄 xchartprocs.c
字号:
Wobject *get_wobject(ob) Objects *ob;{ Wobject *wo = charts; while(wo) { if(wo->obj == ob) return(wo); wo = wo->next; } return(NULL);}/**********************************************************************/Chartfile *new_chartfile(sig_name, filename) char *sig_name, *filename;{ if(sig_name && *sig_name && filename && *filename) { Chartfile *cf = (Chartfile*)malloc(sizeof(Chartfile)); char *name; extern char fontfile[]; Pixfont *pf, *open_label_font(), *xv_pf_default(); if(cf && (name = (char*)malloc(strlen(sig_name)+1))) { strcpy(name,sig_name); cf->sig_name = name; cf->chart_name = name = malloc(strlen(filename)+1); strcpy(name,filename); cf->type = 1; cf->eventx = 0; cf->eventy = 0; cf->color = TEXT_COLOR; cf->changed = 0; if(*fontfile && (pf = open_label_font(fontfile))) cf->font = pf; else cf->font = xv_pf_default(); cf->first_word = NULL; cf->next = NULL; return(cf); } else printf("Allocation failure in new_chartfile()\n"); return(NULL); }}/**********************************************************************/Chartfile *get_chartfile(ob,chart_file_name) Wobject *ob; char *chart_file_name;{ if(ob && chart_file_name && *chart_file_name) { Chartfile *cp = ob->charts; while(cp) { if(!strcmp(chart_file_name,cp->chart_name)) return(cp); cp = cp->next; } } return(NULL);}/**********************************************************************/Hit *new_hit(start,end,like) double start, end, like;{ Hit *h; if((h = (Hit*)malloc(sizeof(Hit)))) { h->start = start; h->end = end; h->like = like; h->next = NULL; return(h); } else printf("Allocation failure in new_hit()\n"); return(NULL);}/**********************************************************************/Word *new_word(prev,start,end,like,wrd) Word *prev; double start, end, like; char *wrd;{ Word *w; if((w = (Word*)malloc(sizeof(Word))) && (w->hits = new_hit(start,end,like))) { if(wrd && *wrd) { w->str = (char*)malloc(strlen(wrd)+1); strcpy(w->str,wrd); } else w->str = NULL; w->hits->next = NULL; w->next = NULL; w->plotted = FALSE; w->prev = prev; if(prev) prev->next = w; w->color = -1; return(w); } else printf("Allocation problems in new_word()\n"); return(NULL);}/**********************************************************************//* If w->str is unique, link w into the Chartfile, else link w->hitsinto the duplicate Word entry in Chartfile and then free w. Occurrencesof a word are stored sorted by increasing time. */append_word(w,cf) Word *w; Chartfile *cf;{ if(w && cf && w->hits) { register Word *wp; if((wp = cf->first_word)) { register Hit *h, *hn = w->hits; do { if(!strcmp(wp->str,w->str)) { h = wp->hits; if(h) { if(hn->end <= h->start) { hn->next = h; wp->hits = hn; } else while(h) { if((! h->next) || (h->next->start >= hn->end)) { hn->next = h->next; h->next = hn; break; } h = h->next; } } else wp->hits = hn; free(w); return(TRUE); } wp = wp->next; } while(wp != cf->first_word); /* This was a unique new word; add to list. */ cf->first_word->prev->next = w; w->prev = cf->first_word->prev; cf->first_word->prev = w; w->next = cf->first_word; /* CIRCULAR LIST */ } else { /* it's the first entry */ cf->first_word = w; w->next = w; w->prev = w; } return(TRUE); } else printf("Bogus arguments to append_word()\n"); return(FALSE);}/**********************************************************************//* Create a chartfile structure. If the named file exists, read it in and use its contents to fill a structure. */Chartfile *read_charts(ob, chart_file_name) char *chart_file_name; Wobject *ob;{ static int type, nfields, color; static double freq; static char signal[150]; static char comment[MES_BUF_SIZE], font[NAMELEN]; static Selector a2 = {"type", "%d", (char*)&type, NULL}, a3 = {"nfields", "%d", (char*)&nfields, &a2}, a4 = {"signal", "%s", signal, &a3}, a5b = {"color","%d", (char*)&color, &a4}, a5 = {"frequency","%lf", (char*)&freq, &a5b}, a6 = {"font", "%s", font, &a5}, a7 = {"comment", "#str", comment, &a6}; FILE *fopen(), *fp; Chartfile *cf; Pixfont *pf, *xv_pf_open(); if(!ob || !ob->obj) return(NULL); type = 1; nfields = 1; freq = 8000.0; color = -1; *signal = 0; *comment = 0; *font = 0; if((cf = (Chartfile*)get_chartfile(ob,chart_file_name))) { printf("Chart file %s exists(%s %s); request to recreate ignored.\n", chart_file_name,ob->obj->name,cf->chart_name); return(NULL); } if((cf = new_chartfile(ob->obj->name, chart_file_name))) { if((fp = fopen(cf->chart_name,"r"))) { char wrd[200]; int id, nread; double start, end, like; Word *w; char in[MES_BUF_SIZE]; while(fgets(in,MES_BUF_SIZE,fp)) { /* read chart file header */ if(*in == '#') break; get_args(in,&a7); } if(color >= 0) cf->color = color; cf->type = type; cf->freq = freq; if(*font) { if(!(pf = xv_pf_open(font))) printf("Can't open fontfile %s; using default\n",font); else cf->font = pf; } if(*comment) { cf->comment = malloc(strlen(comment)+1); strcpy(cf->comment, comment); } else cf->comment = NULL; /* A typical line in the chart might look like this: 11 1.3 1.7 .2 foobar */ while(fgets(in,MES_BUF_SIZE,fp)) { /* read all words */ if((nread =sscanf(in,"%d%lf%lf%lf%s", &id, &start, &end, &like, wrd)) == 5) { if(cf->type == 2) { /* convert samples to time */ start /= cf->freq; end /= cf->freq; } if(! (w = new_word(NULL,start,end,like,wrd))) { printf("Can't create a new word; chart MUST EXIT!\n"); fclose(fp); return(NULL); } append_word(w,cf); } else { if(nread > 0) fprintf(stderr,"Bad line encountered in chart file %s: %s", chart_file_name, in); } } cf->changed = 0; fclose(fp); } else { printf("Can't open chart file %s for reading.\n",cf->chart_name); return(NULL); } if((!cf->first_word)) { free_chartfile(cf); printf("Empty chartfile (%s)\n",chart_file_name); return(NULL); } cf->next = ob->charts; /* link in the new chartfile */ ob->charts = cf; cf->obj = ob; return(cf); } else printf("Can't create new_chartfile(%s,%s)\n", ob->obj->name,chart_file_name); return(NULL);}/*********************************************************************/plot_chart(cf) Chartfile *cf;{ Pixwin *pw; Pixfont *pf; Rect *rec; Objects *ob; Word *word; Hit *hit; Wobject *wob; Chartfile *cf2; double end; int x, y, n, top, bot, height, xl, yl, label_size, yoffset; int ystep, ymin, ymax, yspace, width, xc, xw, xh; if(!((wob = cf->obj) && (ob = wob->obj) && (cf2 = wob->charts))) { printf("Bad Chartfile passed to plot_chart()\n"); return(FALSE); } if(ob->labels) { /* plotting labels too? */ label_size = wob->label_size; yoffset = (wob->label_loc)? 0 : label_size; } else label_size = yoffset = 0; /* Find total number of charts to plot. */ n = 1; x = 1; while((cf2 = cf2->next)) { n++; if(cf2 == cf) x = n; } pw = canvas_pixwin(ob->canvas); rec = (Rect*)xv_get(ob->canvas, WIN_RECT); height = (rec->r_height - label_size)/n; /* height allocated to each chart */ top = (x-1)*height + yoffset; bot = top + height; end = ob->start + ((ob->sec_cm * rec->r_width) /PIX_PER_CM); pw_rop(pw,0,top,rec->r_width,height, PIX_SRC|PIX_COLOR(0),NULL,0,0); pw_vector(pw, 0, top, rec->r_width, top, PIX_SRC, cf->color); if((word = cf->first_word)) { if((pf = cf->font)) { yspace = (int)(xv_get(pf,FONT_DEFAULT_CHAR_HEIGHT) * 0.2); ystep = ymin = xv_get(pf,FONT_DEFAULT_CHAR_HEIGHT) + yspace; ymax = bot - ystep; } else { fprintf(stderr,"plot_chart: cannot get font\n"); return(FALSE); } width = xv_get(pf,FONT_DEFAULT_CHAR_WIDTH); y = top + yspace*0.5 + xv_get(pf,FONT_DEFAULT_CHAR_HEIGHT); do { hit = word->hits; word->plotted = 0; while(hit) { if((hit->end >= ob->start) && (hit->start <= end)) { if(y < bot) { xl = time_to_x(ob,hit->start); /* left marker */ xh = time_to_x(ob,hit->end); /* right marker */ xc = (xh+xl)/2; /* word center */ x = xc - ((xw = strlen(word->str) * width)/2); /* text left end */ yl = y-ystep; pw_vector(pw, xl, yl, xl, y, PIX_SRC, word->color); pw_vector(pw, xh, yl, xh, y, PIX_SRC, word->color); yl = y - (ystep/2); pw_vector(pw, xl, yl, x, yl, PIX_SRC, word->color); pw_vector(pw, x+xw, yl, xh, yl, PIX_SRC, word->color); pw_text(pw,x,y-yspace,PIX_COLOR(cf->color)|PIX_SRC,pf,word->str); word->plotted = 2; } else word->plotted = 1; /* plottable, but no room */ } hit = hit->next; } if(word->plotted) /* Only inc. y pos. if something was plotted. */ y += ystep; word = word->next; } while(word != cf->first_word); return(TRUE); }}/*********************************************************************/do_chart_menu(cf, event, arg) Chartfile *cf; Event *event; caddr_t arg;{ Objects *ob; char *str, *c; Menu men; Wobject *w; if(cf && (w = cf->obj) && (ob = w->obj) && (men = w->menu)) { cf->eventx = ob->oldcursor_x; cf->eventy = ob->oldcursor_y; remove_cursor(ob->canvas,ob); xv_set(men, MENU_CLIENT_DATA, cf, 0); menu_show(men,ob->canvas,event,NULL); return(TRUE); } else printf("Bad Chartfile passed to do_chart_menu()\n"); return(FALSE);}/*********************************************************************/voidchart_menu_proc(men, item) Menu men; Menu_item item;{ Chartfile *cf; Wobject *w; Objects *ob; if(men && (cf = (Chartfile*)xv_get(men, MENU_CLIENT_DATA)) && (w = cf->obj) && (ob = w->obj)) { char *str, *c; if((str = (char*)xv_get(item,MENU_VALUE))) { if(!strcmp(str,"*SHELL*")) { /* Fork a process? */ Word *wrd; char *cp; Menu_item item; char command[MES_BUF_SIZE], nam[50]; FILE *ft, *fopen(); /* Whatever process gets forked is called with 4 args: <chartname> <word> <time> <output>, where <output> may optionally be used to store results that may then be read as another chartfile. If <output> actually gets created, it is read and displayed. */ if((wrd = get_word_level(cf,cf->eventy))) cp = wrd->str; else cp = "XYZZY"; item = menu_find(men, MENU_VALUE, str, 0); c = (char*)xv_get(item, MENU_STRING); sprintf(nam,"/tmp/chartXXXXXX"); mktemp(nam); sprintf(command,"%s %s %s %f %s \n",c,cf->chart_name,cp, x_to_time(ob,cf->eventx),nam); system(command); if((ft = fopen(nam,"r"))) { /* Was output created? */ extern Panel_item chart_item; extern char chartname[]; fclose(ft); strcpy(chartname,nam); xv_set(chart_item, PANEL_VALUE, chartname,0); newFile(chart_item,NULL); } return; } if(!strcmp(str,"*UP*")) /* Scroll up. */ scroll(w,x_to_time(ob,cf->eventx),cf->eventy,1); if(!strcmp(str,"*DOWN*")) /* Scroll down. */ scroll(w,x_to_time(ob,cf->eventx),cf->eventy,0); if(!strcmp(str,"*UNLOAD*")) { /* remove the chart file? */ if(kill_chartfile(cf->chart_name,ob)) { set_display_size(ob); redo_display(ob); } return; } } } else printf("Bad arguments to chart_menu_proc()\n"); return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -