int module_saveobj_xml(FILE *fp, MODULE *mod){ /**< the stream to write to */ unsigned count = 0; char buffer[1024]; PROPERTY *prop = NULL; OBJECT *obj; CLASS *oclass=NULL; CLASS *pclass = NULL; for(obj = object_get_first(); obj != NULL; obj = obj->next){ char32 oname = "(unidentified)"; if(obj->oclass->module != mod){ continue; } if(obj->name != NULL){ strcpy(oname, obj->name); } else { sprintf(oname, "%s:%i", obj->oclass->name, obj->id); } if ((oclass == NULL) || (obj->oclass != oclass)) oclass = obj->oclass; count += fprintf(fp,"\t\t<object type=\"%s\" id=\"%i\" name=\"%s\">\n", obj->oclass->name, obj->id, oname); /* dump internal properties */ if (obj->parent!=NULL){ if(obj->parent->name != NULL){ strcpy(oname, obj->parent->name); } else { sprintf(oname, "%s:%i", obj->parent->oclass->name, obj->parent->id); } count += fprintf(fp,"\t\t\t<parent>%s</parent>\n", oname); } else { count += fprintf(fp,"\t\t\t<parent>root</parent>\n"); } count += fprintf(fp,"\t\t\t<rank>%d</rank>\n", obj->rank); count += fprintf(fp,"\t\t\t<clock>\n", obj->clock); count += fprintf(fp,"\t\t\t\t <timestamp>%s</timestamp>\n", convert_from_timestamp(obj->clock,buffer,sizeof(buffer))>0?buffer:"(invalid)"); count += fprintf(fp,"\t\t\t</clock>\n"); /* why do latitude/longitude have 2 values? I currently only store as float in the schema... -dc */ if (!isnan(obj->latitude)) count += fprintf(fp,"\t\t\t<latitude>%s</latitude>\n", convert_from_latitude(obj->latitude,buffer,sizeof(buffer))?buffer:"(invalid)"); else count += fprintf(fp, "\t\t\t<latitude>NONE</latitude>\n"); if (!isnan(obj->longitude)) count += fprintf(fp,"\t\t\t<longitude>%s</longitude>\n",convert_from_longitude(obj->longitude,buffer,sizeof(buffer))?buffer:"(invalid)"); else count += fprintf(fp,"\t\t\t<longitude>NONE</longitude>\n"); /* dump properties */ for (prop=oclass->pmap;prop!=NULL && prop->oclass==oclass;prop=prop->next) { char *value = NULL; if((prop->access != PA_PUBLIC) && (prop->access != PA_REFERENCE)) continue; value = object_property_to_string(obj,prop->name); if (value!=NULL){ count += fprintf(fp, "\t\t\t<%s>%s</%s>\n", prop->name, value, prop->name); } } pclass = oclass->parent; while(pclass != NULL){ /* inherited properties */ for (prop=pclass->pmap;prop!=NULL && prop->oclass==pclass;prop=prop->next){ char *value = object_property_to_string(obj,prop->name); if (value!=NULL){ count += fprintf(fp, "\t\t\t<%s>%s</%s>\n", prop->name, value, prop->name); } } pclass = pclass->parent; } count += fprintf(fp,"\t\t</object>\n"); } return count; }
static mxArray *get_object_data(OBJECT *obj) { mxArray *plhs[1]; /* set the standard info */ #define ERROR "(error)" #define NONE "(none)" char *fnames[1024] = {"id","class","parent","rank","clock","latitude","longitude","in_svc","out_svc","flags",NULL}; // }; int nFields = 0; int nData = 0; char value[1024]; PROPERTY *prop; mxArray *pId = mxCreateString(convert_from_object(value,sizeof(value),&obj,NULL)?value:ERROR); mxArray *pClass = mxCreateString(obj->oclass->name); mxArray *pParent = mxCreateString(obj->parent!=NULL&&convert_from_object(value,sizeof(value),&(obj->parent),NULL)?value:NONE); mxArray *pRank = mxCreateNumericMatrix(1,1,mxUINT32_CLASS,mxREAL); mxArray *pClock = mxCreateString(convert_from_timestamp(obj->clock,value,sizeof(value))?value:ERROR); mxArray *pLatitude = mxCreateString(convert_from_latitude(obj->latitude,value,sizeof(value))?value:NONE); mxArray *pLongitude = mxCreateString(convert_from_longitude(obj->longitude,value,sizeof(value))?value:NONE); mxArray *pInSvc = mxCreateString(convert_from_timestamp(obj->in_svc,value,sizeof(value))?value:ERROR); mxArray *pOutSvc = mxCreateString(convert_from_timestamp(obj->out_svc,value,sizeof(value))?value:ERROR); mxArray *pFlags = mxCreateString(convert_from_set(value,sizeof(value),(void*)&obj->flags,object_flag_property())?value:ERROR); *(OBJECTRANK*)mxGetPr(pRank) = obj->rank; /* count the number of header items */ while (fnames[nFields]!=NULL) nFields++; /* count the number of object properties and assign the field names */ for (prop=class_get_first_property(obj->oclass); prop!=NULL; prop=class_get_next_property(prop)) /** @todo don't damage the original fieldname when making it safe for Matlab */ fnames[nFields+nData++] = make_fieldname(prop->name); /* construct the return value */ plhs[0] = mxCreateStructMatrix(1,1,nFields+nData,fnames); /* construct the header fields */ mxSetFieldByNumber(plhs[0],0,0,pId); mxSetFieldByNumber(plhs[0],0,1,pClass); mxSetFieldByNumber(plhs[0],0,2,pParent); mxSetFieldByNumber(plhs[0],0,3,pRank); mxSetFieldByNumber(plhs[0],0,4,pClock); mxSetFieldByNumber(plhs[0],0,5,pLatitude); mxSetFieldByNumber(plhs[0],0,6,pLongitude); mxSetFieldByNumber(plhs[0],0,7,pInSvc); mxSetFieldByNumber(plhs[0],0,8,pOutSvc); mxSetFieldByNumber(plhs[0],0,9,pFlags); /* construct the data fields */ for (prop=class_get_first_property(obj->oclass); prop!=NULL; nFields++,prop=class_get_next_property(prop)) { mxArray *pValue; if (prop->ptype==PT_double) { pValue = mxCreateDoubleMatrix(1,1,mxREAL); *(double*)mxGetPr(pValue) = *object_get_double(obj,prop); } else if (prop->ptype==PT_int32) { pValue = mxCreateDoubleMatrix(1,1,mxREAL); *(double*)mxGetPr(pValue) = (double)*object_get_int32(obj,prop); } else if (prop->ptype==PT_complex) { complex *pData = object_get_complex(obj,prop); pValue = mxCreateDoubleMatrix(1,1,mxCOMPLEX); *(double*)mxGetPr(pValue) = pData->r; *(double*)mxGetPi(pValue) = pData->i; } else { pValue = mxCreateString(object_get_value_by_name(obj,prop->name,value,sizeof(value))?value:ERROR); } mxSetFieldByNumber(plhs[0],0,nFields,pValue); } return plhs[0]; }