source: trunk/mcfio/mcf_NTuIOUtils.c@ 18

Last change on this file since 18 was 2, checked in by Pavel Demin, 16 years ago

first commit

File size: 8.2 KB
RevLine 
[2]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
30extern nTuDDL **NTuDDLList;
31extern int NumOfNTuples;
32
33nTuDDL *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
43nTuDDL *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}
54int 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
86int 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
101int mcfioC_GetNTupleUID(int stream, int id)
102{
103 nTuDDL *ddl = mcf_GetNTuByStreamID(stream, id);
104 return ddl->uid;
105}
106
107void mcfioC_GetNTupleCategory(int stream, int id, char **answer)
108{
109 nTuDDL *ddl = mcf_GetNTuByStreamID(stream, id);
110 *answer = ddl->category;
111}
112
113void mcfioC_GetNTupleTitle(int stream, int id, char **answer)
114{
115 nTuDDL *ddl = mcf_GetNTuByStreamID(stream, id);
116 *answer = ddl->title;
117}
118
119void 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*/
132void CopyVarGenNtuple(varGenNtuple *vFrom, varGenNtuple *vTo)
133{
134 int i, ll;
135
136 if ((vTo == NULL) || (vFrom == NULL)) return;
137 vTo->nameBlank = vFrom->nameBlank;
138 if (vTo->name != NULL) {
139 free(vTo->name);
140 vTo->name = NULL;
141 }
142 if (vFrom->name != NULL) {
143 ll = (1 + strlen(vFrom->name));
144 vTo->name =
145 (char *) malloc(sizeof(char) * ll);
146 strcpy(vTo->name, vFrom->name);
147 }
148 if (vTo->description != NULL) {
149 free(vTo->description);
150 vTo->description = NULL;
151 }
152 if (vFrom->description != NULL) {
153 vTo->description =
154 (char *) malloc(sizeof(char) * (1 + strlen(vFrom->description)));
155 strcpy(vTo->description, vFrom->description);
156 }
157 vTo->type = vFrom->type;
158 vTo->isFixedSize = vFrom->isFixedSize;
159 vTo->numDim = vFrom->numDim;
160 if (vFrom->numDim > 0) {
161 for (i=0; i<vFrom->numDim; i++)
162 vTo->dimensions[i] = vFrom->dimensions[i];
163 }
164 vTo->offset = vFrom->offset;
165 vTo->offsetXDR = vFrom->offsetXDR;
166}
167/*
168** insert this ddl into the Global List, expand the list if need be.
169** Also increment the number of NTuples defined (don't do it twice!).
170*/
171void AddNTuDDLtoList(nTuDDL *ddl)
172{
173 int **ipo;
174
175 NumOfNTuples++;
176 ddl->id = NumOfNTuples;
177 /*
178 ** insert this ddl into the Global List, expand the list if need be
179 */
180 if( (NumOfNTuples - (NumOfNTuples/NTU_START_LIST_SIZE)*NTU_START_LIST_SIZE)
181 == 1 && (NumOfNTuples != 1)) {
182 ipo = (int **) NTuDDLList;
183 NTuDDLList = (nTuDDL **) malloc(sizeof(int *)*
184 ((NumOfNTuples/NTU_START_LIST_SIZE + 1)*NTU_START_LIST_SIZE));
185 memcpy(NTuDDLList, ipo, (sizeof(int *)*(NumOfNTuples-1)));
186 free (ipo);
187 }
188 NTuDDLList[NumOfNTuples-1] = ddl;
189
190}
191/*
192** Free the memory for a Ntuple Data Descrp. Lang (DDL).
193*/
194void DestroyNTuDDL(nTuDDL *ddl)
195{
196 if (ddl->title != NULL) free(ddl->title);
197 if (ddl->category != NULL) free(ddl->category);
198 if (ddl->dbinFileName != NULL) free(ddl->dbinFileName);
199 DestroyGenNtuple(ddl->descrNtu);
200 free(ddl);
201}
202/*
203** Free the memory for a Description NTuple
204** Note : the pointer to adrresses are lost, the user will have to give
205** them to this application back..
206*/
207void DestroyGenNtuple(descrGenNtuple *dNTu)
208{
209 int i;
210
211 if (dNTu == NULL) return;
212 if (dNTu->title != NULL) free(dNTu->title);
213 if (dNTu->description != NULL) free(dNTu->description);
214 if (dNTu->varOrdering != NULL) free(dNTu->varOrdering);
215 if (dNTu->subOffset != NULL) free(dNTu->subOffset);
216 if (dNTu->subXDROffset != NULL) free(dNTu->subXDROffset);
217 for (i=0; i<dNTu->numAvailable; i++)
218 DestroyVarGenNtuple(dNTu->variables[i]);
219 free(dNTu->variables);
220 free(dNTu);
221}
222
223
224void DestroyVarGenNtuple(varGenNtuple *var)
225{
226
227 if (var == NULL) return;
228 if (var->name != NULL) free(var->name);
229 if (var->description != NULL) free(var->description);
230 free(var);
231}
232/*
233 * ValidStr - Validate strings supplied by user
234 *
235 * returns: pointer to valid same or new truncated string
236 *
237 * Note: ** copy string returned, if needed, before calling ValidStr again **
238 */
239char *mcf_ValidStr(char *string, int max_length, char *strKind)
240{
241 static char str[NTU_MAX_CATEGORY_LENGTH+1]; /* make longest string */
242 static char str1[1] = "";
243
244 if (string == NULL)
245 return str1; /* return empty string */
246 if (strlen(string) <= max_length)
247 return string; /* return pointer to same string */
248 fprintf(stderr,
249 "Mcfio_Ntuple: Error. Specified %s string is too long, truncating\n ->%s\n",
250 strKind, string);
251 memset(str, 0, NTU_MAX_CATEGORY_LENGTH+1);
252 return strncpy(str, string, max_length); /* return ptr to trunc. string */
253}
254/*
255** Based on the HistoScope Check Category
256*/
257int mcf_CheckValidCat(char *category, int dotDotDot)
258{
259 static char validChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\
260abcdefghijklmnopqrstuvwxyz1234567890/~!@#$%^&*()_+=-`\"\'\t?><,. ";
261 char *strDots, *error = NULL;
262 int len;
263
264 if (category == NULL)
265 return 1;
266 len = strlen(category);
267 strDots = strstr(category, "...");
268 if (len >= NTU_MAX_CATEGORY_LENGTH)
269 error = "is too long";
270 else if (strspn(category, validChars) != len)
271 error = "contains invalid characters";
272 else if (strstr(category, "//") != NULL)
273 error = "contains \"//\"";
274 else if (category[0] == '/')
275 error = "contains leading slash";
276 else if (category[len-1] == '/')
277 error = "contains trailing slash";
278 else if ((dotDotDot == 0 && strDots != NULL)
279 || (dotDotDot != 0 && strDots != NULL && strDots != category + len-3))
280 error = "contains invalid \"...\"";
281
282 if (error != NULL) {
283 fprintf(stderr, "Error in declared category %s: %s\n",
284 error, category);
285 return 0;
286 } else {
287 return (strDots == NULL ? 1 : -1);
288 }
289}
290
Note: See TracBrowser for help on using the repository browser.