MISR Toolkit  1.5.1
v_nodesv.c
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  Description: This file contains C routines to store and manipulate the
4  values assigned to an attribute or pointer.
5 
6  Author: Randy Davis, University of Colorado LASP
7 
8  Creation Date: 12 March 1989
9  Last Modified: 18 May 1991
10 
11  History:
12 
13  Creation - This set of routines was part of the Version 1 release of
14  the ODLC library.
15 
16  Version 2.0 - 30 August 1990 - R. Davis, U. of Colorado LASP
17  a) Changed to work with Version 2 parameter routines rather than
18  the older Version attribute routines.
19 
20  Version 2.1 - 13 March 1991 - R. Davis, U. of Colorado LASP
21  a) Added routines for cutting, copying and pasting values and
22  recoded NewValue and RemoveValue to use these routines.
23  The paste and cut routines increment and decrement the count
24  of value nodes attached to a parameter. Since NewValue and
25  RemoveValue now use the paste and cut routines, they too
26  update the value node counter. Previously this updating had
27  to be done outside of these routines.
28 
29  Version 2.2 - 18 May 1991 - M. DeMore, Jet Propulsion Laboratory
30  a) Removed include statements that were Unix specific and placed them
31  in odldef.h.
32  b) Added code to NewValue to allocate space for string values rather
33  than using the space passed in.
34 
35 *****************************************************************************/
36 
37 #include <stdlib.h>
38 #include "odldef.h"
39 
40 
41 
42 /*****************************************************************************
43 
44  Routine: NewValue
45 
46  Description: Adds a new value to a parameter.
47 
48  Input:
49  parameter - Pointer to the parameter node to which the value
50  is to be attached.
51  value_data - Pointer to the data value and its format.
52 
53  Output: A pointer to the value node is returned as the function value. A
54  NULL value indicates that the value could not be added, probably
55  due to lack of dynamic memory. The NULL value is also returned
56  if the value data argument is NULL.
57 
58 *****************************************************************************/
59 
60 
62 
63  PARAMETER parameter,
64  VALUE_DATA *value_data)
65 
66 {
67  VALUE value; /* Pointer to the new value node */
68 
69 
70  if (value_data == NULL)
71  {
72  return (NULL);
73  }
74 
75  /* Allocate memory for the new value node */
76 
77  value = (VALUE) malloc (VALUE_NODE_SIZE);
78  if (value == NULL)
79  {
80  /* Error: couldn't allocate memory to store value node */
81 
82  return (NULL);
83  }
84 
85  /* Copy the value data into the new value node */
86 
87  value->item = *value_data;
88 
89  if (parameter != NULL)
90  {
91  /* Attach the new value node to its parameter */
92 
93  PasteValue (parameter, value);
94  }
95  else
96  {
97  /* There is no parameter to attach this value node to, so
98  set the parent and sibling pointers to NULL */
99 
100  value->parameter = NULL;
101  value->left_sibling = NULL;
102  value->right_sibling = NULL;
103  }
104  return (value);
105 }
106 
107 
108 
109 
110 /*****************************************************************************
111 
112  Routine: RemoveValue
113 
114  Description: Removes a value of a parameter. The memory space used to
115  hold the value node plus any additional memory used by
116  the value itself is returned to the system.
117 
118  Input:
119  value - A pointer to the value node to be removed.
120 
121  Output: A pointer to the next value node is returned as the function
122  value. The NULL value is returned if there is no next value
123  for the parameter. The NULL value is also returned if the
124  input argument is NULL.
125 
126 *****************************************************************************/
127 
128 
130 
131  VALUE value)
132 
133 {
134  VALUE r_value; /* Pointer to value's right sibling */
135  struct ODLUnits *units_field; /* Pointer to current units field descriptor*/
136  struct ODLUnits *next_field; /* Pointer to next units field descriptor */
137 
138 
139  /* Return immediately if there is nothing to remove */
140 
141  if (value == NULL)
142  {
143  return (NULL);
144  }
145 
146  /* Free up any memory space used for storing the value. This is the case
147  for string and symbol values and for the units of a real or integer
148  number */
149 
150  switch (value->item.type)
151  {
152  case TV_STRING: case TV_SYMBOL:
153 
154  /* Free up the space used to hold the text of the string or symbol */
155 
156  if(value->item.value.string != NULL)
157  {
158  free (value->item.value.string);
159  value->item.value.string = NULL;
160  }
161  break;
162 
163  case TV_INTEGER: case TV_REAL:
164 
165  /* Free up the space used to hold any units fields */
166 
167  if (value->item.type == TV_INTEGER)
168  {
169  units_field = value->item.value.integer.units;
170  }
171  else
172  {
173  units_field = value->item.value.real.units;
174  }
175 
176  while (units_field != NULL)
177  {
178  next_field = units_field->next_field;
179  free (units_field->designator);
180  free (units_field);
181  units_field = next_field;
182  }
183 
184  break;
185 
186  default:
187 
188  break;
189  }
190 
191  /* Save a pointer to the next value node in the list of values, if any */
192 
193  r_value = value->right_sibling;
194 
195  /* Detach the value from its parameter node */
196 
197  CutValue (value);
198 
199  /* Free up the memory space used to hold the value node */
200 
201  free (value);
202  value = NULL;
203 
204  /* Return a pointer to the next value attached to the parameter, if any */
205 
206  return (r_value);
207 }
208 
209 
210 
211 
212 /*****************************************************************************
213 
214  Routine: FirstValue
215 
216  Description: Returns the first value associated with a parameter.
217 
218  Input:
219  parameter - A pointer to the parameter.
220 
221  Output: A pointer to the first value node is returned as the function.
222  The value NULL is returned if there are no values for this
223  parameter. The NULL value is also returned if the input
224  argument is NULL.
225 
226 *****************************************************************************/
227 
228 
230 
231  PARAMETER parameter)
232 
233 {
234 
235  if (parameter == NULL)
236  {
237  return (NULL);
238  }
239  else
240  {
241  return (parameter->first_value);
242  }
243 
244 }
245 
246 
247 
248 
249 /*****************************************************************************
250 
251  Routine: NextValue
252 
253  Description: Returns the next value of an attribute or pointer.
254 
255  Input:
256  value - A pointer to the current value node.
257 
258  Output: A pointer to the next value node is returned as the function
259  value. The value NULL is returned if there are no more
260  values for the parameter. The NULL value is also returned
261  if the input argument is NULL.
262 
263 *****************************************************************************/
264 
265 
267 
268  VALUE value)
269 
270 {
271 
272  if (value == NULL)
273  {
274  return (NULL);
275  }
276  else
277  {
278  return (value->right_sibling);
279  }
280 
281 }
282 
283 
284 
285 
286 /*****************************************************************************
287 
288  Routine: CopyValue
289 
290  Description: Copies a value node.
291 
292  Input:
293  value - A pointer to the value node to be copied. The
294  input value node remains in the ODL tree after
295  the copy is made.
296 
297  Output: A pointer to the copied value node is returned as the function
298  result. The NULL value is returned if the input argument is
299  NULL or if storage couldn't be obtained for the copy.
300 
301 *****************************************************************************/
302 
303 
305 
306  VALUE value)
307 
308 {
309  VALUE value_copy; /* Copy of the specified value */
310  VALUE_DATA item_copy; /* Copy of the value data structure */
311  struct ODLUnits *units; /* Pointer to units field being copied */
312  struct ODLUnits *units_copy; /* Copy of the units structure */
313  struct ODLUnits *next_field; /* Pointer to next units field, if any */
314  struct ODLUnits *first_units_copy; /* Pointer to first units field copy */
315  struct ODLUnits *last_units_copy; /* Pointer to last units field copy */
316  char *string; /* Copy of name string */
317 
318 
319  if (value == NULL)
320  {
321  return (NULL);
322  }
323 
324  /* Fill in the value type-independent fields of the value data structure */
325 
326  item_copy.type = value->item.type;
327  item_copy.valid = value->item.valid;
328  item_copy.format = value->item.format;
329  item_copy.precision = value->item.precision;
330  item_copy.length = value->item.length;
331 
332  /* Fill in the value type-dependent part of the value data structure */
333 
334  switch (item_copy.type)
335  {
336  case TV_STRING: case TV_SYMBOL:
337 
338  /* Copy character string and attach it to the value node */
339 
340  string = (char *) malloc (item_copy.length+1);
341  if (string == NULL)
342  {
343  /* Error: couldn't get memory for the copy of the string */
344 
345  return (NULL);
346  }
347 
348  item_copy.value.string = strcpy (string, value->item.value.string);
349 
350  break;
351 
352  case TV_INTEGER: case TV_REAL:
353 
354  /* Copy the number value and get a pointer to first units, if any */
355 
356  if (item_copy.type == TV_INTEGER)
357  {
358  item_copy.value.integer.number = value->item.value.integer.number;
359  units = value->item.value.integer.units;
360  }
361  else
362  {
363  item_copy.value.real.number = value->item.value.real.number;
364  units = value->item.value.real.units;
365  }
366 
367  first_units_copy = NULL;
368 
369  while (units != NULL)
370  {
371  /* Allocate space for the copy of the units field */
372 
373  units_copy = (struct ODLUnits *) malloc (sizeof(struct ODLUnits));
374  string = (char *) malloc (strlen (units->designator)+1);
375 
376  if (units_copy == NULL || string == NULL)
377  {
378  /* Error: couldn't get enough storage space to copy
379  the units. Rather than leave the copy operation
380  in an unknown state, we release all storage space
381  and return */
382 
383  units_copy = first_units_copy;
384 
385  while (units_copy != NULL)
386  {
387  next_field = units_copy->next_field;
388  free (units_copy->designator);
389  free (units_copy);
390 
391  units_copy = next_field;
392  }
393 
394  return (NULL);
395  }
396 
397  /* Copy the units information */
398 
399  units_copy->designator = strcpy (string, units->designator);
400  units_copy->exponent = units->exponent;
401  units_copy->next_field = NULL;
402 
403  /* Attach the units information */
404 
405  if (first_units_copy == NULL)
406  {
407  first_units_copy = units_copy;
408  }
409  else
410  {
411  last_units_copy->next_field = units_copy;
412  }
413 
414  last_units_copy = units_copy;
415  units = units->next_field;
416  }
417 
418  /* Attach the units field(s) to the value node */
419 
420  if (item_copy.type == TV_INTEGER)
421  {
422  item_copy.value.integer.units = first_units_copy;
423  }
424  else
425  {
426  item_copy.value.real.units = first_units_copy;
427  }
428 
429  break;
430 
431  case TV_DATE: case TV_TIME: case TV_DATE_TIME:
432 
433  /* Copy the date and time fields */
434 
435  item_copy.value.date_time = value->item.value.date_time;
436 
437  break;
438 
439  default:
440 
441  break;
442  }
443 
444  /* Create a new stand-alone value node containing the copied data */
445 
446  value_copy = NewValue (NULL, &item_copy);
447 
448  return (value_copy);
449 }
450 
451 
452 
453 
454 /*****************************************************************************
455 
456  Routine: CutValue
457 
458  Description: Detaches a value node from its parameter node.
459 
460  Input:
461  value - A pointer to the value node to be cut.
462 
463  Output: A pointer to the cut value node is returned as the function
464  result. The NULL value is returned if the input argument
465  is NULL.
466 
467 *****************************************************************************/
468 
469 
471 
472  VALUE value)
473 
474 {
475  PARAMETER parameter; /* Pointer to parameter node */
476  VALUE l_value; /* Pointer to value's left sibling */
477  VALUE r_value; /* Pointer to value's right sibling */
478 
479  /* Remove the value from the list of values for its parameter node */
480 
481  if (value == NULL)
482  return (NULL);
483 
484  parameter = value->parameter;
485  l_value = value->left_sibling;
486  r_value = value->right_sibling;
487 
488  if (l_value != NULL)
489  {
490  l_value->right_sibling = r_value;
491  }
492  else if (parameter != NULL)
493  {
494  parameter->first_value = r_value;
495  }
496 
497  if (r_value != NULL)
498  {
499  r_value->left_sibling = l_value;
500  }
501  else if (parameter != NULL)
502  {
503  parameter->last_value = l_value;
504  }
505 
506  /* Decrement the count of value nodes attached to the parameter */
507 
508  if (parameter != NULL)
509  {
510  parameter->value_count--;
511  }
512 
513  /* Reset the parent and sibling pointers to indicate this node now
514  stands by itself */
515 
516  value->parameter = NULL;
517  value->left_sibling = NULL;
518  value->right_sibling = NULL;
519 
520  /* Return a pointer to the detached node */
521 
522  return (value);
523 }
524 
525 
526 
527 
528 /*****************************************************************************
529 
530  Routine: PasteValue
531 
532  Description: Attaches a value node to a parameter node.
533 
534  Input:
535  parameter - Pointer to the parameter node to which the value
536  node is to be attached.
537  value - Pointer to the value node to be attached.
538 
539  Output: A pointer to the pasted value node is returned as the function
540  result. A NULL value will be returned if either of the input
541  arguments is NULL.
542 
543 *****************************************************************************/
544 
545 
547 
548  PARAMETER parameter,
549  VALUE value)
550 
551 {
552  VALUE old_last; /* Pointer to previous last value */
553 
554 
555  if (parameter == NULL || value == NULL)
556  {
557  return (NULL);
558  }
559 
560  /* Place the value node at the end of the list of values for the
561  parameter */
562 
563  old_last = parameter->last_value;
564  if (old_last != NULL)
565  {
566  old_last->right_sibling = value;
567  }
568 
569  parameter->last_value = value;
570  if (parameter->first_value == NULL)
571  {
572  parameter->first_value = value;
573  }
574 
575  value->parameter = parameter;
576  value->left_sibling = old_last;
577  value->right_sibling = NULL;
578 
579  /* Increment the count of value nodes attached to the parameter */
580 
581  parameter->value_count++;
582 
583  /* Return a pointer to the pasted value node */
584 
585  return (value);
586 }
struct Value_Node * right_sibling
Definition: odldef.h:232
char format
Definition: odldef.h:215
Definition: odldef.h:97
Definition: odldef.h:97
VALUE FirstValue(PARAMETER parameter)
Definition: v_nodesv.c:229
struct ODLInteger integer
Definition: odldef.h:219
long value_count
Definition: odldef.h:142
struct ODLDate date_time
Definition: odldef.h:221
short length
Definition: odldef.h:217
double number
Definition: odldef.h:184
struct Value_Node * VALUE
Definition: odldef.h:252
VALUE RemoveValue(VALUE value)
Definition: v_nodesv.c:129
HDFFCLIBAPI intf intf _fcd string
Definition: odldef.h:96
VALUE PasteValue(PARAMETER parameter, VALUE value)
Definition: v_nodesv.c:546
struct ODLUnits * next_field
Definition: odldef.h:178
char valid
Definition: odldef.h:214
#define VALUE_NODE_SIZE
Definition: odldef.h:270
VALUE NewValue(PARAMETER parameter, VALUE_DATA *value_data)
Definition: v_nodesv.c:61
struct ODLReal real
Definition: odldef.h:220
struct Value_Data item
Definition: odldef.h:229
long exponent
Definition: odldef.h:177
short precision
Definition: odldef.h:216
VALUE CutValue(VALUE value)
Definition: v_nodesv.c:470
char * string
Definition: odldef.h:222
VALUE CopyValue(VALUE value)
Definition: v_nodesv.c:304
VALUE_TYPE type
Definition: odldef.h:213
struct Value_Node * first_value
Definition: odldef.h:148
char * designator
Definition: odldef.h:176
struct ODLUnits * units
Definition: odldef.h:185
struct ODLUnits * units
Definition: odldef.h:192
struct Parameter_Node * parameter
Definition: odldef.h:230
struct Value_Node * last_value
Definition: odldef.h:149
struct Value_Node * left_sibling
Definition: odldef.h:231
union Value_Data::@2 value
VALUE NextValue(VALUE value)
Definition: v_nodesv.c:266
long number
Definition: odldef.h:191

MISR Toolkit - Copyright © 2005 - 2020 Jet Propulsion Laboratory
Generated on Fri Jun 19 2020 22:49:51