cdi_key.c 7.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "cdi.h"
#include "cdi_int.h"
#include "vlist.h"
#include "grid.h"
#include "zaxis.h"
10
#include "resource_unpack.h"
11

12
enum  {KEY_INT = 1, KEY_FLOAT, KEY_BYTES};
13
14
15
16
17

static
cdi_keys_t *get_keysp(vlist_t *vlistptr, int varID)
{
  cdi_keys_t *keysp = NULL;
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
  if ( varID == CDI_GLOBAL )
    {
      keysp = &vlistptr->keys;
    }
  else
    {
      if ( varID >= 0 && varID < vlistptr->nvars )
	keysp = &(vlistptr->vars[varID].keys);
    }

  return keysp;
}

static
cdi_key_t *new_key(cdi_keys_t *keysp, int key)
{
  xassert(keysp != NULL);

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

  cdi_key_t *keyp = &(keysp->value[keysp->nelems]);
  keysp->nelems++;

  keyp->key = key;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
44
45
  keyp->length = 0;
  keyp->type = 0;
  keyp->v.s = NULL;
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

  return keyp;
}

static
cdi_key_t *find_key(cdi_keys_t *keysp, int key)
{
  xassert(keysp != NULL);

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

  cdi_key_t *keys = keysp->value;
  for ( size_t keyid = 0; keyid < keysp->nelems; keyid++ )
    {
      cdi_key_t *keyp = keys + keyid;
      if ( keyp->key == key )
        return keyp; // Normal return
    }

  return NULL;
}

static
cdi_keys_t *cdi_get_keysp(int objID, int varID)
{
  cdi_keys_t *keysp = NULL;

  /*
  if ( varID == CDI_GLOBAL && reshGetTxCode(objID) == GRID )
    {
      grid_t *gridptr = grid_to_pointer(objID);
      keysp = &gridptr->keys;
    }
  else if ( varID == CDI_GLOBAL && reshGetTxCode(objID) == ZAXIS )
    {
      zaxis_t *zaxisptr = zaxis_to_pointer(objID);
      keysp = &zaxisptr->keys;
    }
  else
  */
86
  if ( reshGetTxCode(objID) == VLIST )
87
88
89
90
91
92
93
94
    {
      vlist_t *vlistptr = vlist_to_pointer(objID);
      keysp = get_keysp(vlistptr, varID);
    }

  return keysp;
}

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

int vlist_key_compare(vlist_t *a, int varIDA, vlist_t *b, int varIDB, int keynum)
{
  cdi_keys_t *keyspa = get_keysp(a, varIDA),
             *keyspb = get_keysp(b, varIDB);
  if (keyspa == NULL && keyspb == NULL)
    return 0;
  xassert(keynum >= 0 && keynum < (int)keyspa->nelems
          && keynum < (int)keyspb->nelems);
  cdi_key_t *keypa = keyspa->value + keynum,
    *keypb = keyspb->value + keynum;

  if ( keypa->key != keypb->key )
    return 1;

  if ( keypa->v.i != keypb->v.i )
    return 1;

  return 0;
}

116

117
void cdiDeleteVarKeys(cdi_keys_t *keysp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119
120
121
{
  for ( int keyid = 0; keyid < (int)keysp->nelems; keyid++ )
    {
      cdi_key_t *keyp = &(keysp->value[keyid]);
122
      if ( keyp->length )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
125
126
127
128
129
130
        {
          free(keyp->v.s);
          keyp->v.s = NULL;
          keyp->length = 0;
        }
    }

  keysp->nelems = 0;
131
132
133
134
135
136
137
138
139
140
141
}


int cdiDeleteKeys(int cdiID, int varID)
{
  int status = CDI_NOERR;

  cdi_keys_t *keysp = cdi_get_keysp(cdiID, varID);
  xassert(keysp != NULL);

  cdiDeleteVarKeys(keysp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
143
144
145

  return status;
}

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//  cdiInqKeyLen: Get the length of the string representation of the key
int cdiInqKeyLen(int cdiID, int varID, int key, int *length)
{
  int status = -1;

  cdi_keys_t *keysp = cdi_get_keysp(cdiID, varID);
  xassert(keysp != NULL);

  cdi_key_t *keyp = find_key(keysp, key);
  if ( keyp != NULL )
    {
      *length = keyp->length;
      if ( *length == 0 ) *length = 1;
      status = CDI_NOERR;
    }

  return status;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
165

166
void cdiCopyVarKeys(cdi_keys_t *keysp, int cdiID2, int varID2)
167
168
169
170
{
  for ( size_t keyid = 0; keyid < keysp->nelems; keyid++ )
    {
      cdi_key_t *keyp = &(keysp->value[keyid]);
171
172
173
174
      if ( keyp->type == KEY_INT )
        cdiDefKeyInt(cdiID2, varID2, keyp->key, keyp->v.i);
      else if ( keyp->type == KEY_BYTES )
        cdiDefKeyBytes(cdiID2, varID2, keyp->key, keyp->v.s, keyp->length);
175
176
177
    }
}

178
179

int cdiCopyKeys(int cdiID1, int varID1, int cdiID2, int varID2)
180
181
182
{
  int status = CDI_NOERR;

183
  cdi_keys_t *keysp = cdi_get_keysp(cdiID1, varID1);
184
185
  xassert(keysp != NULL);

186
187
188
189
190
191
192
193
  cdiCopyVarKeys(keysp, cdiID2, varID2);

  return status;
}


void cdiDefVarKeyInt(cdi_keys_t *keysp, int key, int value)
{
194
195
196
197
198
  cdi_key_t *keyp = find_key(keysp, key);
  if ( keyp == NULL ) keyp = new_key(keysp, key);

  if ( keyp != NULL )
    {
199
      //if ( keyp->v.i != value )
200
        {
201
          keyp->type = KEY_INT;
202
203
204
          keyp->v.i = value;
        }
    }
205
206
207
208
209
210
211
212
213
214
215
}

//  cdiDefKeyInt: Define an integer value from a key of a CDI variable
int cdiDefKeyInt(int cdiID, int varID, int key, int value)
{
  int status = CDI_NOERR;

  cdi_keys_t *keysp = cdi_get_keysp(cdiID, varID);
  xassert(keysp != NULL);

  cdiDefVarKeyInt(keysp, key, value);
216
217
218
219

  return status;
}

220
//  cdiInqKeyInt: Get an integer value from a key of a CDI variable
221
int cdiInqKeyInt(int cdiID, int varID, int key, int *value)
222
{
223
224
225
  int status = -1;

  if ( varID != CDI_GLOBAL ) status = cdiInqKeyInt(cdiID, CDI_GLOBAL, key, value);
226
227
228
229
230
231
232

  cdi_keys_t *keysp = cdi_get_keysp(cdiID, varID);
  xassert(keysp != NULL);

  cdi_key_t *keyp = find_key(keysp, key);
  if ( keyp != NULL ) // key in use
    {
233
      if ( keyp->type == KEY_INT )
234
	{
235
236
          *value = keyp->v.i;
          status = CDI_NOERR;
237
238
239
240
241
242
	}
    }

  return status;
}

243

244
245
void cdiDefVarKeyBytes(cdi_keys_t *keysp, int key, const unsigned char *bytes, int length)
{
246
247
248
249
250
  cdi_key_t *keyp = find_key(keysp, key);
  if ( keyp == NULL ) keyp = new_key(keysp, key);

  if ( keyp != NULL )
    {
251
252
253
254
255
256
257
258
259
260
261
      if ( keyp->length != 0 && keyp->length != length )
        {
          free(keyp->v.s);
          keyp->length = 0;
        }
      if ( keyp->length == 0 )
        {
          keyp->v.s = (unsigned char *) malloc(length);
          keyp->length = length;
        }

262
263
264
      memcpy(keyp->v.s, bytes, length);
      keyp->type = KEY_BYTES;
    }
265
266
267
268
269
270
271
272
273
274
275
}

//  cdiDefKeyBytes: Define a bytes array from a key of a CDI variable
int cdiDefKeyBytes(int cdiID, int varID, int key, const unsigned char *bytes, int length)
{
  int status = CDI_NOERR;

  cdi_keys_t *keysp = cdi_get_keysp(cdiID, varID);
  xassert(keysp != NULL);

  cdiDefVarKeyBytes(keysp, key, bytes, length);
276
277
278
279

  return status;
}

280
281
//  cdiInqKeyBytes: Get a bytes array from a key of a CDI variable
int cdiInqKeyBytes(int cdiID, int varID, int key, unsigned char *bytes, int *length)
282
283
{
  int status = -1;
284
285
  xassert(bytes != NULL);
  xassert(length != NULL);
286

287
  if ( varID != CDI_GLOBAL ) status = cdiInqKeyBytes(cdiID, CDI_GLOBAL, key, bytes, length);
288
289
290
291
292

  cdi_keys_t *keysp = cdi_get_keysp(cdiID, varID);
  xassert(keysp != NULL);

  cdi_key_t *keyp = find_key(keysp, key);
293
  if ( keyp != NULL ) // key in use
294
    {
295
296
297
298
299
300
      if ( keyp->type == KEY_BYTES )
	{
          if ( keyp->length < *length ) *length = keyp->length;
          memcpy(bytes, keyp->v.s, *length);
          status = CDI_NOERR;
	}
301
302
303
304
305
    }

  return status;
}

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//  cdiDefKeyString: Define a string value from a key of a CDI variable
int cdiDefKeyString(int cdiID, int varID, int key, const char *string)
{
  xassert(string != NULL);

  int length = strlen(string)+1;
  int status = cdiDefKeyBytes(cdiID, varID, key, (const unsigned char *) string, length);

  return status;
}

//  cdiInqKeyString: Get a string value from a key of a CDI variable
int cdiInqKeyString(int cdiID, int varID, int key, char *string, int *length)
{
  xassert(string != NULL);
  xassert(length != NULL);

  string[0] = '\0';

  int status = cdiInqKeyBytes(cdiID, varID, key, (unsigned char *) string, length);

  return status;
}

330
331
332
333
334
335
336
337
338
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */