1
/*******************************************************************************
3
* mcf_NTuIOUtils.c -- Utilities to manipulate files within the MCFIO Gen. *
6
* P. Lebrun, September 1995. *
8
*******************************************************************************/
12
#include <sys/param.h>
14
#include <rpc/types.h>
15
#include <sys/types.h>
17
#include "mcf_nTupleDescript.h"
19
#include "mcfio_Dict.h"
20
#include "mcf_NTuIOFiles.h"
21
#include "mcf_NTuIOUtils.h"
22
#include "mcf_ntubld_db.h"
30
extern nTuDDL **NTuDDLList;
31
extern int NumOfNTuples;
33
nTuDDL *mcf_GetNTuByPtrID(int id)
37
if ( (id < 1) || (id > NumOfNTuples)) return NULL;
38
ip = (int **) NTuDDLList;
40
return (nTuDDL *) *ip;
43
nTuDDL *mcf_GetNTuByStreamID(int stream, int id)
48
for (i=0, num=0; i<NumOfNTuples; i++) {
50
if ((ddl->streamId == stream) && (ddl->seqNTuId == id)) return ddl;
54
int mcf_NTuId(int uid, char *category)
57
category Category name, must be an exact match
59
Returns: Macfio_Ntuple id, or -1 if no items matched, or if
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))
76
if (strcmp(category, item->category) == 0)
78
j = strspn(category, " ");
79
if (strcmp((category+j), item->category) == 0)
86
int mcfioC_GetNTupleIds(int stream, int *ids, int max)
91
for (i=0, num=0; i<NumOfNTuples; i++) {
93
if (ddl->streamId == stream) {
94
if (num < max ) ids[num] = ddl->id;
101
int mcfioC_GetNTupleUID(int stream, int id)
103
nTuDDL *ddl = mcf_GetNTuByStreamID(stream, id);
107
void mcfioC_GetNTupleCategory(int stream, int id, char **answer)
109
nTuDDL *ddl = mcf_GetNTuByStreamID(stream, id);
110
*answer = ddl->category;
113
void mcfioC_GetNTupleTitle(int stream, int id, char **answer)
115
nTuDDL *ddl = mcf_GetNTuByStreamID(stream, id);
116
*answer = ddl->title;
119
void mcfioC_GetNTupleName(int stream, int id, char **answer)
121
nTuDDL *ddl = mcf_GetNTuByStreamID(stream, id);
122
if (ddl->reference == NULL)
123
*answer = ddl->descrNtu->title;
124
else *answer = ddl->reference->descrNtu->title;
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.
132
void CopyVarGenNtuple(varGenNtuple *vFrom, varGenNtuple *vTo)
134
char *string, *tc, *tc2;
137
if ((vTo == NULL) || (vFrom == NULL)) return;
138
vTo->nameBlank = vFrom->nameBlank;
139
if (vTo->name != NULL) {
143
if (vFrom->name != NULL) {
144
ll = (1 + strlen(vFrom->name));
146
(char *) malloc(sizeof(char) * ll);
147
strcpy(vTo->name, vFrom->name);
149
if (vTo->description != NULL) {
150
free(vTo->description);
151
vTo->description = NULL;
153
if (vFrom->description != NULL) {
155
(char *) malloc(sizeof(char) * (1 + strlen(vFrom->description)));
156
strcpy(vTo->description, vFrom->description);
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];
165
vTo->offset = vFrom->offset;
166
vTo->offsetXDR = vFrom->offsetXDR;
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!).
172
void AddNTuDDLtoList(nTuDDL *ddl)
177
ddl->id = NumOfNTuples;
179
** insert this ddl into the Global List, expand the list if need be
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)));
189
NTuDDLList[NumOfNTuples-1] = ddl;
193
** Free the memory for a Ntuple Data Descrp. Lang (DDL).
195
void DestroyNTuDDL(nTuDDL *ddl)
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);
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..
209
void DestroyGenNtuple(descrGenNtuple *dNTu)
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);
226
void DestroyVarGenNtuple(varGenNtuple *var)
229
if (var == NULL) return;
230
if (var->name != NULL) free(var->name);
231
if (var->description != NULL) free(var->description);
235
* ValidStr - Validate strings supplied by user
237
* returns: pointer to valid same or new truncated string
239
* Note: ** copy string returned, if needed, before calling ValidStr again **
241
char *mcf_ValidStr(char *string, int max_length, char *strKind)
243
static char str[NTU_MAX_CATEGORY_LENGTH+1]; /* make longest string */
244
static char str1[1] = "";
247
return str1; /* return empty string */
248
if (strlen(string) <= max_length)
249
return string; /* return pointer to same string */
251
"Mcfio_Ntuple: Error. Specified %s string is too long, truncating\n ->%s\n",
253
memset(str, 0, NTU_MAX_CATEGORY_LENGTH+1);
254
return strncpy(str, string, max_length); /* return ptr to trunc. string */
257
** Based on the HistoScope Check Category
259
int mcf_CheckValidCat(char *category, int dotDotDot)
261
static char validChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\
262
abcdefghijklmnopqrstuvwxyz1234567890/~!@#$%^&*()_+=-`\"\'\t?><,. ";
263
char *strDots, *error = NULL;
266
if (category == NULL)
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 \"...\"";
285
fprintf(stderr, "Error in declared category %s: %s\n",
289
return (strDots == NULL ? 1 : -1);