void *object_find_by_id(struct object * root, si_t id) { if(root->id == id) { return root; } else { return object_find_by_id(root->rchild, id); return object_find_by_id(root->lchild, id); } }
/** Convert to an \e object Converts a string to an \e object property. @return 1 on success, 0 on failure, -1 if conversion was incomplete **/ int convert_to_object(const char *buffer, /**< a pointer to the string buffer */ void *data, /**< a pointer to the data */ PROPERTY *prop) /**< a pointer to keywords that are supported */ { CLASSNAME cname; OBJECTNUM id; OBJECT **target = (OBJECT**)data; char oname[256]; if ( strcmp(buffer,"0")==0 ) // NOTE: this is inconsistent with what convert_from_object does for NULL object { *target = NULL; return 1; } else if (sscanf(buffer,"\"%[^\"]\"",oname)==1 || (strchr(buffer,':')==NULL && strncpy(oname,buffer,sizeof(oname)))) { oname[sizeof(oname)-1]='\0'; /* terminate unterminated string */ *target = object_find_name(oname); return (*target)!=NULL; } else if (sscanf(buffer,global_object_scan,cname,&id)==2) { OBJECT *obj = object_find_by_id(id); if(obj == NULL){ /* failure case, make noisy if desired. */ *target = NULL; return 0; } if (obj!=NULL && strcmp(obj->oclass->name,cname)==0) { *target=obj; return 1; } } else *target = NULL; return 0; }
/** Process an incoming XML data request @returns non-zero on success, 0 on failure (errno set) **/ int http_xml_request(HTTP *http,char *uri) { char arg1[1024]="", arg2[1024]=""; int nargs = sscanf(uri,"%1023[^/=\r\n]/%1023[^\r\n=]",arg1,arg2); char *value = strchr(uri,'='); char buffer[1024]=""; OBJECT *obj=NULL; char *id; /* value */ if (value) *value++; /* decode %.. */ http_decode(arg1); http_decode(arg2); if (value) http_decode(value); /* process request */ switch (nargs) { /* get global variable */ case 1: /* find the variable */ if (global_getvar(arg1,buffer,sizeof(buffer))==NULL) { output_error("global variable '%s' not found", arg1); return 0; } /* assignment, if any */ if (value) global_setvar(arg1,value); /* post the response */ http_format(http,"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); http_format(http,"<globalvar>\n\t<name>%s</name>\n\t<value>%s</value>\n</globalvar>\n", arg1, http_unquote(buffer)); http_type(http,"text/xml"); return 1; /* get object property */ case 2: /* find the object */ id = strchr(arg1,':'); if ( id==NULL ) obj = object_find_name(arg1); else obj = object_find_by_id(atoi(id+1)); if ( obj==NULL ) { output_error("object '%s' not found", arg1); return 0; } /* post the current value */ if ( !object_get_value_by_name(obj,arg2,buffer,sizeof(buffer)) ) { output_error("object '%s' property '%s' not found", arg1, arg2); return 0; } /* assignment, if any */ if ( value && !object_set_value_by_name(obj,arg2,value) ) { output_error("cannot set object '%s' property '%s' to '%s'", arg1, arg2, value); return 0; } /* post the response */ http_format(http,"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<property>\n"); http_format(http,"\t<object>%s</object>\n", arg1); http_format(http,"\t<name>%s</name>\n", arg2); http_format(http,"\t<value>%s</value>\n", http_unquote(buffer)); /* TODO add property type info */ http_format(http,"</property>\n"); http_type(http,"text/xml"); return 1; default: return 0; } return 0; }