📄 xample.c
字号:
/* This routine might clean up any displays, deallocate storage, or whever, in preparation to the program exiting. After everything else is done, the terminate_communication() message must be sent. *//*********************************************************************/cleanup(){ /* Unregister this program as an RPC server. */ terminate_communication(mynum, myver);}/* This is called by xwaves when the "detach" command is given. *//*********************************************************************/char *meth_quit(ob, args) Objects *ob; char *args;{ cleanup(); exit(0);}/* This is a bare bones example of new object creation. When a "make" command is sent from xwaves, a routine like the following does the work. *//**********************************************************************/char *meth_make_object(ob,args) Objects *ob; char *args;{ Objects *ob2, *new_objects(); extern Objects *objlist, *new_objects(); extern Methods base_methods; char mes[MES_BUF_SIZE]; *objectname = *file = *signame = 0; if(debug_level) fprintf(stderr,"meth_make_object(%s)\n",(args)? args : "NULL"); if(get_args(args,&a0) && *objectname) { if(! (ob2 = get_receiver(objectname))) { /* does it already exist? */ if((ob2 = new_objects(objectname))) { /* create one */ ob2->next = objlist; /* link it into the master list */ objlist = ob2; /* Here you would typically do something profound, like generate a display. */ return(ok); } else { /* couldn't allocate a new_object() */ fprintf(stderr,"meth_make_object: Allocation failure in new_object()\n."); return(null); } } /* completed new Objects creation */ return(ok); /* (already existed) */ } return(null);}/* This gets called by xwaves whenever an xwaves data display gets changed, or redrawn. Programs that need to keep visually synchronized with xwaves handle it here. *//*********************************************************************/char *meth_redisplay(ob,str) Objects *ob; char *str;{ if(ob && str) { if(debug_level) fprintf(stderr,"meth_redisplay(%s)\n",(str)? str : "NULL"); return(ok); } else fprintf(stderr,"Null object or arg list to meth_redisplay()\n"); return(null);}/* This method is provided for asynchronous passing of return values from xwaves. In this program example, it would not actually ever get called. The str argument contains a character string, the first word of which is a number indicating the ID of the callback. The remainder of the string is passed as an argument to the callback. *//***********************************************************************/char *meth_return(ob,str) Object *ob; char *str;{ if(str && *str) { int id; char *get_next_item(); sscanf(str,"%d",&id); do_return_callback(id, get_next_item(str)); if(debug_level) fprintf(stderr,"meth_return(%s)\n",str); return(ok); } else fprintf(stderr,"Meth_return: bad args\n"); return(null);}/* Here is an example of how an xwaves command that can result in a call to meth_return might be set up. This example is taken from xlabel. When xwaves tells xlabel to refresh the display, xlabel needs to ask xwaves about the location, size and scaling of the associated display./*/*********************************************************************/get_display_attributes(ob) Objects *ob;{ char mes[MES_BUF_SIZE]; int n, id; if(ob && ob->name) { id = set_return_value_callback(meth_redisplay,ob); /* In this example, when do_return_callback() is eventually called, meth_redisplay will be called with two arguments: ob and the second argument passed to do_return_callback. The unique id returned by set_return_value_callback(), keys the query and response together. */ sprintf(mes,"%s get attributes display function %s file %s return_id %d\n", ob->name, thisprog, ob->signal_name, id); /* This call to xwaves eventually results in xwaves responding with a "completion" command, thus calling meth_return above. */ mess_write(mes); return(TRUE); } return(FALSE);}/*********************************************************************//* Here are two "methods lists". These associate a command name with a procedure call. The first list consists of those commands that may only be directed at a named display ensemble object. *//*********************************************************************/Methods meth2 = {"redisplay", meth_redisplay,NULL}, meth1 = { "mark", meth_mark, &meth2};/* This second list defines those commands that may only be directed at the program itself (the so-called base methods). */Methods bm8 = {"completion", meth_return, NULL}, bm5 = {"kill", meth_kill, &bm8}, bm2 = {"quit", meth_quit, &bm5}, bm1 = {"set", meth_set, &bm2}, base_methods = {"make", meth_make_object, &bm1};/*********************************************************************/Objects *new_objects(name) char *name;{ Objects *ob; char *c, *savestring(); if((ob = (Objects*)malloc(sizeof(Objects))) && (c = savestring(name))) { ob->name = c; ob->signal_name = NULL; ob->methods = &meth1; ob->next = NULL; return(ob); } fprintf(stderr,"Can't allocate space for another object\n"); return(NULL);}/* The only thing that kill_proc MUST do is send the message as shown below. It could also do other tidying up in preparation for the program exit. *//*********************************************************************/kill_proc(){ char mess[MES_BUF_SIZE]; sprintf(mess,"%s disconnect function %s\n",host,thisprog); terminal_message( mess ); /* say by by */ exit(0);}/*********************************************************************//* The following utility routines must be in all attachments. They should be left as shown below. *//*********************************************************************/Objects *make_new_object(str) char *str;{ char name[NAMELEN], command[MES_BUF_SIZE]; sscanf(str,"%s",name); if(debug_level) fprintf(stderr,"make_new_object(%s)\n",str); if(strlen(name)) { sprintf(command,"name %s",name); if(!strcmp(ok,meth_make_object(NULL,command))) { return(get_receiver(name)); } } return(NULL);}/*********************************************************************/char *exec_waves(str) char *str;{ extern char *dispatch(); return(dispatch(str));}/*********************************************************************/char *get_receiver_name(ob) Objects *ob;{ return(ob->name);}/*********************************************************************/char *get_methods(ob) Objects *ob;{ extern Methods base_methods; if(ob) return((char*)(ob->methods)); return((char*)&base_methods);}/*********************************************************************/Objects *get_receiver(str) char *str;{ Objects *ob; static char name[NAMELEN]; extern Objects *objlist; ob = objlist; if(str && strlen(str)) { sscanf(str,"%s",name); while(ob) { if(ob->name && (! strcmp(ob->name, name))) { return(ob); } ob = ob->next; } } return(NULL);}/******* END OF FIXED UTILITY ROUTINES *****************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -