vlist_att.c 6.63 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "dmemory.h"

#include "cdi.h"
#include "stream_int.h"
#include "vlist.h"


Uwe Schulzweida's avatar
Uwe Schulzweida committed
12
13
static
CDI_atts *get_attsp(VLIST *vlistptr, int varID)
14
15
16
17
18
19
20
21
22
23
{
  static char func[] = "get_attsp";
  CDI_atts *attsp = NULL;

  if ( varID == CDI_GLOBAL )
    {
      attsp = &vlistptr->atts;
    }
  else
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
24
25
      if ( varID >= 0 && varID < vlistptr->nvars )
	attsp = &(vlistptr->vars[varID].atts);
26
27
28
29
30
31
    }

  return (attsp);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
32
static
33
CDI_att *find_att(CDI_atts *attsp, const char *name)
34
{
35
36
  CDI_att *attp;
  size_t attid;
37
38
39
40
41
42
43
44
  size_t slen;

  assert(attsp != NULL);

  if ( attsp->nelems == 0 ) return NULL;

  slen = strlen(name);

45
  for ( attid = 0; attid < attsp->nelems; attid++ )
46
    {
47
48
      attp = &(attsp->value[attid]);
      if ( attp->namesz == slen )
49
	if ( memcmp(attp->name, name, slen) == 0)
50
	  {
51
	    return (attp); /* Normal return */
52
53
54
55
56
57
58
	  }
    }

  return (NULL);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
59
static
60
CDI_att *new_att(CDI_atts *attsp, const char *name)
61
{
62
63
  static char func[] = "new_att";
  CDI_att *attp;
64
65
66
67
68
69
70
  size_t slen;

  assert(attsp != NULL);
  assert(name  != NULL);

  if ( attsp->nelems == attsp->nalloc ) return (NULL);

71
  attp = &(attsp->value[attsp->nelems]);
72
73
74
75
  attsp->nelems++;

  slen = strlen(name);

76
77
78
  attp->name = (char *) malloc(slen+1);
  memcpy(attp->name, name, slen+1);
  attp->namesz = slen;
79

80
  return (attp);
81
82
83
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
84
static
85
void fill_att(CDI_att *attp, int type, size_t nelems, size_t xsz, const void *xvalue)
86
{
87
  static char func[] = "fill_att";
88

89
  assert(attp != NULL);
90

91
92
93
  attp->xsz = xsz;
  attp->type = type;
  attp->nelems = nelems;
94

95
96
97
98
99
  if ( xsz > 0 )
    {
      attp->xvalue = (void *) malloc(xsz);
      memcpy(attp->xvalue, xvalue, xsz);
    }
100
101
102
103
104
105
106
107
108
109
110
111
112
}


int vlistInqNatts(int vlistID, int varID, int *nattsp)
{
  static char func[] = "vlistInqNatts";
  int status = CDI_NOERR;
  VLIST *vlistptr;
  CDI_atts *attsp;

  vlistptr = vlist_to_pointer(vlistID);
  
  attsp = get_attsp(vlistptr, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
  assert(attsp != NULL);
114
115
116
117
118
119
120

  *nattsp = attsp->nelems;

  return (status);
}


121
int vlistInqAtt(int vlistID, int varID, int attnum, char *name, int *typep, int *lenp)
122
{
123
  static char func[] = "vlistInqAtt";
124
125
  int status = CDI_NOERR;
  VLIST *vlistptr;
126
  CDI_att *attp = NULL;
127
128
129
130
131
132
133
  CDI_atts *attsp;

  assert(name != NULL);

  vlistptr = vlist_to_pointer(vlistID);

  attsp = get_attsp(vlistptr, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
  assert(attsp != NULL);
135

136
137
  if ( attnum >= 0 && attnum < (int)attsp->nelems )
    attp = &(attsp->value[attnum]);
138

139
  if ( attp != NULL ) /* name in use */
140
    {
141
142
143
      memcpy(name, attp->name, attp->namesz+1);
      *typep  = attp->type;
      *lenp   = attp->nelems;
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    }
  else
    {
      name[0] =  0;
      *typep  = -1;
      *lenp   =  0;
    }

  return (status);
}


int vlistDelAtts(int vlistID, int varID)
{
  static char func[] = "vlistDelAtts";
  int status = CDI_NOERR;
  VLIST *vlistptr;
161
  CDI_att *attp = NULL;
162
  CDI_atts *attsp;
163
  int attid;
164
165
166
167

  vlistptr = vlist_to_pointer(vlistID);

  attsp = get_attsp(vlistptr, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168
  assert(attsp != NULL);
169

170
  for ( attid = 0; attid < (int)attsp->nelems; attid++ )
171
    {
172
173
174
      attp = &(attsp->value[attid]);
      if ( attp->name   ) free(attp->name);
      if ( attp->xvalue ) free(attp->xvalue);
175
176
177
178
179
180
181
182
    }

  attsp->nelems = 0;

  return (status);
}


183
int vlistDelAtt(int vlistID, int varID, const char *name)
184
185
186
{
  int status = CDI_NOERR;

187
  fprintf(stderr, "vlistDelAtt not implemented!\n");
188
189
190
191
192

  return (status);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
static
194
int vlist_def_att(int type, int vlistID, int varID, const char *name, size_t len, size_t xsz, const void *xp)
195
{
196
  static char func[] = "vlist_def_att";
197
198
  int status = CDI_NOERR;
  VLIST *vlistptr;
199
  CDI_att *attp;
200
201
202
203
204
205
206
207
208
209
  CDI_atts *attsp;

  if ( len != 0 && xp == NULL ) /* Null arg */
    {
      return (CDI_EINVAL);
    }

  vlistptr = vlist_to_pointer(vlistID);

  attsp = get_attsp(vlistptr, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
  assert(attsp != NULL);
211

212
213
  attp = find_att(attsp, name);
  if ( attp != NULL ) /* name in use */
214
215
216
217
    {
    }
  else
    {
218
219
220
      attp = new_att(attsp, name);
      if ( attp != NULL )
	fill_att(attp, type, len, xsz, xp);
221
222
223
224
225
226
    }

  return (status);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
227
static
228
int vlist_inq_att(int type, int vlistID, int varID, const char *name, size_t mxsz, void *xp)
229
{
230
  static char func[] = "vlist_inq_att";
231
232
  int status = CDI_NOERR;
  VLIST *vlistptr;
233
  CDI_att *attp;
234
235
236
237
238
239
240
241
242
243
244
  CDI_atts *attsp;
  size_t xsz;

  if ( mxsz != 0 && xp == NULL ) /* Null arg */
    {
      return (CDI_EINVAL);
    }

  vlistptr = vlist_to_pointer(vlistID);

  attsp = get_attsp(vlistptr, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245
  assert(attsp != NULL);
246

247
248
  attp = find_att(attsp, name);
  if ( attp != NULL ) /* name in use */
249
    {
250
      xsz = attp->xsz;
251
      if ( mxsz < xsz ) xsz = mxsz;
252
253
      if ( xsz > 0 )
	memcpy(xp, attp->xvalue, xsz);
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    }
  else
    {
      Warning(func, "Internal problem, attribute %s not found!", name);
    }

  return (status);
}


int vlistCopyVarAtts(int vlistID1, int varID_1, int vlistID2, int varID_2)
{
  static char func[] = "vlistCopyVarAtts";
  int status = CDI_NOERR;
  VLIST *vlistptr1;
269
  CDI_att *attp = NULL;
270
  CDI_atts *attsp1;
271
  int attid;
272
273
274
275

  vlistptr1 = vlist_to_pointer(vlistID1);

  attsp1 = get_attsp(vlistptr1, varID_1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
276
  assert(attsp1 != NULL);
277

278
  for ( attid = 0; attid < (int)attsp1->nelems; attid++ )
279
    {
280
281
      attp = &(attsp1->value[attid]);
      vlist_def_att(attp->type, vlistID2, varID_2, attp->name, attp->nelems, attp->xsz, attp->xvalue);
282
283
284
285
286
287
    }

  return (status);
}


288
int vlistDefAttInt(int vlistID, int varID, const char *name, int len, const int *ip)
289
290
291
{
  int status;

292
  status = vlist_def_att(DATATYPE_INT, vlistID, varID, name, (size_t) len, len*sizeof(int), (const void *) ip);
293
294
295
296
297

  return (status);
}


298
int vlistDefAttFlt(int vlistID, int varID, const char *name, int len, const double *dp)
299
300
301
{
  int status;

302
  status = vlist_def_att(DATATYPE_FLT, vlistID, varID, name, (size_t) len, len*sizeof(double), (const void *) dp);
303
304
305
306
307

  return (status);
}


308
int vlistDefAttTxt(int vlistID, int varID, const char *name, int len, const char *tp)
309
310
311
{
  int status;

312
  status = vlist_def_att(DATATYPE_TXT, vlistID, varID, name, (size_t) len, len*sizeof(char), (const void *) tp);
313
314
315
316
317
318

  return (status);
}



319
int vlistInqAttInt(int vlistID, int varID, const char *name, int mlen, int *ip)
320
321
322
{
  int status = CDI_NOERR;

323
  status = vlist_inq_att(DATATYPE_INT, vlistID, varID, name, mlen*sizeof(int), (void *) ip);
324
325
326
327
328

  return (CDI_NOERR);
}


329
int vlistInqAttFlt(int vlistID, int varID, const char *name, int mlen, double *dp)
330
331
332
{
  int status = CDI_NOERR;

333
  status = vlist_inq_att(DATATYPE_FLT, vlistID, varID, name, mlen*sizeof(double), (void *) dp);
334
335
336
337
338

  return (status);
}


339
int vlistInqAttTxt(int vlistID, int varID, const char *name, int mlen, char *tp)
340
341
342
{
  int status = CDI_NOERR;

343
  status = vlist_inq_att(DATATYPE_TXT, vlistID, varID, name, mlen*sizeof(char), (void *) tp);
344
345
346

  return (status);
}