TUM-Logo

libRASCH

 

Home
 

General

About libRASCH/News
Design
Screen shots
Sample programs (with source code)
License
 

Download

libRASCH
Tools
 

Documentation

User
Developer
 

Resources

Mailing list
Supported Formats
Plugins
Status
Links
 
Contact
About this site
 
Last updated
Tue Mar 27 23:03:52 2007
libRASCH: load_xml_v0.c Source File

load_xml_v0.c

Go to the documentation of this file.
00001 
00009 /*----------------------------------------------------------------------------
00010  *
00011  * Copyright (C) 2002-2005, Raphael Schneider
00012  * See the file COPYING for information on usage and redistribution.
00013  *
00014  * $Id: $
00015  *
00016  *--------------------------------------------------------------------------*/
00017 
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include <errno.h>
00022 #include <time.h>
00023 #include <assert.h>
00024 
00025 #include <libxml/tree.h>
00026 #include <libxml/xpath.h>
00027 #include <libxml/parser.h>
00028 #include "mime64.h"
00029 
00030 #ifndef WIN32
00031 #include <unistd.h>
00032 #endif
00033 
00034 #define _LIBRASCH_BUILD
00035 #define _DEFINE_INFO_STRUCTS_PROP
00036 #include <ra_priv.h>
00037 #undef _DEFINE_INFO_STRUCTS
00038 #include <pl_general.h>
00039 #include <ra_linked_list.h>
00040 
00041 
00042 
00043 struct event_info
00044 {
00045         long id;
00046         long start;
00047         long end;
00048 }; /* struct event_info */
00049 
00050 
00059 int
00060 eval_loadxml_v0(meas_handle mh, const char *file)
00061 {
00062         struct ra_meas *mm = (struct ra_meas *) mh;
00063         char fn[MAX_PATH_RA];
00064         int ret;
00065         xmlDocPtr doc;
00066         FILE *fp;
00067 
00068         if (!mm)
00069                 return -1;
00070 
00071         if (file)
00072                 strncpy(fn, file, MAX_PATH_RA);
00073         else
00074                 get_eval_file_xml(mm, fn);
00075 
00076         /* first check if eval-file is available */
00077         fp = fopen(fn, "r");
00078         if (fp == NULL)
00079                 return 0;
00080         fclose(fp);
00081 
00082         if ((doc = xmlParseFile(fn)) == NULL)
00083         {
00084                 _ra_set_error(mh, RA_ERR_READ_EVAL, "libRASCH");
00085                 return -1;
00086         }
00087         if (!doc->children || ((xmlStrcmp(doc->children->name, (xmlChar *)"libRASCH") != 0) && (xmlStrcmp(doc->children->name, (xmlChar *)"libMTK") != 0)))
00088         {
00089                 xmlFreeDoc(doc);
00090                 _ra_set_error(mh, RA_ERR_EVAL_FILE_CORRUPT, "libRASCH");
00091                 return -1;
00092         }
00093 
00094         ret = read_evals_xml(mm, doc->children);
00095 
00096         xmlFreeDoc(doc);
00097 
00098         return ret;
00099 } /* ra_eval_loadxml() */
00100 
00101 
00110 int
00111 get_eval_file_xml(meas_handle mh, char *f)
00112 {
00113         long dir = 0;
00114         value_handle vh;
00115         rec_handle rh;
00116 
00117         rh = ra_rec_get_first(mh, 0);
00118         vh = ra_value_malloc();
00119         if (ra_info_get(rh, RA_INFO_REC_GEN_DIR_L, vh) != 0)
00120         {
00121                 ra_value_free(vh);
00122                 return -1;
00123         }
00124         dir = ra_value_get_long(vh);
00125 
00126         if (ra_info_get(rh, RA_INFO_REC_GEN_PATH_C, vh) != 0)
00127         {
00128                 ra_value_free(vh);
00129                 return -1;
00130         }
00131 
00132         if (dir)
00133                 sprintf(f, "%s%ceval_ra.mte", ra_value_get_string(vh), SEPERATOR);
00134         else
00135                 sprintf(f, "%s.mte", ra_value_get_string(vh));
00136 
00137         ra_value_free(vh);
00138 
00139         return 0;
00140 } /* get_eval_file_xml() */
00141 
00142 
00150 int
00151 read_evals_xml(meas_handle mh, xmlNodePtr parent)
00152 {
00153         struct ra_meas *meas = (struct ra_meas *) mh;
00154         struct eval *e;
00155         int ret = 0;
00156         xmlNodePtr node, child;
00157         xmlChar *c;
00158         value_handle vh;
00159 
00160         vh = ra_value_malloc();
00161 
00162         node = parent->children;
00163         while (node)
00164         {
00165                 if (xmlStrcmp(node->name, (xmlChar *)"Evaluation") != 0)
00166                 {
00167                         node = node->next;
00168                         continue;
00169                 }
00170 
00171                 e = (struct eval *)calloc(1, sizeof(struct eval));
00172                 ra_list_add((void **)&(meas->eval.evaluations), e);
00173 
00174                 e->handle_id = RA_HANDLE_EVAL;
00175                 e->meas = meas;
00176 
00177                 child = get_node(node, "Flags");
00178                 if (child)
00179                 {
00180                         if (xmlHasProp(child, (xmlChar *)"original") != NULL)
00181                                 e->original = 1;
00182                         if (xmlHasProp(child, (xmlChar *)"default") != NULL)
00183                                 e->def = 1;
00184                 }
00185 
00186                 c = xmlGetProp(node, (xmlChar *)"name");
00187                 if (c != NULL)
00188                 {
00189                         ra_value_set_string(vh, (const char *)c);
00190                         ra_eval_attribute(e, "name", vh);
00191                         xmlFree(c);
00192                 }
00193 
00194                 child = get_node(node, "comment");
00195                 c = xmlNodeGetContent(child);
00196                 if (c != NULL)
00197                 {
00198                         ra_value_set_string(vh, (const char *)c);
00199                         ra_eval_attribute(e, "description", vh);
00200                         xmlFree(c);
00201                 }
00202 
00203                 child = get_node(node, "add-ts");
00204                 c = xmlNodeGetContent(child);
00205                 if (c != NULL)
00206                 {
00207                         ra_value_set_string(vh, (const char *)c);
00208                         ra_eval_attribute(e, "add-timestamp", vh);
00209                         xmlFree(c);
00210                 }
00211                 child = get_node(node, "modify-ts");
00212                 c = xmlNodeGetContent(child);
00213                 if (c != NULL)
00214                 {
00215                         ra_value_set_string(vh, (const char *)c);
00216                         ra_eval_attribute(e, "modify-timestamp", vh);
00217                         xmlFree(c);
00218                 }
00219                 child = get_node(node, "user");
00220                 c = xmlNodeGetContent(child);
00221                 if (c != NULL)
00222                 {
00223                         ra_value_set_string(vh, (const char *)c);
00224                         ra_eval_attribute(e, "add-user", vh);
00225                         xmlFree(c);
00226                 }
00227                 child = get_node(node, "host");
00228                 c = xmlNodeGetContent(child);
00229                 if (c != NULL)
00230                 {
00231                         ra_value_set_string(vh, (const char *)c);
00232                         ra_eval_attribute(e, "add-host", vh);
00233                         xmlFree(c);
00234                 }
00235                 child = get_node(node, "program");
00236                 c = xmlNodeGetContent(child);
00237                 if (c != NULL)
00238                 {
00239                         ra_value_set_string(vh, (const char *)c);
00240                         ra_eval_attribute(e, "add-program", vh);
00241                         xmlFree(c);
00242                 }
00243 
00244                 if ((ret = read_sets_xml(e, node)) != 0)
00245                         return ret;
00246 
00247                 node = node->next;
00248         }
00249 
00250         ra_value_free(vh);
00251 
00252         return ret;
00253 } /* read_evals_xml() */
00254 
00255 
00264 xmlNodePtr
00265 get_node(xmlNodePtr parent, char *name)
00266 {
00267         xmlNodePtr child = parent->children;
00268 
00269         while (child)
00270         {
00271                 if (xmlStrcmp(child->name, (xmlChar *)name) == 0)
00272                         return child;
00273                 child = child->next;
00274         }
00275 
00276         return NULL;
00277 } /* get_node() */
00278 
00279 
00287 int
00288 read_sets_xml(struct eval *e, xmlNodePtr parent)
00289 {
00290         int ret = 0;
00291         struct eval_class *cl;
00292         xmlNodePtr child, node = parent->children;
00293         xmlChar *c;
00294         int use_ascii = 0;
00295         long num_events;
00296         struct event_info *evi;
00297 
00298         while (node)
00299         {
00300                 if (xmlStrcmp(node->name, (xmlChar *)"Event-Set") != 0)
00301                 {
00302                         node = node->next;
00303                         continue;
00304                 }
00305 
00306                 cl = calloc(1, sizeof(struct eval_class));
00307                 cl->handle_id = RA_HANDLE_EVAL_CLASS;
00308                 cl->meas = e->meas;
00309                 cl->eval = e;
00310 
00311                 c = xmlGetProp(node, (xmlChar *)"name");
00312                 if (c)
00313                 {
00314                         strncpy(cl->id_ascii, (const char *)c, MAX_ID_LEN);
00315                         cl->id = get_class_id(cl->ascii_id);
00316                         ra_value_set_string(vh, (const char *)c);
00317                         ra_eval_attribute_set(cl, "name", vh);
00318                         xmlFree(c);
00319                 }
00320                 else
00321                 {
00322                         free(cl);
00323                         continue;
00324                         /* TODO: think if an error should be returned */
00325                 }
00326 
00327                 child = get_node(node, "number-events");
00328                 c = xmlNodeGetContent(child);
00329                 if (c)
00330                 {
00331                         num_events = atol((const char *)c);
00332                         xmlFree(c);
00333                 }
00334                 else
00335                 {
00336                         free(cl);
00337                         continue;
00338                         /* TODO: think if an error should be returned */
00339                 }
00340 
00341                 /* add event-class here so we are sure that an ASCII-id and the number
00342                    of events are available */
00343                 ra_list_add((void **)&(e->cl), cl);
00344 
00345                 child = get_node(node, "desc");
00346                 c = xmlNodeGetContent(child);
00347                 if (c)
00348                 {
00349                         ra_value_set_string(vh, (const char *)c);
00350                         ra_eval_attribute_set(cl, "description", vh);
00351                         xmlFree(c);
00352                 }
00353 
00354                 c = xmlGetProp(node, (xmlChar *)"event-encoding");
00355                 if (c && (xmlStrcmp(c, (xmlChar *)"ascii") == 0))
00356                         use_ascii = 1;
00357 
00358                 evi = (struct event_info *)calloc(num_events, sizeof(struct event_info));
00359                 if (type == EVENT_TYPE_CONT)
00360                         ret = read_events_start_end_xml(node, evi, num_events);
00361                 else
00362                 {
00363                         for (l = 0; l < num_events; l++)
00364                                 evi[l].start = evi[l].end = -1;
00365                 }
00366 
00367                 if (ret != 0)
00368                         break;
00369 
00370                 for (l = 0; l < num_events; l++)
00371                         evi[l].id = ra_class_add_event(cl, evi[l].start, evi[l].end);
00372 
00373                 if ((ret = read_props_xml(cl, node, use_ascii, evi, num_events)) != 0)
00374                 {
00375                         free(evi);
00376                         break;
00377                 }
00378 
00379                 if (read_templates_xml(cl, node) != 0)
00380                 {
00381                         free(evi);
00382                         return -1;
00383                 }
00384 
00385                 free(evi);
00386 
00387                 node = node->next;
00388         }
00389 
00390         return ret;
00391 } /* read_sets_xml() */
00392 
00393 
00402 int
00403 read_events_start_end_xml(xmlNodePtr parent, struct event_info *evi, long num_events)
00404 {
00405         xmlChar *c;
00406         void *t;
00407         long size;
00408         xmlNodePtr curr;
00409         long l, data_size;
00410 
00411         data_size = num_events * sizeof(long);
00412 
00413         curr = get_node(parent, "event-start");
00414         c = xmlNodeGetContent(curr);
00415         if (!c)
00416         {
00417                 _ra_set_error(set, RA_ERR_EVAL_FILE_CORRUPT, "libRASCH");
00418                 return -1;
00419         }
00420         decode_base64(c, strlen((const char *)c), &t, &size, data_size);
00421         for (l = 0; l < size; l++)
00422                 evi[l].start = t[l];
00423         xmlFree(c);
00424         free(t);
00425 
00426         curr = get_node(parent, "event-end");
00427         c = xmlNodeGetContent(curr);
00428         if (!c)
00429         {
00430                 _ra_set_error(set, RA_ERR_EVAL_FILE_CORRUPT, "libRASCH");
00431                 return -1;
00432         }
00433         decode_base64(c, strlen((const char *)c), &t, &size, data_size);
00434         for (l = 0; l < size; l++)
00435                 evi[l].end = t[l];
00436         xmlFree(c);
00437         free(t);
00438 
00439         if (size != data_size)
00440         {
00441                 _ra_set_error(set, RA_ERR_EVAL_FILE_CORRUPT, "libRASCH");
00442                 return -1;
00443         }
00444 
00445         return 0;
00446 } /* read_events_start_end_xml() */
00447 
00448 
00458 int
00459 read_props_xml(struct event_set *set, xmlNodePtr parent, int use_ascii, struct event_info *evi, long num_events)
00460 {
00461         xmlNodePtr child, node = parent->children;
00462         xmlChar *c;
00463 
00464         while (node)
00465         {
00466                 struct event_property *prop;
00467 
00468                 if (xmlStrcmp(node->name, (xmlChar *)"Event-Property") != 0)
00469                 {
00470                         node = node->next;
00471                         continue;
00472                 }
00473 
00474                 prop = calloc(1, sizeof(struct event_property));
00475                 prop->handle_id = RA_HANDLE_EVPROP;
00476                 prop->set = set;
00477                 prop->eval = set->eval;
00478                 ra_list_add((void **) &(set->eval->prop), prop);
00479                 set->eval->num_props++;
00480 
00481                 c = xmlGetProp(node, (xmlChar *)"name");
00482                 if (c)
00483                 {
00484                         strncpy(prop->name, (const char *)c, EVAL_MAX_NAME);
00485                         strncpy(prop->id_ascii, (const char *)c, EVAL_MAX_NAME);
00486                         xmlFree(c);
00487                 }
00488                 c = xmlGetProp(node, (xmlChar *)"val-type");
00489                 if (c)
00490                 {
00491                         if (xmlStrcmp(c, (xmlChar *)"short") == 0)
00492                                 prop->val_type = EVPROP_TYPE_SHORT;
00493                         else if (xmlStrcmp(c, (xmlChar *)"long") == 0)
00494                                 prop->val_type = EVPROP_TYPE_LONG;
00495                         else if (xmlStrcmp(c, (xmlChar *)"double") == 0)
00496                                 prop->val_type = EVPROP_TYPE_DOUBLE;
00497                         else if (xmlStrcmp(c, (xmlChar *)"char") == 0)
00498                                 prop->val_type = EVPROP_TYPE_CHAR;
00499                         else if (xmlStrcmp(c, (xmlChar *)"short-array") == 0)
00500                                 prop->val_type = EVPROP_TYPE_SHORT_ARRAY;
00501                         else if (xmlStrcmp(c, (xmlChar *)"long-array") == 0)
00502                                 prop->val_type = EVPROP_TYPE_LONG_ARRAY;
00503                         else if (xmlStrcmp(c, (xmlChar *)"double-array") == 0)
00504                                 prop->val_type = EVPROP_TYPE_DOUBLE_ARRAY;
00505                         else
00506                         {
00507                                 _ra_set_error(set, RA_ERR_EVAL_FILE_CORRUPT, "libRASCH");
00508                                 return -1;      /* file corrupt */
00509                         }
00510 
00511                         xmlFree(c);
00512                 }
00513                 c = xmlGetProp(node, (xmlChar *)"use-minmax");
00514                 if (c)
00515                 {
00516                         if (c[0] == '1')
00517                                 prop->use_minmax = 1;
00518                         else
00519                                 prop->use_minmax = 0;
00520                         xmlFree(c);
00521                 }
00522                 c = xmlGetProp(node, (xmlChar *)"min-value");
00523                 if (c)
00524                 {
00525                         prop->min_value = atof((const char *)c);
00526                         xmlFree(c);
00527                 }
00528                 c = xmlGetProp(node, (xmlChar *)"max-value");
00529                 if (c)
00530                 {
00531                         prop->max_value = atof((const char *)c);
00532                         xmlFree(c);
00533                 }
00534 
00535                 c = xmlGetProp(node, (xmlChar *)"unit");
00536                 if (c)
00537                 {
00538                         strncpy(prop->unit, (const char *)c, EVAL_MAX_UNIT);
00539                         xmlFree(c);
00540                 }
00541 
00542                 child = get_node(node, "desc");
00543                 c = xmlNodeGetContent(child);
00544                 if (c)
00545                 {
00546                         strncpy(prop->desc, (const char *)c, EVAL_MAX_DESC);
00547                         xmlFree(c);
00548                 }
00549 
00550                 read_events_xml(prop, node, use_ascii);
00551 
00552                 node = node->next;
00553         }
00554 
00555         return 0;
00556 } /* read_props_xml() */
00557 
00558 
00568 int
00569 read_events_xml(struct event_property *prop, xmlNodePtr parent, int use_ascii)
00570 {
00571         int ret = 0;
00572         xmlNodePtr curr;
00573         xmlChar *c;
00574 
00575         curr = get_node(parent, "events");
00576 
00577         /* If events stored in ASCII, read them and leave function */
00578         if (use_ascii)
00579                 return handle_value_events_ascii(prop, curr);
00580         
00581         /* get the event string */
00582         c = xmlNodeGetContent(curr);
00583         if (!c)
00584         {
00585                 _ra_set_error(prop, RA_ERR_EVAL_FILE_CORRUPT, "libRASCH");
00586                 return -1;
00587         }
00588 
00589         /* if not a char-event */
00590         if (prop->val_type == EVPROP_TYPE_CHAR)
00591                 ret = handle_char_events(prop, c);
00592         else
00593                 ret = handle_value_events(prop, c);
00594 
00595         xmlFree(c);
00596 
00597         return ret;
00598 } /* read_events_xml() */
00599 
00600 
00609 int
00610 handle_value_events_ascii(struct event_property *prop, xmlNodePtr parent)
00611 {
00612         struct event_set *set = prop->set;
00613         long l, idx;
00614         xmlNodePtr node;
00615         xmlChar *c;
00616 
00617         /* get "destination" type (if available); this is needed when changes in the type of an
00618            event-property happens */
00619         idx = -1;
00620         for (l = 0; l < (sizeof(ra_event_props) / sizeof(struct event_prop_desc)); l++)
00621         {
00622                 if (strcmp(ra_event_props[l].id_ascii, prop->id_ascii) == 0)
00623                 {
00624                         idx = l;
00625                         break;
00626                 }
00627         }       
00628         if (idx != -1)
00629                 prop->val_type = ra_event_props[idx].val_type;
00630 
00631         /* prepare memory for event values */
00632         switch (prop->val_type)
00633         {
00634         case EVPROP_TYPE_SHORT:
00635                 prop->idx = set->n_short;
00636                 set->n_short += 1;
00637                 set->s = realloc(set->s, sizeof(short *) * set->n_short);               
00638                 set->s[prop->idx] = malloc(sizeof(short) * set->num_events);
00639                 break;
00640         case EVPROP_TYPE_LONG:
00641                 prop->idx = set->n_long;
00642                 set->n_long += 1;
00643                 set->l = realloc(set->l, sizeof(long *) * set->n_long);
00644                 set->l[prop->idx] = malloc(sizeof(long) * set->num_events);
00645                 break;
00646         case EVPROP_TYPE_DOUBLE:
00647                 prop->idx = set->n_double;
00648                 set->n_double += 1;
00649                 set->d = realloc(set->d, sizeof(double *) * set->n_double);             
00650                 set->d[prop->idx] = malloc(sizeof(double) * set->num_events);
00651                 break;
00652         case EVPROP_TYPE_CHAR:
00653                 prop->idx = set->n_char;
00654                 set->n_char += 1;
00655                 set->c = realloc(set->d, sizeof(char **) * set->n_char);                
00656                 set->c[prop->idx] = malloc(sizeof(char *) * set->num_events);
00657                 break;
00658         case EVPROP_TYPE_SHORT_ARRAY:
00659                 prop->idx = set->n_short_array;
00660                 set->n_short_array += 1;
00661                 set->sa = realloc(set->sa, sizeof(short **) * set->n_short_array);              
00662                 set->sa[prop->idx] = malloc(sizeof(short *) * set->num_events);
00663                 set->sa_ch = realloc(set->sa_ch, sizeof(short **) * set->n_short_array);                
00664                 set->sa_ch[prop->idx] = malloc(sizeof(short *) * set->num_events);
00665                 set->sa_num_elem = realloc(set->sa_num_elem, sizeof(long *) * set->n_short_array);
00666                 set->sa_num_elem[prop->idx] = malloc(sizeof(long) * set->num_events);
00667                 break;
00668         case EVPROP_TYPE_LONG_ARRAY:
00669                 prop->idx = set->n_long_array;
00670                 set->n_long_array += 1;
00671                 set->la = realloc(set->la, sizeof(long **) * set->n_long_array);
00672                 set->la[prop->idx] = malloc(sizeof(long *) * set->num_events);
00673                 set->la_ch = realloc(set->sa_ch, sizeof(short **) * set->n_short_array);                
00674                 set->la_ch[prop->idx] = malloc(sizeof(short *) * set->num_events);
00675                 set->la_num_elem = realloc(set->la_num_elem, sizeof(long *) * set->n_long_array);
00676                 set->la_num_elem[prop->idx] = malloc(sizeof(long) * set->num_events);
00677                 break;
00678         case EVPROP_TYPE_DOUBLE_ARRAY:
00679                 prop->idx = set->n_double_array;
00680                 set->n_double_array += 1;
00681                 set->da = realloc(set->da, sizeof(double **) * set->n_double_array);
00682                 set->da[prop->idx] = malloc(sizeof(double *) * set->num_events);
00683                 set->da_ch = realloc(set->sa_ch, sizeof(short **) * set->n_short_array);                
00684                 set->da_ch[prop->idx] = malloc(sizeof(short *) * set->num_events);
00685                 set->da_num_elem = realloc(set->da_num_elem, sizeof(long *) * set->n_double_array);
00686                 set->da_num_elem[prop->idx] = malloc(sizeof(long) * set->num_events);
00687                 break;
00688         }
00689 
00690         node = parent->children;
00691         while (node)
00692         {
00693                 long idx;
00694                 c = xmlGetProp(node, (xmlChar *)"index");
00695                 idx = atol((const char *)c);
00696                 xmlFree(c);
00697                 if (idx >= set->num_events)
00698                         return -1; /* FIXME: better error handling */
00699 
00700                 switch (prop->val_type)
00701                 {
00702                 case EVPROP_TYPE_SHORT:
00703                         read_short(node, prop, idx);
00704                         break;
00705                 case EVPROP_TYPE_LONG:
00706                         read_long(node, prop, idx);
00707                         break;
00708                 case EVPROP_TYPE_DOUBLE:
00709                         read_double(node, prop, idx);
00710                         break;
00711                 case EVPROP_TYPE_CHAR:
00712                         read_char(node, prop, idx);
00713                         break;
00714                 case EVPROP_TYPE_SHORT_ARRAY:
00715                         read_short_array(node, prop, idx);
00716                         break;
00717                 case EVPROP_TYPE_LONG_ARRAY:
00718                         read_long_array(node, prop, idx);
00719                         break;
00720                 case EVPROP_TYPE_DOUBLE_ARRAY:
00721                         read_double_array(node, prop, idx);
00722                         break;
00723                 default:
00724                         return -1; /* FIXME: better error handling */
00725                         break;
00726                 }
00727                 
00728                 /* no need to check that node is "event" */
00729                 node = node->next;
00730         }
00731 
00732         return 0;
00733 } /* handle_value_events_ascii() */
00734 
00735 
00745 int
00746 read_short(xmlNodePtr node, struct event_property *prop, long idx)
00747 {
00748         xmlNodePtr content;
00749         xmlChar *c;
00750         struct event_set *set = prop->set;
00751         
00752         content = node->children;
00753         if (!content)
00754                 return -1;
00755 
00756         c = xmlNodeGetContent(content);
00757         set->s[prop->idx][idx] = (short)atoi((const char *)c);
00758         xmlFree(c);
00759 
00760         return 0;
00761 } /* read_short() */
00762 
00763 
00773 int
00774 read_long(xmlNodePtr node, struct event_property *prop, long idx)
00775 {
00776         xmlNodePtr content;
00777         xmlChar *c;
00778         struct event_set *set = prop->set;
00779         
00780         content = node->children;
00781         if (!content)
00782                 return -1;
00783 
00784         c = xmlNodeGetContent(content);
00785         set->l[prop->idx][idx] = (long)atol((const char *)c);
00786         xmlFree(c);
00787 
00788         return 0;
00789 } /* read_long() */
00790 
00791 
00801 int
00802 read_double(xmlNodePtr node, struct event_property *prop, long idx)
00803 {
00804         xmlNodePtr content;
00805         xmlChar *c;
00806         struct event_set *set = prop->set;
00807         
00808         content = node->children;
00809         if (!content)
00810                 return -1;
00811 
00812         c = xmlNodeGetContent(content);
00813         set->d[prop->idx][idx] = xmlXPathCastStringToNumber(c);
00814         xmlFree(c);
00815 
00816         return 0;
00817 } /* read_double() */
00818 
00819 
00829 int
00830 read_char(xmlNodePtr node, struct event_property *prop, long idx)
00831 {
00832         xmlNodePtr content;
00833         xmlChar *c;
00834         struct event_set *set = prop->set;
00835         
00836         content = node->children;
00837         if (!content)
00838                 return -1;
00839 
00840         c = xmlNodeGetContent(content);
00841         set->c[prop->idx][idx] = calloc(strlen((const char *)c) + 1, sizeof(char));
00842         strncpy(set->c[prop->idx][idx], (const char *)c, strlen((const char *)c));
00843         xmlFree(c);
00844 
00845         return 0;
00846 } /* read_char() */
00847 
00848 
00858 int
00859 read_short_array(xmlNodePtr node, struct event_property *prop, long idx)
00860 {
00861         xmlNodePtr content;
00862         struct event_set *set = prop->set;
00863         long n_elem = 0;
00864         short *buf = NULL;
00865         short *ch_buf = NULL;
00866         xmlChar *c;
00867         
00868         content = node->children;
00869         if (!content)
00870                 return -1;
00871 
00872         while (content)
00873         {
00874                 long idx2;
00875                 c = xmlGetProp(content, (xmlChar *)"index");
00876                 idx2 = atol((const char *)c);
00877                 xmlFree(c);
00878                 if ((idx2+1) > n_elem)
00879                 {
00880                         n_elem = idx2 + 1;
00881                         buf = realloc(buf, sizeof(short) * n_elem);
00882                         ch_buf = realloc(ch_buf, sizeof(short) * n_elem);
00883                 }
00884 
00885                 c = xmlGetProp(content, (xmlChar *)"ch");
00886                 ch_buf[idx2] = (short)atol((const char *)c);
00887                 xmlFree(c);
00888                 
00889                 c = xmlNodeGetContent(content);
00890                 buf[idx2] = (short)atoi((const char *)c);
00891                 xmlFree(c);
00892                 
00893                 content = content->next;
00894         }
00895         if (buf)
00896         {
00897                 set->sa[prop->idx][idx] = buf;
00898                 set->sa_ch[prop->idx][idx] = ch_buf;
00899                 set->sa_num_elem[prop->idx][idx] = n_elem;
00900         }
00901 
00902         return 0;
00903 } /* read_short_array() */
00904 
00905 
00915 int
00916 read_long_array(xmlNodePtr node, struct event_property *prop, long idx)
00917 {
00918         xmlNodePtr content;
00919         struct event_set *set = prop->set;
00920         long n_elem = 0;
00921         long *buf = NULL;
00922         short *ch_buf = NULL;
00923         xmlChar *c;
00924         
00925         content = node->children;
00926         if (!content)
00927                 return -1;
00928 
00929         while (content)
00930         {
00931                 long idx2;
00932                 c = xmlGetProp(content, (xmlChar *)"index");
00933                 idx2 = atol((const char *)c);
00934                 xmlFree(c);
00935                 if ((idx2+1) > n_elem)
00936                 {
00937                         n_elem = idx2 + 1;
00938                         buf = realloc(buf, sizeof(long) * n_elem);
00939                 }
00940 
00941                 c = xmlGetProp(content, (xmlChar *)"ch");
00942                 ch_buf[idx2] = (short)atol((const char *)c);
00943                 xmlFree(c);
00944                 
00945                 c = xmlNodeGetContent(content);
00946                 buf[idx2] = (long)atol((const char *)c);
00947                 xmlFree(c);
00948                 
00949                 content = content->next;
00950         }
00951         if (buf)
00952         {
00953                 set->la[prop->idx][idx] = buf;
00954                 set->la_ch[prop->idx][idx] = ch_buf;
00955                 set->la_num_elem[prop->idx][idx] = n_elem;
00956         }
00957 
00958         return 0;
00959 } /* read_long_array() */
00960 
00961 
00971 int
00972 read_double_array(xmlNodePtr node, struct event_property *prop, long idx)
00973 {
00974         xmlNodePtr content;
00975         struct event_set *set = prop->set;
00976         long n_elem = 0;
00977         double *buf = NULL;
00978         short *ch_buf = NULL;
00979         xmlChar *c;
00980         
00981         content = node->children;
00982         if (!content)
00983                 return -1;
00984 
00985         while (content)
00986         {
00987                 long idx2;
00988                 c = xmlGetProp(content, (xmlChar *)"index");
00989                 idx2 = atol((const char *)c);
00990                 xmlFree(c);
00991                 if ((idx2+1) > n_elem)
00992                 {
00993                         n_elem = idx2 + 1;
00994                         buf = realloc(buf, sizeof(double) * n_elem);
00995                 }
00996 
00997                 c = xmlGetProp(content, (xmlChar *)"ch");
00998                 ch_buf[idx2] = (short)atol((const char *)c);
00999                 xmlFree(c);
01000                 
01001                 c = xmlNodeGetContent(content);
01002                 buf[idx2] = xmlXPathCastStringToNumber(c);
01003                 xmlFree(c);
01004                 
01005                 content = content->next;
01006         }
01007         if (buf)
01008         {
01009                 set->da[prop->idx][idx] = buf;
01010                 set->da_ch[prop->idx][idx] = ch_buf;
01011                 set->da_num_elem[prop->idx][idx] = n_elem;
01012         }
01013 
01014         return 0;
01015 } /* read_double_array() */
01016 
01017 
01026 int
01027 handle_char_events(struct event_property *prop, xmlChar *c)
01028 {
01029         int ret = 0;
01030         struct event_set *set = prop->set;
01031         char *p_curr, *p_next;
01032         long cnt;
01033 
01034         prop->idx = set->n_char;
01035         set->n_char += 1;
01036         set->c = realloc(set->c, sizeof(char **) * set->n_char);
01037         memset(&(set->c[prop->idx]), 0, sizeof(char *));
01038         
01039         p_curr = (char *)c;
01040         p_next = find_next_string(p_curr);
01041         cnt = 0;
01042         while (p_curr)
01043         {
01044                 set->c[prop->idx] = realloc(set->c[prop->idx], sizeof(char *) * (cnt + 1));
01045 
01046                 set->c[prop->idx][cnt] = calloc(strlen(p_curr) + 1, sizeof(char));
01047                 strncpy(set->c[prop->idx][cnt], p_curr, strlen(p_curr));
01048                 cnt++;
01049                 
01050                 if (!p_next)
01051                         break;
01052                 
01053                 p_curr = p_next;
01054                 p_next = find_next_string(p_curr);
01055         }
01056 
01057         if (set->num_events == 0)
01058                 set->num_events = cnt;
01059         else
01060         {
01061                 /* TODO: think about what to do if number of events in a set are not the same */
01062                 if (set->num_events > cnt)
01063                         set->num_events = cnt;
01064         }
01065 
01066         return ret;
01067 } /* handle_char_events() */
01068 
01069 
01076 char *
01077 find_next_string(char *s)
01078 {
01079         char *p = s;
01080         int len = strlen(s);
01081 
01082         while ((p = strchr(p, 10)))
01083         {
01084                 if ((!p) || ((p - s) >= (len - 2)))
01085                 {
01086                         if (p)
01087                                 *p = '\0';
01088                         return NULL;
01089                 }
01090 
01091                 if ((p[1] == '.') && (p[2] == 10))
01092                 {
01093                         *p = '\0';
01094                         p += 3;
01095                         break;
01096                 }
01097 
01098                 p++;
01099         }
01100 
01101         if (p >= (s + len))
01102                 return NULL;
01103 
01104         return p;
01105 } /* find_next_string() */
01106 
01107 
01116 int
01117 handle_value_events(struct event_property *prop, xmlChar *c)
01118 {
01119         int ret = 0;
01120         void *t = NULL;
01121         long size, cnt;
01122         struct event_set *set = prop->set;
01123         long data_size, l, idx;
01124         double *buf = NULL;
01125 
01126         /* decode and put values as double in an array */
01127         switch (prop->val_type)
01128         {
01129         case EVPROP_TYPE_SHORT:
01130                 data_size = set->num_events * sizeof(short);
01131                 decode_base64(c, strlen((const char *)c), &t, &size, data_size);
01132                 cnt = set->num_events_alloc = size / sizeof(short);
01133                 buf = malloc(sizeof(double) * set->num_events);
01134                 for (l = 0; l < set->num_events; l++)
01135                         buf[l] = (double)((short *)t)[l];
01136                 break;
01137         case EVPROP_TYPE_LONG:
01138                 data_size = set->num_events * sizeof(long);
01139                 decode_base64(c, strlen((const char *)c), &t, &size, data_size);
01140                 cnt = set->num_events_alloc = size / sizeof(long);
01141                 buf = malloc(sizeof(double) * set->num_events);
01142                 for (l = 0; l < set->num_events; l++)
01143                         buf[l] = (double)((long *)t)[l];
01144                 break;
01145         case EVPROP_TYPE_DOUBLE:
01146                 data_size = set->num_events * sizeof(double);
01147                 decode_base64(c, strlen((const char *)c), &t, &size, data_size);
01148                 cnt = set->num_events_alloc = size / sizeof(double);
01149                 buf = malloc(sizeof(double) * set->num_events);
01150                 memcpy(buf, t, size);
01151                 break;
01152         }
01153         if (t)
01154                 free(t);
01155 
01156         if (set->num_events == 0)
01157                 set->num_events = cnt;
01158         else
01159         {
01160                 /* TODO: think about what to do if number of events in a set are not the same */
01161                 if (set->num_events > cnt)
01162                         set->num_events = cnt;
01163         }
01164 
01165         /* get "destination" type (if available); this is needed when changes in the type of an
01166            event-property happens */
01167         idx = -1;
01168         for (l = 0; l < (sizeof(ra_event_props) / sizeof(struct event_prop_desc)); l++)
01169         {
01170                 if (strcmp(ra_event_props[l].id_ascii, prop->id_ascii) == 0)
01171                 {
01172                         idx = l;
01173                         break;
01174                 }
01175         }
01176         
01177         if (idx != -1)
01178                 prop->val_type = ra_event_props[idx].val_type;
01179         
01180         switch (prop->val_type)
01181         {
01182         case EVPROP_TYPE_SHORT:
01183                 prop->idx = set->n_short;
01184                 set->n_short += 1;
01185                 set->s = realloc(set->s, sizeof(short *) * set->n_short);
01186                 
01187                 set->s[prop->idx] = malloc(sizeof(short) * set->num_events);
01188                 for (l = 0; l < set->num_events; l++)
01189                         set->s[prop->idx][l] = (short)buf[l];
01190                 break;
01191         case EVPROP_TYPE_LONG:
01192                 prop->idx = set->n_long;
01193                 set->n_long += 1;
01194                 set->l = realloc(set->l, sizeof(long *) * set->n_long);
01195                 
01196                 set->l[prop->idx] = malloc(sizeof(long) * set->num_events);
01197                 for (l = 0; l < set->num_events; l++)
01198                         set->l[prop->idx][l] = (long)buf[l];
01199                 break;
01200         case EVPROP_TYPE_DOUBLE:
01201                 prop->idx = set->n_double;
01202                 set->n_double += 1;
01203                 set->d = realloc(set->d, sizeof(double *) * set->n_double);
01204                 
01205                 set->d[prop->idx] = malloc(sizeof(double) * set->num_events);
01206                 memcpy(set->d[prop->idx], buf, sizeof(double) * set->num_events);
01207                 break;
01208         }
01209         if (buf)
01210                 free(buf);
01211 
01212         if (size != data_size)
01213         {
01214                 _ra_set_error(prop, RA_ERR_EVAL_FILE_CORRUPT, "libRASCH");
01215                 return -1;
01216         }
01217 
01218         return ret;
01219 } /* handle_value_events() */
01220 
01221 
01229 int
01230 read_templates_xml(struct event_set *set, xmlNodePtr parent)
01231 {
01232         xmlNodePtr child, node = parent->children;
01233         xmlChar *c;
01234         struct template_struct *templ = set->templ;
01235 
01236         while (node)
01237         {
01238                 if (xmlStrcmp(node->name, (xmlChar *)"Template") != 0)
01239                 {
01240                         node = node->next;
01241                         continue;
01242                 }
01243 
01244                 templ = calloc(1, sizeof(struct template_struct));
01245                 templ->handle_id = RA_HANDLE_TEMPLATE;
01246                 templ->set = set;
01247                 ra_list_add((void **) &(set->templ), templ);
01248                 set->num_templates++;
01249 
01250                 child = node->children;
01251                 while (child)
01252                 {
01253                         if (xmlStrcmp(child->name, (xmlChar *)"Channel") != 0)
01254                         {
01255                                 child = child->next;
01256                                 continue;
01257                         }
01258 
01259                         templ->num_ch++;
01260                         templ->ch = realloc(templ->ch, sizeof(int) * templ->num_ch);
01261                         templ->template_size =
01262                                 realloc(templ->template_size, sizeof(int) * templ->num_ch);
01263                         c = xmlGetProp(child, (xmlChar *)"number");
01264                         if (c)
01265                         {
01266                                 templ->ch[templ->num_ch - 1] = atoi((const char *)c);
01267                                 xmlFree(c);
01268                         }
01269                         c = xmlGetProp(child, (xmlChar *)"size");
01270                         if (c)
01271                         {
01272                                 templ->template_size[templ->num_ch - 1] = atoi((const char *)c);
01273                                 xmlFree(c);
01274                         }
01275 
01276                         child = child->next;
01277                 }
01278 
01279                 c = xmlGetProp(node, (xmlChar *)"event-prop");
01280                 if (c)
01281                 {
01282                         struct event_property *prop = find_prop(set->eval, (const char *)c);
01283                         if (prop)
01284                                 prop->templ = templ;
01285                         xmlFree(c);
01286                 }
01287 
01288                 if (read_single_template_xml(templ, node) != 0)
01289                 {
01290                         _ra_set_error(set, RA_ERR_EVAL_FILE_CORRUPT, "libRASCH");
01291                         return -1;
01292                 }
01293 
01294                 node = node->next;
01295         }
01296 
01297         return 0;
01298 } /* read_templates_xml() */
01299 
01300 
01308 prop_handle
01309 find_prop(struct eval *e, const char *name)
01310 {
01311         struct event_property *prop = e->prop;
01312 
01313         while (prop)
01314         {
01315                 if (strcmp(prop->id_ascii, name) == 0)
01316                         return prop;
01317 
01318                 prop = prop->next;
01319         }
01320 
01321         return NULL;
01322 } /* find_prop() */
01323 
01324 
01332 int
01333 read_single_template_xml(struct template_struct *templ, xmlNodePtr parent)
01334 {
01335         void *t;
01336         long size;
01337         xmlChar *c;
01338         xmlNodePtr curr, node = parent->children;
01339 
01340         while (node)
01341         {
01342                 if (xmlStrcmp(node->name, (xmlChar *)"single") != 0)
01343                 {
01344                         node = node->next;
01345                         continue;
01346                 }
01347 
01348                 templ->num_templates++;
01349                 templ->st = realloc(templ->st, sizeof(struct single_template) * templ->num_templates);
01350 
01351                 c = xmlGetProp(node, (xmlChar *)"number");
01352                 if (c)
01353                 {
01354                         templ->st[templ->num_templates - 1].num = atol((const char *)c);
01355                         xmlFree(c);
01356                 }
01357                 c = xmlGetProp(node, (xmlChar *)"fiducial-offset");
01358                 if (c)
01359                 {
01360                         templ->st[templ->num_templates - 1].fiducial_offset = atoi((const char *)c);
01361                         xmlFree(c);
01362                 }
01363                 c = xmlGetProp(node, (xmlChar *)"type");
01364                 if (c)
01365                 {
01366                         templ->st[templ->num_templates - 1].type = atol((const char *)c);
01367                         xmlFree(c);
01368                 }
01369 
01370                 templ->st[templ->num_templates - 1].data = malloc(sizeof(double *) * templ->num_ch);
01371                 curr = node->children;
01372                 while (curr)
01373                 {
01374                         int idx;
01375 
01376                         if (xmlStrcmp(curr->name, (xmlChar *)"data") != 0)
01377                         {
01378                                 curr = curr->next;
01379                                 continue;
01380                         }
01381 
01382                         c = xmlGetProp(curr, (xmlChar *)"ch-index");
01383                         if (!c)
01384                                 return -1;      /* file corrupt */
01385                         idx = atoi((const char *)c);
01386                         xmlFree(c);
01387                         if ((idx >= templ->num_ch) || (idx < 0))
01388                                 return -1;      /* file corrupt */
01389                         c = xmlNodeGetContent(curr);
01390                         if (!c)
01391                                 return -1;
01392                         decode_base64(c, strlen((const char *)c), &t, &size, -1);
01393                         templ->st[templ->num_templates - 1].data[idx] = malloc(size);
01394                         memcpy(templ->st[templ->num_templates - 1].data[idx], t, size);
01395                         xmlFree(c);
01396                         free(t);
01397 
01398                         curr = curr->next;
01399                 }
01400 
01401                 node = node->next;
01402         }
01403 
01404         return 0;
01405 } /* read_single_tempalate_xml() */

Generated on Fri May 27 11:32:38 2005 for libRASCH by  doxygen 1.4.2