1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright 1993, University Corporation for Atmospheric Research           *
3  * See netcdf/COPYRIGHT file for copying and redistribution conditions.      *
4  *                                                                           *
5  * Copyright by The HDF Group.                                               *
6  * Copyright by the Board of Trustees of the University of Illinois.         *
7  * All rights reserved.                                                      *
8  *                                                                           *
9  * This file is part of HDF.  The full HDF copyright notice, including       *
10  * terms governing use, modification, and redistribution, is contained in    *
11  * the COPYING file, which can be found at the root of the source code       *
12  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/.  *
13  * If you do not have access to either file, you may request a copy from     *
14  * help@hdfgroup.org.                                                        *
15  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
16 
17 /*    $Id$ */
18 
19 #include    "local_nc.h"
20 #include    "alloc.h"
21 
22 
23 NC_iarray *
NC_new_iarray(count,values)24 NC_new_iarray(count, values)
25 unsigned count ;
26 const int *values ;           /* VAX C doesn't like values[] */
27 {
28     NC_iarray *ret ;
29     int *ip ;
30     size_t memlen ;
31 
32     ret = (NC_iarray *)HDmalloc(sizeof(NC_iarray)) ;
33     if( ret == NULL )
34         goto alloc_err ;
35     ret->count = count ;
36     if(count != 0 ) /* allocate */
37       {
38           memlen = count * sizeof(int) ;
39           ret->values = (int *)HDmalloc(memlen) ;
40           if(ret->values == NULL)
41               goto alloc_err ;
42           if(values != NULL) /* copy them in */
43             {
44                 for(ip = ret->values ; count > 0; count--)
45                     *ip++ = *values++ ;
46             }
47       } else {
48           ret->values = NULL ;
49       }
50 
51     return(ret) ;
52     alloc_err :
53         nc_serror("NC_new_iarray") ;
54     return(NULL) ;
55 }
56 
57 
58 /*
59  * Free iarray, and, if needed, its values.
60  *
61  * NOTE: Changed return value to return 'int'
62  *       If successful returns SUCCEED else FAIL -GV 9/19/97
63  */
64 int
NC_free_iarray(iarray)65 NC_free_iarray(iarray)
66 NC_iarray *iarray ;
67 {
68     int ret_value = SUCCEED;
69 
70     if(iarray != NULL)
71       {
72           if(iarray->values != NULL)
73               Free(iarray->values) ;
74           Free(iarray) ;
75       }
76 
77     return ret_value;
78 }
79 
80 
81 bool_t
xdr_NC_iarray(xdrs,ipp)82 xdr_NC_iarray(xdrs, ipp)
83     XDR *xdrs;
84     NC_iarray **ipp;
85 {
86     int *ip ;
87     u_long count = 0;
88     bool_t stat = TRUE ;
89 
90     switch (xdrs->x_op) {
91     case XDR_FREE:
92         NC_free_iarray((*ipp)) ;
93         return(TRUE) ;
94     case XDR_DECODE:
95         /* need the length to pass to new */
96         if (! xdr_u_long(xdrs, &count)) {
97             return (FALSE);
98         }
99         (*ipp) = NC_new_iarray((unsigned)count, (int *)NULL) ;
100         if((*ipp) == NULL)
101             return(FALSE) ;
102         /* then deal with the array */
103         for( ip = (*ipp)->values ; (count > 0 ) && stat ; count-- )
104             stat = xdr_int(xdrs, ip++ ) ;
105         return(stat) ;
106     case XDR_ENCODE:
107         /* first deal with the length */
108         count = (*ipp)->count ;
109         if (! xdr_u_long(xdrs, &count) ) {
110             return (FALSE);
111         }
112         /* then deal with the array */
113         for(ip = (*ipp)->values  ; (count > 0 ) && stat ; count--)
114             stat = xdr_int(xdrs, ip++ ) ;
115         return(stat) ;
116     }
117     return(FALSE) ;
118 }
119 
120 
121 /*
122  * How much space will the xdr'd iarray take.
123  */
NC_xlen_iarray(iarray)124 int NC_xlen_iarray(iarray)
125 NC_iarray *iarray ;
126 {
127     int len = 4 ;
128     if(iarray!=NULL)
129     {
130         len += iarray->count * 4 ;
131     }
132     return(len) ;
133 }
134