File: attr.c

package info (click to toggle)
libhdf4 4.1r3-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 24,092 kB
  • ctags: 26,163
  • sloc: ansic: 204,415; fortran: 29,237; sh: 7,807; makefile: 7,417; cpp: 2,186; pascal: 1,498; asm: 1,027; yacc: 680; lex: 202; sed: 153
file content (570 lines) | stat: -rw-r--r-- 10,555 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
 *	Copyright 1993, University Corporation for Atmospheric Research
 *      See netcdf/COPYRIGHT file for copying and redistribution conditions.
 */
/*	$Id: attr.c,v 1.9 1997/11/05 19:40:05 koziol Exp $ */

#include	<string.h> 
#include	"local_nc.h"
#include	"alloc.h"


NC_attr *
NC_new_attr(name,type,count,values)
const char *name ;
nc_type type ;
unsigned count ;
const void *values ;
{
	NC_attr *ret ;

	ret = (NC_attr *)HDmalloc(sizeof(NC_attr)) ;
	if( ret == NULL )
		goto alloc_err ;

	ret->name = NC_new_string((unsigned)strlen(name),name) ;
	if( ret->name == NULL)
		goto alloc_err ;

	ret->data = NC_new_array(type, count, values) ;
	if( ret->data == NULL)
		goto alloc_err ;
#ifdef HDF
    ret->HDFtype = hdf_map_type(type);
#endif
	return(ret) ;
alloc_err :
	nc_serror("NC_new_attr") ;
	return(NULL) ;
}


/*
 * Free attr
 *
 * NOTE: Changed return value to return 'int' 
 *       If successful returns SUCCEED else FAIL -GV 9/19/97
 */
int
NC_free_attr(attr)
NC_attr *attr ;
{
    int ret_value = SUCCEED;

	if(attr != NULL)
      {
          if (NC_free_string(attr->name) == FAIL)
            {
                ret_value = FAIL;
                goto done;
            }
              
          if (NC_free_array(attr->data) == FAIL)
            {
                ret_value = FAIL;
                goto done;
            }

          Free(attr) ;
      }

done:
    if (ret_value == FAIL)
      { /* Failure cleanup */

      }
     /* Normal cleanup */

    return ret_value;
}


/*
 *  Verify that this is a user nc_type
 */
bool_t
NCcktype(datatype)
nc_type datatype ;
{
	switch(datatype){
	case NC_BYTE :
	case NC_CHAR :
	case NC_SHORT :
	case NC_LONG :
	case NC_FLOAT :
	case NC_DOUBLE :
		return(TRUE) ;
	}
	NCadvise(NC_EBADTYPE, "Unknown type %d", datatype) ;
	return(FALSE) ;
}


/*
 * Given cdfid and varid, return handle to array of attributes
 *  else NULL on error
 */
static NC_array **
NC_attrarray( cdfid, varid )
int cdfid ;
int varid ;
{
	NC *handle ;
	NC_array **ap ;

	handle = NC_check_id(cdfid) ;
	if(handle == NULL)
		return(NULL) ;

	if(varid == NC_GLOBAL) /* Global attribute, attach to cdf */
	{
		ap = &(handle->attrs) ;
	}else if(handle->vars != NULL && varid >= 0 && varid < handle->vars->count)
	{
		ap = (NC_array **)handle->vars->values ;
		ap += varid ;
		ap = &(((NC_var *)(*ap))->attrs) ; /* Whew! */
	} else {
		NCadvise(NC_EINVAL, "%d is not a valid variable id", varid) ;
		ap = NULL ;
	}
	return(ap) ;
}


/*
 * Step thru NC_ATTRIBUTE array, seeking match on name.
 *  return match or NULL if Not Found.
 */
NC_attr **
NC_findattr(ap, name)
NC_array **ap ;
const char *name ;
{
    NC_attr **attr ;
    int attrid ;
    size_t len ;
    
    if(*ap == NULL)
        return(NULL) ;
    
    attr = (NC_attr **) (*ap)->values ;
    
    len = strlen(name) ;
    
    for(attrid = 0 ; attrid < (*ap)->count ; attrid++, attr++)
	{
            if( len == (*attr)->name->len && 
               strncmp(name, (*attr)->name->values, len) == 0)
                {
                    return(attr) ; /* Normal return */
                }
        }
    return(NULL) ;
}


/*
 * Look up by cdfid, varid and name, return NULL if not found
 */
static NC_attr **
NC_lookupattr( cdfid, varid, name, verbose)  
int cdfid ;
int varid ;
const char *name ; /* attribute name */
bool_t verbose ;
{
	NC_array **ap ;
	NC_attr **attr ;

	ap = NC_attrarray(cdfid, varid) ;
	if(ap == NULL)
		return(NULL) ;

	attr = NC_findattr(ap, name ) ;
	if(verbose && attr == NULL)
		NCadvise(NC_ENOTATT, "attribute \"%s\" not found",name) ;
	return( attr ) ;
}


/*
 * Common code for attput and attcopy
 */
static int
NC_aput(cdfid, ap, name, datatype, count, values)
int cdfid ;
NC_array **ap ;
const char *name ;
nc_type datatype ;
unsigned count ;
const void *values ;
{
	NC *handle ;
	NC_attr *attr[1] ;
	NC_attr **atp, *old ;

	handle = NC_check_id(cdfid) ;
	if(handle == NULL)
		return(-1) ;
	if(!(handle->flags & NC_RDWR))
		return(-1) ;

	if(*ap == NULL ) /* first time */
	{
		if( !NC_indefine(cdfid,TRUE) )
			return(-1) ;

		attr[0] = NC_new_attr(name,datatype,count,values) ;
		if(attr[0] == NULL)
			return(-1) ;
		*ap = NC_new_array(NC_ATTRIBUTE,(unsigned)1, (Void*)attr) ;
		if(*ap == NULL)
			return(-1) ;
		return((*ap)->count -1) ;
	}
	/* else */
	if( (atp = NC_findattr(ap, name)) != NULL) /* name in use */
	{
		if( NC_indefine(cdfid,FALSE) )
		{
			old = *atp ;
			*atp = NC_new_attr(name,datatype,count,values) ;
			if(*atp == NULL)
			{
				*atp = old ;
				return(-1) ;
			}
			NC_free_attr(old) ;
			return((*ap)->count -1) ;
		} 
		/* else */
		if( NC_re_array((*atp)->data, datatype,count,values) == NULL) 
		{
		NCadvise(NC_ENOTINDEFINE, "Can't increase size unless in define mode") ;
			return(-1) ;
		}
		/* else */
#ifdef HDF
                (*atp)->HDFtype = hdf_map_type(datatype);
#endif
		if(handle->flags & NC_HSYNC)
		{
			handle->xdrs->x_op = XDR_ENCODE ;
			if(!xdr_cdf(handle->xdrs, &handle) )
				return(-1) ;
			handle->flags &= ~(NC_NDIRTY | NC_HDIRTY) ;
		} else
			handle->flags |= NC_HDIRTY ;
		return((*ap)->count -1) ;
	} 
	/* else */
	if((*ap)->count >= MAX_NC_ATTRS)
	{
		NCadvise(NC_EMAXATTS, "maximum number of attributes %d exceeded",
			(*ap)->count ) ;
		return(-1) ;
	}
	/* else */
	if( NC_indefine(cdfid,TRUE) )
	{
		attr[0] = NC_new_attr(name,datatype,count,values) ;
		if(attr[0] == NULL)
			return(-1) ;
		if( NC_incr_array((*ap), (Void *)attr) == NULL)
			return(-1) ;
		return((*ap)->count -1) ;
	}
	/* else */
	return(-1) ;
}


int ncattput(cdfid, varid, name, datatype, count, values)
int cdfid ;
int varid ;
const char *name ;
nc_type datatype ;
int count ;
const ncvoid *values ;
{
	NC_array **ap ;

	cdf_routine_name = "ncattput" ;

	ap = NC_attrarray(cdfid, varid) ;
	if(ap == NULL)
		return(-1) ;

	if(count < 0 )
	{
		NCadvise(NC_EINVAL, "Invalid length %d", count) ;
		return(-1) ;
	}

	if(!NCcktype(datatype))
		return(-1) ;

	return( NC_aput(cdfid, ap, name, datatype, (unsigned)count,
		values) ) ;
}


int ncattname( cdfid, varid, attnum, name)
int cdfid ;
int varid ;
int attnum ;
char *name ;
{
	NC_array **ap ;
	NC_attr **attr ;

	cdf_routine_name = "ncattname" ;

	ap = NC_attrarray(cdfid, varid) ;
	if(ap == NULL || *ap == NULL)
		return(-1) ;

	if( attnum < 0 || attnum >= (*ap)->count)
	{
		NCadvise(NC_ENOTATT, "%d is not a valid attribute id", attnum) ;
		return(-1) ;
	}

	attr = (NC_attr **) (*ap)->values ;
	attr += attnum ;
#ifdef HDF
	(void)memcpy( name, (*attr)->name->values, (*attr)->name->len) ;
#else
	(void)strncpy( name, (*attr)->name->values, (*attr)->name->len) ;
#endif
	name[(*attr)->name->len] = 0 ;

	return(attnum) ;
}


int ncattinq( cdfid, varid, name, datatypep, countp)  
int cdfid ;
int varid ;
const char *name ; /* input, attribute name */
nc_type *datatypep ;
int *countp ;
{
	NC_attr **attr ;

	cdf_routine_name = "ncattinq" ;

	attr = NC_lookupattr(cdfid,varid,name,TRUE) ;
	if(attr == NULL)
		return(-1) ;

	if(datatypep != 0)
		*datatypep = (*attr)->data->type ;
	if(countp != 0)
		*countp = (*attr)->data->count ;
	return(1) ;
}


int ncattrename(cdfid, varid, name, newname)
int cdfid ;
int varid ; 
const char *name ;
const char *newname ;
{
	
	NC *handle ;
	NC_attr **attr ;
	NC_string *new, *old ;

	cdf_routine_name = "cdfattrrename" ;

	handle = NC_check_id(cdfid) ;
	if(handle == NULL)
		return(-1) ;
	if(!(handle->flags & NC_RDWR))
		return(-1) ;

	attr = NC_lookupattr(cdfid,varid,name,TRUE) ;
	if(attr == NULL)
		return(-1) ;

	if( NC_lookupattr(cdfid,varid,newname,FALSE) != NULL) /* name in use */
		return(-1) ;

	old = (*attr)->name ;
	if( NC_indefine(cdfid,FALSE) )
	{
		new = NC_new_string((unsigned)strlen(newname),newname) ;
		if( new == NULL)
			return(-1) ;
		(*attr)->name = new ;
		NC_free_string(old) ;
		return(1) ;
	} /* else */
	new = NC_re_string(old, (unsigned)strlen(newname),newname) ;
	if( new == NULL)
		return(-1) ;
	(*attr)->name = new ;
	if(handle->flags & NC_HSYNC)
	{
		handle->xdrs->x_op = XDR_ENCODE ;
		if(!xdr_cdf(handle->xdrs, &handle) )
			return(-1) ;
		handle->flags &= ~(NC_NDIRTY | NC_HDIRTY) ;
	} else
		handle->flags |= NC_HDIRTY ;
	return(1) ;
}


int ncattcopy( incdf, invar, name, outcdf, outname)
int incdf ;
int invar ;
const char *name ;
int outcdf ;
int outname ;
{
	NC_attr **attr ;
	NC_array **ap ;

	cdf_routine_name = "ncattcopy" ;

	attr = NC_lookupattr(incdf,invar,name,TRUE) ;
	if(attr == NULL)
		return(-1) ;
	
	ap = NC_attrarray(outcdf, outname) ;
	if(ap == NULL)
		return(-1) ;

	return( NC_aput(outcdf, ap, name,
		(*attr)->data->type, (*attr)->data->count,
		(*attr)->data->values) ) ;
}


int ncattdel(cdfid, varid, name)
int cdfid ;
int varid ; 
const char *name ;
{
	
	NC_array **ap ;
	NC_attr **attr ;
	NC_attr *old = NULL ;
	int attrid ;
	int len ;

	cdf_routine_name = "ncattdel" ;

	if( !NC_indefine(cdfid,TRUE) )
		return(-1) ;

	ap = NC_attrarray(cdfid, varid) ;
	if(ap == NULL || *ap == NULL)
		return(-1) ;

	attr = (NC_attr **) (*ap)->values ;

	len = strlen(name) ;
	for(attrid = 0 ; attrid < (*ap)->count ; attrid++, attr++)
	{
		if( len == (*attr)->name->len &&
			strncmp(name, (*attr)->name->values, len) == 0)
		{
			old = *attr ;
			break ;
		}
	}
	if( attrid == (*ap)->count )
	{
		NCadvise(NC_ENOTATT, "attribute \"%s\" not found",name) ;
		return(-1) ;
	}
	/* shuffle down */
	for(attrid++ ; attrid < (*ap)->count ; attrid++)
	{
		*attr = *(attr + 1) ;
		attr++ ;
	}
	/* decrement count */
	(*ap)->count-- ;

	NC_free_attr(old) ;

	return(1) ;
}


int ncattget(cdfid, varid, name, values)
int cdfid ;
int varid ; 
const char *name ;
ncvoid *values ;
{
	
	NC_attr **attr ;

	cdf_routine_name = "ncattget" ;

	attr = NC_lookupattr(cdfid,varid,name,TRUE) ;
	if(attr == NULL)
		return(-1) ;

	NC_copy_arrayvals((char *)values, (*attr)->data) ;

	return(1) ;
}


bool_t
xdr_NC_attr(xdrs, app)
	XDR *xdrs;
	NC_attr **app;
{
    bool_t ret_value;

	if( xdrs->x_op == XDR_FREE)
	{
		NC_free_attr((*app)) ;
		return(TRUE) ;
	}

	if( xdrs->x_op == XDR_DECODE )
	{
		*app = (NC_attr *)HDmalloc(sizeof(NC_attr)) ;
		if( *app == NULL )
		{
			nc_serror("xdr_NC_attr") ;
			return(FALSE) ;
		}
	}

	if( !xdr_NC_string(xdrs, &((*app)->name)))
		return(FALSE) ;
	ret_value = xdr_NC_array(xdrs, &((*app)->data)) ;
#ifdef HDF
        (*app)->HDFtype = hdf_map_type(((*app)->data)->type);
#endif
	return ret_value;
}


/*
 * How much space will the xdr'd attr take.
 */
int NC_xlen_attr(app)
NC_attr **app ;
{
	int len ;

	if(*app == NULL)
		return(4) ;

	len = NC_xlen_string((*app)->name) ;
	len += NC_xlen_array((*app)->data) ;

	return(len) ;
}