Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hddm_t.c
Go to the documentation of this file.
1 /*
2  * hddm_t.c - DO NOT EDIT THIS FILE
3  *
4  * This file was generated automatically from the file ex1.xml by hddm-c.
5  * This c file contains the i/o interface to the c structures described
6  * in the data model (from ex1.xml). Any program that needs access to the
7  * data described in the model can compile this c source file, and make use
8  * of the input/output services provided.
9  *
10  * The hddm data model tool set was written by
11  * Richard Jones, University of Connecticut.
12  *
13  * For more information see the following web site
14  *
15  * http://zeus.phys.uconn.edu/halld/datamodel/doc
16  */
17 
18 #include "hddm_t.h"
19 
21 {
22  int rep = (n > 1) ? n-1 : 0;
23  int size = sizeof(t_Hits_t) + rep * sizeof(t_Hit_t);
24  t_Hits_t* p = malloc(size);
25  memset(p,0,size);
26  return p;
27 }
28 
30 {
31  int rep = (n > 1) ? n-1 : 0;
32  int size = sizeof(t_Sides_t) + rep * sizeof(t_Side_t);
33  t_Sides_t* p = malloc(size);
34  memset(p,0,size);
35  return p;
36 }
37 
39 {
40  int rep = (n > 1) ? n-1 : 0;
41  int size = sizeof(t_Slabs_t) + rep * sizeof(t_Slab_t);
42  t_Slabs_t* p = malloc(size);
43  memset(p,0,size);
44  return p;
45 }
46 
48 {
49  int size = sizeof(t_ForwardTOF_t);
50  t_ForwardTOF_t* p = malloc(size);
51  memset(p,0,size);
52  return p;
53 }
54 
56 {
57  int size = sizeof(t_HDDM_t);
58  t_HDDM_t* p = malloc(size);
59  memset(p,0,size);
60  return p;
61 }
62 
64 "<HDDM class=\"t\" version=\"1.0\">\n"
65 " <forwardTOF>\n"
66 " <slab y=\"float\" repeat=\"*\">\n"
67 " <side end=\"int\" repeat=\"*\">\n"
68 " <hit t=\"float\" dE=\"float\" repeat=\"*\" />\n"
69 " </side>\n"
70 " </slab>\n"
71 " </forwardTOF>\n"
72 "</HDDM>\n";
73 
74 int* sp;
75 int* dp;
76 
78 {
79  int size = *(sp++);
80  int rep = *(sp++);
81  t_Hits_t* his = 0;
82  if (size > 0)
83  {
84  int r;
85  his = make_t_Hits(rep);
86  his->mult = rep;
87  for (r = 0; r < rep; r++ )
88  {
89  dp = (int*) his;
90  *(dp++) = *(sp++);
91  *(dp++) = *(sp++);
92  }
93  }
94  return his;
95 }
96 
98 {
99  int size = *(sp++);
100  int rep = *(sp++);
101  t_Sides_t* his = 0;
102  if (size > 0)
103  {
104  int r;
105  his = make_t_Sides(rep);
106  his->mult = rep;
107  for (r = 0; r < rep; r++ )
108  {
109  dp = (int*) his;
110  *(dp++) = *(sp++);
111  }
112  }
113  return his;
114 }
115 
117 {
118  int size = *(sp++);
119  int rep = *(sp++);
120  t_Slabs_t* his = 0;
121  if (size > 0)
122  {
123  int r;
124  his = make_t_Slabs(rep);
125  his->mult = rep;
126  for (r = 0; r < rep; r++ )
127  {
128  dp = (int*) his;
129  *(dp++) = *(sp++);
130  }
131  }
132  return his;
133 }
134 
136 {
137  int size = *(sp++);
138  t_ForwardTOF_t* his = 0;
139  if (size > 0)
140  {
141  his = make_t_ForwardTOF();
142  {
143  dp = (int*) his;
144  }
145  }
146  return his;
147 }
148 
150 {
151  int size = *(sp++);
152  t_HDDM_t* his = 0;
153  if (size > 0)
154  {
155  his = make_t_HDDM();
156  {
157  dp = (int*) his;
158  }
159  }
160  return his;
161 }
162 
163 void* unpack_NULL()
164 {
165  int size = *(sp++);
166  sp += size;
167  return 0;
168 }
169 
171 {
172  t_HDDM_t* this = 0;
173  int ret = 0;
174  int* buff = malloc(1000000);
175  sp = buff;
176 
177  if (fp && (fp->iomode == HDDM_STREAM_INPUT))
178  {
179  ret = fread(buff,sizeof(int),1,fp->fd);
180  }
181  if (ret)
182  {
183  ret = *buff;
184  ret -= fread(&sp[1],sizeof(int),ret,fp->fd);
185  }
186  else
187  {
188  free(buff);
189  return 0;
190  }
191  if (ret == 0)
192  {
193  int s;
194  void* ptrStack[10000];
195  void* (*unpacker)() = fp->unpacker[0];
196  ptrStack[0] = (*unpacker)();
197  for (s = 1; s < fp->sections; s++)
198  {
199  int ptrOffset = fp->ptrOffset[s];
200  int level = fp->modelLevel[s];
201  int* ptr = ptrStack[level - 1];
202  if (ptr != 0)
203  {
204  void** pp = (void**) &ptr[ptrOffset];
205  unpacker = fp->unpacker[s];
206  ptrStack[level] = (*unpacker)();
207  *pp = ptrStack[level];
208  }
209  else
210  {
211  ptrStack[level] = 0;
212  }
213  }
214  }
215  free(buff);
216  return this;
217 }
218 
219 static int pack_t_Hits(t_Hits_t* pp, t_iostream* fp)
220 {
221  int* dpp = dp++;
222  int* spp = (int*) pp;
223  int* dpm = dp++;
224  if (spp != 0)
225  {
226  int n;
227  *dpm = *(spp++);
228  for (n = 0; n < *dpm; n++ )
229  {
230  *(dp++) = *(spp++);
231  *(dp++) = *(spp++);
232  }
233  free(pp);
234  }
235  *dpp = dp - dpp - 1;
236  return *dpp;
237 }
238 
239 static int pack_t_Sides(t_Sides_t* pp, t_iostream* fp)
240 {
241  int* dpp = dp++;
242  int* spp = (int*) pp;
243  int* dpm = dp++;
244  if (spp != 0)
245  {
246  int n;
247  *dpm = *(spp++);
248  for (n = 0; n < *dpm; n++ )
249  {
250  *(dp++) = *(spp++);
251  pack_t_Hits(*(t_Hits_t**) spp++, fp);
252  }
253  free(pp);
254  }
255  *dpp = dp - dpp - 1;
256  return *dpp;
257 }
258 
259 static int pack_t_Slabs(t_Slabs_t* pp, t_iostream* fp)
260 {
261  int* dpp = dp++;
262  int* spp = (int*) pp;
263  int* dpm = dp++;
264  if (spp != 0)
265  {
266  int n;
267  *dpm = *(spp++);
268  for (n = 0; n < *dpm; n++ )
269  {
270  *(dp++) = *(spp++);
271  pack_t_Sides(*(t_Sides_t**) spp++, fp);
272  }
273  free(pp);
274  }
275  *dpp = dp - dpp - 1;
276  return *dpp;
277 }
278 
280 {
281  int* dpp = dp++;
282  int* spp = (int*) pp;
283  if (spp != 0)
284  {
285  {
286  pack_t_Slabs(*(t_Slabs_t**) spp++, fp);
287  }
288  free(pp);
289  }
290  *dpp = dp - dpp - 1;
291  return *dpp;
292 }
293 
294 static int pack_t_HDDM(t_HDDM_t* pp, t_iostream* fp)
295 {
296  int* dpp = dp++;
297  int* spp = (int*) pp;
298  if (spp != 0)
299  {
300  {
301  pack_t_ForwardTOF(*(t_ForwardTOF_t**) spp++, fp);
302  }
303  free(pp);
304  }
305  *dpp = dp - dpp - 1;
306  return *dpp;
307 }
308 
310 {
311  int ret = 0;
312  int* buff = malloc(1000000);
313  dp = buff;
314  sp = (int*) p;
315 
316  if (p != 0)
317  {
318  pack_t_HDDM((t_HDDM_t*) (sp++), fp);
319  }
320  if (fp && (fp->iomode == HDDM_STREAM_OUTPUT))
321  {
322  ret = fwrite(buff,sizeof(int),*buff+1,fp->fd);
323  }
324  free(buff);
325  return ret;
326 }
327 
328 static int getTag(char* d, char* tag)
329 {
330  int level;
331  char line[500];
332  strncpy(line,d,500);
333  line[499] = 0;
334  level = strlen(strtok(line,"<"));
335  strncpy(tag,strtok(NULL," >"),500);
336  return level/2;
337 }
338 
339 static char* getEndTag(char* d, char* tag)
340 {
341  char line[500];
342  char endTag[510];
343  strncpy(line,d,500);
344  line[499] = 0;
345  if (strstr(strtok(line,"\n"),"/>") == 0)
346  {
347  sprintf(endTag,"</%s>",tag);
348  }
349  else
350  {
351  strcpy(endTag,"/>");
352  }
353  return strstr(d,endTag);
354 }
355 
356 static int matches(char* d, char* c, t_iostream* fp)
357 {
358  char ctag[500];
359  char dtag[500];
360  int clevel, dlevel;
361  dlevel = getTag(d,dtag);
362  while ((clevel = getTag(c,ctag)) == dlevel)
363  {
364  int ptrSeqNo = 0;
365  if ((clevel == dlevel) && (strcmp(ctag,dtag) == 0))
366  {
367  int len = index(c+1,'\n') - c;
368  if (strncmp(c,d,len) != 0)
369  {
370  return -1;
371  }
372  fp->modelLevel[fp->sections] = dlevel;
373  if (strcmp(dtag,"hit") == 0)
374  {
375  fp->unpacker[fp->sections] = (void*) unpack_t_Hits;
376  fp->ptrOffset[fp->sections] = 2 + ptrSeqNo*sizeof(int*)/sizeof(int);
377  ++fp->sections;
378  }
379  else if (strcmp(dtag,"side") == 0)
380  {
381  fp->unpacker[fp->sections] = (void*) unpack_t_Sides;
382  fp->ptrOffset[fp->sections] = 1 + ptrSeqNo*sizeof(int*)/sizeof(int);
383  ++fp->sections;
384  }
385  else if (strcmp(dtag,"slab") == 0)
386  {
387  fp->unpacker[fp->sections] = (void*) unpack_t_Slabs;
388  fp->ptrOffset[fp->sections] = 1 + ptrSeqNo*sizeof(int*)/sizeof(int);
389  ++fp->sections;
390  }
391  else if (strcmp(dtag,"forwardTOF") == 0)
392  {
393  fp->unpacker[fp->sections] = (void*) unpack_t_ForwardTOF;
394  fp->ptrOffset[fp->sections] = 0 + ptrSeqNo*sizeof(int*)/sizeof(int);
395  ++fp->sections;
396  }
397  else if (strcmp(dtag,"HDDM") == 0)
398  {
399  fp->unpacker[fp->sections] = (void*) unpack_t_HDDM;
400  fp->ptrOffset[fp->sections] = 0 + ptrSeqNo*sizeof(int*)/sizeof(int);
401  ++fp->sections;
402  }
403  else
404  {
405  return -1;
406  }
407  c = index(c+1,'\n');
408  d = index(d+1,'\n');
409  while (getTag(d,dtag) > dlevel)
410  {
411  if (matches(d,c,fp))
412  {
413  return -1;
414  }
415  else
416  {
417  d = getEndTag(d,dtag);
418  }
419  d = index(d+1,'\n');
420  }
421  return 0;
422  }
423  else
424  {
425  c = getEndTag(c,ctag);
426  ++ptrSeqNo;
427  }
428  c = index(c+1,'\n');
429  }
430  fp->unpacker[fp->sections++] = unpack_NULL;
431  return 0;
432 }
433 
435 {
436  t_iostream* fp = malloc(sizeof(t_iostream));
437  char* p;
438  fp->fd = fopen(filename,"r");
439  if (fp->fd == 0)
440  {
441  free(fp);
442  return 0;
443  }
445  fp->hddm = malloc(1000000);
446  p = fp->hddm;
447  *p = 0;
448  while (strcmp(p,"</HDDM>\n") != 0)
449  {
450  int len = strlen(fp->hddm);
451  if (len < 990000)
452  {
453  p = fp->hddm + len;
454  fgets(p,10000,fp->fd);
455  }
456  else
457  {
458  break;
459  }
460  }
461  fp->sections = 0;
462  if (matches(fp->hddm,HDDM_t_DocumentString,fp) != 0)
463  {
464  fprintf(stderr,"HDDM Error: ");
465  fprintf(stderr,"input file data model ");
466  fprintf(stderr,"does not match ");
467  fprintf(stderr,"compiled code.\n");
468  fprintf(stderr,"Please recompile.\n");
469  exit(9);
470  }
471  fp->filename = malloc(strlen(filename) + 1);
472  strcpy(fp->filename,filename);
473  return fp;
474 }
475 
477 {
478  int len;
479  t_iostream* fp = malloc(sizeof(t_iostream));
480  fp->fd = fopen(filename,"w");
481  if (fp->fd == 0)
482  {
483  free(fp);
484  return 0;
485  }
487  len = strlen(HDDM_t_DocumentString);
488  fp->hddm = malloc(len + 1);
489  strcpy(fp->hddm,HDDM_t_DocumentString);
490  if (fwrite(fp->hddm,1,len,fp->fd) != len)
491  {
492  fprintf(stderr,"HDDM Error: ");
493  fprintf(stderr,"error writing to ");
494  fprintf(stderr,"output file %s",filename);
495  exit(9);
496  }
497  fp->filename = malloc(strlen(filename) + 1);
498  strcpy(fp->filename,filename);
499  return fp;
500 }
501 
503 {
504  fclose(fp->fd);
505  free(fp->hddm);
506  free(fp->filename);
507  free(fp);
508 }
static int pack_t_Hits(t_Hits_t *pp, t_iostream *fp)
Definition: hddm_t.c:219
t_Sides_t * make_t_Sides(int n)
Definition: hddm_t.c:29
#define HDDM_STREAM_OUTPUT
Definition: hddm_t.h:101
t_iostream * open_t_HDDM(char *filename)
Definition: hddm_t.c:434
char * hddm
Definition: hddm_t.h:106
static int pack_t_ForwardTOF(t_ForwardTOF_t *pp, t_iostream *fp)
Definition: hddm_t.c:279
Definition: hddm_t.h:27
char * filename
Definition: hddm_t.h:107
sprintf(text,"Post KinFit Cut")
#define c
TString filename
void * unpack_NULL()
Definition: hddm_t.c:163
t_iostream * init_t_HDDM(char *filename)
Definition: hddm_t.c:476
int * sp
Definition: hddm_t.c:74
static char index(char c)
Definition: base64.cpp:115
t_Slabs_t * make_t_Slabs(int n)
Definition: hddm_t.c:38
static int pack_t_Sides(t_Sides_t *pp, t_iostream *fp)
Definition: hddm_t.c:239
int flush_t_HDDM(t_HDDM_t *p, t_iostream *fp)
Definition: hddm_t.c:309
static int pack_t_HDDM(t_HDDM_t *pp, t_iostream *fp)
Definition: hddm_t.c:294
void *(* unpacker[10000])()
Definition: hddm_t.h:109
FILE * fd
Definition: hddm_t.h:104
static int pack_t_Slabs(t_Slabs_t *pp, t_iostream *fp)
Definition: hddm_t.c:259
void close_t_HDDM(t_iostream *fp)
Definition: hddm_t.c:502
t_HDDM_t * read_t_HDDM(t_iostream *fp)
Definition: hddm_t.c:170
t_ForwardTOF_t * make_t_ForwardTOF()
Definition: hddm_t.c:47
static t_Sides_t * unpack_t_Sides()
Definition: hddm_t.c:97
static char * getEndTag(char *d, char *tag)
Definition: hddm_t.c:339
int sections
Definition: hddm_t.h:108
int mult
Definition: hddm_t.h:47
int mult
Definition: hddm_t.h:61
int * dp
Definition: hddm_t.c:75
int mult
Definition: hddm_t.h:33
static int matches(char *d, char *c, t_iostream *fp)
Definition: hddm_t.c:356
int ptrOffset[10000]
Definition: hddm_t.h:111
int iomode
Definition: hddm_t.h:105
#define HDDM_STREAM_INPUT
Definition: hddm_t.h:100
t_Hits_t * make_t_Hits(int n)
Definition: hddm_t.c:20
static t_ForwardTOF_t * unpack_t_ForwardTOF()
Definition: hddm_t.c:135
static t_Slabs_t * unpack_t_Slabs()
Definition: hddm_t.c:116
static t_Hits_t * unpack_t_Hits()
Definition: hddm_t.c:77
int modelLevel[10000]
Definition: hddm_t.h:110
char HDDM_t_DocumentString[]
Definition: hddm_t.c:63
static t_HDDM_t * unpack_t_HDDM()
Definition: hddm_t.c:149
static int getTag(char *d, char *tag)
Definition: hddm_t.c:328
t_HDDM_t * make_t_HDDM()
Definition: hddm_t.c:55