~maddevelopers/mg5amcnlo/2.9.4

« back to all changes in this revision

Viewing changes to vendor/StdHEP/mcfio/src/mcf_NTuIOUtils.c

pass to v2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
*                                                                              *
 
3
* mcf_NTuIOUtils.c -- Utilities to manipulate files within the MCFIO Gen.      *
 
4
*                                       Ntuple schema                          *
 
5
*                                                                              *
 
6
*       P. Lebrun, September 1995.                                             *
 
7
*                                                                              *
 
8
*******************************************************************************/
 
9
#include <stdio.h>
 
10
#include <string.h>
 
11
#include <stdlib.h>
 
12
#include <sys/param.h>
 
13
#include <limits.h>
 
14
#include <rpc/types.h>
 
15
#include <sys/types.h>
 
16
#include <rpc/xdr.h>
 
17
#include "mcf_nTupleDescript.h"
 
18
#include "mcf_xdr.h"
 
19
#include "mcfio_Dict.h"
 
20
#include "mcf_NTuIOFiles.h"
 
21
#include "mcf_NTuIOUtils.h"
 
22
#include "mcf_ntubld_db.h"
 
23
#ifndef False
 
24
#define False 0
 
25
#endif
 
26
#ifndef True
 
27
#define True 1
 
28
#endif
 
29
 
 
30
extern nTuDDL **NTuDDLList;
 
31
extern int NumOfNTuples;
 
32
 
 
33
nTuDDL *mcf_GetNTuByPtrID(int id)
 
34
{
 
35
    int **ip;
 
36
    
 
37
    if ( (id < 1) || (id > NumOfNTuples)) return NULL;
 
38
    ip = (int **) NTuDDLList;
 
39
    ip += (id-1);
 
40
    return (nTuDDL *) *ip; 
 
41
}
 
42
     
 
43
nTuDDL *mcf_GetNTuByStreamID(int stream, int id)
 
44
{
 
45
     int i, num;
 
46
     nTuDDL *ddl;
 
47
     
 
48
     for (i=0, num=0; i<NumOfNTuples; i++) { 
 
49
          ddl = NTuDDLList[i];
 
50
          if ((ddl->streamId == stream) && (ddl->seqNTuId == id)) return ddl;
 
51
     }
 
52
     return NULL;
 
53
}   
 
54
int mcf_NTuId(int uid, char *category)
 
55
/* 
 
56
        uid             Unique User id 
 
57
        category        Category name, must be an exact match
 
58
 
 
59
        Returns:        Macfio_Ntuple id, or -1 if no items matched, or if 
 
60
                        Category is illegal..
 
61
*/
 
62
{
 
63
        int i, j, **ip;
 
64
        nTuDDL *item;
 
65
        char *cat;
 
66
        
 
67
     if (!mcf_CheckValidCat(category, FALSE))  return -1;
 
68
     ip = (int **) NTuDDLList;
 
69
     cat = mcf_ValidStr(category, NTU_MAX_CATEGORY_LENGTH, "category");
 
70
     for (i=0; i< NumOfNTuples; i++, ip++) {
 
71
         item = (nTuDDL *) *ip;
 
72
         if (item->uid == uid) { /* Look first at uid, if match, */
 
73
                                    /* Confirm with Category */
 
74
            if ((category == NULL) && (item->category == NULL)) 
 
75
                                        return (item->id);
 
76
            if (strcmp(category, item->category) == 0)
 
77
                                        return (item->id);
 
78
            j = strspn(category, " ");  
 
79
            if (strcmp((category+j), item->category) == 0)
 
80
                                        return (item->id); 
 
81
         }
 
82
     }
 
83
     return -1;
 
84
}
 
85
 
 
86
int mcfioC_GetNTupleIds(int stream, int *ids, int max)
 
87
{
 
88
     int i, num;
 
89
     nTuDDL *ddl;
 
90
     
 
91
     for (i=0, num=0; i<NumOfNTuples; i++) { 
 
92
          ddl = NTuDDLList[i];
 
93
          if (ddl->streamId == stream) {
 
94
              if (num < max ) ids[num] = ddl->id;
 
95
              num++;
 
96
          }
 
97
     }
 
98
     return num;
 
99
}   
 
100
 
 
101
int mcfioC_GetNTupleUID(int stream, int id)
 
102
{
 
103
   nTuDDL *ddl = mcf_GetNTuByStreamID(stream, id);
 
104
   return ddl->uid;
 
105
}
 
106
 
 
107
void mcfioC_GetNTupleCategory(int stream, int id, char **answer)
 
108
{
 
109
   nTuDDL *ddl = mcf_GetNTuByStreamID(stream, id);
 
110
   *answer = ddl->category;
 
111
}
 
112
 
 
113
void mcfioC_GetNTupleTitle(int stream, int id, char **answer)
 
114
{
 
115
   nTuDDL *ddl = mcf_GetNTuByStreamID(stream, id);
 
116
   *answer = ddl->title;
 
117
}
 
118
 
 
119
void mcfioC_GetNTupleName(int stream, int id, char **answer)
 
120
{
 
121
   nTuDDL *ddl = mcf_GetNTuByStreamID(stream, id);
 
122
   if (ddl->reference == NULL) 
 
123
       *answer = ddl->descrNtu->title;
 
124
   else *answer = ddl->reference->descrNtu->title;    
 
125
}
 
126
 
 
127
/*
 
128
** Copy utility routine for a General Ntuple Variable descriptor d/s
 
129
** It is the responsability of the usr to allocate memory for the 
 
130
** structure where data will be copied to.
 
131
*/           
 
132
void CopyVarGenNtuple(varGenNtuple *vFrom, varGenNtuple *vTo)
 
133
{
 
134
    char *string, *tc, *tc2;
 
135
    int i, ll;
 
136
 
 
137
    if ((vTo == NULL)  || (vFrom == NULL)) return;
 
138
    vTo->nameBlank = vFrom->nameBlank;
 
139
    if (vTo->name != NULL) {
 
140
        free(vTo->name);
 
141
        vTo->name = NULL;
 
142
    }
 
143
    if (vFrom->name != NULL) {
 
144
        ll = (1 + strlen(vFrom->name));
 
145
        vTo->name = 
 
146
           (char *) malloc(sizeof(char) * ll);
 
147
        strcpy(vTo->name, vFrom->name);
 
148
    }
 
149
    if (vTo->description != NULL) {
 
150
         free(vTo->description);
 
151
         vTo->description = NULL;
 
152
    }
 
153
    if (vFrom->description != NULL) {
 
154
        vTo->description = 
 
155
           (char *) malloc(sizeof(char) * (1 + strlen(vFrom->description)));
 
156
        strcpy(vTo->description, vFrom->description);
 
157
    }
 
158
    vTo->type = vFrom->type;
 
159
    vTo->isFixedSize = vFrom->isFixedSize;
 
160
    vTo->numDim = vFrom->numDim;
 
161
    if (vFrom->numDim > 0)  {
 
162
           for (i=0; i<vFrom->numDim; i++)
 
163
              vTo->dimensions[i] = vFrom->dimensions[i]; 
 
164
    }
 
165
    vTo->offset = vFrom->offset;
 
166
    vTo->offsetXDR = vFrom->offsetXDR;
 
167
}
 
168
/*
 
169
** insert this ddl into the Global List, expand the list if need be.
 
170
** Also increment the number of NTuples defined (don't do it twice!). 
 
171
*/
 
172
void AddNTuDDLtoList(nTuDDL *ddl)
 
173
{
 
174
    int i, **ipo;
 
175
    
 
176
    NumOfNTuples++;
 
177
    ddl->id = NumOfNTuples;
 
178
    /*
 
179
    ** insert this ddl into the Global List, expand the list if need be
 
180
    */
 
181
    if( (NumOfNTuples - (NumOfNTuples/NTU_START_LIST_SIZE)*NTU_START_LIST_SIZE)
 
182
                     == 1 && (NumOfNTuples != 1)) {
 
183
            ipo = (int **) NTuDDLList;
 
184
            NTuDDLList  = (nTuDDL **) malloc(sizeof(int *)*
 
185
              ((NumOfNTuples/NTU_START_LIST_SIZE + 1)*NTU_START_LIST_SIZE));
 
186
            memcpy(NTuDDLList, ipo, (sizeof(int *)*(NumOfNTuples-1)));
 
187
            free (ipo);
 
188
    }
 
189
    NTuDDLList[NumOfNTuples-1] = ddl;
 
190
            
 
191
}
 
192
/*
 
193
** Free the memory for a Ntuple Data Descrp. Lang (DDL).  
 
194
*/
 
195
void DestroyNTuDDL(nTuDDL *ddl)
 
196
{
 
197
   int i;
 
198
   if (ddl->title != NULL) free(ddl->title);
 
199
   if (ddl->category != NULL) free(ddl->category);
 
200
   if (ddl->dbinFileName != NULL) free(ddl->dbinFileName);
 
201
   DestroyGenNtuple(ddl->descrNtu);
 
202
   free(ddl);
 
203
 
204
/*
 
205
** Free the memory for a Description NTuple
 
206
** Note : the pointer to adrresses are lost, the user will have to give 
 
207
** them to this application back..
 
208
*/
 
209
void DestroyGenNtuple(descrGenNtuple *dNTu)
 
210
{
 
211
    int i;
 
212
 
 
213
    if (dNTu == NULL) return;
 
214
    if (dNTu->title != NULL) free(dNTu->title);
 
215
    if (dNTu->description != NULL) free(dNTu->description);
 
216
    if (dNTu->varOrdering != NULL) free(dNTu->varOrdering);
 
217
    if (dNTu->subOffset != NULL) free(dNTu->subOffset);
 
218
    if (dNTu->subXDROffset != NULL) free(dNTu->subXDROffset);
 
219
    for (i=0; i<dNTu->numAvailable; i++)
 
220
         DestroyVarGenNtuple(dNTu->variables[i]);
 
221
    free(dNTu->variables);
 
222
    free(dNTu);     
 
223
}    
 
224
 
 
225
 
 
226
void DestroyVarGenNtuple(varGenNtuple *var)
 
227
{
 
228
 
 
229
    if (var == NULL) return;
 
230
    if (var->name != NULL) free(var->name);
 
231
    if (var->description != NULL) free(var->description);
 
232
    free(var);
 
233
}    
 
234
/*
 
235
 * ValidStr - Validate strings supplied by user
 
236
 *
 
237
 *            returns: pointer to valid same or new truncated string
 
238
 *
 
239
 *  Note: ** copy string returned, if needed, before calling ValidStr again **
 
240
 */
 
241
char *mcf_ValidStr(char *string, int max_length, char *strKind)
 
242
{
 
243
    static char str[NTU_MAX_CATEGORY_LENGTH+1];      /* make longest string */
 
244
    static char str1[1] = "";
 
245
    
 
246
    if (string == NULL)
 
247
        return str1;                       /* return empty string           */
 
248
    if (strlen(string) <= max_length)
 
249
        return string;                     /* return pointer to same string */
 
250
    fprintf(stderr,
 
251
      "Mcfio_Ntuple: Error. Specified %s string is too long, truncating\n     ->%s\n",
 
252
         strKind, string);
 
253
    memset(str, 0, NTU_MAX_CATEGORY_LENGTH+1);
 
254
    return strncpy(str, string, max_length); /* return ptr to trunc. string */
 
255
}
 
256
/*
 
257
** Based on the HistoScope Check Category 
 
258
*/      
 
259
int mcf_CheckValidCat(char *category, int dotDotDot)
 
260
{
 
261
    static char validChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\
 
262
abcdefghijklmnopqrstuvwxyz1234567890/~!@#$%^&*()_+=-`\"\'\t?><,. ";
 
263
    char *strDots, *error = NULL;
 
264
    int len;
 
265
    
 
266
    if (category == NULL)
 
267
        return 1;
 
268
    len = strlen(category);
 
269
    strDots = strstr(category, "...");
 
270
    if (len >= NTU_MAX_CATEGORY_LENGTH)
 
271
        error = "is too long";
 
272
    else if (strspn(category, validChars) != len)
 
273
        error = "contains invalid characters";
 
274
    else if (strstr(category, "//") != NULL)
 
275
        error = "contains \"//\"";
 
276
    else if (category[0] == '/')
 
277
        error = "contains leading slash";
 
278
    else if (category[len-1] == '/')
 
279
        error = "contains trailing slash";
 
280
    else if ((dotDotDot == 0 && strDots != NULL) 
 
281
          || (dotDotDot != 0 && strDots != NULL && strDots != category + len-3))
 
282
        error = "contains invalid \"...\"";
 
283
        
 
284
    if (error != NULL) {
 
285
        fprintf(stderr, "Error in declared category %s: %s\n",
 
286
                error, category);
 
287
        return 0;
 
288
    } else {
 
289
        return (strDots == NULL ? 1 : -1);
 
290
    }
 
291
}
 
292