cdi_key.c 7.34 KB
Newer Older
1
2
#include "cdi.h"
#include "cdi_int.h"
3
#include "grid.h"
4
#include "vlist.h"
5
#include "resource_unpack.h"
6

7
enum  {KEY_INT = 1, KEY_FLOAT, KEY_BYTES};
8
9

static
10
cdi_keys_t *vlist_get_keysp(vlist_t *vlistptr, int varID)
11
12
{
  cdi_keys_t *keysp = NULL;
13

14
15
16
17
18
19
20
21
22
23
24
25
26
  if ( varID == CDI_GLOBAL )
    {
      keysp = &vlistptr->keys;
    }
  else
    {
      if ( varID >= 0 && varID < vlistptr->nvars )
	keysp = &(vlistptr->vars[varID].keys);
    }

  return keysp;
}

27
28
29
30
31
32
33
34
35
36
37
38
39
static
cdi_keys_t *grid_get_keysp(vlist_t *gridptr, int varID)
{
  cdi_keys_t *keysp = NULL;

  if ( varID == CDI_GLOBAL )
    {
      keysp = &gridptr->keys;
    }

  return keysp;
}

40
41
42
43
44
45
46
47
48
49
50
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
51
52
53
  keyp->length = 0;
  keyp->type = 0;
  keyp->v.s = NULL;
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

  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;

81
  if ( reshGetTxCode(objID) == GRID )
82
83
84
85
    {
      grid_t *gridptr = grid_to_pointer(objID);
      keysp = &gridptr->keys;
    }
86
  /*
87
88
89
90
91
92
93
  else if ( varID == CDI_GLOBAL && reshGetTxCode(objID) == ZAXIS )
    {
      zaxis_t *zaxisptr = zaxis_to_pointer(objID);
      keysp = &zaxisptr->keys;
    }
  else
  */
94
  if ( reshGetTxCode(objID) == VLIST )
95
96
    {
      vlist_t *vlistptr = vlist_to_pointer(objID);
97
      keysp = vlist_get_keysp(vlistptr, varID);
98
99
100
101
102
    }

  return keysp;
}

103
104
105

int vlist_key_compare(vlist_t *a, int varIDA, vlist_t *b, int varIDB, int keynum)
{
106
107
  cdi_keys_t *keyspa = vlist_get_keysp(a, varIDA),
             *keyspb = vlist_get_keysp(b, varIDB);
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  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;
}

124

125
void cdiDeleteVarKeys(cdi_keys_t *keysp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
127
128
129
{
  for ( int keyid = 0; keyid < (int)keysp->nelems; keyid++ )
    {
      cdi_key_t *keyp = &(keysp->value[keyid]);
130
      if ( keyp->length )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
133
134
135
136
137
138
        {
          free(keyp->v.s);
          keyp->v.s = NULL;
          keyp->length = 0;
        }
    }

  keysp->nelems = 0;
139
140
141
142
143
144
145
146
147
148
149
}


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
150
151
152
153

  return status;
}

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//  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
173

174
void cdiCopyVarKeys(cdi_keys_t *keysp, int cdiID2, int varID2)
175
176
177
178
{
  for ( size_t keyid = 0; keyid < keysp->nelems; keyid++ )
    {
      cdi_key_t *keyp = &(keysp->value[keyid]);
179
180
181
182
      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);
183
184
185
    }
}

186
187

int cdiCopyKeys(int cdiID1, int varID1, int cdiID2, int varID2)
188
189
190
{
  int status = CDI_NOERR;

191
  cdi_keys_t *keysp = cdi_get_keysp(cdiID1, varID1);
192
193
  xassert(keysp != NULL);

194
195
196
197
198
199
200
201
  cdiCopyVarKeys(keysp, cdiID2, varID2);

  return status;
}


void cdiDefVarKeyInt(cdi_keys_t *keysp, int key, int value)
{
202
203
204
205
206
  cdi_key_t *keyp = find_key(keysp, key);
  if ( keyp == NULL ) keyp = new_key(keysp, key);

  if ( keyp != NULL )
    {
207
      //if ( keyp->v.i != value )
208
        {
209
          keyp->type = KEY_INT;
210
211
212
          keyp->v.i = value;
        }
    }
213
214
215
216
217
218
219
220
221
222
223
}

//  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);
224
225
226
227

  return status;
}

228
//  cdiInqKeyInt: Get an integer value from a key of a CDI variable
229
int cdiInqKeyInt(int cdiID, int varID, int key, int *value)
230
{
231
232
233
  int status = -1;

  if ( varID != CDI_GLOBAL ) status = cdiInqKeyInt(cdiID, CDI_GLOBAL, key, value);
234
235
236
237
238
239
240

  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
    {
241
      if ( keyp->type == KEY_INT )
242
	{
243
244
          *value = keyp->v.i;
          status = CDI_NOERR;
245
246
247
248
249
250
	}
    }

  return status;
}

251

252
253
void cdiDefVarKeyBytes(cdi_keys_t *keysp, int key, const unsigned char *bytes, int length)
{
254
255
256
257
258
  cdi_key_t *keyp = find_key(keysp, key);
  if ( keyp == NULL ) keyp = new_key(keysp, key);

  if ( keyp != NULL )
    {
259
260
261
262
263
264
265
      if ( keyp->length != 0 && keyp->length != length )
        {
          free(keyp->v.s);
          keyp->length = 0;
        }
      if ( keyp->length == 0 )
        {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
266
          keyp->v.s = (unsigned char *) malloc((size_t) length);
267
268
269
          keyp->length = length;
        }

270
271
272
      memcpy(keyp->v.s, bytes, length);
      keyp->type = KEY_BYTES;
    }
273
274
275
276
277
278
279
280
281
282
283
}

//  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);
284
285
286
287

  return status;
}

288
289
//  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)
290
291
{
  int status = -1;
292
293
  xassert(bytes != NULL);
  xassert(length != NULL);
294

295
  if ( varID != CDI_GLOBAL ) status = cdiInqKeyBytes(cdiID, CDI_GLOBAL, key, bytes, length);
296
297
298
299
300

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

  cdi_key_t *keyp = find_key(keysp, key);
301
  if ( keyp != NULL ) // key in use
302
    {
303
304
305
306
307
308
      if ( keyp->type == KEY_BYTES )
	{
          if ( keyp->length < *length ) *length = keyp->length;
          memcpy(bytes, keyp->v.s, *length);
          status = CDI_NOERR;
	}
309
310
311
312
313
    }

  return status;
}

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//  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;
}

338
339
340
341
342
343
344
345
346
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */